-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlocal_stream_cxx.cc
184 lines (162 loc) · 4.43 KB
/
local_stream_cxx.cc
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
#include <cstdio>
#include <cstdint>
#include <cstdlib>
#include <cilk/cilk.h>
#include <cassert>
#include <cstring>
#include <emu_c_utils/emu_c_utils.h>
#include <emu_cxx_utils/for_each.h>
#include "common.h"
using namespace emu;
struct local_stream {
long * a;
long * b;
long * c;
long n;
local_stream(long n) : n(n)
{
a = (long*)malloc(n * sizeof(long));
assert(a);
b = (long*)malloc(n * sizeof(long));
assert(b);
c = (long*)malloc(n * sizeof(long));
assert(c);
}
~local_stream()
{
free(a);
free(b);
free(c);
}
void init()
{
memset(a, 0, n * sizeof(long));
memset(b, 0, n * sizeof(long));
memset(c, 0, n * sizeof(long));
}
void validate()
{
for (long i = 0; i < n; ++i) {
if (c[i] != 3) {
LOG("VALIDATION ERROR: c[%li] == %li (supposed to be 3)\n", i, c[i]);
exit(1);
}
}
}
void
add_serial()
{
for (long i = 0; i < n; ++i) {
c[i] = a[i] + b[i];
}
}
void
add_cilk_for()
{
#ifndef NO_GRAINSIZE_COMPUTE
#pragma cilk grainsize = n / num_threads
#endif
cilk_for (long i = 0; i < n; ++i) {
c[i] = a[i] + b[i];
}
}
void
add_sequential()
{
parallel::for_each(seq, a, a + n, [this](long& a_ref) {
long i = &a_ref - a;
c[i] = a[i] + b[i];
});
}
void
add_parallel()
{
parallel::for_each(par, a, a + n, [this](long& a_ref) {
long i = &a_ref - a;
c[i] = a[i] + b[i];
});
}
void
add_static()
{
parallel::for_each(fixed, a, a + n, [this](long& a_ref) {
long i = &a_ref - a;
c[i] = a[i] + b[i];
});
}
void
add_dynamic()
{
parallel::for_each(dyn, a, a + n, [this](long& a_ref) {
long i = &a_ref - a;
c[i] = a[i] + b[i];
});
}
void
run(const char * name, long num_trials)
{
for (long trial = 0; trial < num_trials; ++trial) {
hooks_set_attr_i64("trial", trial);
double time_ms = 0;
#define RUN_BENCHMARK(X) \
do { \
hooks_region_begin(name); \
X(); \
time_ms = hooks_region_end(); \
} while(false)
if (!strcmp(name, "serial")) {
RUN_BENCHMARK(add_serial);
} else if (!strcmp(name, "cilk_for")) {
RUN_BENCHMARK(add_cilk_for);
} else if (!strcmp(name, "seq")) {
RUN_BENCHMARK(add_sequential);
} else if (!strcmp(name, "par")) {
RUN_BENCHMARK(add_parallel);
} else if (!strcmp(name, "dyn")) {
RUN_BENCHMARK(add_dynamic);
} else if (!strcmp(name, "fixed")) {
RUN_BENCHMARK(add_static);
} else {
printf("Mode %s not implemented!", name);
exit(1);
}
#undef RUN_BENCHMARK
double mbytes_per_second = (1e-6 * n * sizeof(long) * 3)
/ (1e-3 * time_ms);
LOG("%3.2f MB/s\n", mbytes_per_second);
}
}
};
int main(int argc, char** argv)
{
struct {
const char* mode;
long log2_num_elements;
long num_trials;
} args;
if (argc != 4) {
LOG("Usage: %s mode log2_num_elements num_trials\n", argv[0]);
exit(1);
} else {
args.mode = argv[1];
args.log2_num_elements = atol(argv[2]);
args.num_trials = atol(argv[4]);
if (args.log2_num_elements <= 0) { LOG("log2_num_elements must be > 0"); exit(1); }
if (args.num_trials <= 0) { LOG("num_trials must be > 0"); exit(1); }
}
hooks_set_attr_str("mode", args.mode);
hooks_set_attr_i64("log2_num_elements", args.log2_num_elements);
long n = 1L << args.log2_num_elements;
LOG("Initializing arrays with %li elements each (%li MiB)\n",
n, (n * sizeof(long)) / (1024*1024));
local_stream benchmark(n);
#ifndef NO_VALIDATE
benchmark.init();
#endif
LOG("Doing vector addition using %s\n", args.mode);
benchmark.run(args.mode, args.num_trials);
#ifndef NO_VALIDATE
benchmark.validate();
#endif
return 0;
}