package edu.rice.hj.example.primers; import edu.rice.hj.api.HjDataDrivenFuture; import static edu.rice.hj.Module1.*; /** *

FibWithDdfs class.

* * @author Shams Imam (shams@rice.edu) */ public class FibWithDdfs { /** *

fib.

* * @param n a int. * @param res a {@link edu.rice.hj.api.HjDataDrivenFuture} object. */ public static void fib(final int n, HjDataDrivenFuture res) { if (n <= 0) { res.put(0); return; } else if (n == 1) { res.put(1); return; } // compute f1 asynchronously final HjDataDrivenFuture f1 = newDDF(); asyncNb(() -> fib(n - 1, f1)); // compute f2 serially (f1 is done asynchronously). final HjDataDrivenFuture f2 = newDDF(); fib(n - 2, f2); // wait for dependences, before updating the result asyncNbAwait(f1, f2, () -> res.put(f1.safeGet() + f2.safeGet())); } /** *

main.

* * @param args an array of {@link String} objects. */ public static void main(final String[] args) { final int N = 10; final int[] result = new int[N]; launchHabaneroApp(() -> { for (int i = 0; i < N; i++) { final int index = i; final HjDataDrivenFuture f1 = newDDF(); fib(index, f1); asyncNbAwait(f1, () -> result[index] = f1.safeGet()); } }); for (int i = 0; i < N; i++) { System.out.printf("fib(%2d) = %2d \n", i, result[i]); } } }