ComPPare 1.0.0
Loading...
Searching...
No Matches
config.hpp
Go to the documentation of this file.
1/*
2
3Copyright 2025 | Leong Fan FUNG | funglf | stanleyfunglf@gmail.com
4
5Permission is hereby granted, free of charge, to any person obtaining a copy
6of this software and associated documentation files (the “Software”), to deal
7in the Software without restriction, including without limitation the rights
8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9copies of the Software, and to permit persons to whom the Software is
10furnished to do so, subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21SOFTWARE.
22
23*/
24
34#pragma once
35#include <chrono>
36#include <utility>
37#include <string_view>
38
39namespace comppare
40{
47 class config
48 {
49 public:
51 using clock_t = std::chrono::steady_clock;
52
54 using time_point_t = std::chrono::time_point<clock_t>;
55
57 config(const config &) = delete;
59 config(config &&) = delete;
61 config &operator=(const config &) = delete;
63 config &operator=(config &&) = delete;
64
66 static uint64_t warmup_iters() { return instance().warmup_iters_; }
68 static void set_warmup_iters(uint64_t v) { instance().warmup_iters_ = v; }
69
71 static uint64_t bench_iters() { return instance().bench_iters_; }
73 static void set_bench_iters(uint64_t v) { instance().bench_iters_ = v; }
74
76 static void reset_roi_us() { instance().roi_ = double(0.0); }
77
84 static void set_roi_us(const time_point_t &start, const time_point_t &end) { instance().roi_ = std::chrono::duration<double, std::micro>(end - start).count(); }
86 static void set_roi_us(const float start, const float end) { instance().roi_ = static_cast<double>(end - start); }
88 static void set_roi_us(const double start, const double end) { instance().roi_ = end - start; }
89
97 template <typename Rep, typename Period>
98 static void set_roi_us(std::chrono::duration<Rep, Period> v)
99 {
100 double micros = std::chrono::duration<double, std::micro>(v).count();
101 instance().roi_ = micros;
102 }
104 static void set_roi_us(const double v) { instance().roi_ = v; }
106 static void set_roi_us(const float v) { instance().roi_ = static_cast<double>(v); }
107
114 template <typename Rep, typename Period>
115 static void increment_roi_us(std::chrono::duration<Rep, Period> v)
116 {
117 double micros = std::chrono::duration<double, std::micro>(v).count();
118 instance().roi_ += micros;
119 }
121 static void increment_roi_us(const double v) { instance().roi_ += v; }
123 static void increment_roi_us(const float v) { instance().roi_ += static_cast<double>(v); }
124
130 static void set_warmup_us(const time_point_t &start, const time_point_t &end) { instance().warmup_ = std::chrono::duration<double, std::micro>(end - start).count(); }
136 static void set_warmup_us(const float start, const float end) { instance().warmup_ = static_cast<double>(end - start); }
142 static void set_warmup_us(const double start, const double end) { instance().warmup_ = end - start; }
143
151 template <typename Rep, typename Period>
152 static void set_warmup_us(std::chrono::duration<Rep, Period> v)
153 {
154 double micros = std::chrono::duration<double, std::micro>(v).count();
155 instance().warmup_ = micros;
156 }
158 static void set_warmup_us(const double v) { instance().warmup_ = v; }
160 static void set_warmup_us(const float v) { instance().warmup_ = static_cast<double>(v); }
161
163 static double get_roi_us() { return instance().roi_; }
165 static double get_warmup_us() { return instance().warmup_; }
166
173 template <std::floating_point T = double>
174 static T &fp_tolerance()
175 {
176 static T tol = std::numeric_limits<T>::epsilon() * 1e3; // Default tolerance
177 return tol;
178 }
179
186 template <std::floating_point T = double>
187 static void set_fp_tolerance(T v)
188 {
189 fp_tolerance<T>() = v;
190 }
191
197 static void set_all_fp_tolerance(long double v)
198 {
199 fp_tolerance<float>() = static_cast<float>(v);
200 fp_tolerance<double>() = static_cast<double>(v);
201 fp_tolerance<long double>() = v;
202 }
203
204 private:
210 config() = default;
211
213 static config &instance()
214 {
215 static config inst;
216 return inst;
217 }
218
220 double roi_{0.0};
222 double warmup_{0.0};
223
225 uint64_t warmup_iters_{100};
227 uint64_t bench_iters_{100};
228 };
229
230 /*
231 Singleton Class for the current state
232 */
233
243 {
244 public:
246 current_state(const current_state &) = delete;
253
255 static bool using_plugin() { return instance().using_plugin_; }
257 static void set_using_plugin(bool v) { instance().using_plugin_ = v; }
258
260 static std::string_view impl_name() { return instance().impl_name_; }
262 static void set_impl_name(std::string_view name) { instance().impl_name_ = name; }
263
264 private:
266 current_state() = default;
267
270 {
271 static current_state inst;
272 return inst;
273 }
274
276 bool using_plugin_{false};
278 std::string_view impl_name_;
279 };
280
281}
Configuration singleton for the ComPPare library.
Definition config.hpp:48
static void set_warmup_us(const double start, const double end)
Set the warmup us value using double.
Definition config.hpp:142
static void set_roi_us(std::chrono::duration< Rep, Period > v)
Set the roi us value using a std::chrono::duration.
Definition config.hpp:98
static void set_roi_us(const float v)
Set the roi us value using float.
Definition config.hpp:106
static void set_warmup_us(const double v)
Set the warmup us value using double.
Definition config.hpp:158
static config & instance()
Get the singleton instance of the config class.
Definition config.hpp:213
std::chrono::steady_clock clock_t
Type alias for the clock used in timing operations.
Definition config.hpp:51
config()=default
Construct a new config object.
config & operator=(config &&)=delete
Deleted move assignment operator.
config(const config &)=delete
Deleted copy constructor.
static void set_warmup_iters(uint64_t v)
Set the number of warmup iterations.
Definition config.hpp:68
static void increment_roi_us(const float v)
Increment the roi us value using float.
Definition config.hpp:123
double roi_
The current roi us value.
Definition config.hpp:220
std::chrono::time_point< clock_t > time_point_t
Type alias for the time point used in timing operations.
Definition config.hpp:54
static void increment_roi_us(const double v)
Increment the roi us value using double.
Definition config.hpp:121
uint64_t bench_iters_
The number of benchmark iterations.
Definition config.hpp:227
static void set_roi_us(const double v)
Set the roi us value using double.
Definition config.hpp:104
static double get_roi_us()
Get the current roi us value.
Definition config.hpp:163
static uint64_t bench_iters()
Get the number of benchmark iterations.
Definition config.hpp:71
static void reset_roi_us()
Reset the ROI time to zero.
Definition config.hpp:76
uint64_t warmup_iters_
The number of warmup iterations.
Definition config.hpp:225
static T & fp_tolerance()
Get the floating-point tolerance for a specific floating-point type.
Definition config.hpp:174
static void set_roi_us(const float start, const float end)
Set the roi us value using float.
Definition config.hpp:86
config & operator=(const config &)=delete
Deleted copy assignment operator.
static double get_warmup_us()
Get the current warmup us value.
Definition config.hpp:165
static void increment_roi_us(std::chrono::duration< Rep, Period > v)
Increment the roi us value using time points.
Definition config.hpp:115
static uint64_t warmup_iters()
Get the number of warmup iterations.
Definition config.hpp:66
static void set_warmup_us(const float v)
Set the warmup us value using float.
Definition config.hpp:160
static void set_fp_tolerance(T v)
Set the floating-point tolerance for a specific floating-point type.
Definition config.hpp:187
double warmup_
The current warmup us value.
Definition config.hpp:222
config(config &&)=delete
Deleted move constructor.
static void set_warmup_us(const time_point_t &start, const time_point_t &end)
Set the warmup us value using time points.
Definition config.hpp:130
static void set_warmup_us(std::chrono::duration< Rep, Period > v)
Set the warmup us value using a std::chrono::duration.
Definition config.hpp:152
static void set_all_fp_tolerance(long double v)
Set the floating-point tolerance for all supported types.
Definition config.hpp:197
static void set_roi_us(const double start, const double end)
Set the roi us value using double.
Definition config.hpp:88
static void set_bench_iters(uint64_t v)
Set the number of benchmark iterations.
Definition config.hpp:73
static void set_roi_us(const time_point_t &start, const time_point_t &end)
Set the roi us value using time points.
Definition config.hpp:84
static void set_warmup_us(const float start, const float end)
Set the warmup us value using float.
Definition config.hpp:136
Singleton class for the current state.
Definition config.hpp:243
static current_state & instance()
Get the singleton instance of the current_state class.
Definition config.hpp:269
current_state(current_state &&)=delete
Deleted move constructor.
static void set_using_plugin(bool v)
Set if a plugin is being used currently.
Definition config.hpp:257
current_state(const current_state &)=delete
Deleted copy constructor.
current_state & operator=(current_state &&)=delete
Deleted move assignment operator.
static void set_impl_name(std::string_view name)
Set the name of the current implementation.
Definition config.hpp:262
current_state & operator=(const current_state &)=delete
Deleted copy assignment operator.
bool using_plugin_
The current state of plugin usage.
Definition config.hpp:276
current_state()=default
Private Default kconstructor.
std::string_view impl_name_
The name of the current implementation.
Definition config.hpp:278
static bool using_plugin()
Get if a plugin is being used currently.
Definition config.hpp:255
static std::string_view impl_name()
Get the name of the current implementation.
Definition config.hpp:260
ComPPare framework main namespace.