Eigen::KahanSum< Scalar > Class Template Reference

Kahan algorithm based accumulator. More...

#include <SparseInverse.h>

Public Member Functions

Scalar value ()
 
void operator+= (Scalar increment)
 

Private Attributes

Scalar _sum {}
 
Scalar _correction {}
 

Detailed Description

template<typename Scalar>
class Eigen::KahanSum< Scalar >

Kahan algorithm based accumulator.

The Kahan sum algorithm guarantees to bound the error from floating point accumulation to a fixed value, regardless of the number of accumulations performed. Naive accumulation accumulates errors O(N), and pairwise O(logN). However pairwise also requires O(logN) memory while Kahan summation requires O(1) memory, but 4x the operations / latency.

NB! Do not enable associative math optimizations, they may cause the Kahan summation to be optimized out leaving you with naive summation again.

Member Function Documentation

◆ operator+=()

template<typename Scalar >
void Eigen::KahanSum< Scalar >::operator+= ( Scalar  increment)
inline
43  {
44  const Scalar correctedIncrement = increment + _correction;
45  const Scalar previousSum = _sum;
46  _sum += correctedIncrement;
47  _correction = correctedIncrement - (_sum - previousSum);
48  }
SCALAR Scalar
Definition: bench_gemm.cpp:45
Scalar _correction
Definition: SparseInverse.h:38
Scalar _sum
Definition: SparseInverse.h:37

References Eigen::KahanSum< Scalar >::_correction, and Eigen::KahanSum< Scalar >::_sum.

◆ value()

template<typename Scalar >
Scalar Eigen::KahanSum< Scalar >::value ( )
inline
41 { return _sum; }

References Eigen::KahanSum< Scalar >::_sum.

Member Data Documentation

◆ _correction

template<typename Scalar >
Scalar Eigen::KahanSum< Scalar >::_correction {}
private

◆ _sum

template<typename Scalar >
Scalar Eigen::KahanSum< Scalar >::_sum {}
private

The documentation for this class was generated from the following file: