forked from ehein6/emu-microbench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
global_stream.c
346 lines (306 loc) · 11.2 KB
/
global_stream.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
339
340
341
342
343
344
345
346
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <cilk/cilk.h>
#include <assert.h>
#include <string.h>
#include "common.h"
#include <emu_c_utils/emu_c_utils.h>
#include "recursive_spawn.h"
typedef struct global_stream_data {
emu_chunked_array array_a;
emu_chunked_array array_b;
emu_chunked_array array_c;
long ** a;
long ** b;
long ** c;
long n;
long num_threads;
} global_stream_data;
// #define INDEX(PTR, BLOCK, I) (PTR[I/BLOCK][I%BLOCK])
#define INDEX(PTR, BLOCK, I) (PTR[I >> PRIORITY(BLOCK)][I&(BLOCK-1)])
void
global_stream_init(global_stream_data * data, long n)
{
data->n = n;
emu_chunked_array_replicated_init(&data->array_a, n, sizeof(long));
data->a = (long**)data->array_a.data;
emu_chunked_array_replicated_init(&data->array_b, n, sizeof(long));
data->b = (long**)data->array_b.data;
emu_chunked_array_replicated_init(&data->array_c, n, sizeof(long));
data->c = (long**)data->array_c.data;
#ifdef __le64__
// Replicate pointers to all other nodelets
data = mw_get_nth(data, 0);
for (long i = 1; i < NODELETS(); ++i) {
global_stream_data * remote_data = mw_get_nth(data, i);
memcpy(remote_data, data, sizeof(global_stream_data));
}
#endif
#ifndef NO_VALIDATE
emu_chunked_array_set_long(&data->array_a, 1);
emu_chunked_array_set_long(&data->array_b, 2);
emu_chunked_array_set_long(&data->array_c, 0);
#endif
}
void
global_stream_deinit(global_stream_data * data)
{
emu_chunked_array_replicated_deinit(&data->array_a);
emu_chunked_array_replicated_deinit(&data->array_b);
emu_chunked_array_replicated_deinit(&data->array_c);
}
static void
global_stream_validate_worker(emu_chunked_array * array, long begin, long end, va_list args)
{
long * c = emu_chunked_array_index(array, begin);
for (long i = 0; i < end - begin; ++i) {
if (c[i] != 3) {
LOG("VALIDATION ERROR: c[%li] == %li (supposed to be 3)\n", begin + i, c[i]);
exit(1);
}
}
}
void
global_stream_validate(global_stream_data * data)
{
emu_chunked_array_apply(&data->array_c, GLOBAL_GRAIN(data->n),
global_stream_validate_worker
);
}
// serial - just a regular for loop
void
global_stream_add_serial(global_stream_data * data)
{
long block_sz = data->n / NODELETS();
for (long i = 0; i < data->n; ++i) {
INDEX(data->c, block_sz, i) = INDEX(data->a, block_sz, i) + INDEX(data->b, block_sz, i);
}
}
// cilk_for - cilk_for loop with grainsize set to control number of threads
void
global_stream_add_cilk_for(global_stream_data * data)
{
long block_sz = data->n / NODELETS();
#pragma cilk grainsize = data->n / data->num_threads
cilk_for (long i = 0; i < data->n; ++i) {
INDEX(data->c, block_sz, i) = INDEX(data->a, block_sz, i) + INDEX(data->b, block_sz, i);
}
}
void
recursive_spawn_add_worker(long begin, long end, global_stream_data *data)
{
long block_sz = data->n / NODELETS();
for (long i = begin; i < end; ++i) {
INDEX(data->c, block_sz, i) = INDEX(data->a, block_sz, i) + INDEX(data->b, block_sz, i);
}
}
void
recursive_spawn_add(long begin, long end, long grain, global_stream_data *data)
{
RECURSIVE_CILK_SPAWN(begin, end, grain, recursive_spawn_add, data);
}
// recursive_spawn - recursively spawn threads to subdivide the range until the grain size is reached
void
global_stream_add_recursive_spawn(global_stream_data * data)
{
recursive_spawn_add(0, data->n, data->n / data->num_threads, data);
}
// serial_spawn - spawn one thread to handle each grain-sized chunk of the range
void
global_stream_add_serial_spawn(global_stream_data * data)
{
long grain = data->n / data->num_threads;
for (long i = 0; i < data->n; i += grain) {
long begin = i;
long end = begin + grain <= data->n ? begin + grain : data->n;
cilk_spawn recursive_spawn_add_worker(begin, end, data);
}
cilk_sync;
}
void
serial_remote_spawn_level2(long begin, long end, long * a, long * b, long * c)
{
for (long i = begin; i < end; ++i) {
c[i] = a[i] + b[i];
}
}
void
serial_remote_spawn_level1(long * a, long * b, long * c, long n, long grain)
{
for (long i = 0; i < n; i += grain) {
long begin = i;
long end = begin + grain <= n ? begin + grain : n;
cilk_spawn serial_remote_spawn_level2(begin, end, a, b, c);
}
cilk_sync;
}
// serial_remote_spawn - remote spawn a thread on each nodelet, then do a serial spawn locally
void
global_stream_add_serial_remote_spawn(global_stream_data * data)
{
// Each thread will be responsible for the elements on one nodelet
long local_n = data->n / NODELETS();
// Calculate the grain so we get the right number of threads globally
long grain = data->n / data->num_threads;
// Spawn a thread on each nodelet
for (long i = 0; i < NODELETS(); ++i) {
cilk_spawn_at(data->a[i]) serial_remote_spawn_level1(data->a[i], data->b[i], data->c[i], local_n, grain);
}
cilk_sync;
}
void
recursive_remote_spawn_level2_worker(long begin, long end, long * a, long * b, long * c)
{
for (long i = begin; i < end; ++i) {
c[i] = a[i] + b[i];
}
}
void
recursive_remote_spawn_level2(long begin, long end, long grain, long * a, long * b, long * c)
{
RECURSIVE_CILK_SPAWN(begin, end, grain, recursive_remote_spawn_level2, a, b, c);
}
void
recursive_remote_spawn_level1(long low, long high, global_stream_data * data)
{
for (;;) {
long count = high - low;
if (count == 1) break;
long mid = low + count / 2;
cilk_spawn_at(data->a[low]) recursive_remote_spawn_level1(low, mid, data);
low = mid;
}
/* Recursive base case: call worker function */
long local_n = data->n / NODELETS();
long grain = data->n / data->num_threads;
recursive_remote_spawn_level2(0, local_n, grain, data->a[low], data->b[low], data->c[low]);
}
// recursive_remote_spawn - Recursively spawns threads to divice up the loop range, using remote spawns where possible.
void
global_stream_add_recursive_remote_spawn(global_stream_data * data)
{
recursive_remote_spawn_level1(0, NODELETS(), data);
}
void
global_stream_add_library_worker(emu_chunked_array * array, long begin, long end, va_list args)
{
(void)array;
global_stream_data * data = va_arg(args, global_stream_data *);
long block_sz = data->n / NODELETS();
long * c = &INDEX(data->c, block_sz, begin);
long * b = &INDEX(data->b, block_sz, begin);
long * a = &INDEX(data->a, block_sz, begin);
for (long i = 0; i < end-begin; ++i) {
c[i] = a[i] + b[i];
}
}
void
global_stream_add_library(global_stream_data * data)
{
emu_chunked_array_apply(&data->array_a, data->n / data->num_threads,
global_stream_add_library_worker, data
);
}
// serial_remote_spawn_shallow - same as serial_remote_spawn, but with only one level of spawning
void
global_stream_add_serial_remote_spawn_shallow(global_stream_data * data)
{
long local_n = data->n / NODELETS();
long grain = data->n / data->num_threads;
for (long i = 0; i < NODELETS(); ++i) {
long * a = data->a[i];
long * b = data->b[i];
long * c = data->c[i];
for (long j = 0; j < local_n; j += grain) {
long begin = j;
long end = begin + grain <= local_n ? begin + grain : local_n;
cilk_spawn_at(a) serial_remote_spawn_level2(begin, end, a, b, c);
}
}
cilk_sync;
}
void global_stream_run(
global_stream_data * data,
const char * name,
void (*benchmark)(global_stream_data *),
long num_trials)
{
for (long trial = 0; trial < num_trials; ++trial) {
hooks_set_attr_i64("trial", trial);
hooks_region_begin(name);
benchmark(data);
double time_ms = hooks_region_end();
double bytes_per_second = time_ms == 0 ? 0 :
(data->n * sizeof(long) * 3) / (time_ms/1000);
LOG("%3.2f MB/s\n", bytes_per_second / (1000000));
}
}
replicated global_stream_data data;
int main(int argc, char** argv)
{
struct {
const char* mode;
long log2_num_elements;
long num_threads;
long num_trials;
} args;
if (argc != 5) {
LOG("Usage: %s mode log2_num_elements num_threads num_trials\n", argv[0]);
exit(1);
} else {
args.mode = argv[1];
args.log2_num_elements = atol(argv[2]);
args.num_threads = atol(argv[3]);
args.num_trials = atol(argv[4]);
if (args.log2_num_elements <= 0) { LOG("log2_num_elements must be > 0"); exit(1); }
if (args.num_threads <= 0) { LOG("num_threads must be > 0"); exit(1); }
if (args.num_trials <= 0) { LOG("num_trials must be > 0"); exit(1); }
}
hooks_set_attr_str("spawn_mode", args.mode);
hooks_set_attr_i64("log2_num_elements", args.log2_num_elements);
hooks_set_attr_i64("num_threads", args.num_threads);
hooks_set_attr_i64("num_nodelets", NODELETS());
hooks_set_attr_i64("num_bytes_per_element", sizeof(long) * 3);
long n = 1L << args.log2_num_elements;
long mbytes = n * sizeof(long) / (1024*1024);
long mbytes_per_nodelet = mbytes / NODELETS();
LOG("Initializing arrays with %li elements each (%li MiB total, %li MiB per nodelet)\n", 3 * n, 3 * mbytes, 3 * mbytes_per_nodelet);
fflush(stdout);
data.num_threads = args.num_threads;
global_stream_init(&data, n);
LOG("Doing vector addition using %s\n", args.mode); fflush(stdout);
#define RUN_BENCHMARK(X) global_stream_run(&data, args.mode, X, args.num_trials)
if (!strcmp(args.mode, "cilk_for")) {
RUN_BENCHMARK(global_stream_add_cilk_for);
} else if (!strcmp(args.mode, "serial_spawn")) {
RUN_BENCHMARK(global_stream_add_serial_spawn);
} else if (!strcmp(args.mode, "serial_remote_spawn")) {
runtime_assert(data.num_threads >= NODELETS(), "serial_remote_spawn mode will always use at least one thread per nodelet");
RUN_BENCHMARK(global_stream_add_serial_remote_spawn);
} else if (!strcmp(args.mode, "serial_remote_spawn_shallow")) {
runtime_assert(data.num_threads >= NODELETS(), "serial_remote_spawn_shallow mode will always use at least one thread per nodelet");
RUN_BENCHMARK(global_stream_add_serial_remote_spawn_shallow);
} else if (!strcmp(args.mode, "recursive_spawn")) {
RUN_BENCHMARK(global_stream_add_recursive_spawn);
} else if (!strcmp(args.mode, "recursive_remote_spawn")) {
runtime_assert(data.num_threads >= NODELETS(), "recursive_remote_spawn mode will always use at least one thread per nodelet");
RUN_BENCHMARK(global_stream_add_recursive_remote_spawn);
} else if (!strcmp(args.mode, "library")) {
runtime_assert(data.num_threads >= NODELETS(), "emu_for_2d mode will always use at least one thread per nodelet");
RUN_BENCHMARK(global_stream_add_library);
} else if (!strcmp(args.mode, "serial")) {
runtime_assert(data.num_threads == 1, "serial mode can only use one thread");
RUN_BENCHMARK(global_stream_add_serial);
} else {
LOG("Mode %s not implemented!", args.mode);
}
#ifndef NO_VALIDATE
LOG("Validating results...");
global_stream_validate(&data);
LOG("OK\n");
#endif
global_stream_deinit(&data);
return 0;
}