package edu.rice.hj.example.comp322; import edu.rice.hj.api.*; import edu.rice.hj.runtime.config.HjSystemProperty; import edu.rice.hj.runtime.metrics.AbstractMetricsManager; import java.util.Random; import static edu.rice.hj.Module1.*; /** * VectorAdd --- Compute the sum of two vectors *

* The purpose of this example is to illustrate abstract metrics and Amdahl's Law * * @author Vivek Sarkar (vsarkar@rice.edu) */ public class VectorAddPlaces { /** * Constant DEFAULT_N=100 */ public static final int DEFAULT_N = 10_000_000; /** * Constant ERROR_MSG="Incorrect argument for array size" */ public static final String ERROR_MSG = "Incorrect argument for array size (should be > 0), assuming n = " + DEFAULT_N; /** * Constant DEFAULT_NUM_SEQ=0 */ public static final int DEFAULT_NUM_SEQ = 0; // Add vectors X and Y and store the result in Z /** *

main.

* * @param argv an array of {@link String} objects. */ public static void main(final String[] argv) { // Initialization final int n = (argv.length > 0) ? Integer.parseInt(argv[0]) : DEFAULT_N; final double[] X = new double[n]; final double[] Y = new double[n]; final double[] Z = new double[n]; final Random myRand = new Random(n); for (int i = 0; i < n; i++) { X[i] = myRand.nextInt(n); Y[i] = myRand.nextInt(n); } HjSystemProperty.numPlaces.setProperty(Runtime.getRuntime().availableProcessors()); HjSystemProperty.numWorkers.setProperty(1); HjSystemProperty.abstractMetrics.setProperty(true); HjSystemProperty.executionGraph.setProperty(true); HjSystemProperty.speedUpGraph.setProperty(true); launchHabaneroApp(new HjSuspendable() { @Override public void run() throws SuspendableException { finish(new HjSuspendable() { @Override public void run() throws SuspendableException { vectorAdd(X, Y, Z); } }); } }, new Runnable() { @Override public void run() { final HjMetrics actualMetrics = abstractMetrics(); AbstractMetricsManager.dumpStatistics(actualMetrics); } }); } /** *

vectorAdd.

* * @param X an array of double. * @param Y an array of double. * @param Z an array of double. */ public static void vectorAdd(final double[] X, final double[] Y, final double[] Z) throws SuspendableException { finish(new HjSuspendable() { @Override public void run() throws SuspendableException { final HjPlace hjPlace = here(); if (hjPlace.id() != 0) { throw new IllegalStateException("Computation didn't start at home place!"); } final int numPlaces = hjPlace.numPlaces(); final int range = X.length; if (range % numPlaces != 0) { throw new IllegalStateException("Range cannot be divided evenly among available places!"); } final int fragmentLength = range / numPlaces; // Add elements fragmentLength to X.length-1 in parallel for (int i = 1; i < numPlaces; i++) { final int startIndex = i * fragmentLength; final int endIndex = startIndex + fragmentLength; asyncNbAt(hjPlace.place(i), new HjRunnable() { @Override public void run() { for (int ii = startIndex; ii < endIndex; ii++) { doWork(1); Z[ii] = X[ii] + Y[ii]; } } }); } // Add the first fragmentLength elements sequentially at current place for (int i = 0; i < fragmentLength; i++) { doWork(1); Z[i] = X[i] + Y[i]; } } }); System.out.printf("vectorAdd completed with Z[0] = %8.1f and Z[%d] = %8.1f\n", Z[0], Z.length - 1, Z[Z.length - 1]); } }