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 static edu.rice.hj.Module1.*; /** * PipelineWithFutures --- Computes (x + 1)^2 using futures *

* The purpose of this example is to illustrate abstract metrics while using futures. It computes (x + 1)^2 using a * pipeline to compute the individual fragments x ^ 2, 2 * x, and 1 in left-to-right evaluation order. The final stage * of the pipeline prints the result * * @author Shams Imam (shams@rice.edu) */ public class PipelineWithFutures { private static final int[] INPUTS = {1, 2, 3, 4, 5, 6, 7, 8, 9}; /** *

main.

* * @param args an array of {@link String} objects. */ public static void main(final String[] args) { HjSystemProperty.abstractMetrics.setProperty(true); launchHabaneroApp(new HjSuspendable() { @Override public void run() throws SuspendableException { finish(new HjSuspendable() { @Override public void run() throws SuspendableException { // compute the individual elements in parallel for (final int input : INPUTS) { pipeline(new Data(input, 0)); } } }); final HjMetrics actualMetrics = abstractMetrics(); AbstractMetricsManager.dumpStatistics(actualMetrics); } }); } private static void pipeline(final Data data) { // computes x ^ 2 + accum final HjFuture firstFuture = futureNb(new HjCallable() { @Override public Data call() throws SuspendableException { final Data dataItem = data; final int input = dataItem.input; doWork(2); // one for multiple, one for add final int result = dataItem.accum + (input * input); return new Data(input, result); } }); // computes 2 * x + accum final HjFuture secondFuture = futureNb(new HjCallable() { @Override public Data call() throws SuspendableException { final Data dataItem = firstFuture.get(); final int input = dataItem.input; doWork(2); // one for multiple, one for add final int result = dataItem.accum + (2 * input); return new Data(input, result); } }); // computes 1 + accum final HjFuture thirdFuture = futureNb(new HjCallable() { @Override public Data call() throws SuspendableException { final Data dataItem = secondFuture.get(); final int input = dataItem.input; doWork(1); // one for add final int result = dataItem.accum + 1; return new Data(input, result); } }); // prints the result async(new HjSuspendable() { @Override public void run() throws SuspendableException { final Data dataItem = thirdFuture.get(); final int input = dataItem.input; final int result = dataItem.accum; System.out.printf(" f(%3d) = %3d \n", input, result); } }); } private static class Data { final int input; final int accum; private Data(final int input, final int accum) { this.input = input; this.accum = accum; } } }