import edu.rice.hj.api.HjMetrics; import edu.rice.hj.runtime.config.HjSystemProperty; import edu.rice.hj.runtime.metrics.AbstractMetricsManager; import static edu.rice.hj.Module1.*; /** * This class contains a test harness for the SumReduction client. * It assumes availability of a completed GeneralizedReduce class to be implemented by you. * * This is a test class for your homework and should not be modified. * * @author Vivek Sarkar (vsarkar@rice.edu) */ public class TestSumReduction { /** * The main method. Input parameters are as follows: * 1 input - the first input is the length of the array to use * No other usage is allowed * @param args - input paramters */ public static void main(final String[] args) { int n; if (args.length == 1) { n = Integer.parseInt(args[0]); } else { throw new RuntimeException("Usage: run TestSumReduction n"); } final Integer[] inputArray = new Integer[n]; for (int i = 0; i < n; i++) { inputArray[i] = i; } final Integer correctSum = calculateCorrectSum(inputArray); System.setProperty(HjSystemProperty.abstractMetrics.propertyKey(), "true"); initializeHabanero(); reduction(inputArray, correctSum); finalizeHabanero(); final HjMetrics actualMetrics = abstractMetrics(); AbstractMetricsManager.dumpStatistics(actualMetrics); } /** * Calculate the sum of the input array using array reduction. * @param inputArray The input array * @param correctSum The correct reduction value. Should be tested against * to determine validity of parallel reduction */ public static void reduction( final Integer[] inputArray, final Integer correctSum) { final GeneralizedReduceApp app = new SumReduction(); final GeneralizedReduce red = new GeneralizedReduce<>(app); final Integer output = red.run(inputArray); System.out.println("Correct output = " + correctSum); System.out.println("Reduced output = " + output); if (correctSum.equals(output)) { System.out.println("Reduction SUCCEEDED"); } else { System.out.println("Reduction FAILED"); } } /** * Sums the elements in the array. Gaurenteed ot be * correct for testing purposes * * @param inputArray An array of integers * @return The sum of all elements in the array */ public static Integer calculateCorrectSum(final Integer[] inputArray) { int sum = 0; for (final Integer loopItem : inputArray) { sum += loopItem; } return sum; } }