Matrix Multiplication using ForkJoin
Finally, we show how to adapt the Jama algorithm to jsr166y/forkjoin
. We begin by creating a ParallelDoubleArray, based on the input array b[0]; this is because the outer loop of the original Jama algorithm iterates over j from 0 to p; this is the same indexing as for the array b[0]. However, we do not care about the contents of the array (see the sidebar below). Next, we use ParallelArray.replaceWithMappedIndex
(Ops.IntAndDoubleToDouble
op)
to run the inner loop in parallel over the entire array. This performs the equivalent of the sequential for loop
for (int j = 0; j <>
or the threaded
for (int j = from; j <>
with the ForkJoin framework manages the partitioning and outer loop for us. It passes the index j to theOps.IntAndDoubleToDouble operator via its op(int a, double b)
method as well as the value b[0][j], which is unused. The method body transposes Bcolj, then performs the two nested loops. Note that since ForkJoin uses a single operator instance (from the anonymous inner class), we must allocate the transient Bcolj array inside the op method. If this were not a local array inside the operation method but was instead and instance field, multiple threads would update it concurrently while working on different columns, causing corruption. (Tim Peierls caught this bug in an earlier version of this algorithm.) Like the threaded example above, this implementation does not require explicit synchronization.
Read full article from Soo Hwan's Blog: Matrix multiplication
No comments:
Post a Comment