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