The Sparta Modeling Framework
Loading...
Searching...
No Matches
HistogramFunctionManager.hpp
Go to the documentation of this file.
1// <HistogramFunctionManager.h> -*- C++ -*-
2
3
59#pragma once
60
61#include <iostream>
62#include <functional>
63#include <map>
64#include <string>
65
69
70namespace sparta{
71
72 // The signature of the methods that should be
73 // registered with this macro is aliased here.
74 template<typename T>
75 using HistStatCalcFcn = double (*)(const T*);
76
88 public:
89
94
99
104
109
114 static FunctionManager function_manager;
115 return function_manager;
116 }
117
122 const std::string& getToken() const {
123 return token_;
124 }
125
134 template<typename T>
135 MetaStruct::enable_if_t<
136 std::is_same<MetaStruct::decay_t<T>,
137 CycleHistogramTreeNode>::value, void>
138 add(const std::string& name, HistStatCalcFcn<T> fcn){
139
140 // Registering the same function twice should throw an exception.
141 if(functions_cycle_.find(name) != functions_cycle_.end()){
143 "This method " + name + " was already registered with the REGISTER_HISTOGRAM_STAT_CALC_FCN macro.");
144 }
145 functions_cycle_[name] = fcn;
146 }
147
156 template<typename T>
157 MetaStruct::enable_if_t<
158 std::is_same<MetaStruct::decay_t<T>,
159 HistogramTreeNode>::value, void>
160 add(const std::string& name, HistStatCalcFcn<T> fcn){
161
162 // Registering the same function twice should throw an exception.
163 if(functions_normal_.find(name) != functions_normal_.end()){
165 "This method " + name + "was already registered with the REGISTER_HISTOGRAM_STAT_CALC_FCN macro.");
166 }
167 functions_normal_[name] = fcn;
168 }
169
179 template<typename T>
180 MetaStruct::enable_if_t<
181 std::is_same<MetaStruct::decay_t<T>,
183 HistStatCalcFcn<T>&>
184 find(const std::string& name){
185 const auto& iter = functions_cycle_.find(name);
186
187 // If no such function pointers were found, it should throw.
188 if(iter == functions_cycle_.end()){
190 "This method " + name + " was never registered with the REGISTER_HISTOGRAM_STAT_CALC_FCN macro.");
191 }
192 return iter->second;
193 }
194
204 template<typename T>
205 MetaStruct::enable_if_t<
206 std::is_same<MetaStruct::decay_t<T>,
207 HistogramTreeNode>::value,
208 HistStatCalcFcn<T>&>
209 find(const std::string& name){
210 const auto& iter = functions_normal_.find(name);
211
212 // If no such function pointers were found, it should throw.
213 if(iter == functions_normal_.end()){
215 "This method " + name + " was never registered with the REGISTER_HISTOGRAM_STAT_CALC_FCN macro.");
216 }
217 return iter->second;
218 }
219 private:
220
221 // Singleton private default constructor.
222 FunctionManager() = default;
223
224 // Constant string token which the users will use as a prefix
225 // while writing their statdefinitions in yaml files.
226 const std::string token_ = "hist_def";
227
228 // The map of method names to function pointers for CycleHistogramNodes.
229 std::map<std::string, HistStatCalcFcn<CycleHistogramTreeNode>> functions_cycle_;
230
231 // The map of method names to function pointers for CycleHistogramNodes.
232 std::map<std::string, HistStatCalcFcn<HistogramTreeNode>> functions_normal_;
233 };
234}
235
254#define REGISTER_HISTOGRAM_STAT_CALC_FCN(histogram_type, fcn_name) \
255{ \
256 static_assert(MetaStruct::matches_any<sparta::histogram_type, \
257 sparta::HistogramTreeNode, \
258 sparta::CycleHistogramTreeNode>::value, \
259 "Invalid class type used for histogram stat calculation registration."); \
260 std::string key = #fcn_name; \
261 sparta::HistStatCalcFcn<sparta::histogram_type> callable = fcn_name; \
262 sparta::FunctionManager::get().add<sparta::histogram_type>(key, callable); \
263}
CycleHistogram implementation using sparta CycleCounter.
Histogram implementation using sparta Counters.
Contains a collection implementation of various compile-time metaprogramming and Type-Detection APIs ...
CycleHistogramTreeNode class for uint64_t values.
Singleton Function Manager class. This class contains two maps and one constant string token....
MetaStruct::enable_if_t< std::is_same< MetaStruct::decay_t< T >, HistogramTreeNode >::value, void > add(const std::string &name, HistStatCalcFcn< T > fcn)
This method adds an entry to one of the internal maps. This method takes as parameters,...
static FunctionManager & get()
This method returns the singleton instance of Function Manager.
const std::string & getToken() const
This method returns the constant string "hist_def" which should be prefixed in the stat pathnames in ...
MetaStruct::enable_if_t< std::is_same< MetaStruct::decay_t< T >, HistogramTreeNode >::value, HistStatCalcFcn< T > & > find(const std::string &name)
This method finds the function pointer mapped to the name. This method takes as parameters,...
MetaStruct::enable_if_t< std::is_same< MetaStruct::decay_t< T >, CycleHistogramTreeNode >::value, HistStatCalcFcn< T > & > find(const std::string &name)
This method finds the function pointer mapped to the name. This method takes as parameters,...
MetaStruct::enable_if_t< std::is_same< MetaStruct::decay_t< T >, CycleHistogramTreeNode >::value, void > add(const std::string &name, HistStatCalcFcn< T > fcn)
This method adds an entry to one of the internal maps. This method takes as parameters,...
FunctionManager(const FunctionManager &)=delete
Not copy-constructable.
FunctionManager(FunctionManager &&)=delete
Not move-constructable.
FunctionManager & operator=(const FunctionManager &)=delete
Not copy-assignable.
Used to construct and throw a standard C++ exception. Inherits from std::exception.
Macros for handling exponential backoff.