SST  6.0.0
StructuralSimulationToolkit
accumulator.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_STATS_ACCUMULATOR
14 #define _H_SST_CORE_STATS_ACCUMULATOR
15 
16 #include <sst/core/stats/basestats.h>
17 
18 namespace SST {
19 namespace Statistics {
20 
21 /**
22  \class Accumulator
23 
24  Allows the online gathering of statistical information about a single quantity. The basic
25  statistics are captured online removing the need to keep a copy of the values of interest.
26 
27  @tparam NumberBase A template for the basic numerical type of values
28 */
29 <template typename NumberBase>
31 
32  public:
33  /**
34  Create a new Accumulator class with initial values set to a zero count,
35  zero sum statistic of interest.
36  */
37  Accumulator(char* name) :
38  BaseStatistic(name) {
39 
40  count = 0;
41  sum = 0;
42  sum_sq = 0;
43  }
44 
45  /**
46  Provides the sum of the values presented so far.
47  @return The sum of values presented to the class so far.
48  */
49  NumberBase getSum() {
50  return sum;
51  }
52 
53  /**
54  Provides the sum of each value squared presented to the class so far.
55  @return The sum of squared values presented to the class so far.
56  */
57  NumberBase getSumSquared() {
58  return sum_sq;
59  }
60 
61  /**
62  Present a new value to the class to be included in the statistics.
63  @param value New value to be presented
64  */
65  void add(NumberBase value) {
66  if(enabled) {
67  sum += value;
68  sum_sq += (value * value);
69  count++;
70  }
71  }
72 
73  /**
74  Present an array of values to the class to be included in the statistics.
75  @param values The array of values to be added to the statistics collection
76  @param length The length of the array being presented
77  */
78  void add(NumberBase* values, uint32_t length) {
79  if(enabled) {
80  for(uint32_t i = 0; i < length; ++i) {
81  sum += values[i];
82  sum_sq += (values[i] * values[i]);
83  }
84 
85  count += (uint64_t) length;
86  }
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  return (count > 0) ? (sum / (NumberBase) count) : 0;
95  }
96 
97  /**
98  Get the variance of the values presented so far
99  @return The variance of the values presented so far
100  */
101  NumberBase getVariance() {
102  return (count > 0) ?
103  ((sum_sq * count) - (sum * sum)) :
104  0;
105  }
106 
107  /**
108  Get the standard deviation of the values presented so far
109  @return The standard deviation of the values presented so far
110  */
111  NumberBase getStandardDeviation() {
112  return (NumberBase) std::sqrt( (double) getVariance() );
113  }
114 
115  /**
116  Get a count of the number of elements presented to the statistics collection so far.
117  @return Count the number of values presented to the class.
118  */
119  uint64_t getCount() {
120  return count;
121  }
122 
123  private:
124  NumberBase sum;
125  NumberBase sum_sq;
126  uint64_t count;
127 
128 };
129 
130 }
131 }
132 
133 #endif
void add(NumberBase value)
Present a new value to the class to be included in the statistics.
Definition: accumulator.h:65
NumberBase getVariance()
Get the variance of the values presented so far.
Definition: accumulator.h:101
Definition: action.cc:17
Accumulator(char *name)
Create a new Accumulator class with initial values set to a zero count, zero sum statistic of interes...
Definition: accumulator.h:37
Allows the online gathering of statistical information about a single quantity.
Definition: accumulator.h:30
NumberBase getArithmeticMean()
Get the arithmetic mean of the values presented so far.
Definition: accumulator.h:93
Definition: basestats.h:29
NumberBase getSum()
Provides the sum of the values presented so far.
Definition: accumulator.h:49
void add(NumberBase *values, uint32_t length)
Present an array of values to the class to be included in the statistics.
Definition: accumulator.h:78
NumberBase getStandardDeviation()
Get the standard deviation of the values presented so far.
Definition: accumulator.h:111
NumberBase getSumSquared()
Provides the sum of each value squared presented to the class so far.
Definition: accumulator.h:57
uint64_t getCount()
Get a count of the number of elements presented to the statistics collection so far.
Definition: accumulator.h:119