Parallel Summation in Java
Summation is the common operation of adding a sequence of numbers together, resulting in their total. The trivial implementation is to iterate over the full collection of numbers, keeping a running total as you progress. For small sequences, a single threaded implementation will suffice, however, when the size increases use of other available CPU cores helps provide necessary speed optimisations. As addition is an associative operation it makes no difference to the end result in which order we process the collection, this behavior works well for are implementation design. Below is an example implementation which splits the summation of a sequence of numbers into (close to) equal collections, each being processed in parallel within their own thread.
Looking at the implementation above you will notice that I have taken advantage of static functionality to combine both the sum and thread instances required to complete the task. Calling ‘parallelSum’ with a single argument (being the specified array), the system is queried on how many available processing cores are present. We then create ‘Summation’ instances that are supplied with the low and high range of indexes within the subject array they are required to process. These are then started and subsequently joined into the main thread for the final round of partial sum addition to complete the process.
So as to see the benefits of parallelising such an operation, an example benchmark has been provided below. The implementation is provided in this case with 100,000,000 random integers between 1 and 100, and timed on its performance to run as both a single and parallel operation.
Looking at the results above, you will see that using the parallelised approach provides us with noticeable speed gains. An interesting observation I made when running the benchmark was that the speed increases after splitting the subject operation into 2 threads did not significantly alter the time taken.