Thursday, October 23, 2008

Java Fork/Join Framework

In order to take advantage of the multi-core capabilities of the existing and future machines, programmers still have to convert their algorithms manually. The Java fork/join framework, created by Doug Lea under the auspices of the JSR166 expert group, is a concurrency primitive that aims to facilitate this conversion effort.

The fork/join is multi-core-friendly, lightweight parallel framework, that uses the strategy of recursively splitting a task into smaller sub-tasks; forking the sub-tasks into separate processes or threads, so that they run in parallel on multiple cores; and joining all sub-tasks to compose a result to return. The fork/join framework is expected to be added to Java 7.

The code listing shown here demonstrates a simple parallel algorithm to compute the Fibonacci numbers. On a two-core machine, it achieves a nearly twofold speedup over an equivalent sequential algorithm (click to see a clearer image):

Note that the program strives to describe the algorithm based on the fork/join primitives; the details of threading and scheduling are hidden by the framework. Notice also that the program moves to sequential computation for problems below a certain size. This kind of tuning is usually required to produce optimal efficiency from distributed algorithms.

No comments:

Post a Comment