SST  10.0.0
StructuralSimulationToolkit
stataccumulator.h
1 // Copyright 2009-2020 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-2020, 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 
13 #ifndef _H_SST_CORE_ACCUMULATOR_STATISTIC_
14 #define _H_SST_CORE_ACCUMULATOR_STATISTIC_
15 
16 #include <cmath>
17 #include <limits>
18 
19 #include "sst/core/sst_types.h"
20 #include "sst/core/warnmacros.h"
21 
22 #include "sst/core/statapi/statbase.h"
23 #include "sst/core/statapi/statoutput.h"
24 
25 namespace SST {
26 namespace Statistics {
27 
28 // NOTE: When calling base class members of classes derived from
29 // a templated base class. The user must use "this->" in
30 // order to call base class members (to avoid a compiler
31 // error) because they are "nondependant named" and the
32 // templated base class is a "dependant named". The
33 // compiler will not look in dependant named base classes
34 // when looking up independent names.
35 // See: http://www.parashift.com/c++-faq-lite/nondependent-name-lookup-members.html
36 
37 /**
38  \class AccumulatorStatistic
39 
40  Allows the online gathering of statistical information about a single quantity. The basic
41  statistics are captured online removing the need to keep a copy of the values of interest.
42 
43  @tparam NumberBase A template for the basic numerical type of values
44 */
45 
46 template <typename NumberBase>
47 class AccumulatorStatistic : public Statistic<NumberBase>
48 {
49 public:
50  SST_ELI_DECLARE_STATISTIC_TEMPLATE(
52  "sst",
53  "AccumulatorStatistic",
54  SST_ELI_ELEMENT_VERSION(1,0,0),
55  "Accumulate all contributions to a statistic",
56  "SST::Statistic<T>")
57 
58  AccumulatorStatistic(BaseComponent* comp, const std::string& statName, const std::string& statSubId, Params& statParams)
59  : Statistic<NumberBase>(comp, statName, statSubId, statParams)
60  {
61  m_sum = static_cast<NumberBase>(0);
62  m_sum_sq = static_cast<NumberBase>(0);
63  m_min = std::numeric_limits<NumberBase>::max();
64  m_max = std::numeric_limits<NumberBase>::min();
65 
66  // Set the Name of this Statistic
67  this->setStatisticTypeName("Accumulator");
68  }
69 
71 
72 protected:
73  /**
74  Present a new value to the class to be included in the statistics.
75  @param value New value to be presented
76  */
77  void addData_impl(NumberBase value) override
78  {
79  m_sum += value;
80  m_sum_sq += (value * value);
81  m_min = ( value < m_min ) ? value : m_min;
82  m_max = ( value > m_max ) ? value : m_max;
83  }
84 
85  void addData_impl_Ntimes(uint64_t N, NumberBase value) override {
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()
98  {
99  return m_sum;
100  }
101 
102  /**
103  Provides the maxmimum value presented so far.
104  @return The maximum of values presented to the class so far
105  */
106  NumberBase getMax()
107  {
108  return m_max;
109  }
110 
111  /**
112  Provides the minimum value presented so far.
113  @return The minimum of values presented to the class so far
114  */
115  NumberBase getMin()
116  {
117  return m_min;
118  }
119 
120  /**
121  Provides the sum of each value squared presented to the class so far.
122  @return The sum of squared values presented to the class so far.
123  */
124  NumberBase getSumSquared()
125  {
126  return m_sum_sq;
127  }
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()
154  {
155  return (NumberBase) std::sqrt( (double) getVariance() );
156  }
157 
158  /**
159  Get a count of the number of elements presented to the statistics collection so far.
160  @return Count the number of values presented to the class.
161  */
162  uint64_t getCount()
163  {
164  return this->getCollectionCount();
165  }
166 
167  void clearStatisticData() override
168  {
169  m_sum = 0;
170  m_sum_sq =0;
171  m_min = std::numeric_limits<NumberBase>::max();
172  m_max = std::numeric_limits<NumberBase>::min();
173  this->setCollectionCount(0);
174  }
175 
176  void registerOutputFields(StatisticFieldsOutput* statOutput) override
177  {
178  h_sum = statOutput->registerField<NumberBase>("Sum");
179  h_sumsq = statOutput->registerField<NumberBase>("SumSQ");
180  h_count = statOutput->registerField<uint64_t> ("Count");
181  h_min = statOutput->registerField<NumberBase>("Min");
182  h_max = statOutput->registerField<NumberBase>("Max");
183  }
184 
185  void outputStatisticFields(StatisticFieldsOutput* statOutput, bool UNUSED(EndOfSimFlag)) override
186  {
187  statOutput->outputField(h_sum, m_sum);
188  statOutput->outputField(h_sumsq, m_sum_sq);
189  statOutput->outputField(h_count, getCount());
190 
191  if( 0 == getCount() ) {
192  statOutput->outputField(h_min, 0);
193  statOutput->outputField(h_max, 0);
194  } else {
195  statOutput->outputField(h_min, m_min);
196  statOutput->outputField(h_max, m_max);
197  }
198  }
199 
200  bool isStatModeSupported(StatisticBase::StatMode_t mode) const override
201  {
202  switch(mode){
203  case StatisticBase::STAT_MODE_COUNT:
204  case StatisticBase::STAT_MODE_PERIODIC:
205  case StatisticBase::STAT_MODE_DUMP_AT_END:
206  return true;
207  default:
208  return false;
209  }
210  return false;
211  }
212 
213 private:
214  NumberBase m_sum;
215  NumberBase m_sum_sq;
216  NumberBase m_min;
217  NumberBase m_max;
218 
219  StatisticOutput::fieldHandle_t h_sum;
220  StatisticOutput::fieldHandle_t h_sumsq;
221  StatisticOutput::fieldHandle_t h_count;
222  StatisticOutput::fieldHandle_t h_max;
223  StatisticOutput::fieldHandle_t h_min;
224 };
225 
226 
227 } //namespace Statistics
228 } //namespace SST
229 
230 #endif
void addData_impl(NumberBase value) override
Present a new value to the class to be included in the statistics.
Definition: stataccumulator.h:77
void registerOutputFields(StatisticFieldsOutput *statOutput) override
Called by the system to tell the Statistic to register its output fields.
Definition: stataccumulator.h:176
NumberBase getMin()
Provides the minimum value presented so far.
Definition: stataccumulator.h:115
Allows the online gathering of statistical information about a single quantity.
Definition: stataccumulator.h:47
uint64_t getCollectionCount() const
Return the current collection count.
Definition: statbase.h:155
void clearStatisticData() override
Inform the Statistic to clear its data.
Definition: stataccumulator.h:167
Forms the template defined base class for statistics gathering within SST.
Definition: elementinfo.h:42
NumberBase getMax()
Provides the maxmimum value presented so far.
Definition: stataccumulator.h:106
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:241
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:124
Definition: statoutput.h:144
Main component object for the simulation.
Definition: baseComponent.h:52
StatMode_t
Statistic collection mode.
Definition: statbase.h:68
Parameter store.
Definition: params.h:44
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:162
virtual void setCollectionCount(uint64_t newCount)
Set the current collection count to a defined value.
Definition: statbase.cc:78
NumberBase getArithmeticMean()
Get the arithmetic mean of the values presented so far.
Definition: stataccumulator.h:133
NumberBase getStandardDeviation()
Get the standard deviation of the values presented so far.
Definition: stataccumulator.h:153