SST 16.0.0
Structural Simulation Toolkit
stataccumulator.h
1// Copyright 2009-2026 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-2026, 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::Statistics {
24
25// NOTE: When calling base class members of classes derived from
26// a templated base class. The user must use "this->" in
27// order to call base class members (to avoid a compiler
28// error) because they are "nondependant named" and the
29// templated base class is a "dependant named". The
30// compiler will not look in dependant named base classes
31// when looking up independent names.
32// See: http://www.parashift.com/c++-faq-lite/nondependent-name-lookup-members.html
33
34/**
35 \class AccumulatorStatistic
36
37 Allows the online gathering of statistical information about a single quantity. The basic
38 statistics are captured online removing the need to keep a copy of the values of interest.
39
40 @tparam NumberBase A template for the basic numerical type of values
41*/
42
43template <typename NumberBase>
44class AccumulatorStatistic : public Statistic<NumberBase>
45{
46public:
47 SST_ELI_DECLARE_STATISTIC_TEMPLATE(
48 AccumulatorStatistic,
49 "sst",
50 "AccumulatorStatistic",
51 SST_ELI_ELEMENT_VERSION(1,0,0),
52 "Accumulate all contributions to a statistic",
53 "SST::Statistic<T>")
54
55 AccumulatorStatistic(
56 BaseComponent* comp, const std::string& statName, const std::string& statSubId, Params& statParams) :
57 Statistic<NumberBase>(comp, statName, statSubId, statParams)
58 {
59 m_sum = static_cast<NumberBase>(0);
60 m_sum_sq = static_cast<NumberBase>(0);
61 m_min = std::numeric_limits<NumberBase>::max();
62 m_max = std::numeric_limits<NumberBase>::min();
63 }
64
65 ~AccumulatorStatistic() {}
66
67 AccumulatorStatistic() :
69 {} // For serialization only
70
71 virtual const std::string& getStatTypeName() const override { return stat_type_; }
72
73
75 {
77 SST_SER(m_sum);
78 SST_SER(m_sum_sq);
79 SST_SER(m_min);
80 SST_SER(m_max);
81 // Remaining fields will be reset by statistics output object
82 }
83
84protected:
85 /**
86 Present a new value to the class to be included in the statistics.
87 @param value New value to be presented
88 */
89 void addData_impl(NumberBase value) override
90 {
91 m_sum += value;
92 m_sum_sq += (value * value);
93 m_min = (value < m_min) ? value : m_min;
94 m_max = (value > m_max) ? value : m_max;
95 }
96
97 void addData_impl_Ntimes(uint64_t N, NumberBase value) override
98 {
99 m_sum += N * value;
100 m_sum_sq += N * value * value;
101 m_min = (value < m_min) ? value : m_min;
102 m_max = (value > m_max) ? value : m_max;
103 }
104
105public:
106 /**
107 Provides the sum of the values presented so far.
108 @return The sum of values presented to the class so far.
109 */
110 NumberBase getSum() { return m_sum; }
111
112 /**
113 Provides the maxmimum value presented so far.
114 @return The maximum of values presented to the class so far
115 */
116 NumberBase getMax() { return m_max; }
117
118 /**
119 Provides the minimum value presented so far.
120 @return The minimum of values presented to the class so far
121 */
122 NumberBase getMin() { return m_min; }
123
124 /**
125 Provides the sum of each value squared presented to the class so far.
126 @return The sum of squared values presented to the class so far.
127 */
128 NumberBase getSumSquared() { return m_sum_sq; }
129
130 /**
131 Get the arithmetic mean of the values presented so far
132 @return The arithmetic mean of the values presented so far.
133 */
134 NumberBase getArithmeticMean()
135 {
136 uint64_t count = getCount();
137 return (count > 0) ? (m_sum / (NumberBase)count) : 0;
138 }
139
140 /**
141 Get the variance of the values presented so far
142 @return The variance of the values presented so far
143 */
144 NumberBase getVariance()
145 {
146 uint64_t count = getCount();
147 return (count > 0) ? (m_sum_sq * count) - (m_sum * m_sum) : 0;
148 }
149
150 /**
151 Get the standard deviation of the values presented so far
152 @return The standard deviation of the values presented so far
153 */
154 NumberBase getStandardDeviation() { return (NumberBase)std::sqrt((double)getVariance()); }
155
156 /**
157 Get a count of the number of elements presented to the statistics collection so far.
158 @return Count the number of values presented to the class.
159 */
160 uint64_t getCount() { return this->getCollectionCount(); }
161
162 void clearStatisticData() override
163 {
164 m_sum = 0;
165 m_sum_sq = 0;
166 m_min = std::numeric_limits<NumberBase>::max();
167 m_max = std::numeric_limits<NumberBase>::min();
168 this->setCollectionCount(0);
169 }
170
172 {
173 h_sum = statOutput->registerField<NumberBase>("Sum");
174 h_sumsq = statOutput->registerField<NumberBase>("SumSQ");
175 h_count = statOutput->registerField<uint64_t>("Count");
176 h_min = statOutput->registerField<NumberBase>("Min");
177 h_max = statOutput->registerField<NumberBase>("Max");
178 }
179
180 void outputStatisticFields(StatisticFieldsOutput* statOutput, bool UNUSED(EndOfSimFlag)) override
181 {
182 statOutput->outputField(h_sum, m_sum);
183 statOutput->outputField(h_sumsq, m_sum_sq);
184 statOutput->outputField(h_count, getCount());
185
186 if ( 0 == getCount() ) {
187 statOutput->outputField(h_min, 0);
188 statOutput->outputField(h_max, 0);
189 }
190 else {
191 statOutput->outputField(h_min, m_min);
192 statOutput->outputField(h_max, m_max);
193 }
194 }
195
196private:
197 NumberBase m_sum;
198 NumberBase m_sum_sq;
199 NumberBase m_min;
200 NumberBase m_max;
201
202 StatisticOutput::fieldHandle_t h_sum;
203 StatisticOutput::fieldHandle_t h_sumsq;
204 StatisticOutput::fieldHandle_t h_count;
205 StatisticOutput::fieldHandle_t h_max;
206 StatisticOutput::fieldHandle_t h_min;
207
208 inline static const std::string stat_type_ = "Accumulator";
209};
210
211} // namespace SST::Statistics
212
213#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:43
Parameter store.
Definition params.h:65
void addData_impl(NumberBase value) override
Present a new value to the class to be included in the statistics.
Definition stataccumulator.h:89
NumberBase getVariance()
Get the variance of the values presented so far.
Definition stataccumulator.h:144
NumberBase getMax()
Provides the maxmimum value presented so far.
Definition stataccumulator.h:116
NumberBase getStandardDeviation()
Get the standard deviation of the values presented so far.
Definition stataccumulator.h:154
NumberBase getArithmeticMean()
Get the arithmetic mean of the values presented so far.
Definition stataccumulator.h:134
NumberBase getSum()
Provides the sum of the values presented so far.
Definition stataccumulator.h:110
void registerOutputFields(StatisticFieldsOutput *statOutput) override
Called by the system to tell the Statistic to register its output fields.
Definition stataccumulator.h:171
NumberBase getSumSquared()
Provides the sum of each value squared presented to the class so far.
Definition stataccumulator.h:128
void serialize_order(SST::Core::Serialization::serializer &ser) override
Serialization.
Definition stataccumulator.h:74
void clearStatisticData() override
Inform the Statistic to clear its data.
Definition stataccumulator.h:162
virtual const std::string & getStatTypeName() const override
Return the Statistic type name.
Definition stataccumulator.h:71
uint64_t getCount()
Get a count of the number of elements presented to the statistics collection so far.
Definition stataccumulator.h:160
NumberBase getMin()
Provides the minimum value presented so far.
Definition stataccumulator.h:122
virtual void setCollectionCount(uint64_t new_count)
Set the current collection count to a defined value.
Definition statbase.cc:92
uint64_t getCollectionCount() const
Return the current collection count.
Definition statbase.h:173
Definition statoutput.h:172
fieldHandle_t registerField(const char *fieldName)
Register a field to be output (templated function).
Definition statoutput.h:268
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)
Definition statbase.h:432
virtual void serialize_order(SST::Core::Serialization::serializer &ser) override
Serialization.
Definition statbase.h:416