-
-
Notifications
You must be signed in to change notification settings - Fork 421
/
coroutines_call_overhead.cpp
304 lines (238 loc) · 8.39 KB
/
coroutines_call_overhead.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright (c) 2006, Giovanni P. Deretta
// Copyright (c) 2012 Hartmut Kaiser
// Copyright (c) 2011-2014 Bryce Adelstein-Lelbach
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <hpx/hpx_init.hpp>
#include <hpx/hpx.hpp>
#include <boost/format.hpp>
#include <boost/random.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <chrono>
#include <cstdint>
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
#include "worker_timed.hpp"
char const* benchmark_name = "Context Switching Overhead - HPX";
using namespace boost::program_options;
using namespace hpx::threads;
using hpx::threads::coroutine_type;
using std::cout;
///////////////////////////////////////////////////////////////////////////////
std::uint64_t payload = 0;
std::uint64_t contexts = 1000;
std::uint64_t iterations = 100000;
std::uint64_t seed = 0;
bool header = true;
///////////////////////////////////////////////////////////////////////////////
std::string format_build_date(std::string timestamp)
{
std::chrono::time_point<std::chrono::system_clock> now =
std::chrono::system_clock::now();
std::time_t current_time = std::chrono::system_clock::to_time_t(now);
std::string ts = std::ctime(¤t_time);
ts.resize(ts.size()-1); // remove trailing '\n'
return ts;
}
///////////////////////////////////////////////////////////////////////////////
void print_results(
double w_M
// , std::vector<std::string> const& counter_shortnames
// , std::shared_ptr<hpx::util::activate_counters> ac
)
{
// std::vector<hpx::performance_counters::counter_value> counter_values;
// if (ac)
// counter_values = ac->evaluate_counters(launch::sync);
if (header)
{
cout << "# BENCHMARK: " << benchmark_name << "\n";
cout << "# VERSION: " << HPX_HAVE_GIT_COMMIT << " "
<< format_build_date(__DATE__) << "\n"
<< "#\n";
// Note that if we change the number of fields above, we have to
// change the constant that we add when printing out the field # for
// performance counters below (e.g. the last_index part).
cout <<
"## 0:PLOAD:Payload [micro-seconds] - Independent Variable\n"
"## 1:OSTHRDS:OS-Threads - Independent Variable\n"
"## 2:CTXS:# of Contexts - Independent Variable\n"
"## 3:ITER:# of Iterations - Independent Variable\n"
"## 4:SEED:PRNG seed - Independent Variable\n"
"## 5:WTIME_CS:Walltime/Context Switch [nano-seconds]\n"
;
/*
std::uint64_t const last_index = 5;
for (std::uint64_t i = 0; i < counter_shortnames.size(); ++i)
{
cout << "## "
<< (i + 1 + last_index) << ":"
<< counter_shortnames[i] << ":"
<< ac->name(i);
if (!ac->unit_of_measure(i).empty())
cout << " [" << ac->unit_of_measure(i) << "]";
cout << "\n";
}
*/
}
std::uint64_t const os_thread_count = hpx::get_os_thread_count();
double w_T = iterations*payload*os_thread_count*1e-6;
// double E = w_T/w_M;
double O = w_M-w_T;
/*
cout << "w_T " << w_T << "\n"
<< "w_M " << w_M << "\n"
<< "E " << E << "\n"
<< "O " << O << "\n"
;
*/
cout << ( boost::format("%lu %lu %lu %lu %lu %.14g")
% payload
% os_thread_count
% contexts
% iterations
% seed
% (((O/(2*iterations*os_thread_count))*1e9))
// % (((walltime/(2*iterations*os_thread_count))*1e9)
);
/*
if (ac)
{
for (std::uint64_t i = 0; i < counter_shortnames.size(); ++i)
cout << ( boost::format(" %.14g")
% counter_values[i].get_value<double>());
}
*/
cout << "\n";
}
///////////////////////////////////////////////////////////////////////////////
struct kernel
{
hpx::threads::thread_result_type operator()(thread_state_ex_enum) const
{
worker_timed(payload * 1000);
return hpx::threads::thread_result_type(hpx::threads::pending, nullptr);
}
bool operator!() const { return true; }
};
double perform_2n_iterations()
{
std::vector<coroutine_type*> coroutines;
std::vector<std::uint64_t> indices;
coroutines.reserve(contexts);
indices.reserve(iterations);
boost::random::mt19937_64 prng(seed);
boost::random::uniform_int_distribution<std::uint64_t>
dist(0, contexts - 1);
kernel k;
for (std::uint64_t i = 0; i < contexts; ++i)
{
coroutine_type* c = new coroutine_type(k);
coroutines.push_back(c);
}
for (std::uint64_t i = 0; i < iterations; ++i)
indices.push_back(dist(prng));
///////////////////////////////////////////////////////////////////////
// Warmup
for (std::uint64_t i = 0; i < iterations; ++i)
{
(*coroutines[indices[i]])(wait_signaled);
}
hpx::util::high_resolution_timer t;
for (std::uint64_t i = 0; i < iterations; ++i)
{
(*coroutines[indices[i]])(wait_signaled);
}
double elapsed = t.elapsed();
for (std::uint64_t i = 0; i < contexts; ++i)
{
delete coroutines[i];
}
coroutines.clear();
return elapsed;
}
int hpx_main(
variables_map& vm
)
{
{
if (vm.count("no-header"))
header = false;
if (!seed)
seed = std::uint64_t(std::time(nullptr));
std::uint64_t const os_thread_count = hpx::get_os_thread_count();
std::vector<hpx::shared_future<double> > futures;
std::uint64_t num_thread = hpx::get_worker_thread_num();
for (std::uint64_t i = 0; i < os_thread_count; ++i)
{
if (num_thread == i) continue;
futures.push_back(hpx::async(&perform_2n_iterations));
}
double total_elapsed = perform_2n_iterations();
for (std::uint64_t i = 0; i < futures.size(); ++i)
total_elapsed += futures[i].get();
print_results(total_elapsed);
///////////////////////////////////////////////////////////////////////
/*
std::vector<std::string> counter_shortnames;
std::vector<std::string> counters;
if (vm.count("counter"))
{
std::vector<std::string> raw_counters =
vm["counter"].as<std::vector<std::string> >();
for (std::uint64_t i = 0; i < raw_counters.size(); ++i)
{
std::vector<std::string> entry;
boost::algorithm::split(entry, raw_counters[i],
boost::algorithm::is_any_of(","),
boost::algorithm::token_compress_on);
HPX_ASSERT(entry.size() == 2);
counter_shortnames.push_back(entry[0]);
counters.push_back(entry[1]);
}
}
std::shared_ptr<hpx::util::activate_counters> ac;
if (!counters.empty())
ac.reset(new hpx::util::activate_counters(counters));
*/
}
return hpx::finalize();
}
///////////////////////////////////////////////////////////////////////////////
int main(
int argc
, char* argv[]
)
{
// Configure application-specific options.
options_description cmdline("usage: " HPX_APPLICATION_STRING " [options]");
cmdline.add_options()
( "payload"
, value<std::uint64_t>(&payload)->default_value(0)
, "artificial delay of each coroutine")
( "contexts"
, value<std::uint64_t>(&contexts)->default_value(100000)
, "number of contexts use")
( "iterations"
, value<std::uint64_t>(&iterations)->default_value(100000)
, "number of iterations to invoke (2 * iterations context switches "
"will occur)")
( "seed"
, value<std::uint64_t>(&seed)->default_value(0)
, "seed for the pseudo random number generator (if 0, a seed is "
"choosen based on the current system time)")
/*
( "counter"
, value<std::vector<std::string> >()->composing()
, "activate and report the specified performance counter")
*/
( "no-header"
, "do not print out the header")
;
// Initialize and run HPX.
return hpx::init(cmdline, argc, argv);
}