SST  15.1.0
StructuralSimulationToolkit
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 
23 namespace SST {
24 namespace 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 
44 template <typename NumberBase>
45 class AccumulatorStatistic : public Statistic<NumberBase>
46 {
47 public:
48  SST_ELI_DECLARE_STATISTIC_TEMPLATE(
50  "sst",
51  "AccumulatorStatistic",
52  SST_ELI_ELEMENT_VERSION(1,0,0),
53  "Accumulate all contributions to a statistic",
54  "SST::Statistic<T>")
55 
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 
67 
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 
85 protected:
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 
106 public:
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 
172  void registerOutputFields(StatisticFieldsOutput* statOutput) override
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 private:
198  NumberBase m_sum;
199  NumberBase m_sum_sq;
200  NumberBase m_min;
201  NumberBase m_max;
202 
203  StatisticOutput::fieldHandle_t h_sum;
204  StatisticOutput::fieldHandle_t h_sumsq;
205  StatisticOutput::fieldHandle_t h_count;
206  StatisticOutput::fieldHandle_t h_max;
207  StatisticOutput::fieldHandle_t h_min;
208 
209  inline static const std::string stat_type_ = "Accumulator";
210 };
211 
212 } // namespace Statistics
213 } // namespace SST
214 
215 #endif // SST_CORE_STATAPI_STATACCUMULATOR_H
void addData_impl(NumberBase value) override
Present a new value to the class to be included in the statistics.
Definition: stataccumulator.h:90
void registerOutputFields(StatisticFieldsOutput *statOutput) override
Called by the system to tell the Statistic to register its output fields.
Definition: stataccumulator.h:172
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:42
virtual void setCollectionCount(uint64_t new_count)
Set the current collection count to a defined value.
Definition: statbase.cc:92
virtual const std::string & getStatTypeName() const override
Return the Statistic type name.
Definition: stataccumulator.h:72
NumberBase getMin()
Provides the minimum value presented so far.
Definition: stataccumulator.h:123
Definition: action.cc:18
Allows the online gathering of statistical information about a single quantity.
Definition: stataccumulator.h:45
void clearStatisticData() override
Inform the Statistic to clear its data.
Definition: stataccumulator.h:163
Forms the template defined base class for statistics gathering within SST.
Definition: elementinfo.h:46
NumberBase getMax()
Provides the maxmimum value presented so far.
Definition: stataccumulator.h:117
NumberBase getVariance()
Get the variance of the values presented so far.
Definition: stataccumulator.h:145
fieldHandle_t registerField(const char *fieldName)
Register a field to be output (templated function)
Definition: statoutput.h:267
virtual void outputField(fieldHandle_t fieldHandle, int32_t data)
Output field data.
NumberBase getSumSquared()
Provides the sum of each value squared presented to the class so far.
Definition: stataccumulator.h:129
Definition: statoutput.h:170
Main component object for the simulation.
Definition: baseComponent.h:64
Parameter store.
Definition: params.h:63
NumberBase getSum()
Provides the sum of the values presented so far.
Definition: stataccumulator.h:111
uint64_t getCount()
Get a count of the number of elements presented to the statistics collection so far.
Definition: stataccumulator.h:161
NumberBase getArithmeticMean()
Get the arithmetic mean of the values presented so far.
Definition: stataccumulator.h:135
virtual void serialize_order(SST::Core::Serialization::serializer &ser) override
Serialization.
Definition: statbase.h:415
NumberBase getStandardDeviation()
Get the standard deviation of the values presented so far.
Definition: stataccumulator.h:155
uint64_t getCollectionCount() const
Return the current collection count.
Definition: statbase.h:172
void serialize_order(SST::Core::Serialization::serializer &ser) override
Serialization.
Definition: stataccumulator.h:75