forked from halide/Halide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistogram.cpp
More file actions
59 lines (43 loc) · 1.21 KB
/
histogram.cpp
File metadata and controls
59 lines (43 loc) · 1.21 KB
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
#include <stdio.h>
#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;
}
Image<float> in(W, H);
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
in(x, y) = float(rand() & 0x000000ff);
reference_hist[uint8_t(in(x, y))] += 1;
}
}
Func hist("hist");
Var x;
RDom r(in);
hist(x) = 0;
hist(clamp(cast<int>(in(r.x, r.y)), 0, 255)) += 1;
hist.compute_root();
Func g;
g(x) = hist(x+10);
// No parallel reductions
/*
Target target = get_jit_target_from_environment();
if (target.has_gpu_feature()) {
hist.gpu_tile(x, 64);
hist.update().gpu_tile(r.x, r.y, 16, 16);
}
*/
Image<int32_t> histogram = g.realize(10); // buckets 10-20 only
for (int i = 10; i < 20; i++) {
if (histogram(i-10) != reference_hist[i]) {
printf("Error: bucket %d is %d instead of %d\n", i, histogram(i), reference_hist[i]);
return -1;
}
}
printf("Success!\n");
return 0;
}