Created
June 14, 2010 11:59
-
-
Save alcides/437592 to your computer and use it in GitHub Desktop.
Fibonnaci example implemented on the Aeminium Runtime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package aeminiumruntime.examples.fjtests; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.concurrent.Callable; | |
import aeminiumruntime.Runtime; | |
import aeminiumruntime.Task; | |
import aeminiumruntime.Body; | |
import aeminiumruntime.simpleparallel.ParallelRuntime; | |
public class AeminiumFib { | |
private static int MAX_CALC = 47; | |
final static Runtime rt = new ParallelRuntime(); | |
public static Body createFibBody(final int n, final int[] solution, | |
final int solpos) { | |
return new Body() { | |
public void execute() { | |
if (n <= 1) { | |
Task base = rt.createNonBlockingTask(new Body() { | |
public void execute() { | |
solution[solpos] = 1; | |
} | |
}); | |
rt.schedule(base, Runtime.NO_DEPS); | |
} else { | |
final int[] previous = { -1, -1 }; | |
Collection<Task> branchesDeps = new ArrayList<Task>(); | |
Task branch1 = rt.createNonBlockingTask(createFibBody( | |
n - 2, previous, 0)); | |
rt.schedule(branch1, Runtime.NO_DEPS); | |
branchesDeps.add(branch1); | |
Task branch2 = rt.createNonBlockingTask(createFibBody( | |
n - 1, previous, 1)); | |
rt.schedule(branch2, Runtime.NO_DEPS); | |
branchesDeps.add(branch2); | |
Task join = rt.createNonBlockingTask(new Body() { | |
public void execute() { | |
solution[solpos] = previous[0] + previous[1]; | |
System.out.println(solution[solpos]); | |
} | |
}); | |
rt.schedule(join, branchesDeps); | |
} | |
} | |
}; | |
} | |
public static void main(String[] args) { | |
rt.init(); | |
Task t1 = rt.createNonBlockingTask(new Body() { | |
@Override | |
public void execute() { | |
final int[] result = {-1}; | |
Task calc = rt.createNonBlockingTask(createFibBody(MAX_CALC, result, 0)); | |
rt.schedule(calc, Runtime.NO_DEPS); | |
Collection<Task> printDeps = new ArrayList<Task>(); | |
printDeps.add(calc); | |
Task print = rt.createBlockingTask(new Callable<Body>() { | |
@Override | |
public Body call() throws Exception { | |
return new Body() { | |
@Override | |
public void execute() { | |
System.out.println(result[0]); | |
} | |
}; | |
}} | |
); | |
rt.schedule(print, printDeps); | |
} | |
}); | |
rt.schedule(t1, Runtime.NO_DEPS); | |
rt.shutdown(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment