Search in sources :

Example 6 with ParallelTeam

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

the class RotamerOptimization method distanceMatrix.

/**
 * Calculates a residue-residue distance matrix.
 * <p>
 * Residue-residue distance is defined as the shortest atom-atom distance in
 * any possible rotamer-rotamer pair if the residues are neighbors (central
 * atom-central atom distances are within a cutoff). Otherewise, distances
 * are set to a default of Double.MAX_VALUE.
 * </p>
 * <p>
 * The intent of using a neighbor list is to avoid tediously searching
 * rotamer- rotamer pairs when two residues are so far apart we will never
 * need the exact distance. We use the distance matrix for adding residues
 * to the sliding window and determining whether to set 3-body energy to
 * 0.0.
 * </p>
 * <p>
 * If the central atoms are too distant from each other, we can safely
 * assume no atoms will ever be close enough for addition to sliding window
 * or to cause explicit calculation of 3-body energy.
 * </p>
 */
private void distanceMatrix() {
    distanceMatrix = new double[numResidues - 1][][][];
    long numDistances = 0L;
    for (int i = 0; i < (numResidues - 1); i++) {
        Residue residuei = allResiduesArray[i];
        int lengthRi;
        try {
            if (checkIfForced(residuei)) {
                lengthRi = 1;
            } else {
                lengthRi = residuei.getRotamers(library).length;
            }
        } catch (IndexOutOfBoundsException ex) {
            if (useForcedResidues) {
                logger.warning(ex.toString());
            } else {
                logIfMaster(format(" Non-forced Residue i %s has null rotamers.", residuei.toFormattedString(false, true)), Level.WARNING);
            }
            continue;
        }
        distanceMatrix[i] = new double[lengthRi][][];
        for (int ri = 0; ri < lengthRi; ri++) {
            distanceMatrix[i][ri] = new double[numResidues][];
            for (int j = (i + 1); j < numResidues; j++) {
                Residue residuej = allResiduesArray[j];
                int lengthRj;
                try {
                    if (checkIfForced(residuej)) {
                        lengthRj = 1;
                    } else {
                        lengthRj = residuej.getRotamers(library).length;
                    }
                } catch (IndexOutOfBoundsException ex) {
                    if (useForcedResidues) {
                        logger.warning(ex.toString());
                    } else {
                        logIfMaster(format(" Residue j %s has null rotamers.", residuej.toFormattedString(false, true)));
                    }
                    continue;
                }
                distanceMatrix[i][ri][j] = new double[lengthRj];
                numDistances += lengthRj;
                if (!lazyMatrix) {
                    fill(distanceMatrix[i][ri][j], Double.MAX_VALUE);
                } else {
                    fill(distanceMatrix[i][ri][j], -1.0);
                }
            }
        }
    }
    logger.info(format(" Number of pairwise distances: %d", numDistances));
    if (!lazyMatrix) {
        ResidueState[] orig = ResidueState.storeAllCoordinates(allResiduesList);
        int nMultiRes = 0;
        /**
         * Build a list that contains one atom from each Residues: CA from
         * amino acids, C1 from nucleic acids, or the first atom otherwise.
         */
        Atom[] atoms = new Atom[numResidues];
        for (int i = 0; i < numResidues; i++) {
            Residue residuei = allResiduesArray[i];
            atoms[i] = residuei.getReferenceAtom();
            if (residuei instanceof MultiResidue) {
                ++nMultiRes;
            }
        }
        /**
         * Use of the pre-existing ParallelTeam causes a conflict when
         * MultiResidues must re-init the force field. Temporary solution
         * for sequence optimization: if > 1 residue optimized, run on only
         * one thread.
         */
        int nThreads = 1;
        if (molecularAssembly.getPotentialEnergy().getParallelTeam() != null) {
            nThreads = (nMultiRes > 1) ? 1 : molecularAssembly.getPotentialEnergy().getParallelTeam().getThreadCount();
        } else {
            // Suggested: nThreads = (nMultiRes > 1) ? 1 : ParallelTeam.getDefaultThreadCount();
            nThreads = 16;
        }
        ParallelTeam parallelTeam = new ParallelTeam(nThreads);
        Crystal crystal = molecularAssembly.getCrystal();
        int nSymm = crystal.spaceGroup.getNumberOfSymOps();
        logger.info("\n Computing Residue Distance Matrix");
        double nlistCutoff = Math.max(Math.max(distance, twoBodyCutoffDist), threeBodyCutoffDist);
        /**
         * I think this originated from the fact that side-chain
         * (and later nucleic acid) atoms could be fairly distant
         * from the reference atom.
         */
        double magicNumberBufferOfUnknownOrigin = 25.0;
        nlistCutoff += magicNumberBufferOfUnknownOrigin;
        NeighborList neighborList = new NeighborList(null, crystal, atoms, nlistCutoff, 0.0, parallelTeam);
        // Expand coordinates
        double[][] xyz = new double[nSymm][3 * numResidues];
        double[] in = new double[3];
        double[] out = new double[3];
        for (int iSymOp = 0; iSymOp < nSymm; iSymOp++) {
            SymOp symOp = crystal.spaceGroup.getSymOp(iSymOp);
            for (int i = 0; i < numResidues; i++) {
                int i3 = i * 3;
                int iX = i3 + 0;
                int iY = i3 + 1;
                int iZ = i3 + 2;
                Atom atom = atoms[i];
                in[0] = atom.getX();
                in[1] = atom.getY();
                in[2] = atom.getZ();
                crystal.applySymOp(in, out, symOp);
                xyz[iSymOp][iX] = out[0];
                xyz[iSymOp][iY] = out[1];
                xyz[iSymOp][iZ] = out[2];
            }
        }
        // Build the residue neighbor-list.
        int[][][] lists = new int[nSymm][numResidues][];
        boolean[] use = new boolean[numResidues];
        fill(use, true);
        boolean forceRebuild = true;
        boolean printLists = false;
        long neighborTime = -System.nanoTime();
        neighborList.buildList(xyz, lists, use, forceRebuild, printLists);
        neighborTime += System.nanoTime();
        logger.info(format(" Built residue neighbor list:           %8.3f sec", neighborTime * 1.0e-9));
        DistanceRegion distanceRegion = new DistanceRegion(parallelTeam.getThreadCount(), numResidues, crystal, lists, neighborList.getPairwiseSchedule());
        long parallelTime = -System.nanoTime();
        try {
            parallelTeam.execute(distanceRegion);
        } catch (Exception e) {
            String message = " Exception compting residue distance matrix.";
            logger.log(Level.SEVERE, message, e);
        }
        parallelTime += System.nanoTime();
        logger.info(format(" Pairwise distance matrix:              %8.3f sec\n", parallelTime * 1.0e-9));
        ResidueState.revertAllCoordinates(allResiduesList, orig);
        try {
            parallelTeam.shutdown();
        } catch (Exception ex) {
            logger.warning(format(" Exception shutting down parallel team for the distance matrix: %s", ex.toString()));
        }
    }
}
Also used : SymOp(ffx.crystal.SymOp) ParallelTeam(edu.rit.pj.ParallelTeam) NeighborList(ffx.potential.nonbonded.NeighborList) ResidueState(ffx.potential.bonded.ResidueState) Atom(ffx.potential.bonded.Atom) IOException(java.io.IOException) NACorrectionException(ffx.potential.bonded.NACorrectionException) Residue(ffx.potential.bonded.Residue) MultiResidue(ffx.potential.bonded.MultiResidue) MultiResidue(ffx.potential.bonded.MultiResidue) Crystal(ffx.crystal.Crystal)

