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