00001 // Copyright 2009-2015 Sandia Corporation. Under the terms 00002 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S. 00003 // Government retains certain rights in this software. 00004 // 00005 // Copyright (c) 2009-2015, Sandia Corporation 00006 // All rights reserved. 00007 // 00008 // This file is part of the SST software package. For license 00009 // information, see the LICENSE file in the top level directory of the 00010 // distribution. 00011 00012 00013 #ifndef _H_SST_CORE_STATS_ACCUMULATOR 00014 #define _H_SST_CORE_STATS_ACCUMULATOR 00015 00016 #include <sst/core/stats/basestats.h> 00017 00018 namespace SST { 00019 namespace Statistics { 00020 00021 /** 00022 \class Accumulator 00023 00024 Allows the online gathering of statistical information about a single quantity. The basic 00025 statistics are captured online removing the need to keep a copy of the values of interest. 00026 00027 @tparam NumberBase A template for the basic numerical type of values 00028 */ 00029 <template typename NumberBase> 00030 class Accumulator : public SST::Statistics::BaseStatistic { 00031 00032 public: 00033 /** 00034 Create a new Accumulator class with initial values set to a zero count, 00035 zero sum statistic of interest. 00036 */ 00037 Accumulator(char* name) : 00038 BaseStatistic(name) { 00039 00040 count = 0; 00041 sum = 0; 00042 sum_sq = 0; 00043 } 00044 00045 /** 00046 Provides the sum of the values presented so far. 00047 @return The sum of values presented to the class so far. 00048 */ 00049 NumberBase getSum() { 00050 return sum; 00051 } 00052 00053 /** 00054 Provides the sum of each value squared presented to the class so far. 00055 @return The sum of squared values presented to the class so far. 00056 */ 00057 NumberBase getSumSquared() { 00058 return sum_sq; 00059 } 00060 00061 /** 00062 Present a new value to the class to be included in the statistics. 00063 @param value New value to be presented 00064 */ 00065 void add(NumberBase value) { 00066 if(enabled) { 00067 sum += value; 00068 sum_sq += (value * value); 00069 count++; 00070 } 00071 } 00072 00073 /** 00074 Present an array of values to the class to be included in the statistics. 00075 @param values The array of values to be added to the statistics collection 00076 @param length The length of the array being presented 00077 */ 00078 void add(NumberBase* values, uint32_t length) { 00079 if(enabled) { 00080 for(uint32_t i = 0; i < length; ++i) { 00081 sum += values[i]; 00082 sum_sq += (values[i] * values[i]); 00083 } 00084 00085 count += (uint64_t) length; 00086 } 00087 } 00088 00089 /** 00090 Get the arithmetic mean of the values presented so far 00091 @return The arithmetic mean of the values presented so far. 00092 */ 00093 NumberBase getArithmeticMean() { 00094 return (count > 0) ? (sum / (NumberBase) count) : 0; 00095 } 00096 00097 /** 00098 Get the variance of the values presented so far 00099 @return The variance of the values presented so far 00100 */ 00101 NumberBase getVariance() { 00102 return (count > 0) ? 00103 ((sum_sq * count) - (sum * sum)) : 00104 0; 00105 } 00106 00107 /** 00108 Get the standard deviation of the values presented so far 00109 @return The standard deviation of the values presented so far 00110 */ 00111 NumberBase getStandardDeviation() { 00112 return (NumberBase) std::sqrt( (double) getVariance() ); 00113 } 00114 00115 /** 00116 Get a count of the number of elements presented to the statistics collection so far. 00117 @return Count the number of values presented to the class. 00118 */ 00119 uint64_t getCount() { 00120 return count; 00121 } 00122 00123 private: 00124 NumberBase sum; 00125 NumberBase sum_sq; 00126 uint64_t count; 00127 00128 }; 00129 00130 } 00131 } 00132 00133 #endif
1.6.1