The parallel implementation of the multiplication operation.

This method uses a digit-based arithmetics where the numbers are stored with characters representing the digits, based on on the standard C++ std::string.

The multiplication uses the method that we can apply on paper-based calculations:

  4478 * 379
------
 13434       <-- 4478 * 3 (shifted by two to the left) (two: the number of remaining number of the digits in the multiplier)
  31346      <-- 4478 * 7 (     -"-   one     -"-    )
   40302     <-- 4478 * 9
--------
 1697162     <-- the sum of the above values

The key to the parallelization of this method is that we can split up the one-digit multiplications into groups. These groups can then be executed as parallel tasks and at the end we have to rotate the results by the proper value (the remaining number of the digits in the multiplier) and then add these values to calculate the final result.

Measurement results (horizontal cells: the length of the numbers - both the multiplicand and the multiplier):

Threads1000500010000
04883
13947
22012
31420
41139

It is interesting to note that even the one-threaded parallel process improves the performance. The background of this phenomenon is that while the non-parallel implementation shifts the subresults at every digit of the multiplier - with the performance overhead of the string resizing -, the parallel implementation does this only in the subtasks. When the execution of the subtasks finishes the subresults of these tasks are shifted to the proper position with only on string resizing and therefore the overall number of resizings is smaller in this case.


Attila NAGY Last updated: 2018-08-30