In pseudocode, the pairwise summation algorithm for an array x of length n ≥ 0 can be written:
For some sufficiently small N, this algorithm switches to a naive loop-based summation as a base case, whose error bound is O(Nε).8 The entire sum has a worst-case error that grows asymptotically as O(ε log n) for large n, for a given condition number (see below).
In an algorithm of this sort (as for divide and conquer algorithms in general9), it is desirable to use a larger base case in order to amortize the overhead of the recursion. If N = 1, then there is roughly one recursive subroutine call for every input, but more generally there is one recursive call for (roughly) every N/2 inputs if the recursion stops at exactly n = N. By making N sufficiently large, the overhead of recursion can be made negligible (precisely this technique of a large base case for recursive summation is employed by high-performance FFT implementations10).
Regardless of N, exactly n−1 additions are performed in total, the same as for naive summation, so if the recursion overhead is made negligible then pairwise summation has essentially the same computational cost as for naive summation.
A variation on this idea is to break the sum into b blocks at each recursive stage, summing each block recursively, and then summing the results, which was dubbed a "superblock" algorithm by its proposers.11 The above pairwise algorithm corresponds to b = 2 for every stage except for the last stage which is b = N.
Dalton, Wang & Blainey (2014) describe a iterative, "shift-reduce" formulation for pairwise summation. It can be unrolled and sped up using SIMD instructions. The non-unrolled version is:12
Suppose that one is summing n values xi, for i = 1, ..., n. The exact sum is:
(computed with infinite precision).
With pairwise summation for a base case N = 1, one instead obtains S n + E n {\displaystyle S_{n}+E_{n}} , where the error E n {\displaystyle E_{n}} is bounded above by:13
where ε is the machine precision of the arithmetic being employed (e.g. ε ≈ 10−16 for standard double precision floating point). Usually, the quantity of interest is the relative error | E n | / | S n | {\displaystyle |E_{n}|/|S_{n}|} , which is therefore bounded above by:
In the expression for the relative error bound, the fraction (Σ|xi|/|Σxi|) is the condition number of the summation problem. Essentially, the condition number represents the intrinsic sensitivity of the summation problem to errors, regardless of how it is computed.14 The relative error bound of every (backwards stable) summation method by a fixed algorithm in fixed precision (i.e. not those that use arbitrary-precision arithmetic, nor algorithms whose memory and time requirements change based on the data), is proportional to this condition number.15 An ill-conditioned summation problem is one in which this ratio is large, and in this case even pairwise summation can have a large relative error. For example, if the summands xi are uncorrelated random numbers with zero mean, the sum is a random walk and the condition number will grow proportional to n {\displaystyle {\sqrt {n}}} . On the other hand, for random inputs with nonzero mean the condition number asymptotes to a finite constant as n → ∞ {\displaystyle n\to \infty } . If the inputs are all non-negative, then the condition number is 1.
Note that the 1 − ε log 2 n {\displaystyle 1-\varepsilon \log _{2}n} denominator is effectively 1 in practice, since ε log 2 n {\displaystyle \varepsilon \log _{2}n} is much smaller than 1 until n becomes of order 21/ε, which is roughly 101015 in double precision.
In comparison, the relative error bound for naive summation (simply adding the numbers in sequence, rounding at each step) grows as O ( ε n ) {\displaystyle O(\varepsilon n)} multiplied by the condition number.16 In practice, it is much more likely that the rounding errors have a random sign, with zero mean, so that they form a random walk; in this case, naive summation has a root mean square relative error that grows as O ( ε n ) {\displaystyle O(\varepsilon {\sqrt {n}})} and pairwise summation has an error that grows as O ( ε log n ) {\displaystyle O(\varepsilon {\sqrt {\log n}})} on average.17
Pairwise summation is the default summation algorithm in NumPy18 and the Julia technical-computing language,19 where in both cases it was found to have comparable speed to naive summation (thanks to the use of a large base case).
Other software implementations include the HPCsharp library20 for the C# language and the standard library summation21 in D.
Higham, Nicholas J. (1993), "The accuracy of floating point summation", SIAM Journal on Scientific Computing, 14 (4): 783–799, Bibcode:1993SJSC...14..783H, CiteSeerX 10.1.1.43.3535, doi:10.1137/0914050 /wiki/SIAM_Journal_on_Scientific_Computing ↩
Manfred Tasche and Hansmartin Zeuner Handbook of Analytic-Computational Methods in Applied Mathematics Boca Raton, FL: CRC Press, 2000). ↩
S. G. Johnson and M. Frigo, "Implementing FFTs in practice, in Fast Fourier Transforms, edited by C. Sidney Burrus (2008). http://cnx.org/content/m16336/latest/ ↩
Higham, Nicholas (2002). Accuracy and Stability of Numerical Algorithms (2 ed). SIAM. pp. 81–82. ↩
Radu Rugina and Martin Rinard, "Recursion unrolling for divide and conquer programs," in Languages and Compilers for Parallel Computing, chapter 3, pp. 34–48. Lecture Notes in Computer Science vol. 2017 (Berlin: Springer, 2001). http://people.csail.mit.edu/rinard/paper/lcpc00.pdf ↩
Anthony M. Castaldo, R. Clint Whaley, and Anthony T. Chronopoulos, "Reducing floating-point error in dot product using the superblock family of algorithms," SIAM J. Sci. Comput., vol. 32, pp. 1156–1174 (2008). ↩
Dalton, Barnaby; Wang, Amy; Blainey, Bob (16 February 2014). SIMDizing pairwise sums: a summation algorithm balancing accuracy with throughput. 2014 Workshop on Workshop on Programming Models for SIMD/Vector Processing - WPMVP ’14. pp. 65–70. doi:10.1145/2568058.2568070. /wiki/Doi_(identifier) ↩
L. N. Trefethen and D. Bau, Numerical Linear Algebra (SIAM: Philadelphia, 1997). ↩
ENH: implement pairwise summation, github.com/numpy/numpy pull request #3685 (September 2013). https://github.com/numpy/numpy/pull/3685 ↩
RFC: use pairwise summation for sum, cumsum, and cumprod, github.com/JuliaLang/julia pull request #4039 (August 2013). https://github.com/JuliaLang/julia/pull/4039 ↩
https://github.com/DragonSpit/HPCsharp HPCsharp nuget package of high performance C# algorithms https://github.com/DragonSpit/HPCsharp ↩
"std.algorithm.iteration - D Programming Language". dlang.org. Retrieved 2021-04-23. https://dlang.org/phobos/std_algorithm_iteration.html#sum ↩