Search in sources :

Example 11 with ParallelTeam

use of edu.rit.pj.ParallelTeam in project ffx by mjschnie.

the class Real3DParallel method main.

/**
 * Test the real 3D FFT.
 *
 * @param args an array of {@link java.lang.String} objects.
 * @throws java.lang.Exception if any.
 * @since 1.0
 */
public static void main(String[] args) throws Exception {
    int dimNotFinal = 128;
    int ncpu = ParallelTeam.getDefaultThreadCount();
    int reps = 5;
    if (args != null) {
        try {
            dimNotFinal = Integer.parseInt(args[0]);
            if (dimNotFinal < 1) {
                dimNotFinal = 100;
            }
            ncpu = Integer.parseInt(args[1]);
            if (ncpu < 1) {
                ncpu = ParallelTeam.getDefaultThreadCount();
            }
            reps = Integer.parseInt(args[2]);
            if (reps < 1) {
                reps = 5;
            }
        } catch (Exception e) {
        }
    }
    if (dimNotFinal % 2 != 0) {
        dimNotFinal++;
    }
    final int dim = dimNotFinal;
    System.out.println(String.format("Initializing a %d cubed grid for %d CPUs.\n" + "The best timing out of %d repititions will be used.", dim, ncpu, reps));
    Real3D real3D = new Real3D(dim, dim, dim);
    ParallelTeam parallelTeam = new ParallelTeam(ncpu);
    Real3DParallel real3DParallel = new Real3DParallel(dim, dim, dim, parallelTeam);
    final int dimCubed = (dim + 2) * dim * dim;
    final double[] data = new double[dimCubed];
    final double[] work = new double[dimCubed];
    // Parallel Array Initialization.
    try {
        parallelTeam.execute(new ParallelRegion() {

            @Override
            public void run() {
                try {
                    execute(0, dim - 1, new IntegerForLoop() {

                        @Override
                        public void run(int lb, int ub) {
                            Random randomNumberGenerator = new Random(1);
                            int index = dim * dim * lb;
                            for (int z = lb; z <= ub; z++) {
                                for (int y = 0; y < dim; y++) {
                                    for (int x = 0; x < dim; x++) {
                                        double randomNumber = randomNumberGenerator.nextDouble();
                                        data[index] = randomNumber;
                                        index++;
                                    }
                                }
                            }
                        }
                    });
                } catch (Exception e) {
                    System.out.println(e.toString());
                    System.exit(-1);
                }
            }
        });
    } catch (Exception e) {
        System.out.println(e.toString());
        System.exit(-1);
    }
    double toSeconds = 0.000000001;
    long parTime = Long.MAX_VALUE;
    long seqTime = Long.MAX_VALUE;
    real3D.setRecip(work);
    real3DParallel.setRecip(work);
    for (int i = 0; i < reps; i++) {
        System.out.println(String.format("Iteration %d", i + 1));
        long time = System.nanoTime();
        real3D.fft(data);
        real3D.ifft(data);
        time = (System.nanoTime() - time);
        System.out.println(String.format("Sequential: %8.3f", toSeconds * time));
        if (time < seqTime) {
            seqTime = time;
        }
        time = System.nanoTime();
        real3D.convolution(data);
        time = (System.nanoTime() - time);
        System.out.println(String.format("Sequential: %8.3f (Convolution)", toSeconds * time));
        if (time < seqTime) {
            seqTime = time;
        }
        time = System.nanoTime();
        real3DParallel.fft(data);
        real3DParallel.ifft(data);
        time = (System.nanoTime() - time);
        System.out.println(String.format("Parallel:   %8.3f", toSeconds * time));
        if (time < parTime) {
            parTime = time;
        }
        time = System.nanoTime();
        real3DParallel.convolution(data);
        time = (System.nanoTime() - time);
        System.out.println(String.format("Parallel:   %8.3f (Convolution)\n", toSeconds * time));
        if (time < parTime) {
            parTime = time;
        }
    }
    System.out.println(String.format("Best Sequential Time:  %8.3f", toSeconds * seqTime));
    System.out.println(String.format("Best Parallel Time:    %8.3f", toSeconds * parTime));
    System.out.println(String.format("Speedup: %15.5f", (double) seqTime / parTime));
}
Also used : IntegerForLoop(edu.rit.pj.IntegerForLoop) ParallelTeam(edu.rit.pj.ParallelTeam) Random(java.util.Random) ParallelRegion(edu.rit.pj.ParallelRegion)

Example 12 with ParallelTeam

use of edu.rit.pj.ParallelTeam in project ffx by mjschnie.

the class RowMajorComplex3DCuda method main.

