valFuzz 1.2.0
Loading...
Searching...
No Matches
benchmark.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
6#pragma once
7
8#include <atomic>
9#include <cmath>
10#include <cstdlib>
11#include <ctime>
12#include <deque>
13#include <filesystem>
14#include <fstream>
15#include <functional>
16#include <iostream>
17#include <limits>
18#include <mutex>
19#include <optional>
20#include <string>
21#include <tuple>
22#include <valfuzz/common.hpp>
23#include <valfuzz/reporter.hpp>
24
25#ifdef openMP
26#include <omp.h>
27#endif
28
29namespace valfuzz
30{
31
32#define NUM_ITERATIONS_BENCHMARK 10000
33
34/* Benchmarks */
35
36#define BENCHMARK(name, pretty_name) \
37 void name([[maybe_unused]] const std::string &benchmark_name); \
38 static struct name##_register \
39 { \
40 name##_register() \
41 { \
42 valfuzz::add_benchmark(pretty_name, name); \
43 } \
44 } name##_register_instance; \
45 void name([[maybe_unused]] const std::string &benchmark_name)
46
47#define RUN_BENCHMARK(input_size, ...) \
48 { \
49 std::cout << std::flush; \
50 std::chrono::duration<double> total = \
51 std::chrono::duration<double>::zero(); \
52 double mean = 0.0; \
53 double M2 = 0.0; \
54 const int N_ITER = valfuzz::get_num_iterations_benchmark(); \
55 double *times = new double[N_ITER]; \
56 /* run twice to warm up the cache */ \
57 __VA_ARGS__; \
58 __VA_ARGS__; \
59 for (int i = 0; i < N_ITER; i++) \
60 { \
61 auto start = std::chrono::high_resolution_clock::now(); \
62 __VA_ARGS__; \
63 auto end = std::chrono::high_resolution_clock::now(); \
64 std::chrono::duration<double> elapsed = end - start; \
65 total += elapsed; \
66 double e = elapsed.count(); \
67 times[i] = e; \
68 /* Welford's algorithm */ \
69 double delta = e - mean; \
70 mean += delta / (i + 1); \
71 M2 += (e - mean) * delta; \
72 } \
73 double variance = M2 / N_ITER; \
74 std::sort(times, times + N_ITER); \
75 struct valfuzz::report rep = { \
76 benchmark_name, \
77 (long unsigned int) input_size, \
78 times[N_ITER > 1 ? 1 : 0], \
79 times[N_ITER > 1 ? N_ITER - 2 : N_ITER - 1], \
80 times[N_ITER / 2], \
81 total.count() / N_ITER, \
82 std::sqrt(variance), \
83 times[N_ITER / 4], \
84 times[3 * N_ITER / 4], \
85 }; \
86 std::lock_guard<std::mutex> lock(valfuzz::get_stream_mutex()); \
87 std::cout \
88 << valfuzz::reporter_eg.report(&rep, valfuzz::get_reporter()).str() \
89 << std::flush; \
90 if (valfuzz::get_save_to_file()) \
91 { \
92 valfuzz::get_save_file() \
93 << valfuzz::reporter_eg.report(&rep, valfuzz::get_reporter()).str() \
94 << std::flush; \
95 } \
96 }
97
98typedef std::function<void(std::string)> benchmark_function;
99typedef std::pair<std::string, benchmark_function> benchmark_pair;
100
101unsigned long get_cache_l3_size();
102bool &get_do_benchmarks();
105std::string &get_one_benchmark();
106unsigned long get_num_benchmarks();
107std::deque<benchmark_pair> &get_benchmarks();
108std::atomic<bool> &get_save_to_file();
109std::ofstream &get_save_file();
110
111void set_save_to_file(bool save_to_file);
112void set_save_file(const std::filesystem::path &save_to_file_path);
113void add_benchmark(const std::string &name, benchmark_function benchmark);
114void set_do_benchmarks(bool do_benchmarks);
115void set_num_iterations_benchmark(int num_iterations_benchmark);
116void set_run_one_benchmark(bool run_one_benchmark);
117void set_one_benchmark(const std::string &one_benchmark);
118
119void run_benchmarks();
120
121} // namespace valfuzz
int & get_num_iterations_benchmark()
Definition benchmark.cpp:77
bool & get_do_benchmarks()
Definition benchmark.cpp:62
void set_save_file(const std::filesystem::path &save_to_file_path)
void set_num_iterations_benchmark(int num_iterations_benchmark)
bool & get_run_one_benchmark()
Definition benchmark.cpp:92
void set_save_to_file(bool save_to_file)
std::pair< std::string, benchmark_function > benchmark_pair
Definition benchmark.hpp:99
void set_one_benchmark(const std::string &one_benchmark)
std::ofstream & get_save_file()
void set_do_benchmarks(bool do_benchmarks)
void add_benchmark(const std::string &name, benchmark_function benchmark)
unsigned long get_num_benchmarks()
Definition benchmark.cpp:86
void run_benchmarks()
std::atomic< bool > & get_save_to_file()
Definition benchmark.cpp:11
std::string & get_one_benchmark()
unsigned long get_cache_l3_size()
Definition benchmark.cpp:56
std::function< void(std::string)> benchmark_function
Definition benchmark.hpp:98
std::deque< benchmark_pair > & get_benchmarks()
Definition benchmark.cpp:71
void set_run_one_benchmark(bool run_one_benchmark)