package edu.rice.hj.example.comp322.labs.lab3;
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.*;
/**
* ArraySum2.hj --- Parallel recursive example program for computing the sum of an array using futures.
*
* This example program creates an array of n random int's, and computes their sum in parallel. The default value of n
* is 1024, but any size can be provided as argv[0] by using the command "run ArraySum2 size" in the DrHJ Interactions
* Pane or "hj ArraySum2 size" on the command line.
*
* NOTE: this example program is intended for illustrating abstract performance metrics, and is not intended to be used
* as abenchmark for real performance.
*
* @author Vivek Sarkar (vsarkar@rice.edu)
*/
public class ArraySum2 {
static final int default_n = 128;
static final String err = "Incorrect argument for array size (should be > 0), assuming n = " + default_n;
static int computeSum(final int[] X, final int lo, final int hi) throws SuspendableException {
if (lo > hi) {
return 0;
} else if (lo == hi) {
return X[lo];
} else {
final int mid = (lo + hi) / 2;
final HjFuture sum1 = futureNb(new HjCallable() {
@Override
public Integer call() throws SuspendableException {
return computeSum(X, lo, mid);
}
});
final HjFuture sum2 = futureNb(new HjCallable() {
@Override
public Integer call() throws SuspendableException {
return computeSum(X, mid + 1, hi);
}
});
final int local_sum = sum1.get() + sum2.get();
doWork(1); // Assume that add takes 1 unit of time, and that everything else is free
return local_sum;
}
} // computeSum
/**
* main.
*
* @param argv an array of {@link String} objects.
*/
public static void main(final String[] argv) {
// Setup metrics
HjSystemProperty.abstractMetrics.setProperty(true);
launchHabaneroApp(new HjSuspendable() {
@Override
public void run() throws SuspendableException {
// Initialization
int n;
final int[] X;
if (argv.length != 0) {
try {
n = Integer.parseInt(argv[0]);
if (n <= 0) {
// Bad value of n
System.out.println(err);
n = default_n;
}
} catch (final Throwable e) {
System.out.println(err);
n = default_n;
}
} else { // argv.length == 0
n = default_n;
}
X = new int[n];
final Random myRand = new Random(n);
for (int i = 0; i < n; i++) {
X[i] = myRand.nextInt(n);
}
// Recursive parallel computation
final int sum = computeSum(X, 0, n - 1);
// Output
System.out.println("Sum of " + n + " elements = " + sum);
}
}, new Runnable() {
@Override
public void run() {
// Print out the metrics data
final HjMetrics actualMetrics = abstractMetrics();
AbstractMetricsManager.dumpStatistics(actualMetrics);
}
});
}
}