Similar to Multilisp and other functional programming languages, CHARM++ provides the abstraction of futures. In simple terms, a future is a contract with the runtime system to evaluate an expression asynchronously with the calling program. This mechanism promotes the evaluation of expressions in parallel as several threads concurrently evaluate the futures created by a program.
In some ways, a future resembles lazy evaluation. Each future is assigned to a particular thread (or to a chare, in CHARM++ ) and its value will be eventually delivered to the calling program. Once the future is created, a reference is returned immediately. If the value is needed, however, the calling program blocks until the value is available.
CHARM++ provides all the necessary infrastructure to use futures by means of the following functions:
To illustrate the use of all these functions, a Fibonacci example in CHARM++ using futures in presented below:
The constant THRESHOLD sets a limit value for computing the Fibonacci number with futures or just with the sequential procedure. Given value n, the program creates two futures using CkCreateFuture. Those futures are used to create two new chares that will carry on the computation. Next, the program blocks until the two values of the Fibonacci's recurrence have been evaluated. Function CkWaitFuture is used for that purpose. Finally, the program checks whether it is the root of the evaluation or not. The very first chare created with a future is going to be the root. If a chare is not a root, it must indicate its future has finished computing the value. CkSendToFuture is meant to return the value for the current future.
Other functions complete the API for futures. CkReleaseFuture destroys a future. CkProbeFuture test if the future has already finished computing the value of the expression.
The CONVERSE version of future functions can be found in the CONVERSE manual.
February 12, 2012
Charm Homepage