The Sparta Modeling Framework
Loading...
Searching...
No Matches
BasicHistogram.hpp
Go to the documentation of this file.
1// <BasicHistogram.h> -*- C++ -*-
2
7#pragma once
8
9#include <string>
10#include <vector>
11#include <sstream>
12#include "sparta/statistics/Counter.hpp"
13
14namespace sparta
15{
41template<typename BucketT, bool ASSERT_ON_UNDERFLOW=false>
43{
44public:
53 const std::string &name,
54 const std::string &desc,
55 const std::vector<BucketT> &buckets) :
56 bucket_vals_(buckets)
57 {
58 sparta_assert(std::is_sorted(buckets.begin(), buckets.end()), "Buckets must be sorted");
59
60 const auto bucket_size = bucket_vals_.size();
61 ctrs_.reserve(bucket_size);
62
63 // create one counter per bucket
64 for (unsigned i = 0; i < bucket_size; ++i)
65 {
66 auto &v = bucket_vals_[i];
67 std::ostringstream os_name;
68 if (v < 0)
69 {
70 os_name << name << "_n" << -v; // negative
71 }
72 else
73 {
74 os_name << name << '_' << v;
75 }
76
77 std::ostringstream os_desc;
78 if (i == 0)
79 {
80 os_desc << desc << " with values less than or equal to " << v;
81 }
82 else
83 {
84 os_desc << desc << " with values greater than " << bucket_vals_[i-1] << " and less than or equal to " << v;
85 }
86
87 ctrs_.emplace_back(&sset, os_name.str(), os_desc.str(), sparta::Counter::COUNT_NORMAL);
88 }
89 }
90
93
103 void addValue(const BucketT &val)
104 {
105 // upper_bound will yield the bucket beyond the one we want
106 auto bucket = std::upper_bound(bucket_vals_.begin(), bucket_vals_.end(), val);
107
108 // check for underflow (value below first bucket)
109 if (bucket == bucket_vals_.begin())
110 {
111 sparta_assert(!ASSERT_ON_UNDERFLOW, "Value below first bucket");
112 ++ctrs_[0]; // put underflow in first bucket
113 return;
114 }
115 //else calculate offset into counter array
116 auto off = bucket - bucket_vals_.begin() - 1;
117 ++ctrs_[off];
118 }
119
122 BasicHistogram( BasicHistogram &&) = delete;
123 const BasicHistogram & operator=(const BasicHistogram &) = delete;
124 BasicHistogram & operator=( BasicHistogram &&) = delete;
125
126private: // data
127 std::vector<BucketT> bucket_vals_;
128 std::vector<sparta::Counter> ctrs_;
129};
130} // namespace sparta
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
BasicHistogram(sparta::StatisticSet &sset, const std::string &name, const std::string &desc, const std::vector< BucketT > &buckets)
Construct a BasicHistogram.
~BasicHistogram()
Destroy, non-virtual.
void addValue(const BucketT &val)
Charge a bucket where the given val falls.
BasicHistogram(const BasicHistogram &)=delete
Disallow copies/assignment/move.
@ COUNT_NORMAL
Counter counts the number of times something happens like one would expect. This is a weakly monotoni...
Set of StatisticDef and CounterBase-derived objects for visiblility through a sparta Tree.
Macros for handling exponential backoff.