def test_fib(): def fib(i): if i < 3: return taskloaf.ready(1) else: return (fib(i - 1) .then(lambda a: fib(i - 2).then(lambda b: a + b)) .unwrap()) ctx = taskloaf.launch_mpi() assert(fib(20).get() == 6765)
# future. You can think of a future as a box around a value that # will eventually be computed, but has not necessarily been computed # yet. Futures are eventually "filled" after the task has run. return tl.async(lambda: fib_serial(index)); else: # "when_all" tells taskloaf to return a new future that will be filled # with the values from the futures that are passed as arguments # after all those futures have been filled. # "when_all" is short for "when all of these have been computed" return tl.when_all( fib(index - 1), fib(index - 2) # "future.then" tells taskloaf to run the provided function on the # values inside the future after it has been filled ).then(lambda x, y: x + y) # Finish up... def done(x): print(x) # shutdown tells taskloaf that it should end the program and not expect any # more tasks to run. return tl.shutdown() def main(): import sys n = int(sys.argv[1]) # Calculate the specified fibonacci number, then print it and shutdown return fib(n).then(done) # Launch an MPI taskloaf application with main as the first task. tl.launch_mpi(main);
def test_send_task(): ctx = taskloaf.launch_mpi() assert(taskloaf.task(1, fnc).get() == 10)