ParallelArray API is a proposal for integrating data parallelism into the Java language. Here is an overview by Brian Goetz: http://www.ibm.com/developerworks/java/library/j-jtp11137.html
The canonical example for
ParallelArray shows how to compute an average of student grades in parallel. Note the declaration of
students as a
ParallelArray rather than an array, the introduction of a filter ‘isSenior’ and a selector ‘selectGpa’, and the use of an
Closures may help in making this code less verbose. Here is an example based on the BGGA proposal:
However, what we’re interested in with this use case is expressing so-called “reduction operations” over arrays using generators, filters and selectors, and run them in parallel. Making Java a functional language is an interesting but different topic.
Here is the syntax used by Ateji Parallel Extensions for expressing reduction operations (we actually call them “comprehensions”, as in “list comprehension”). This syntax should look familiar, as it inspired by the set notation used in high school mathematics.
students remains a classical Java array. This code is sequential, it becomes parallel when a parallel operator is inserted right after the for keyword:
The parallel operator on reduction expressions provides almost linear speedup on multi-core processors, as soon as the amount of computation for each array element is more than a couple of additions.
Using the comprehension notation, how would you express the number of students graduating this year ? The average of their grades ?