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