SST  6.1.0
StructuralSimulationToolkit
histo.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_HISTO
14 #define _H_SST_CORE_STATS_HISTO
15 
16 #include <sst/core/output.h>
17 #include <sst/core/stats/basestats.h>
18 
19 #include <stdint.h>
20 #include <map>
21 
22 using namespace std;
23 
24 namespace SST {
25 namespace Statistics {
26 
27 /**
28  \class Histogram
29  Holder of data grouped into pre-determined width bins.
30  \tparam HistoBinType is the type of the data held in each bin (i.e. what data type described the width of the bin)
31  \tparam HistoCountType is the count type of data held in each bin (i.e. what data type counts the number of items held in the bin itself)
32 */
33 template<class HistoBinType, class HistoCountType>
35  public:
36  /**
37  Creates a new bin with a specific bin width
38  \param binW The width of the bin
39  */
40  Histogram(const std::string name, HistoBinType binW) :
41  BaseStatistic(name) {
42 
43  totalSummed = 0;
44  itemCount = 0;
45  binWidth = binW;
46  minVal = 0;
47  maxVal = 0;
48  }
49 
50  /**
51  Creates a new bin with a specific bin width
52  \param[binW] The width of the bin
53  \param[name] Pointer to a name of the histogram variable (this is a description for the statistic engine to use in output)
54  */
55  Histogram(const char* name, HistoBinType binW) :
56  BaseStatistic(name) {
57 
58  totalSummed = 0;
59  itemCount = 0;
60  binWidth = binW;
61  minVal = 0;
62  maxVal = 0;
63  }
64 
65  /**
66  Adds a new value to the histogram. The correct bin is identified and then incremented. If no bin can be found
67  to hold the value then a new bin is created.
68  */
69  void add(HistoBinType value) {
70  if(enabled) {
71  HistoBinType bin_start = binWidth * (value / binWidth);
72  histo_itr bin_itr = bins.find(bin_start);
73 
74  if(bin_itr == bins.end()) {
75  bins.insert(std::pair<HistoBinType, HistoCountType>(bin_start, (HistoCountType) 1));
76  } else {
77  bin_itr->second++;
78  }
79 
80  itemCount++;
81  totalSummed += value;
82 
83  if(1 == itemCount) {
84  minVal = bin_start;
85  maxVal = bin_start;
86  } else {
87  minVal = (minVal < bin_start) ? minVal : bin_start;
88  maxVal = (maxVal > bin_start) ? maxVal : bin_start;
89  }
90  }
91  }
92 
93  /**
94  Count how many bins are active in this histogram
95  */
96  HistoCountType getBinCount() {
97  return bins.size();
98  }
99 
100  /**
101  Get the width of a bin in this histogram
102  */
103  HistoBinType getBinWidth() {
104  return binWidth;
105  }
106 
107  /**
108  Get the count of items in the bin by the start value (e.g. give me the count of items in the bin which begins at value X).
109  \return The count of items in the bin else 0.
110  */
111  HistoCountType getBinCountByBinStart(HistoBinType v) {
112  histo_itr bin_itr = bins.find(v);
113 
114  if(bin_itr == bins.end()) {
115  return (HistoCountType) 0;
116  } else {
117  return bins[v];
118  }
119  }
120 
121  /**
122  Get the smallest start value of a bin in this histogram (i.e. the minimum value possibly represented by this histogram)
123  */
124  HistoBinType getBinStart() {
125  return minVal;
126  }
127 
128  /**
129  Get the largest possible value represented by this histogram (i.e. the highest value in any of items bins rounded above to the size of the bin)
130  */
131  HistoBinType getBinEnd() {
132  return maxVal + binWidth;
133  }
134 
135  /**
136  Get the total number of items contained in all bins
137  \return The number of items contained in all bins
138  */
139  HistoCountType getItemCount() {
140  return itemCount;
141  }
142 
143  /**
144  Sum up every item presented for storage in the histogram
145  \return The sum of all values added into the histogram
146  */
147  HistoBinType getValuesSummed() {
148  return totalSummed;
149  }
150 
151  /**
152  * Iterator over the histogram bins
153  */
154  typedef typename std::map<HistoBinType, HistoCountType>::iterator histo_itr;
155 
156  private:
157  /**
158  The minimum value in the Histogram
159  */
160  HistoBinType minVal;
161  /**
162  The maximum bin-value of the Histrogram (i.e. the maximum value rounded up)
163  */
164  HistoBinType maxVal;
165  /**
166  The width of a Histogram bin
167  */
168  HistoBinType binWidth;
169  /**
170  The sum of all values added into the Histogram, this is calculated and the sum of all values presented
171  to be entered into the Histogram not with bin-width multiplied by the (max-min)/2 of the bin.
172  */
173  HistoBinType totalSummed;
174  /**
175  Total count of items added to the Histogram
176  */
177  HistoCountType itemCount;
178 
179  /**
180  A map of the the bin starts to the bin counts
181  */
182  std::map<HistoBinType, HistoCountType> bins;
183 };
184 
185 }
186 }
187 
188 #endif
HistoCountType getItemCount()
Get the total number of items contained in all bins.
Definition: histo.h:139
HistoCountType getBinCountByBinStart(HistoBinType v)
Get the count of items in the bin by the start value (e.g.
Definition: histo.h:111
HistoBinType getBinEnd()
Get the largest possible value represented by this histogram (i.e.
Definition: histo.h:131
Definition: action.cc:17
Definition: basestats.h:29
std::map< HistoBinType, HistoCountType >::iterator histo_itr
Iterator over the histogram bins.
Definition: histo.h:154
void add(HistoBinType value)
Adds a new value to the histogram.
Definition: histo.h:69
Histogram(const std::string name, HistoBinType binW)
Creates a new bin with a specific bin width.
Definition: histo.h:40
Holder of data grouped into pre-determined width bins.
Definition: histo.h:34
HistoBinType getValuesSummed()
Sum up every item presented for storage in the histogram.
Definition: histo.h:147
HistoBinType getBinWidth()
Get the width of a bin in this histogram.
Definition: histo.h:103
Histogram(const char *name, HistoBinType binW)
Creates a new bin with a specific bin width.
Definition: histo.h:55
HistoCountType getBinCount()
Count how many bins are active in this histogram.
Definition: histo.h:96
HistoBinType getBinStart()
Get the smallest start value of a bin in this histogram (i.e.
Definition: histo.h:124