forked from therault/ttg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mramisc.h
81 lines (67 loc) · 1.88 KB
/
mramisc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#ifndef MADMISC_H_INCL
#define MADMISC_H_INCL
#include <iostream>
#include <utility>
#include <vector>
#include <array>
#include "mrarange.h"
namespace mra {
/// Implements simple Kahan or compensated summation
template <typename T>
class KahanAccumulator {
T sum;
T c;
public:
KahanAccumulator() {} // Must be empty for use in shared memory
KahanAccumulator(T s) : sum(s), c(0) {}
KahanAccumulator& operator=(const T s) {
sum = s;
c = 0;
return *this;
}
KahanAccumulator& operator+=(const T input) {
T y = input - c;
T t = sum + y;
c = (t - sum) - y;
sum = t;
return *this;
}
KahanAccumulator& operator+=(const KahanAccumulator& input) {
(*this) += input.sum;
(*this) += -input.c;
return *this;
}
operator T() const {
return sum;
}
};
/// Easy printing of pairs
template <typename T, typename R>
std::ostream& operator<<(std::ostream& s, const std::pair<T,R>& a) {
s << "(" << a.first << "," << a.second << ")";
return s;
}
/// Easy printing of arrays
template <typename T, size_t N>
std::ostream& operator<<(std::ostream& s, const std::array<T,N>& a) {
s << "[";
for (auto i : range(a.size())) {
s << a[i];
if (i != a.size()-1) s << ", ";
}
s << "]";
return s;
}
/// Easy printing of vectors
template <typename T>
std::ostream& operator<<(std::ostream& s, const std::vector<T>& a) {
s << "[";
for (auto i : range(a.size())) {
s << a[i];
if (i != a.size()-1) s << ", ";
}
s << "]";
return s;
}
}
#endif