forked from ehein6/emu-microbench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spawn_rate.c
338 lines (290 loc) · 8.36 KB
/
spawn_rate.c
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <cilk/cilk.h>
#include <assert.h>
#include <string.h>
#include <emu_c_utils/emu_c_utils.h>
#include "recursive_spawn.h"
#include "common.h"
/*
* Goal: Quantify the thread spawn overhead in different circumstances:
* - recursive vs serial spawn tree
* - number of arguments passed to worker thread
* - worker thread has stack or is stackless
* Secondary goal: reproduce issues with va_arg in a minimal test case
*/
typedef struct spawn_rate_data {
long * array;
long n;
} spawn_rate_data;
replicated spawn_rate_data data;
#define DO_WORK(begin, end) \
do { \
for (long *p = begin; p < end; ++p) { \
*p = 1; \
} \
} while (0)
// Stackless worker function
noinline void
light_worker(long * begin, long * end)
{
DO_WORK(begin, end);
}
// Delegates to light_worker, so must allocate a stack frame
noinline void
heavy_worker(long * begin, long * end)
{
light_worker(begin, end);
}
// Recursive spawn, work function is inline
noinline void
recursive_spawn_inline_worker(long * begin, long * end, long grain)
{
for (;;) {
long count = end - begin;
if (count <= grain) break;
long * mid = begin + count / 2;
cilk_spawn recursive_spawn_inline_worker(begin, mid, grain);
begin = mid;
}
DO_WORK(begin, end);
}
// Recursive spawn, work function is stackless
noinline void
recursive_spawn_light_worker(long * begin, long * end, long grain)
{
for (;;) {
long count = end - begin;
if (count <= grain) break;
long * mid = begin + count / 2;
cilk_spawn recursive_spawn_light_worker(begin, mid, grain);
begin = mid;
}
light_worker(begin, end);
}
// Recursive spawn, work function has stack
noinline void
recursive_spawn_heavy_worker(long * begin, long * end, long grain)
{
for (;;) {
long count = end - begin;
if (count <= grain) break;
long * mid = begin + count / 2;
cilk_spawn recursive_spawn_heavy_worker(begin, mid, grain);
begin = mid;
}
heavy_worker(begin, end);
}
// Serial spawn, work function is stackless
noinline void
serial_spawn_light_worker(long * begin, long * end, long grain)
{
for (long * first = begin; first < end; first += grain) {
long * last = first + grain <= end ? first + grain : end;
cilk_spawn light_worker(first, last);
}
}
// Recursive spawn, work function has stack
noinline void
serial_spawn_heavy_worker(long * begin, long * end, long grain)
{
for (long * first = begin; first < end; first += grain) {
long * last = first + grain <= end ? first + grain : end;
cilk_spawn heavy_worker(first, last);
}
}
// Worker used by emu_c_utils
noinline void
library_inline_worker(long begin, long end, va_list args)
{
// NOTE begin and end are passed as indices, not pointers
long* array = va_arg(args, long*);
long * first = array + begin;
long * last = array + end;
DO_WORK(first, last);
}
noinline void
library_light_worker(long begin, long end, va_list args)
{
// NOTE begin and end are passed as indices, not pointers
long* array = va_arg(args, long*);
long * first = array + begin;
long * last = array + end;
light_worker(first, last);
}
noinline void
library_heavy_worker(long begin, long end, va_list args)
{
// NOTE begin and end are passed as indices, not pointers
long* array = va_arg(args, long*);
long * first = array + begin;
long * last = array + end;
heavy_worker(first, last);
}
void
init(long n)
{
data.n = n;
data.array = mw_localmalloc(n * sizeof(long), &n);
assert(data.array);
}
void
deinit()
{
mw_localfree(data.array);
}
void
clear()
{
memset(data.array, 0, data.n * sizeof(long));
}
void
validate()
{
bool success = true;
for (long i = 0; i < data.n; ++i) {
if (data.array[i] != 1) {
success = false;
break;
}
}
if (success) {
LOG("PASSED\n");
} else {
LOG("FAILED\n");
exit(1);
}
}
// Do all the writes within a single function
noinline void
do_inline()
{
DO_WORK(data.array, data.array + data.n);
}
// Call light_worker on each element
noinline void
do_light()
{
long * begin = data.array;
long * end = data.array + data.n;
for (long *i = begin; i < end; ++i) {
light_worker(i, i + 1);
}
}
// Call heavy_worker on each element
noinline void
do_heavy()
{
long * begin = data.array;
long * end = data.array + data.n;
for (long *i = begin; i < end; ++i) {
heavy_worker(i, i + 1);
}
}
// Spawn light_worker for each element
noinline void
do_serial_spawn_light() {
serial_spawn_light_worker(data.array, data.array + data.n, 1);
}
// Spawn heavy_worker for each element
noinline void
do_serial_spawn_heavy() {
serial_spawn_heavy_worker(data.array, data.array + data.n, 1);
}
// Recursive spawn tree, leaf threads do work inline
noinline void
do_recursive_spawn_inline() {
recursive_spawn_inline_worker(data.array, data.array + data.n, 1);
}
// Recursive spawn tree, leaf threads call light_worker
noinline void
do_recursive_spawn_light() {
recursive_spawn_light_worker(data.array, data.array + data.n, 1);
}
// Recursive spawn tree, leaf threads call heavy_worker
noinline void
do_recursive_spawn_heavy() {
recursive_spawn_heavy_worker(data.array, data.array + data.n, 1);
}
// Do the work with an emu_c_utils library call
noinline void
do_library_inline()
{
emu_local_for(0, data.n, 1, library_inline_worker, data.array);
}
noinline void
do_library_light()
{
emu_local_for(0, data.n, 1, library_light_worker, data.array);
}
noinline void
do_library_heavy()
{
emu_local_for(0, data.n, 1, library_heavy_worker, data.array);
}
double
run_baseline(const char * name, void (*benchmark)(), long num_trials)
{
double total_time_ms = 0;
for (long trial = 0; trial < num_trials; ++trial) {
hooks_set_attr_i64("trial", trial);
hooks_region_begin(name);
benchmark();
total_time_ms += hooks_region_end();
}
return total_time_ms / num_trials;
}
void
run_spawn(double serial_time_ms, const char * name, void (*benchmark)(), long num_trials)
{
// Run the benchmark
double time_ms = run_baseline(name, benchmark, num_trials);
// Subtract the time taken to do the work serially
double spawn_time_ms = time_ms - serial_time_ms;
// Compute spawn rate in threads per second
double threads_per_second = spawn_time_ms == 0 ? 0 :
data.n / (spawn_time_ms/1000);
// Print result
LOG("%s: %3.2f million threads/s\n", name, threads_per_second / (1000000));
}
int main(int argc, char** argv)
{
struct {
long log2_num_threads;
long num_trials;
} args;
if (argc != 3) {
LOG("Usage: %s log2_num_threads num_trials\n", argv[0]);
exit(1);
} else {
args.log2_num_threads = atol(argv[1]);
args.num_trials = atol(argv[2]);
if (args.log2_num_threads <= 0) { LOG("num_threads must be > 0"); exit(1); }
if (args.num_trials <= 0) { LOG("num_trials must be > 0"); exit(1); }
}
long n = 1UL << args.log2_num_threads;
LOG("Initializing array with %li elements (%li MiB)\n",
n, (n * sizeof(long)) / (1024*1024)); fflush(stdout);
init(n);
// TODO if validation desired, need to clear after each trial
clear();
hooks_set_attr_i64("log2_num_threads", args.log2_num_threads);
hooks_set_attr_i64("num_nodelets", NODELETS());
double inline_time_ms = run_baseline("inline", do_inline, args.num_trials);
double light_time_ms = run_baseline("light", do_light, args.num_trials);
double heavy_time_ms = run_baseline("heavy", do_heavy, args.num_trials);
#define RUN_BENCHMARK(SERIAL_TIME, NAME) \
run_spawn(SERIAL_TIME, #NAME, do_##NAME, args.num_trials)
RUN_BENCHMARK(light_time_ms, serial_spawn_light);
RUN_BENCHMARK(heavy_time_ms, serial_spawn_heavy);
RUN_BENCHMARK(inline_time_ms, recursive_spawn_inline);
RUN_BENCHMARK(light_time_ms, recursive_spawn_light);
RUN_BENCHMARK(heavy_time_ms, recursive_spawn_heavy);
RUN_BENCHMARK(inline_time_ms, library_inline);
RUN_BENCHMARK(light_time_ms, library_light);
RUN_BENCHMARK(heavy_time_ms, library_heavy);
deinit();
return 0;
}