Peano 4
Loading...
Searching...
No Matches
Measurement.cpp
Go to the documentation of this file.
1// This file is part of the Peano project. For conditions of distribution and
2// use, please see the copyright notice at www.peano-framework.org
3
4#include "Measurement.h"
5
6#include <cmath>
7#include <limits>
8#include <sstream>
9
10#include "tarch/Assertions.h"
11#include "tarch/la/Scalar.h"
13
14
15tarch::logging::Log tarch::timing::Measurement::_log("tarch::timing::Measurement");
16
17
19 _accuracy(accuracy),
20 _accumulatedValue(0.0),
21 _lastValue(0.0),
22 _accumulatedSquares(0.0),
23 _numberOfMeasurements(0.0),
24 _isAccurateValue(false),
25 _min(std::numeric_limits<double>::max()),
26 _max(-std::numeric_limits<double>::max()),
27 _minMeasurement(-1),
28 _maxMeasurement(-1) {
29
30 assertion1(accuracy >= 0.0, accuracy);
31}
32
33
35 _accumulatedValue = 0.0;
36 _accumulatedSquares = 0.0;
37 _numberOfMeasurements = 0.0;
38 _isAccurateValue = false;
39 _min = std::numeric_limits<double>::max();
40 _max = -std::numeric_limits<double>::max();
41 _minMeasurement = -1;
42 _maxMeasurement = -1;
43}
44
45
47 assertion(_numberOfMeasurements > 0.0);
48 return _accumulatedValue / _numberOfMeasurements;
49}
50
51
53 assertion(_numberOfMeasurements > 0.0);
54 return (_accumulatedValue + newValue) / (_numberOfMeasurements + 1.0);
55}
56
57
58double tarch::timing::Measurement::getAccumulatedValue() const { return _accumulatedValue; }
59
60
62 return _lastValue;
63}
64
65
67 if (tarch::la::smaller(_numberOfMeasurements, 1.0)) {
68 assertionEquals(_accumulatedValue, 0.0);
69 return 0.0;
70 } else {
71 return getMeanValue();
72 }
73}
74
75
77 if (tarch::la::smaller(_numberOfMeasurements, 1.0)) {
78 assertionEquals(_accumulatedValue, 0.0);
79 return 0.0;
80 } else {
81 return std::sqrt(std::abs(_accumulatedSquares / _numberOfMeasurements - getMeanValue() * getMeanValue()));
82 }
83}
84
85
86bool tarch::timing::Measurement::isAccurateValue() const { return _isAccurateValue; }
87
88
89void tarch::timing::Measurement::setAccuracy(const double& value) {
90 assertion(value >= 0.0);
91 _accuracy = value;
92}
93
94
96 assertion(factor > 0.0);
97 _accuracy /= factor;
98}
99
100
101void tarch::timing::Measurement::setValue(const double& value) {
102 if (tarch::la::smallerEquals(_numberOfMeasurements, 1.0)) {
103 assertion(!_isAccurateValue);
104 } else {
105 const double differenceDueToNewValue = tarch::la::smallerEquals(getMeanValue(), 1.0)
106 ? std::abs(getMeanValueOfNextStep(value) - getMeanValue())
107 : std::abs(getMeanValueOfNextStep(value) / getMeanValue() - 1.0);
108 _isAccurateValue = _numberOfMeasurements > 0 && differenceDueToNewValue < _accuracy;
109 }
110
111 if (_min > value) {
112 _min = value;
113 _minMeasurement = _numberOfMeasurements;
114 }
115 if (_max < value) {
116 _max = value;
117 _maxMeasurement = _numberOfMeasurements;
118 }
119
120 _accumulatedValue += value;
121 _accumulatedSquares += value * value;
122 _numberOfMeasurements += 1.0;
123 _lastValue = value;
124
125 logDebug(
126 "setValue",
127 "set "
128 << static_cast<int>(_numberOfMeasurements) << "th value = " << value << ", current deviation="
129 << ((_accumulatedValue + value) / (_numberOfMeasurements + 1) - _accumulatedValue / _numberOfMeasurements)
130 << ", accumulatedValue=" << _accumulatedValue << ", _numberOfMeasurements" << _numberOfMeasurements
131 << ", isAccurateValue=" << _isAccurateValue << ", _accuracy=" << _accuracy
132 );
133}
134
135
136int tarch::timing::Measurement::getNumberOfMeasurements() const { return static_cast<int>(_numberOfMeasurements); }
137
138
140 std::ostringstream msg;
141 msg << "(avg=";
142 if (_numberOfMeasurements > 0) {
143 msg << getValue();
144 } else {
145 msg << "nan";
146 }
147 msg
148 << ",#measurements=" << _numberOfMeasurements << ",max=" << _max << "(value #"
149 << (static_cast<int>(_maxMeasurement)) << ")"
150 << ",min=" << _min << "(value #" << (static_cast<int>(_minMeasurement)) << ")";
151
152 if (_numberOfMeasurements > 0) {
153 msg
154 << ",+" << (_max - getValue()) / getValue() * 100.0 << "%"
155 << ",-" << (getValue() - _min) / getValue() * 100.0 << "%"
156 << ",std-deviation=" << getStandardDeviation();
157 }
158
159 if (_accuracy > 0.0) {
160 msg << ",eps=" << _accuracy;
161 if (!_isAccurateValue) {
162 msg << " [not a valid averaged value yet]";
163 } else {
164 msg << " [converged]";
165 }
166 }
167
168 msg << ")";
169
170 return msg.str();
171}
172
173
175 assertion1(isAccurateValue(), toString());
176 return _max;
177}
178
179
181 assertion1(isAccurateValue(), toString());
182 return _min;
183}
184
185
186double tarch::timing::Measurement::getAccuracy() const { return _accuracy; }
#define assertionEquals(lhs, rhs)
#define assertion1(expr, param)
#define assertion(expr)
#define logDebug(methodName, logMacroMessageStream)
Definition Log.h:50
Log Device.
Definition Log.h:516
double getAccumulatedValue() const
Measurement(const double &accuracy=0.0)
void increaseAccuracy(const double &factor)
void setAccuracy(const double &value)
double getMeanValueOfNextStep(double newValue) const
static tarch::logging::Log _log
Definition Measurement.h:31
double getStandardDeviation() const
We did face some seg faults (very rarely) where the parameter under the square root did become slight...
std::string toString() const
void setValue(const double &value)
Set the value.
bool isAccurateValue() const
Is value accurate.
double getMostRecentValue() const
STL namespace.
static bool smaller(double lhs, double rhs, double tolerance=NUMERICAL_ZERO_DIFFERENCE) InlineMethod
Smaller operator for floating point values.
bool smallerEquals(double lhs, double rhs, double tolerance=NUMERICAL_ZERO_DIFFERENCE)
std::string toString(MemoryLocation value)