/**
 * <p>
 * main</p>
 *
 * @param args an array of {@link java.lang.String} objects.
 * @throws java.lang.Exception if any.
 */
public static void main(String[] args) throws Exception {
    int dimNotFinal = 64;
    int reps = 10;
    if (args != null) {
        try {
            dimNotFinal = Integer.parseInt(args[0]);
            if (dimNotFinal < 1) {
                dimNotFinal = 64;
            }
            reps = Integer.parseInt(args[2]);
            if (reps < 1) {
                reps = 5;
            }
        } catch (Exception e) {
        }
    }
    final int dim = dimNotFinal;
    System.out.println(String.format(" Initializing a %d cubed grid.\n" + " The best timing out of %d repititions will be used.", dim, reps));
    final int dimCubed = dim * dim * dim;
    /**
     * Create an array to save the initial input and result.
     */
    double[] orig = new double[dimCubed];
    double[] answer = new double[dimCubed];
    double[] data = new double[dimCubed * 2];
    double[] recip = new double[dimCubed];
    Random random = new Random(1);
    for (int x = 0; x < dim; x++) {
        for (int y = 0; y < dim; y++) {
            for (int z = 0; z < dim; z++) {
                int index = RowMajorComplex3D.iComplex3D(x, y, z, dim, dim);
                orig[index / 2] = random.nextDouble();
                recip[index / 2] = orig[index / 2];
            }
        }
    }
    RowMajorComplex3D complex3D = new RowMajorComplex3D(dim, dim, dim);
    RowMajorComplex3DParallel complex3DParallel = new RowMajorComplex3DParallel(dim, dim, dim, new ParallelTeam(), IntegerSchedule.fixed());
    RowMajorComplex3DCuda complex3DCUDA = new RowMajorComplex3DCuda(dim, dim, dim, data, recip);
    Thread cudaThread = new Thread(complex3DCUDA);
    cudaThread.setPriority(Thread.MAX_PRIORITY);
    cudaThread.start();
    double toSeconds = 0.000000001;
    long parTime = Long.MAX_VALUE;
    long seqTime = Long.MAX_VALUE;
    long clTime = Long.MAX_VALUE;
    complex3D.setRecip(recip);
    for (int i = 0; i < reps; i++) {
        for (int j = 0; j < dimCubed; j++) {
            data[j * 2] = orig[j];
            data[j * 2 + 1] = 0.0;
        }
        long time = System.nanoTime();
        complex3D.convolution(data);
        time = (System.nanoTime() - time);
        System.out.println(String.format(" %2d Sequential: %8.3f", i + 1, toSeconds * time));
        if (time < seqTime) {
            seqTime = time;
        }
    }
    for (int j = 0; j < dimCubed; j++) {
        answer[j] = data[j * 2];
    }
    complex3DParallel.setRecip(recip);
    for (int i = 0; i < reps; i++) {
        for (int j = 0; j < dimCubed; j++) {
            data[j * 2] = orig[j];
            data[j * 2 + 1] = 0.0;
        }
        long time = System.nanoTime();
        complex3DParallel.convolution(data);
        time = (System.nanoTime() - time);
        System.out.println(String.format(" %2d Parallel:   %8.3f", i + 1, toSeconds * time));
        if (time < parTime) {
            parTime = time;
        }
    }
    double maxError = Double.MIN_VALUE;
    double rmse = 0.0;
    for (int i = 0; i < dimCubed; i++) {
        double error = Math.abs(answer[i] - data[2 * i]);
        if (error > maxError) {
            maxError = error;
        }
        rmse += error * error;
    }
    rmse /= dimCubed;
    rmse = Math.sqrt(rmse);
    logger.info(String.format(" Parallel RMSE:   %12.10f, Max: %12.10f", rmse, maxError));
    for (int i = 0; i < reps; i++) {
        for (int j = 0; j < dimCubed; j++) {
            data[j * 2] = orig[j];
            data[j * 2 + 1] = 0.0;
        }
        long time = System.nanoTime();
        complex3DCUDA.convolution(data);
        time = (System.nanoTime() - time);
        System.out.println(String.format(" %2d CUDA:     %8.3f", i + 1, toSeconds * time));
        if (time < clTime) {
            clTime = time;
        }
    }
    maxError = Double.MIN_VALUE;
    double avg = 0.0;
    rmse = 0.0;
    for (int i = 0; i < dimCubed; i++) {
        double error = Math.abs((answer[i] - data[2 * i]) / dimCubed);
        avg += error;
        if (error > maxError) {
            maxError = error;
        }
        rmse += error * error;
    }
    rmse /= dimCubed;
    avg /= dimCubed;
    rmse = Math.sqrt(rmse);
    logger.info(String.format(" CUDA RMSE:   %12.10f, Max: %12.10f, Avg: %12.10f", rmse, maxError, avg));
    complex3DCUDA.free();
    complex3DCUDA = null;
    System.out.println(String.format(" Best Sequential Time:  %8.3f", toSeconds * seqTime));
    System.out.println(String.format(" Best Parallel Time:    %8.3f", toSeconds * parTime));
    System.out.println(String.format(" Best CUDA Time:        %8.3f", toSeconds * clTime));
    System.out.println(String.format(" Parallel Speedup: %15.5f", (double) seqTime / parTime));
    System.out.println(String.format(" CUDA Speedup:     %15.5f", (double) seqTime / clTime));
}
Also used : Random(java.util.Random) ParallelTeam(edu.rit.pj.ParallelTeam)

Example 13 with ParallelTeam

use of edu.rit.pj.ParallelTeam in project ffx by mjschnie.

the class PDBFileMatcher method match.

/**
 * Primary class method: matches match files to source files, and if any
 * fixer flags are set, calls fixFiles to edit the match files.
 */
public void match() {
    try {
        if (parallel) {
            parallelTeam = new ParallelTeam();
            try {
                parallelTeam.execute(new MatchingRegion());
            } catch (Exception ex) {
                logger.severe(String.format(" Exception matching files in parallel: %s", ex.toString()));
            }
            for (int i = 0; i < iterationTimes.length; i++) {
                logger.info(String.format(" Iteration %d time: %12.9f sec", i, 1.0E-9 * iterationTimes[i]));
            }
        } else {
            sequentialFileMatch();
        }
        fixAtoms = fixSSBonds || fixBFactors;
        fixModel = fixAtoms || headerLink || fixCryst;
        if (fixModel) {
            fixFiles();
        }
    } catch (Exception ex) {
        logger.severe(String.format(" Error in matching: %s", ex.toString()));
    }
}
Also used : ParallelTeam(edu.rit.pj.ParallelTeam) StructureException(org.biojava.bio.structure.StructureException) IOException(java.io.IOException)

Example 14 with ParallelTeam

use of edu.rit.pj.ParallelTeam in project ffx by mjschnie.

the class CrystalReciprocalSpaceTest method test1NSFPermanent.

@Test
public void test1NSFPermanent() {
    String filename = "ffx/xray/structures/1NSF.pdb";
    int index = filename.lastIndexOf(".");
    String name = filename.substring(0, index);
    // load the structure
    ClassLoader cl = this.getClass().getClassLoader();
    File structure = new File(cl.getResource(filename).getPath());
    // load any properties associated with it
    CompositeConfiguration properties = Keyword.loadProperties(structure);
    Crystal crystal = new Crystal(115.996, 115.996, 44.13, 90.0, 90.0, 120.0, "P6");
    Resolution resolution = new Resolution(1.89631);
    ReflectionList reflectionList = new ReflectionList(crystal, resolution);
    DiffractionRefinementData refinementData = new DiffractionRefinementData(properties, reflectionList);
    ForceFieldFilter forceFieldFilter = new ForceFieldFilter(properties);
    ForceField forceField = forceFieldFilter.parse();
    // associate molecular assembly with the structure, set up forcefield
    MolecularAssembly molecularAssembly = new MolecularAssembly(name);
    molecularAssembly.setFile(structure);
    molecularAssembly.setForceField(forceField);
    PDBFilter pdbFile = new PDBFilter(structure, molecularAssembly, forceField, properties);
    pdbFile.readFile();
    pdbFile.applyAtomProperties();
    molecularAssembly.finalize(true, forceField);
    ForceFieldEnergy energy = ForceFieldEnergy.energyFactory(molecularAssembly, pdbFile.getCoordRestraints());
    List<Atom> atomList = molecularAssembly.getAtomList();
    Atom[] atomArray = atomList.toArray(new Atom[atomList.size()]);
    // set up FFT and run it
    ParallelTeam parallelTeam = new ParallelTeam();
    CrystalReciprocalSpace crs = new CrystalReciprocalSpace(reflectionList, atomArray, parallelTeam, parallelTeam);
    crs.computeAtomicDensity(refinementData.fc);
    // tests
    ComplexNumber b = new ComplexNumber(-496.999, 431.817);
    HKL hkl = reflectionList.getHKL(1, 9, 4);
    ComplexNumber a = refinementData.getFc(hkl.index());
    System.out.println("1 9 4: " + a.toString() + " | " + b.toString() + " | " + a.divides(b).toString());
    assertEquals("1 9 4 reflection should be correct", -493.7799429881329, a.re(), 0.0001);
    assertEquals("1 9 4 reflection should be correct", 460.7022632345927, a.im(), 0.0001);
    b.re(-129.767);
    b.im(-76.9812);
    hkl = reflectionList.getHKL(5, 26, 8);
    a = refinementData.getFc(hkl.index());
    System.out.println("5 26 8: " + a.toString() + " | " + b.toString() + " | " + a.divides(b).toString());
    assertEquals("5 26 8 reflection should be correct", -123.05535567943377, a.re(), 0.0001);
    assertEquals("5 26 8 reflection should be correct", -74.59007322382718, a.im(), 0.0001);
}
Also used : ParallelTeam(edu.rit.pj.ParallelTeam) CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) ForceFieldFilter(ffx.potential.parsers.ForceFieldFilter) HKL(ffx.crystal.HKL) ForceFieldEnergy(ffx.potential.ForceFieldEnergy) ReflectionList(ffx.crystal.ReflectionList) ComplexNumber(ffx.numerics.ComplexNumber) Atom(ffx.potential.bonded.Atom) MolecularAssembly(ffx.potential.MolecularAssembly) ForceField(ffx.potential.parameters.ForceField) File(java.io.File) PDBFilter(ffx.potential.parsers.PDBFilter) Crystal(ffx.crystal.Crystal) Resolution(ffx.crystal.Resolution) Test(org.junit.Test)

