SST 15.0
Structural Simulation Toolkit
stataccumulator.h
1// Copyright 2009-2025 NTESS. Under the terms
2// of Contract DE-NA0003525 with NTESS, the U.S.
3// Government retains certain rights in this software.
4//
5// Copyright (c) 2009-2025, NTESS
6// All rights reserved.
7//
8// This file is part of the SST software package. For license
9// information, see the LICENSE file in the top level directory of the
10// distribution.
11
12#ifndef SST_CORE_STATAPI_STATACCUMULATOR_H
13#define SST_CORE_STATAPI_STATACCUMULATOR_H
14
15#include "sst/core/sst_types.h"
16#include "sst/core/statapi/statbase.h"
17#include "sst/core/statapi/statoutput.h"
18#include "sst/core/warnmacros.h"
19
20#include <cmath>
21#include <limits>
22
23namespace SST {
24namespace Statistics {
25
26// NOTE: When calling base class members of classes derived from
27// a templated base class. The user must use "this->" in
28// order to call base class members (to avoid a compiler
29// error) because they are "nondependant named" and the
30// templated base class is a "dependant named". The
31// compiler will not look in dependant named base classes
32// when looking up independent names.
33// See: http://www.parashift.com/c++-faq-lite/nondependent-name-lookup-members.html
34
35/**
36 \class AccumulatorStatistic
37
38 Allows the online gathering of statistical information about a single quantity. The basic
39 statistics are captured online removing the need to keep a copy of the values of interest.
40
41 @tparam NumberBase A template for the basic numerical type of values
42*/
43
44template <typename NumberBase>
45class AccumulatorStatistic : public Statistic<NumberBase>
46{
47public:
48 SST_ELI_DECLARE_STATISTIC_TEMPLATE(
49 AccumulatorStatistic,
50 "sst",
51 "AccumulatorStatistic",
52 SST_ELI_ELEMENT_VERSION(1,0,0),
53 "Accumulate all contributions to a statistic",
54 "SST::Statistic<T>")
55
56 AccumulatorStatistic(
57 BaseComponent* comp, const std::string& statName, const std::string& statSubId, Params& statParams) :
58 Statistic<NumberBase>(comp, statName, statSubId, statParams)
59 {
60 m_sum = static_cast<NumberBase>(0);
61 m_sum_sq = static_cast<NumberBase>(0);
62 m_min = std::numeric_limits<NumberBase>::max();
63 m_max = std::numeric_limits<NumberBase>::min();
64 }
65
66 ~AccumulatorStatistic() {}
67
68 AccumulatorStatistic() :
70 {} // For serialization only
71
72 virtual const std::string& getStatTypeName() const override { return stat_type_; }
73
74
76 {
78 SST_SER(m_sum);
79 SST_SER(m_sum_sq);
80 SST_SER(m_min);
81 SST_SER(m_max);
82 // Remaining fields will be reset by statistics output object
83 }
84
85protected:
86 /**
87 Present a new value to the class to be included in the statistics.
88 @param value New value to be presented
89 */
90 void addData_impl(NumberBase value) override
91 {
92 m_sum += value;
93 m_sum_sq += (value * value);
94 m_min = (value < m_min) ? value : m_min;
95 m_max = (value > m_max) ? value : m_max;
96 }
97
98 void addData_impl_Ntimes(uint64_t N, NumberBase value) override
99 {
100 m_sum += N * value;
101 m_sum_sq += N * value * value;
102 m_min = (value < m_min) ? value : m_min;
103 m_max = (value > m_max) ? value : m_max;
104 }
105
106public:
107 /**
108 Provides the sum of the values presented so far.
109 @return The sum of values presented to the class so far.
110 */
111 NumberBase getSum() { return m_sum; }
112
113 /**
114 Provides the maxmimum value presented so far.
115 @return The maximum of values presented to the class so far
116 */
117 NumberBase getMax() { return m_max; }
118
119 /**
120 Provides the minimum value presented so far.
121 @return The minimum of values presented to the class so far
122 */
123 NumberBase getMin() { return m_min; }
124
125 /**
126 Provides the sum of each value squared presented to the class so far.
127 @return The sum of squared values presented to the class so far.
128 */
129 NumberBase getSumSquared() { return m_sum_sq; }
130
131 /**
132 Get the arithmetic mean of the values presented so far
133 @return The arithmetic mean of the values presented so far.
134 */
135 NumberBase getArithmeticMean()
136 {
137 uint64_t count = getCount();
138 return (count > 0) ? (m_sum / (NumberBase)count) : 0;
139 }
140
141 /**
142 Get the variance of the values presented so far
143 @return The variance of the values presented so far
144 */
145 NumberBase getVariance()
146 {
147 uint64_t count = getCount();
148 return (count > 0) ? (m_sum_sq * count) - (m_sum * m_sum) : 0;
149 }
150
151 /**
152 Get the standard deviation of the values presented so far
153 @return The standard deviation of the values presented so far
154 */
155 NumberBase getStandardDeviation() { return (NumberBase)std::sqrt((double)getVariance()); }
156
157 /**
158 Get a count of the number of elements presented to the statistics collection so far.
159 @return Count the number of values presented to the class.
160 */
161 uint64_t getCount() { return this->getCollectionCount(); }
162
163 void clearStatisticData() override
164 {
165 m_sum = 0;
166 m_sum_sq = 0;
167 m_min = std::numeric_limits<NumberBase>::max();
168 m_max = std::numeric_limits<NumberBase>::min();
169 this->setCollectionCount(0);
170 }
171
173 {
174 h_sum = statOutput->registerField<NumberBase>("Sum");
175 h_sumsq = statOutput->registerField<NumberBase>("SumSQ");
176 h_count = statOutput->registerField<uint64_t>("Count");
177 h_min = statOutput->registerField<NumberBase>("Min");
178 h_max = statOutput->registerField<NumberBase>("Max");
179 }
180
181 void outputStatisticFields(StatisticFieldsOutput* statOutput, bool UNUSED(EndOfSimFlag)) override
182 {
183 statOutput->outputField(h_sum, m_sum);
184 statOutput->outputField(h_sumsq, m_sum_sq);
185 statOutput->outputField(h_count, getCount());
186
187 if ( 0 == getCount() ) {
188 statOutput->outputField(h_min, 0);
189 statOutput->outputField(h_max, 0);
190 }
191 else {
192 statOutput->outputField(h_min, m_min);
193 statOutput->outputField(h_max, m_max);
194 }
195 }
196
197 bool isStatModeSupported(StatisticBase::StatMode_t mode) const override
198 {
199 switch ( mode ) {
200 case StatisticBase::STAT_MODE_COUNT:
201 case StatisticBase::STAT_MODE_PERIODIC:
202 case StatisticBase::STAT_MODE_DUMP_AT_END:
203 return true;
204 default:
205 return false;
206 }
207 return false;
208 }
209
210private:
211 NumberBase m_sum;
212 NumberBase m_sum_sq;
213 NumberBase m_min;
214 NumberBase m_max;
215
216 StatisticOutput::fieldHandle_t h_sum;
217 StatisticOutput::fieldHandle_t h_sumsq;
218 StatisticOutput::fieldHandle_t h_count;
219 StatisticOutput::fieldHandle_t h_max;
220 StatisticOutput::fieldHandle_t h_min;
221
222 inline static const std::string stat_type_ = "Accumulator";
223};
224
225} // namespace Statistics
226} // namespace SST
227
228#endif // SST_CORE_STATAPI_STATACCUMULATOR_H
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition serializer.h:45
Parameter store.
Definition params.h:58
void addData_impl(NumberBase value) override
Present a new value to the class to be included in the statistics.
Definition stataccumulator.h:90
NumberBase getVariance()
Get the variance of the values presented so far.
Definition stataccumulator.h:145
NumberBase getMax()
Provides the maxmimum value presented so far.
Definition stataccumulator.h:117
NumberBase getStandardDeviation()
Get the standard deviation of the values presented so far.
Definition stataccumulator.h:155
NumberBase getArithmeticMean()
Get the arithmetic mean of the values presented so far.
Definition stataccumulator.h:135
NumberBase getSum()
Provides the sum of the values presented so far.
Definition stataccumulator.h:111
void registerOutputFields(StatisticFieldsOutput *statOutput) override
Called by the system to tell the Statistic to register its output fields.
Definition stataccumulator.h:172
NumberBase getSumSquared()
Provides the sum of each value squared presented to the class so far.
Definition stataccumulator.h:129
void serialize_order(SST::Core::Serialization::serializer &ser) override
Serialization.
Definition stataccumulator.h:75
void clearStatisticData() override
Inform the Statistic to clear its data.
Definition stataccumulator.h:163
virtual const std::string & getStatTypeName() const override
Return the Statistic type name.
Definition stataccumulator.h:72
uint64_t getCount()
Get a count of the number of elements presented to the statistics collection so far.
Definition stataccumulator.h:161
NumberBase getMin()
Provides the minimum value presented so far.
Definition stataccumulator.h:123
virtual void setCollectionCount(uint64_t new_count)
Set the current collection count to a defined value.
Definition statbase.cc:142
StatMode_t
Statistic collection mode STAT_MODE_UNDEFINED - unknown mode STAT_MODE_COUNT - generating statistic o...
Definition statbase.h:57
uint64_t getCollectionCount() const
Return the current collection count.
Definition statbase.h:162
Definition statoutput.h:170
fieldHandle_t registerField(const char *fieldName)
Register a field to be output (templated function)
Definition statoutput.h:266
virtual void outputField(fieldHandle_t fieldHandle, int32_t data)
Output field data.
Statistic(BaseComponent *comp, const std::string &stat_name, const std::string &stat_sub_id, Params &stat_params, bool null_stat=false)
Definition statbase.h:437
virtual void serialize_order(SST::Core::Serialization::serializer &ser) override
Serialization.
Definition statbase.h:420