SST  14.1.0
StructuralSimulationToolkit
stataccumulator.h
1 // Copyright 2009-2024 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-2024, 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  // Set the Name of this Statistic
66  this->setStatisticTypeName("Accumulator");
67  }
68 
70 
71  AccumulatorStatistic() : Statistic<NumberBase>() {} // For serialization only
72 
74  {
76  ser& m_sum;
77  ser& m_sum_sq;
78  ser& m_min;
79  ser& m_max;
80  // Remaining fields will be reset by statistics output object
81  }
82 
83 protected:
84  /**
85  Present a new value to the class to be included in the statistics.
86  @param value New value to be presented
87  */
88  void addData_impl(NumberBase value) override
89  {
90  m_sum += value;
91  m_sum_sq += (value * value);
92  m_min = (value < m_min) ? value : m_min;
93  m_max = (value > m_max) ? value : m_max;
94  }
95 
96  void addData_impl_Ntimes(uint64_t N, NumberBase value) override
97  {
98  m_sum += N * value;
99  m_sum_sq += N * value * value;
100  m_min = (value < m_min) ? value : m_min;
101  m_max = (value > m_max) ? value : m_max;
102  }
103 
104 public:
105  /**
106  Provides the sum of the values presented so far.
107  @return The sum of values presented to the class so far.
108  */
109  NumberBase getSum() { return m_sum; }
110 
111  /**
112  Provides the maxmimum value presented so far.
113  @return The maximum of values presented to the class so far
114  */
115  NumberBase getMax() { return m_max; }
116 
117  /**
118  Provides the minimum value presented so far.
119  @return The minimum of values presented to the class so far
120  */
121  NumberBase getMin() { return m_min; }
122 
123  /**
124  Provides the sum of each value squared presented to the class so far.
125  @return The sum of squared values presented to the class so far.
126  */
127  NumberBase getSumSquared() { return m_sum_sq; }
128 
129  /**
130  Get the arithmetic mean of the values presented so far
131  @return The arithmetic mean of the values presented so far.
132  */
133  NumberBase getArithmeticMean()
134  {
135  uint64_t count = getCount();
136  return (count > 0) ? (m_sum / (NumberBase)count) : 0;
137  }
138 
139  /**
140  Get the variance of the values presented so far
141  @return The variance of the values presented so far
142  */
143  NumberBase getVariance()
144  {
145  uint64_t count = getCount();
146  return (count > 0) ? (m_sum_sq * count) - (m_sum * m_sum) : 0;
147  }
148 
149  /**
150  Get the standard deviation of the values presented so far
151  @return The standard deviation of the values presented so far
152  */
153  NumberBase getStandardDeviation() { return (NumberBase)std::sqrt((double)getVariance()); }
154 
155  /**
156  Get a count of the number of elements presented to the statistics collection so far.
157  @return Count the number of values presented to the class.
158  */
159  uint64_t getCount() { return this->getCollectionCount(); }
160 
161  void clearStatisticData() override
162  {
163  m_sum = 0;
164  m_sum_sq = 0;
165  m_min = std::numeric_limits<NumberBase>::max();
166  m_max = std::numeric_limits<NumberBase>::min();
167  this->setCollectionCount(0);
168  }
169 
170  void registerOutputFields(StatisticFieldsOutput* statOutput) override
171  {
172  h_sum = statOutput->registerField<NumberBase>("Sum");
173  h_sumsq = statOutput->registerField<NumberBase>("SumSQ");
174  h_count = statOutput->registerField<uint64_t>("Count");
175  h_min = statOutput->registerField<NumberBase>("Min");
176  h_max = statOutput->registerField<NumberBase>("Max");
177  }
178 
179  void outputStatisticFields(StatisticFieldsOutput* statOutput, bool UNUSED(EndOfSimFlag)) override
180  {
181  statOutput->outputField(h_sum, m_sum);
182  statOutput->outputField(h_sumsq, m_sum_sq);
183  statOutput->outputField(h_count, getCount());
184 
185  if ( 0 == getCount() ) {
186  statOutput->outputField(h_min, 0);
187  statOutput->outputField(h_max, 0);
188  }
189  else {
190  statOutput->outputField(h_min, m_min);
191  statOutput->outputField(h_max, m_max);
192  }
193  }
194 
195  bool isStatModeSupported(StatisticBase::StatMode_t mode) const override
196  {
197  switch ( mode ) {
198  case StatisticBase::STAT_MODE_COUNT:
199  case StatisticBase::STAT_MODE_PERIODIC:
200  case StatisticBase::STAT_MODE_DUMP_AT_END:
201  return true;
202  default:
203  return false;
204  }
205  return false;
206  }
207 
208 private:
209  NumberBase m_sum;
210  NumberBase m_sum_sq;
211  NumberBase m_min;
212  NumberBase m_max;
213 
214  StatisticOutput::fieldHandle_t h_sum;
215  StatisticOutput::fieldHandle_t h_sumsq;
216  StatisticOutput::fieldHandle_t h_count;
217  StatisticOutput::fieldHandle_t h_max;
218  StatisticOutput::fieldHandle_t h_min;
219 };
220 
221 } // namespace Statistics
222 } // namespace SST
223 
224 #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:88
void registerOutputFields(StatisticFieldsOutput *statOutput) override
Called by the system to tell the Statistic to register its output fields.
Definition: stataccumulator.h:170
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:43
NumberBase getMin()
Provides the minimum value presented so far.
Definition: stataccumulator.h:121
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:161
Forms the template defined base class for statistics gathering within SST.
Definition: elementinfo.h:45
NumberBase getMax()
Provides the maxmimum value presented so far.
Definition: stataccumulator.h:115
NumberBase getVariance()
Get the variance of the values presented so far.
Definition: stataccumulator.h:143
fieldHandle_t registerField(const char *fieldName)
Register a field to be output (templated function)
Definition: statoutput.h:259
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:127
Definition: statoutput.h:162
Main component object for the simulation.
Definition: baseComponent.h:62
StatMode_t
Statistic collection mode.
Definition: statbase.h:49
Parameter store.
Definition: params.h:55
NumberBase getSum()
Provides the sum of the values presented so far.
Definition: stataccumulator.h:109
uint64_t getCount()
Get a count of the number of elements presented to the statistics collection so far.
Definition: stataccumulator.h:159
virtual void setCollectionCount(uint64_t newCount)
Set the current collection count to a defined value.
Definition: statbase.cc:94
NumberBase getArithmeticMean()
Get the arithmetic mean of the values presented so far.
Definition: stataccumulator.h:133
virtual void serialize_order(SST::Core::Serialization::serializer &ser) override
Serialization.
Definition: statbase.h:389
NumberBase getStandardDeviation()
Get the standard deviation of the values presented so far.
Definition: stataccumulator.h:153
uint64_t getCollectionCount() const
Return the current collection count.
Definition: statbase.h:137
void serialize_order(SST::Core::Serialization::serializer &ser) override
Serialization.
Definition: stataccumulator.h:73