-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfactor_benchmark.cpp
424 lines (328 loc) · 10.9 KB
/
rfactor_benchmark.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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include "Halide.h"
#include "benchmark.h"
#include <stdio.h>
#include <memory>
using namespace Halide;
using namespace Halide::Internal;
// Controls the size of the input data
#define N1 4
#define N2 4
int one_d_max() {
const int size = 1024 * 1024 * N1 * N2;
ImageParam A(Float(32), 1);
RDom r(0, size);
Func max_ref("max_ref");
max_ref() = 0.0f;
max_ref() = max(max_ref(), abs(A(r)));
Func maxf("maxf");
maxf() = 0.0f;
RVar rxo, rxi, rxio, rxii;
maxf() = max(maxf(), abs(A(r)));
maxf.update().split(r.x, rxo, rxi, 4*8192);
Var u, v;
Func intm = maxf.update().rfactor(rxo, u);
intm.compute_root()
.update()
.parallel(u)
.split(rxi, rxio, rxii, 8)
.rfactor(rxii, v)
.compute_at(intm, u)
.vectorize(v)
.update()
.vectorize(v);
const int trials = 10;
const int iterations = 10;
Image<float> vec_A(size);
Image<float> ref_output = Image<float>::make_scalar();
Image<float> output = Image<float>::make_scalar();
// init randomly
for (int ix = 0; ix < size; ix++) {
vec_A(ix) = rand();
}
A.set(vec_A);
double t_ref = benchmark(trials, iterations, [&]() {
max_ref.realize(ref_output);
});
double t = benchmark(trials, iterations, [&]() {
maxf.realize(output);
});
float gbits = 32.0 * size / 1e9; // bits per seconds
printf("Max ref: %fms, %f Gbps\n", t_ref * 1e3, (gbits / t_ref));
printf("Max with rfactor: %fms, %f Gbps\n", t * 1e3, (gbits / t));
double improve = t_ref / t;
printf("Improvement: %f\n\n", improve);
return 0;
}
int two_d_histogram() {
int W = 1024*N1, H = 1024*N2;
Image<uint8_t> in(W, H);
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
in(x, y) = rand();
}
}
Func hist("hist"), ref("ref");
Var x, y;
RDom r(0, W, 0, H);
ref(x) = 0;
ref(in(r.x, r.y)) += 1;
hist(x) = 0;
hist(in(r.x, r.y)) += 1;
Var u;
RVar ryo, ryi;
hist
.update()
.split(r.y, ryo, ryi, 16)
.rfactor(ryo, u)
.compute_root()
.vectorize(x, 8)
.update().parallel(u);
hist.update().vectorize(x, 8);
const int trials = 10;
const int iterations = 10;
ref.realize(256);
hist.realize(256);
Image<int> result(256);
double t_ref = benchmark(trials, iterations, [&]() {
ref.realize(result);
});
double t = benchmark(trials, iterations, [&]() {
hist.realize(result);
});
double gbits = in.type().bits * W * H / 1e9; // bits per seconds
printf("Histogram ref: %fms, %f Gbps\n", t_ref * 1e3, (gbits / t_ref));
printf("Histogram with rfactor: %fms, %f Gbps\n", t * 1e3, (gbits / t));
double improve = t_ref / t;
printf("Improvement: %f\n\n", improve);
return 0;
}
int four_d_argmin() {
const int size = 64;
Func amin("amin"), ref("ref");
ImageParam input(UInt(8), 4);
RDom r(0, size, 0, size, 0, size, 0, size);
ref() = Tuple(255, 0, 0, 0, 0);
ref() = Tuple(min(ref()[0], input(r.x, r.y, r.y, r.z)),
select(ref()[0] < input(r.x, r.y, r.y, r.z), ref()[1], r.x),
select(ref()[0] < input(r.x, r.y, r.y, r.z), ref()[2], r.y),
select(ref()[0] < input(r.x, r.y, r.z, r.w), ref()[3], r.z),
select(ref()[0] < input(r.x, r.y, r.z, r.w), ref()[4], r.w));
amin() = Tuple(255, 0, 0, 0, 0);
amin() = Tuple(min(amin()[0], input(r.x, r.y, r.z, r.w)),
select(amin()[0] < input(r.x, r.y, r.z, r.w), amin()[1], r.x),
select(amin()[0] < input(r.x, r.y, r.z, r.w), amin()[2], r.y),
select(amin()[0] < input(r.x, r.y, r.z, r.w), amin()[3], r.z),
select(amin()[0] < input(r.x, r.y, r.z, r.w), amin()[4], r.w));
Var u;
Func intm1 = amin.update(0).rfactor(r.w, u);
intm1.compute_root();
intm1.update(0).parallel(u);
Var v;
RVar rxo, rxi;
Func intm2 = intm1.update(0).split(r.x, rxo, rxi, 16).rfactor(rxi, v);
intm2.compute_at(intm1, u);
intm2.update(0).vectorize(v);
const int iterations = 10;
const int trials = 10;
Image<uint8_t> vec(size, size, size, size);
// init randomly
for (int iw = 0; iw < size; iw++) {
for (int iz = 0; iz < size; iz++) {
for (int iy = 0; iy < size; iy++) {
for (int ix = 0; ix < size; ix++) {
vec(ix, iy, iz, iw) = (rand() % size);
}
}
}
}
input.set(vec);
ref.realize();
amin.realize();
double t_ref = benchmark(trials, iterations, [&]() {
ref.realize();
});
double t = benchmark(trials, iterations, [&]() {
amin.realize();
});
float gbits = input.type().bits() * vec.number_of_elements() / 1e9; // bits per seconds
printf("Argmin ref: %fms, %f Gbps\n", t_ref * 1e3, (gbits / t_ref));
printf("Argmin with rfactor: %fms, %f Gbps\n", t * 1e3, (gbits / t));
double improve = t_ref / t;
printf("Improvement: %f\n\n", improve);
return 0;
}
int complex_multiply() {
const int size = 1024*1024*N1 * N2;
Func mult("mult"), ref("ref");
// TODO: change to float
ImageParam input0(Int(32), 1);
ImageParam input1(Int(32), 1);
RDom r(0, size);
ref() = Tuple(1, 0);
ref() = Tuple(ref()[0]*input0(r.x) - ref()[1]*input1(r.x),
ref()[0]*input1(r.x) + ref()[1]*input0(r.x));
mult() = Tuple(1, 0);
mult() = Tuple(mult()[0]*input0(r.x) - mult()[1]*input1(r.x),
mult()[0]*input1(r.x) + mult()[1]*input0(r.x));
RVar rxi, rxo, rxii, rxio;
mult.update(0).split(r.x, rxo, rxi, 2*8192);
Var u, v;
Func intm = mult.update().rfactor(rxo, u);
intm.compute_root()
.vectorize(u, 8)
.update()
.parallel(u)
.split(rxi, rxio, rxii, 8)
.rfactor(rxii, v)
.compute_at(intm, u)
.vectorize(v)
.update()
.vectorize(v);
const int trials = 10;
const int iterations = 10;
Image<int32_t> vec0(size), vec1(size);
// init randomly
for (int ix = 0; ix < size; ix++) {
vec0(ix) = (rand() % size);
vec1(ix) = (rand() % size);
}
input0.set(vec0);
input1.set(vec1);
ref.realize();
mult.realize();
double t_ref = benchmark(trials, iterations, [&]() {
ref.realize();
});
double t = benchmark(trials, iterations, [&]() {
mult.realize();
});
float gbits = input0.type().bits() * size * 2 / 1e9; // bits per seconds
printf("Complex-multiply ref: %fms, %f Gbps\n", t_ref * 1e3, (gbits / t_ref));
printf("Complex-multiply with rfactor: %fms, %f Gbps\n", t * 1e3, (gbits / t));
double improve = t_ref / t;
printf("Improvement: %f\n\n", improve);
return 0;
}
int dot_product() {
const int size = 1024 * 1024 * N1 * N2;
ImageParam A(Float(32), 1);
ImageParam B(Float(32), 1);
Param<int> p;
RDom r(0, size);
// Reference implementation
Func dot_ref("dot_ref");
dot_ref() = 0.0f;
dot_ref() += (A(r.x))*B(r.x);
Func dot("dot");
dot() = 0.0f;
dot() += (A(r.x))*B(r.x);
RVar rxo, rxi, rxio, rxii;
dot.update().split(r.x, rxo, rxi, 4*8192);
Var u, v;
Func intm = dot.update().rfactor(rxo, u);
intm.compute_root()
.update()
.parallel(u)
.split(rxi, rxio, rxii, 8)
.rfactor(rxii, v)
.compute_at(intm, u)
.vectorize(v)
.update()
.vectorize(v);
const int trials = 10;
const int iterations = 10;
Image<float> vec_A(size), vec_B(size);
Image<float> ref_output = Image<float>::make_scalar();
Image<float> output = Image<float>::make_scalar();
// init randomly
for (int ix = 0; ix < size; ix++) {
vec_A(ix) = rand();
vec_B(ix) = rand();
}
A.set(vec_A);
B.set(vec_B);
double t_ref = benchmark(trials, iterations, [&]() {
dot_ref.realize(ref_output);
});
double t = benchmark(trials, iterations, [&]() {
dot.realize(output);
});
// Note that LLVM autovectorizes the reference!
float gbits = 32 * size * (2 / 1e9); // bits per seconds
printf("Dot-product ref: %fms, %f Gbps\n", t_ref * 1e3, (gbits / t_ref));
printf("Dot-product with rfactor: %fms, %f Gbps\n", t * 1e3, (gbits / t));
double improve = t_ref / t;
printf("Improvement: %f\n\n", improve);
return 0;
}
int kitchen_sink() {
const int size = 1024 * 1024 * N1 * N2;
ImageParam A(Int(32), 1);
RDom r(0, size);
Func sink_ref("sink_ref");
sink_ref() = {0, 0, int(0x80000000), 0, int(0x7fffffff), 0, 0, 0};
sink_ref() = {sink_ref()[0] * A(r), // Product
sink_ref()[1] + A(r), // Sum
max(sink_ref()[2], A(r)), // Max
select(sink_ref()[2] > A(r), sink_ref()[3], r), // Argmax
min(sink_ref()[4], A(r)), // Min
select(sink_ref()[4] < A(r), sink_ref()[5], r), // Argmin
sink_ref()[6] + A(r)*A(r), // Sum of squares
sink_ref()[7] + select(A(r) % 2 == 0, 1, 0) // Number of even items
};
Func sink("sink");
sink() = {0, 0, int(0x80000000), 0, int(0x7fffffff), 0, 0, 0};
sink() = {sink()[0] * A(r), // Product
sink()[1] + A(r), // Sum
max(sink()[2], A(r)), // Max
select(sink()[2] > A(r), sink()[3], r), // Argmax
min(sink()[4], A(r)), // Min
select(sink()[4] < A(r), sink()[5], r), // Argmin
sink()[6] + A(r)*A(r), // Sum of squares
sink()[7] + select(A(r) % 2 == 0, 1, 0) // Number of even items
};
RVar rxo, rxi, rxio, rxii;
sink.update().split(r.x, rxo, rxi, 8192);
Var u, v;
Func intm = sink.update().rfactor(rxo, u);
intm.compute_root()
.update()
.parallel(u)
.split(rxi, rxio, rxii, 8)
.rfactor(rxii, v)
.compute_at(intm, u)
.vectorize(v)
.update()
.vectorize(v);
const int trials = 10;
const int iterations = 10;
Image<int32_t> vec_A(size);
// init randomly
for (int ix = 0; ix < size; ix++) {
vec_A(ix) = rand();
}
A.set(vec_A);
double t_ref = benchmark(trials, iterations, [&]() {
sink_ref.realize();
});
double t = benchmark(trials, iterations, [&]() {
sink.realize();
});
float gbits = 8 * size * (2 / 1e9); // bits per seconds
printf("Kitchen sink ref: %fms, %f Gbps\n", t_ref * 1e3, (gbits / t_ref));
printf("Kitchen sink with rfactor: %fms, %f Gbps\n", t * 1e3, (gbits / t));
double improve = t_ref / t;
printf("Improvement: %f\n\n", improve);
return 0;
}
int main(int argc, char **argv) {
// These benchmarks require the newest rfactor implementation
one_d_max();
two_d_histogram();
four_d_argmin();
complex_multiply();
dot_product();
kitchen_sink();
printf("Success!\n");
return 0;
}