SST  7.0.0
StructuralSimulationToolkit
stataccumulator.h
1 // Copyright 2009-2017 Sandia Corporation. Under the terms
2 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2017, Sandia Corporation
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 
18 #include <sst/core/sst_types.h>
19 #include <sst/core/warnmacros.h>
20 
21 #include <sst/core/statapi/statbase.h>
22 #include <sst/core/statapi/statoutput.h>
23 
24 namespace SST {
25 namespace Statistics {
26 
27 // NOTE: When calling base class members of classes derived from
28 // a templated base class. The user must use "this->" in
29 // order to call base class members (to avoid a compilier
30 // error) because they are "nondependant named" and the
31 // templated base class is a "dependant named". The
32 // compilier will not look in dependant named base classes
33 // when looking up independant names.
34 // See: http://www.parashift.com/c++-faq-lite/nondependent-name-lookup-members.html
35 
36 /**
37  \class AccumulatorStatistic
38 
39  Allows the online gathering of statistical information about a single quantity. The basic
40  statistics are captured online removing the need to keep a copy of the values of interest.
41 
42  @tparam NumberBase A template for the basic numerical type of values
43 */
44 
45 template <typename NumberBase>
46 class AccumulatorStatistic : public Statistic<NumberBase>
47 {
48 public:
49 
50  AccumulatorStatistic(BaseComponent* comp, const std::string& statName, const std::string& statSubId, Params& statParams)
51  : Statistic<NumberBase>(comp, statName, statSubId, statParams)
52  {
53  m_sum = 0;
54  m_sum_sq = 0;
55 
56  // Set the Name of this Statistic
57  this->setStatisticTypeName("Accumulator");
58  }
59 
61 
62 protected:
63  /**
64  Present a new value to the class to be included in the statistics.
65  @param value New value to be presented
66  */
67  void addData_impl(NumberBase value) override
68  {
69  m_sum += value;
70  m_sum_sq += (value * value);
71  }
72 
73 public:
74  /**
75  Provides the sum of the values presented so far.
76  @return The sum of values presented to the class so far.
77  */
78  NumberBase getSum()
79  {
80  return m_sum;
81  }
82 
83  /**
84  Provides the sum of each value squared presented to the class so far.
85  @return The sum of squared values presented to the class so far.
86  */
87  NumberBase getSumSquared()
88  {
89  return m_sum_sq;
90  }
91 
92  /**
93  Get the arithmetic mean of the values presented so far
94  @return The arithmetic mean of the values presented so far.
95  */
96  NumberBase getArithmeticMean()
97  {
98  uint64_t count = getCount();
99  return (count > 0) ? (m_sum / (NumberBase) count) : 0;
100  }
101 
102  /**
103  Get the variance of the values presented so far
104  @return The variance of the values presented so far
105  */
106  NumberBase getVariance()
107  {
108  uint64_t count = getCount();
109  return (count > 0) ? (m_sum_sq * count) - (m_sum * m_sum) : 0;
110  }
111 
112  /**
113  Get the standard deviation of the values presented so far
114  @return The standard deviation of the values presented so far
115  */
116  NumberBase getStandardDeviation()
117  {
118  return (NumberBase) std::sqrt( (double) getVariance() );
119  }
120 
121  /**
122  Get a count of the number of elements presented to the statistics collection so far.
123  @return Count the number of values presented to the class.
124  */
125  uint64_t getCount()
126  {
127  return this->getCollectionCount();
128  }
129 
130  void clearStatisticData() override
131  {
132  m_sum = 0;
133  m_sum_sq =0;
134  this->setCollectionCount(0);
135  }
136 
137  void registerOutputFields(StatisticOutput* statOutput) override
138  {
139  Field1 = statOutput->registerField<NumberBase>("Sum");
140  Field2 = statOutput->registerField<NumberBase>("SumSQ");
141  Field3 = statOutput->registerField<uint64_t> ("Count");
142  }
143 
144  void outputStatisticData(StatisticOutput* statOutput, bool UNUSED(EndOfSimFlag)) override
145  {
146  statOutput->outputField(Field1, m_sum);
147  statOutput->outputField(Field2, m_sum_sq);
148  statOutput->outputField(Field3, getCount());
149  }
150 
151  bool isStatModeSupported(StatisticBase::StatMode_t mode) const override
152  {
153  if (mode == StatisticBase::STAT_MODE_COUNT) {
154  return true;
155  }
156  if (mode == StatisticBase::STAT_MODE_PERIODIC) {
157  return true;
158  }
159  return false;
160  }
161 
162 private:
163  NumberBase m_sum;
164  NumberBase m_sum_sq;
165 
166  StatisticOutput::fieldHandle_t Field1, Field2, Field3;
167 };
168 
169 } //namespace Statistics
170 } //namespace SST
171 
172 #endif
void addData_impl(NumberBase value) override
Present a new value to the class to be included in the statistics.
Definition: stataccumulator.h:67
void registerOutputFields(StatisticOutput *statOutput) override
Called by the system to tell the Statistic to register its output fields.
Definition: stataccumulator.h:137
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
Definition: action.cc:17
Allows the online gathering of statistical information about a single quantity.
Definition: stataccumulator.h:46
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:130
Forms the template defined base class for statistics gathering within SST.
Definition: statbase.h:294
NumberBase getVariance()
Get the variance of the values presented so far.
Definition: stataccumulator.h:106
NumberBase getSumSquared()
Provides the sum of each value squared presented to the class so far.
Definition: stataccumulator.h:87
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:78
uint64_t getCount()
Get a count of the number of elements presented to the statistics collection so far.
Definition: stataccumulator.h:125
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:96
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:116