package edu.rice.hj.example.comp322; import edu.rice.hj.api.HjSuspendable; import edu.rice.hj.api.HjSuspendingProcedure; import edu.rice.hj.api.SuspendableException; import java.util.Random; import static edu.rice.hj.Module1.*; /** * ParallelMap --- Cubes the elements of an array *

* The purpose of this example is to illustrate abstract metrics and Amdahl's Law * using a simple map function. * * @author Vivek Sarkar (vsarkar@rice.edu) */ public class ParallelMap { /** * Constant DEFAULT_N=100 */ public static final int DEFAULT_N = 100; // Store the cubes of corresponding elements from X into Y /** *

mapFuncForCubes.

* * @param X an array of double. * @param Y an array of double. */ public static void mapFuncForCubes(final double[] X, final double[] Y) throws SuspendableException { forall(0, X.length - 1, new HjSuspendingProcedure() { @Override public void apply(final Integer i) throws SuspendableException { doWork(1); Y[i] = Math.pow(X[i], 3); } }); } /** *

main.

* * @param argv an array of {@link String} objects. */ public static void main(final String[] argv) { // Initialization final int n; if (argv.length > 0) { n = Integer.parseInt(argv[0]); } else { n = DEFAULT_N; } final double[] X = new double[n]; final double[] Y = new double[n]; final Random myRand = new Random(n); for (int i = 0; i < n; i++) { X[i] = myRand.nextInt(n); } launchHabaneroApp(new HjSuspendable() { @Override public void run() throws SuspendableException { mapFuncForCubes(X, Y); } }); System.out.println("Done cubing elements of X into Y"); final int numElementsToPrint = Math.min(n - 1, 8); printSomeElements(X, numElementsToPrint, "Elements of X: "); printSomeElements(Y, numElementsToPrint, "Elements of Y: "); } private static void printSomeElements(final double[] anArray, final int numElems, final String prefixMsg) { System.out.print(prefixMsg); for (int i = 0; i < numElems; i++) { System.out.printf("%10.1f ", anArray[i]); } System.out.println("..."); } }