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 static edu.rice.hj.Module1.*; /** * ParallelMap --- Converts input strings to lowercase and removes punctuation *

* 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 ParallelMap2 { /** * Constant DEFAULT_N=100 */ public static final int DEFAULT_N = 100; private static final String[] INPUTS = ("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed " + "do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis " + "nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.").split("\\s+"); // Store the cubes of corresponding elements from X into Y /** *

mapFuncForLowercase.

* * @param X an array of {@link String} objects. * @param Y an array of {@link String} objects. */ public static void mapFuncForLowercase(final String[] X, final String[] Y) throws SuspendableException { forall(0, X.length - 1, new HjSuspendingProcedure() { @Override public void apply(final Integer i) throws SuspendableException { final String loopStr = X[i]; doWork(loopStr.length()); // work equals length of input string final String resStr = loopStr.toLowerCase().replaceAll("\\W", ""); Y[i] = resStr; } }); } /** *

main.

* * @param argv an array of {@link String} objects. */ public static void main(final String[] argv) { // Initialization final int n = INPUTS.length; final String[] X = new String[n]; final String[] Y = new String[n]; System.arraycopy(INPUTS, 0, X, 0, n); launchHabaneroApp(new HjSuspendable() { @Override public void run() throws SuspendableException { mapFuncForLowercase(X, Y); } }); System.out.println("Done 'lowercasing' the elements of X into Y"); final int numElementsToPrint = Math.min(n - 1, 10); printSomeElements(X, numElementsToPrint, "Elements of X: "); printSomeElements(Y, numElementsToPrint, "Elements of Y: "); } private static void printSomeElements(final String[] anArray, final int numElems, final String prefixMsg) { System.out.print(prefixMsg); for (int i = 0; i < numElems; i++) { System.out.printf("%s ", anArray[i]); } System.out.println("..."); } }