template<typename T = double, unsigned
int N = 1>
class ROOT::Math::KahanSum< T, N >
The Kahan summation is a compensated summation algorithm, which significantly reduces numerical errors when adding a sequence of finite-precision floating point numbers.
This is done by keeping a separate running compensation (a variable to accumulate small errors).
Auto-vectorisable accumulation
This class can internally use multiple accumulators (template parameter N
). When filled from a collection that supports index access from a contiguous block of memory, compilers such as gcc, clang and icc can auto-vectorise the accumulation. This happens by cycling through the internal accumulators based on the value of "`index % N`", so N
accumulators can be filled from a block of N
numbers in a single instruction.
The usage of multiple accumulators might slightly increase the precision in comparison to the single-accumulator version with N = 1
. This depends on the order and magnitude of the numbers being accumulated. Therefore, in rare cases, the accumulation result can change in dependence of N, even when the data are identical. The magnitude of such differences is well below the precision of the floating point type, and will therefore mostly show in the compensation sum(see Carry()). Increasing the number of accumulators therefore only makes sense to speed up the accumulation, but not to increase precision.
- Parameters
-
T | The type of the values to be accumulated. |
N | Number of accumulators. Defaults to 1. Ideal values are the widths of a vector register on the relevant architecture. Depending on the instruction set, good values are:
- AVX2-float: 8
- AVX2-double: 4
- AVX512-float: 16
- AVX512-double: 8
|
Examples
std::vector<double> numbers(1000);
for (std::size_t i=0; i<1000; ++i) {
numbers[i] = rand();
}
k.
Add(numbers.begin(), numbers.end());
The Kahan summation is a compensated summation algorithm, which significantly reduces numerical error...
void Add(T x)
Single-element accumulation. Will not vectorise.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
static KahanSum< T, N > Accumulate(Iterator begin, Iterator end, T initialValue=T{})
Iterate over a range and return an instance of a KahanSum.
Definition at line 122 of file Util.h.
|
template<class Iterator > |
| KahanSum (Iterator sumBegin, Iterator sumEnd, Iterator carryBegin, Iterator carryEnd) |
| Initialise the sum with a pre-existing state. More...
|
|
template<unsigned int M> |
| KahanSum (KahanSum< T, M > const &other) |
| Constructor to create a KahanSum from another KahanSum with a different number of accumulators. More...
|
|
| KahanSum (T initialSumValue, T initialCarryValue) |
| Initialise with a sum value and a carry value. More...
|
|
| KahanSum (T initialValue=T{}) |
| Initialise the sum. More...
|
|
template<class Container_t > |
void | Add (const Container_t &inputs) |
| Fill from a container that supports index access. More...
|
|
template<class Iterator > |
void | Add (Iterator begin, Iterator end) |
| Accumulate from a range denoted by iterators. More...
|
|
void | Add (T x) |
| Single-element accumulation. Will not vectorise. More...
|
|
void | AddIndexed (T input, std::size_t index) |
| Add input to the sum. More...
|
|
T | Carry () const |
|
| operator T () const |
| Auto-convert to type T. More...
|
|
template<typename U > |
KahanSum & | operator+= (const KahanSum< U > &arg) |
| Add arg into accumulator. Does not vectorise. More...
|
|
KahanSum< T, N > & | operator+= (T arg) |
| Add arg into accumulator. Does not vectorise. More...
|
|
KahanSum< T, N > & | operator-= (KahanSum< T, N > const &other) |
| Subtract other KahanSum. More...
|
|
T | Result () const |
|
T | Sum () const |
|