Example 15 with ParallelTeam

use of edu.rit.pj.ParallelTeam in project ffx by mjschnie.

the class Rescore method rescoreSingle.

private File rescoreSingle(File modelFile, RescoreStrategy rscType, DoubleIndexPair[] energies, int i) {
    Path filepath = generatePath(modelFile);
    if (filepath == null) {
        logger.warning(String.format(" Could not generate path to file %s", modelFile.toPath()));
        return null;
    }
    String filename = pwdPath.relativize(filepath).toString();
    File retFile = modelFile;
    try {
        MolecularAssembly assembly = utils.open(filename);
        switch(rscType) {
            case NO_RESCORE:
                logger.warning(" Rescore is being called with rscType = NO_RESCORE");
                break;
            case ENERGY_EVAL:
                break;
            case MINIMIZE:
                logger.info(String.format("\n Running minimize on %s", filename));
                logger.info(String.format(" RMS gradient convergence criteria: %f", eps));
                utils.energy(assembly);
                utils.minimize(assembly, eps);
                String ext = FilenameUtils.getExtension(filename);
                ext = ".".concat(ext);
                if (resultDir != null) {
                    filename = FilenameUtils.getBaseName(filename);
                    filename = FilenameUtils.concat(resultPath.toString(), filename);
                } else {
                    filename = FilenameUtils.removeExtension(filename);
                }
                filename = filename.concat(fileSuffix).concat(ext);
                retFile = new File(filename);
                if (ext.toUpperCase().contains("XYZ")) {
                    utils.saveAsXYZ(assembly, retFile);
                } else {
                    utils.saveAsPDB(assembly, retFile);
                }
                break;
            case XRAY_MIN:
                logger.info(String.format("\n Running x-ray minimize on %s", filename));
                DiffractionFile diffractionFile = null;
                if (diffractionFiles.isEmpty()) {
                    diffractionFile = new DiffractionFile(assembly, 1.0, false);
                    diffractionFiles.add(diffractionFile);
                }
                CompositeConfiguration properties = Keyword.loadProperties(modelFile);
                DiffractionData diffractionData = new DiffractionData(assembly, properties, SolventModel.POLYNOMIAL, diffractionFiles.toArray(new DiffractionFile[diffractionFiles.size()]));
                diffractionData.scaleBulkFit();
                diffractionData.printStats();
                utils.energy(assembly);
                RefinementMinimize refinementMinimize = new RefinementMinimize(diffractionData, refinementMode);
                if (eps < 0.0) {
                    eps = refinementMinimize.getEps();
                }
                logger.info(String.format("\n RMS gradient convergence criteria: %8.5f max number of iterations %d", eps, maxiter));
                refinementMinimize.minimize(eps, maxiter);
                diffractionData.scaleBulkFit();
                diffractionData.printStats();
                ext = FilenameUtils.getExtension(filename);
                ext = ".".concat(ext);
                if (resultDir != null) {
                    filename = FilenameUtils.getBaseName(filename);
                    filename = FilenameUtils.concat(resultPath.toString(), filename);
                } else {
                    filename = FilenameUtils.removeExtension(filename);
                }
                filename = filename.concat(fileSuffix);
                diffractionData.writeData(filename + ".mtz");
                filename = filename.concat(ext);
                diffractionData.writeModel(filename);
                retFile = new File(filename);
                if (diffractionFile != null) {
                    try {
                        diffractionFiles.remove(diffractionFile);
                    } catch (UnsupportedOperationException ex) {
                        // This should never occur, because diffractionFiles should be of a List type supporting remove(object).
                        diffractionFiles = new ArrayList<>();
                    }
                }
                break;
            case RS_MIN:
                logger.info(String.format("\n Running real-space minimize on %s", filename));
                RealSpaceFile realspaceFile = null;
                if (mapFiles.isEmpty()) {
                    realspaceFile = new RealSpaceFile(assembly);
                    mapFiles.add(realspaceFile);
                }
                properties = Keyword.loadProperties(modelFile);
                RealSpaceData realspaceData = new RealSpaceData(assembly, properties, new ParallelTeam(), mapFiles.toArray(new RealSpaceFile[mapFiles.size()]));
                utils.energy(assembly);
                refinementMinimize = new RefinementMinimize(realspaceData, refinementMode);
                if (eps < 0.0) {
                    eps = 1.0;
                }
                logger.info(String.format("\n RMS gradient convergence criteria: %8.5f max number of iterations %d", eps, maxiter));
                refinementMinimize.minimize(eps, maxiter);
                ext = FilenameUtils.getExtension(filename);
                ext = ".".concat(ext);
                if (resultDir != null) {
                    filename = FilenameUtils.getBaseName(filename);
                    filename = FilenameUtils.concat(resultPath.toString(), filename);
                } else {
                    filename = FilenameUtils.removeExtension(filename);
                }
                filename = filename.concat(fileSuffix).concat(ext);
                retFile = new File(filename);
                if (ext.toUpperCase().contains("XYZ")) {
                    utils.saveAsXYZ(assembly, retFile);
                } else {
                    utils.saveAsPDB(assembly, retFile);
                }
                if (realspaceFile != null) {
                    try {
                        mapFiles.remove(realspaceFile);
                    } catch (UnsupportedOperationException ex) {
                        // This should never occur, because diffractionFiles should be of a List type supporting remove(object).
                        mapFiles = new ArrayList<>();
                    }
                }
                break;
            default:
                logger.severe(" No valid rescore type: FFX will not continue.");
        }
        double e = utils.returnEnergy(assembly);
        energies[i] = new DoubleIndexPair(i, e);
        utils.close(assembly);
    } catch (Exception ex) {
        logger.warning(String.format(" Exception rescoring on file %s", filename));
        logger.info(ex.toString());
    }
    return retFile;
}
Also used : Path(java.nio.file.Path) ClusterStructures.generatePath(ffx.algorithms.ClusterStructures.generatePath) DoubleIndexPair(ffx.utilities.DoubleIndexPair) ParallelTeam(edu.rit.pj.ParallelTeam) RealSpaceFile(ffx.realspace.RealSpaceFile) CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DiffractionFile(ffx.xray.parsers.DiffractionFile) MolecularAssembly(ffx.potential.MolecularAssembly) RealSpaceData(ffx.realspace.RealSpaceData) DiffractionFile(ffx.xray.parsers.DiffractionFile) File(java.io.File) RealSpaceFile(ffx.realspace.RealSpaceFile)

Aggregations

ParallelTeam (edu.rit.pj.ParallelTeam)16 IOException (java.io.IOException)7 Random (java.util.Random)7 File (java.io.File)5 Crystal (ffx.crystal.Crystal)4 MolecularAssembly (ffx.potential.MolecularAssembly)4 Atom (ffx.potential.bonded.Atom)4 CompositeConfiguration (org.apache.commons.configuration.CompositeConfiguration)4 IntegerForLoop (edu.rit.pj.IntegerForLoop)3 ParallelRegion (edu.rit.pj.ParallelRegion)3 ReflectionList (ffx.crystal.ReflectionList)3 Resolution (ffx.crystal.Resolution)3 ForceFieldEnergy (ffx.potential.ForceFieldEnergy)3 NACorrectionException (ffx.potential.bonded.NACorrectionException)3 HKL (ffx.crystal.HKL)2 ComplexNumber (ffx.numerics.ComplexNumber)2 MultiResidue (ffx.potential.bonded.MultiResidue)2 Residue (ffx.potential.bonded.Residue)2 ForceField (ffx.potential.parameters.ForceField)2 ForceFieldFilter (ffx.potential.parsers.ForceFieldFilter)2