Example 7 with ParallelTeam

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

the class Complex3DOpenCL 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;
    boolean transferOnly = false;
    if (args != null) {
        try {
            dimNotFinal = Integer.parseInt(args[0]);
            if (dimNotFinal < 1) {
                dimNotFinal = 64;
            }
            reps = Integer.parseInt(args[1]);
            if (reps < 1) {
                reps = 5;
            }
            int transfer = Integer.parseInt(args[2]);
            if (transfer == 1) {
                transferOnly = true;
            }
        } 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);
    int index = 0;
    for (int k = 0; k < dim; k++) {
        for (int j = 0; j < dim; j++) {
            for (int i = 0; i < dim; i++) {
                orig[index] = random.nextDouble();
                recip[index] = orig[index];
                // recip[index] = 1.0;
                index++;
            }
        }
    }
    Complex3D complex3D = new Complex3D(dim, dim, dim);
    Complex3DParallel complex3DParallel = new Complex3DParallel(dim, dim, dim, new ParallelTeam(), IntegerSchedule.fixed());
    complex3DParallel.setRecip(recip);
    Complex3DOpenCL complex3DOpenCL = new Complex3DOpenCL(dim, dim, dim);
    complex3DOpenCL.setTransferOnly(transferOnly);
    Thread openCLThread = new Thread(complex3DOpenCL);
    openCLThread.setPriority(Thread.MAX_PRIORITY);
    openCLThread.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);
        // complex3D.fft(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];
    }
    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);
        // complex3DParallel.fft(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));
    complex3DOpenCL.setRecip(recip);
    DoubleBuffer doubleBuffer = complex3DOpenCL.getDoubleBuffer();
    for (int i = 0; i < reps; i++) {
        for (int j = 0; j < dimCubed; j++) {
            doubleBuffer.put(j * 2, orig[j]);
            doubleBuffer.put(j * 2 + 1, 0.0);
        // data[j * 2] = orig[j];
        // data[j * 2 + 1] = 0.0;
        }
        doubleBuffer.rewind();
        long time = System.nanoTime();
        complex3DOpenCL.convolution(data);
        // complex3DOpenCL.fft(data);
        time = (System.nanoTime() - time);
        System.out.println(String.format(" %2d OpenCL:     %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++) {
        /*
             if (i < 10) {
             System.out.println(String.format(" %8.3f %8.3f %8.3f", orig[i], answer[i], doubleBuffer.get(2 * i)));
             }
             */
        double error = Math.abs(answer[i] - doubleBuffer.get(2 * i));
        avg += error;
        if (error > maxError) {
            maxError = error;
        }
        rmse += error * error;
    }
    rmse /= dimCubed;
    avg /= dimCubed;
    rmse = Math.sqrt(rmse);
    logger.info(String.format(" OpenCL RMSE:   %12.10f, Max: %12.10f, Avg: %12.10f", rmse, maxError, avg));
    complex3DOpenCL.free();
    complex3DOpenCL = 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 OpenCL Time:      %8.3f", toSeconds * clTime));
    System.out.println(String.format(" Parallel Speedup:      %8.3fx", (double) seqTime / parTime));
    System.out.println(String.format(" OpenCL Speedup:        %8.3fx", (double) seqTime / clTime));
}
Also used : DoubleBuffer(java.nio.DoubleBuffer) ParallelTeam(edu.rit.pj.ParallelTeam) IOException(java.io.IOException) Random(java.util.Random)

Example 8 with ParallelTeam

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

the class RowMajorComplex3DParallel method main.

/**
 * Test the Complex3DParallel 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) {
        }
    }
    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));
    // One dimension of the serial array divided by the number of threads.
    Complex3D columnComplex3D = new Complex3D(dim, dim, dim);
    RowMajorComplex3D rowComplex3D = new RowMajorComplex3D(dim, dim, dim);
    ParallelTeam parallelTeam = new ParallelTeam(ncpu);
    Complex3DParallel columnComplex3DParallel = new Complex3DParallel(dim, dim, dim, parallelTeam);
    RowMajorComplex3DParallel rowComplex3DParallel = new RowMajorComplex3DParallel(dim, dim, dim, parallelTeam);
    final int dimCubed = dim * dim * dim;
    final double[] rowOrig = new double[dimCubed * 2];
    final double[] columnOrig = new double[dimCubed * 2];
    final double[] rowMajor = new double[dimCubed * 2];
    final double[] columnMajor = new double[dimCubed * 2];
    double[] rowRecip = new double[dimCubed];
    double[] columnRecip = new double[dimCubed];
    Random randomNumberGenerator = new Random();
    for (int x = 0; x < dim; x++) {
        for (int y = 0; y < dim; y++) {
            for (int z = 0; z < dim; z++) {
                int columnMajorIndex = Complex3D.iComplex3D(x, y, z, dim, dim);
                int rowMajorIndex = RowMajorComplex3D.iComplex3D(x, y, z, dim, dim);
                double randomNumber = randomNumberGenerator.nextDouble();
                rowOrig[rowMajorIndex] = randomNumber;
                rowRecip[rowMajorIndex / 2] = randomNumber;
                columnOrig[columnMajorIndex] = randomNumber;
                columnRecip[columnMajorIndex / 2] = randomNumber;
            }
        }
    }
    rowComplex3D.setRecip(rowRecip);
    rowComplex3DParallel.setRecip(rowRecip);
    columnComplex3D.setRecip(columnRecip);
    columnComplex3DParallel.setRecip(columnRecip);
    rowRecip = null;
    columnRecip = null;
    System.arraycopy(rowOrig, 0, rowMajor, 0, dimCubed * 2);
    System.arraycopy(columnOrig, 0, columnMajor, 0, dimCubed * 2);
    rowComplex3D.convolution(rowMajor);
    columnComplex3D.convolution(columnMajor);
    for (int x = 0; x < dim; x++) {
        for (int y = 0; y < dim; y++) {
            for (int z = 0; z < dim; z++) {
                int columnMajorIndex = Complex3D.iComplex3D(x, y, z, dim, dim);
                int rowMajorIndex = RowMajorComplex3D.iComplex3D(x, y, z, dim, dim);
                double error = rowMajor[rowMajorIndex] - columnMajor[columnMajorIndex];
                if (Math.abs(error) > 1.0e-6) {
                    logger.info(String.format("Error (%d,%d,%d): %12.6f", x, y, z, error));
                }
            }
        }
    }
    logger.info("Single threaded column-major and row-major agree.");
    System.arraycopy(rowOrig, 0, rowMajor, 0, dimCubed * 2);
    rowComplex3D.convolution(rowMajor);
    // note - using the columnMajor array for temporary storage
    System.arraycopy(rowOrig, 0, columnMajor, 0, dimCubed * 2);
    rowComplex3DParallel.convolution(columnMajor);
    for (int x = 0; x < dim; x++) {
        for (int y = 0; y < dim; y++) {
            for (int z = 0; z < dim; z++) {
                int rowMajorIndex = RowMajorComplex3D.iComplex3D(x, y, z, dim, dim);
                double error = rowMajor[rowMajorIndex] - columnMajor[rowMajorIndex];
                if (Math.abs(error) > 1.0e-6) {
                    logger.info(String.format("Error (%d,%d,%d): %12.6f", x, y, z, error));
                }
            }
        }
    }
    logger.info("Single threaded and SMP row-major agree.");
    System.arraycopy(rowOrig, 0, rowMajor, 0, dimCubed * 2);
    System.arraycopy(columnOrig, 0, columnMajor, 0, dimCubed * 2);
    rowComplex3DParallel.convolution(rowMajor);
    columnComplex3DParallel.convolution(columnMajor);
    for (int x = 0; x < dim; x++) {
        for (int y = 0; y < dim; y++) {
            for (int z = 0; z < dim; z++) {
                int columnMajorIndex = Complex3D.iComplex3D(x, y, z, dim, dim);
                int rowMajorIndex = RowMajorComplex3D.iComplex3D(x, y, z, dim, dim);
                double error = rowMajor[rowMajorIndex] - columnMajor[columnMajorIndex];
                if (Math.abs(error) > 1.0e-6) {
                    logger.info(String.format("Error (%d,%d,%d): %12.6f", x, y, z, error));
                }
            }
        }
    }
    logger.info("SMP column-major and row-major agree.");
    double toSeconds = 0.000000001;
    long parTime = Long.MAX_VALUE;
    long seqTime = Long.MAX_VALUE;
    for (int i = 0; i < reps; i++) {
        System.arraycopy(rowOrig, 0, rowMajor, 0, dimCubed * 2);
        System.out.println(String.format("Iteration %d", i + 1));
        long time = System.nanoTime();
        rowComplex3D.fft(rowMajor);
        rowComplex3D.ifft(rowMajor);
        time = (System.nanoTime() - time);
        System.out.println(String.format("Sequential: %8.3f", toSeconds * time));
        if (time < seqTime) {
            seqTime = time;
        }
        System.arraycopy(rowOrig, 0, rowMajor, 0, dimCubed * 2);
        time = System.nanoTime();
        rowComplex3D.convolution(rowMajor);
        time = (System.nanoTime() - time);
        System.out.println(String.format("Sequential: %8.3f (Convolution)", toSeconds * time));
        if (time < seqTime) {
            seqTime = time;
        }
        System.arraycopy(rowOrig, 0, rowMajor, 0, dimCubed * 2);
        time = System.nanoTime();
        rowComplex3DParallel.fft(rowMajor);
        rowComplex3DParallel.ifft(rowMajor);
        time = (System.nanoTime() - time);
        System.out.println(String.format("Parallel:   %8.3f", toSeconds * time));
        if (time < parTime) {
            parTime = time;
        }
        System.arraycopy(rowOrig, 0, rowMajor, 0, dimCubed * 2);
        time = System.nanoTime();
        rowComplex3DParallel.convolution(rowMajor);
        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 : ParallelTeam(edu.rit.pj.ParallelTeam) Random(java.util.Random)

Example 9 with ParallelTeam

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

the class Complex3DCuda 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[1]);
            if (reps < 1) {
                reps = 10;
            }
        } 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);
    int index = 0;
    for (int k = 0; k < dim; k++) {
        for (int j = 0; j < dim; j++) {
            for (int i = 0; i < dim; i++) {
                orig[index] = random.nextDouble();
                // recip[index] = orig[index];
                recip[index] = 1.0;
                index++;
            }
        }
    }
    Complex3D complex3D = new Complex3D(dim, dim, dim);
    Complex3DParallel complex3DParallel = new Complex3DParallel(dim, dim, dim, new ParallelTeam(), IntegerSchedule.fixed());
    complex3DParallel.setRecip(recip);
    Complex3DCuda complex3DCUDA = new Complex3DCuda(dim, dim, dim);
    Thread cudaThread = new Thread(complex3DCUDA);
    cudaThread.setPriority(Thread.MAX_PRIORITY);
    cudaThread.start();
    complex3DCUDA.setRecip(recip);
    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);
        complex3D.fft(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];
    }
    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);
        complex3DParallel.fft(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));
    DoubleBuffer cudaBuffer = complex3DCUDA.getDoubleBuffer();
    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;
            cudaBuffer.put(j * 2, orig[j]);
            cudaBuffer.put(j * 2 + 1, 0.0);
        }
        long time = System.nanoTime();
        // complex3DCUDA.convolution(data);
        complex3DCUDA.fft(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] - cudaBuffer.get(2 * i));
        // double error = Math.abs(answer[i] / dimCubed -  data[2 * i]);
        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 : DoubleBuffer(java.nio.DoubleBuffer) Random(java.util.Random) ParallelTeam(edu.rit.pj.ParallelTeam)

Example 10 with ParallelTeam

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

the class Complex3DParallel method main.

/**
 * Test the Complex3DParallel 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) {
        }
    }
    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));
    // One dimension of the serial array divided by the number of threads.
    Complex3D complexDoubleFFT3D = new Complex3D(dim, dim, dim);
    ParallelTeam parallelTeam = new ParallelTeam(ncpu);
    Complex3DParallel parallelComplexDoubleFFT3D = new Complex3DParallel(dim, dim, dim, parallelTeam);
    final int dimCubed = dim * dim * dim;
    final double[] data = new double[dimCubed * 2];
    final double[] work = new double[dimCubed * 2];
    // Parallel Array Initialization.
    try {
        parallelTeam.execute(new ParallelRegion() {

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

                        @Override
                        public void run(final int lb, final int ub) {
                            Random randomNumberGenerator = new Random(1);
                            int index = dim * dim * lb * 2;
                            for (int i = lb; i <= ub; i++) {
                                for (int j = 0; j < dim; j++) {
                                    for (int k = 0; k < dim; k++) {
                                        double randomNumber = randomNumberGenerator.nextDouble();
                                        data[index] = randomNumber;
                                        index += 2;
                                    }
                                }
                            }
                        }
                    });
                } 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;
    complexDoubleFFT3D.setRecip(work);
    parallelComplexDoubleFFT3D.setRecip(work);
    for (int i = 0; i < reps; i++) {
        System.out.println(String.format("Iteration %d", i + 1));
        long time = System.nanoTime();
        complexDoubleFFT3D.fft(data);
        complexDoubleFFT3D.ifft(data);
        time = (System.nanoTime() - time);
        System.out.println(String.format("Sequential: %8.3f", toSeconds * time));
        if (time < seqTime) {
            seqTime = time;
        }
        time = System.nanoTime();
        complexDoubleFFT3D.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();
        parallelComplexDoubleFFT3D.fft(data);
        parallelComplexDoubleFFT3D.ifft(data);
        time = (System.nanoTime() - time);
        System.out.println(String.format("Parallel:   %8.3f", toSeconds * time));
        if (time < parTime) {
            parTime = time;
        }
        time = System.nanoTime();
        parallelComplexDoubleFFT3D.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));
    parallelTeam.shutdown();
}
Also used : IntegerForLoop(edu.rit.pj.IntegerForLoop) ParallelTeam(edu.rit.pj.ParallelTeam) Random(java.util.Random) ParallelRegion(edu.rit.pj.ParallelRegion)

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