-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.cpp
179 lines (142 loc) · 4.97 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <cstdio>
#include <barrier>
#include <chrono>
#include <future>
#include <mutex>
#include <thread>
#include <unistd.h>
#include <fmt/core.h>
#include <tinyopt/tinyopt.h>
#include "experiment.h"
#include "instrument.h"
#include "timers.h"
#ifdef NB_INSTRUMENT
constexpr bool with_instrument = true;
#else
constexpr bool with_instrument = false;
#endif
using value_type = double;
const char* usage_str =
"[OPTION]...\n"
"\n"
" -g, --gpu=bench Perform benchmark on gpu, one of: none, gemm, stream\n"
" -c, --cpu=bench Perform benchmark on cpu, one of: none, gemm, stream\n"
" -d, --duration=N duration in N seconds\n"
" -b, --batch enable batch mode (less verbose output)\n"
" -h, --help Display usage information and exit\n";
// There is a worker thread for each hardware target
constexpr int num_workers = with_gpu? 2: 1;
std::barrier B(num_workers+1);
bool batch_mode = false;
template <class... Args>
void print_safe(fmt::format_string<Args...> s, Args&&... args) {
static std::mutex stdout_guard;
std::lock_guard<std::mutex> _(stdout_guard);
fmt::print(s, std::forward<Args>(args)...);
std::fflush(stdout);
}
struct config {
experiment gpu;
experiment cpu;
// duration of the benchmark in seconds
uint32_t duration = 10;
};
void run_work(std::unique_ptr<benchmark> state, std::string prefix, std::uint32_t total_duration);
int main(int argc, char** argv) {
config cfg;
try {
using namespace to::literals;
auto help = [argv0 = argv[0]] { to::usage(argv0, usage_str); };
std::pair<const char*, benchmark_kind> functions[] = {
{"none", benchmark_kind::none},
{"gemm", benchmark_kind::gemm},
{"stream", benchmark_kind::stream},
};
std::string gpu="none";
std::string cpu="none";
to::option opts[] = {
{gpu, "-g"_compact, "--gpu"},
{cpu, "-c"_compact, "--cpu"},
{cfg.duration, "-d"_compact, "--duration"},
{to::action(help), to::flag, to::exit, "-h", "--help"},
{to::set(batch_mode, true), to::flag, "-b", "--batch"},
};
if (!to::run(opts, argc, argv+1)) return 0;
cfg.cpu = {cpu};
cfg.gpu = {gpu};
if (argv[1]) throw to::option_error("unrecogonized argument", argv[1]);
}
catch (to::option_error& e) {
to::usage_error(argv[0], usage_str, e.what());
return 1;
}
if (!batch_mode) {
print_safe("--------- Node-Burn ---------\n");
print_safe("experiments:\n");
print_safe(" gpu: {}\n", cfg.gpu);
print_safe(" cpu: {}\n", cfg.cpu);
print_safe(" duration: {} seconds\n", cfg.duration);
print_safe("-----------------------------\n\n");
}
if (cfg.gpu.kind!=benchmark_kind::none && !with_gpu) {
print_safe("WARNING: GPU is not enabled. Recompile with GPU enabled to burn the GPU.\n");
exit(1);
}
using job_handle = decltype (std::async(std::launch::async, [](){return;}));
std::vector<job_handle> jobs;
auto instrument = get_instrument();
if (with_gpu) {
jobs.push_back(
std::async(
std::launch::async,
run_work,
get_gpu_benchmark(cfg.gpu), "gpu", cfg.duration));
}
jobs.push_back(
std::async(
std::launch::async,
run_work,
get_cpu_benchmark(cfg.cpu), "cpu", cfg.duration));
// all tasks wait after starting initialisation
B.arrive_and_wait();
instrument->start();
// START
// all tasks start workload
if (!batch_mode) print_safe("\n--- burning for {} seconds\n\n", cfg.duration);
// all tasks wait after finishing work
B.arrive_and_wait();
instrument->stop();
// STOP
for (auto& job: jobs) job.wait();
char host[512];
gethostname(host, 511);
instrument->print_result(fmt::format("{}:", host));
return 0;
}
void run_work(std::unique_ptr<benchmark> state, std::string prefix, std::uint32_t total_duration) {
using namespace std::chrono_literals;
std::vector<double> times;
times.reserve(100000);
auto start_init = timestamp();
char host[512];
gethostname(host, 511);
std::string full_prefix = host + (":" + prefix);
// intialise state and run once
state->init();
state->run();
// synch before burning
state->synchronize();
if (!batch_mode) print_safe("{} finished initialisation in {} seconds\n", full_prefix, duration(start_init));
B.arrive_and_wait();
auto start_fire = timestamp();
while (duration(start_fire)<total_duration) {
auto start = timestamp();
state->run();
state->synchronize();
auto stop = timestamp();
times.push_back(duration(start, stop));
}
B.arrive_and_wait();
if (!batch_mode || state->kind!=benchmark_kind::none)
print_safe("{} {}\n", full_prefix, state->report(times));
}