package edu.rice.hj.example.primers; import edu.rice.hj.api.HjFinishAccumulator; import edu.rice.hj.api.HjOperator; import static edu.rice.hj.Module1.*; /** * Send operations within registered finish are not reflected into the result until the end-finish point. Any task can * access accumulators within registered finish. * * @author Shams Imam (shams@rice.edu) */ public class FinishAccumulatorExample2 { /** *

main.

* * @param args an array of {@link String} objects. */ public static void main(final String[] args) { launchHabaneroApp(() -> { final HjFinishAccumulator a = newFinishAccumulator(HjOperator.SUM, int.class); final HjFinishAccumulator b = newFinishAccumulator(HjOperator.MIN, double.class); // Send by parent a.put(1); b.put(10.0); checkAccumulators(a, b, 1, 10.0, "A"); finish(a, b, () -> { a.put(1); b.put(7.0); // send by child 1 asyncNb(() -> { a.put(1); b.put(5.5); checkAccumulators(a, b, 1, 10.0, "B"); }); // send by child 2 finish(() -> { asyncNb(() -> { a.put(1); b.put(3.5); checkAccumulators(a, b, 1, 10.0, "C"); }); }); checkAccumulators(a, b, 1, 10.0, "D"); }); checkAccumulators(a, b, 4, 3.5, "E"); }); } private static void checkAccumulators( final HjFinishAccumulator a, final HjFinishAccumulator b, final int expectedA, final double expectedB, final String prefixString) { assert (a.get().intValue() == expectedA); assert (Double.compare(b.get().doubleValue(), expectedB) == 0); System.out.println(prefixString + ": a = " + a.get() + ", b = " + b.get()); } }