-
Notifications
You must be signed in to change notification settings - Fork 12
/
test_histogram.cpp
64 lines (46 loc) · 1.43 KB
/
test_histogram.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
#include <Halide.h>
using namespace Halide;
int main(int argc, char **argv) {
int W = 128, H = 128;
// Compute a random image and its true histogram
int reference_hist[256];
for (int i = 0; i < 256; i++) {
reference_hist[i] = 0;
}
int k = 0;
Image<float> in(W, H);
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
in(x, y) = float((k++) % 256);
reference_hist[uint8_t(in(x, y))] += 1;
}
}
Func hist("hist");
RDom r(in);
hist(clamp(cast<int>(in(r.x, r.y)), 0, 255))++;
Func hist2("hist");
RDom r2(in);
Expr ex = clamp(cast<int>(in(r.x, r.y)), 0, 255);
hist2(ex, ex % 4)++;
if (use_gpu()) {
/* no support for atomic operations make this fail */
hist.cudaTile(hist.arg(0), 64);
hist.update().cudaTile(r.x, r.y, 16, 16);
} else {
// Grab a handle to the update step of a reduction for scheduling
// using the "update()" method.
Var xi, yi;
hist.update().tile(r.x, r.y, xi, yi, 32, 32);
}
Image<int32_t> h = hist.realize(256);
Image<int32_t> hh = hist2.realize(256, 4);
for (int i = 0; i < 256; i++) {
printf("%d\n", hh(i,0));
if (h(i) != reference_hist[i]) {
printf("Error: bucket %d is %d instead of %d\n", i, h(i), reference_hist[i]);
return -1;
}
}
printf("Success!\n");
return 0;
}