File: histogram.cpp

package info (click to toggle)
halide 21.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,752 kB
  • sloc: cpp: 289,334; ansic: 22,751; python: 7,486; makefile: 4,299; sh: 2,508; java: 1,549; javascript: 282; pascal: 207; xml: 127; asm: 9
file content (92 lines) | stat: -rw-r--r-- 2,445 bytes parent folder | download | duplicates (3)
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
#include "Halide.h"
#include <stdio.h>

using namespace Halide;

template<typename ITYPE, typename HTYPE>
bool test() {
    int W = 128, H = 128;

    // Compute a random image and its true histogram
    HTYPE reference_hist[256];
    for (int i = 0; i < 256; i++) {
        reference_hist[i] = HTYPE(0);
    }

    Buffer<ITYPE> in(W, H);
    for (int y = 0; y < H; y++) {
        for (int x = 0; x < W; x++) {
            in(x, y) = ITYPE(rand() & 0x000000ff);
            reference_hist[uint8_t(in(x, y))] += HTYPE(1);
        }
    }

    Func hist("hist"), g("g");
    Var x;

    RDom r(in);
    hist(x) = cast<HTYPE>(0);
    hist(clamp(cast<int>(in(r.x, r.y)), 0, 255)) += cast<HTYPE>(1);
    g(x) = hist(x + 10);

    // No parallel reductions
    /*
    Target target = get_jit_target_from_environment();
    if (target.has_gpu_feature()) {
        Var xi;
        hist.gpu_tile(x, xi, 64);
        RVar rxi, ryi;
        hist.update().gpu_tile(r.x, r.y, rxi, ryi, 16, 16);
    }
    */
    Target target = get_jit_target_from_environment();
    if (target.has_feature(Target::HVX)) {
        const int vector_size = target.has_feature(Target::HVX) ? 128 : 64;
        g
            .hexagon()
            .vectorize(x, vector_size);

        hist
            .compute_at(g, Var::outermost())
            .vectorize(x, vector_size);

        if (target.features_any_of({Target::HVX_v65, Target::HVX_v66,
                                    Target::HVX_v68})) {
            hist.store_in(MemoryType::VTCM);

            hist
                .update(0)
                .allow_race_conditions()
                .vectorize(r.x, vector_size);
        }
    } else {
        hist.compute_root();
    }

    Buffer<HTYPE> histogram = g.realize({128});  // buckets 10-137

    for (int i = 10; i < 138; i++) {
        if (histogram(i - 10) != reference_hist[i]) {
            printf("Error: bucket %d is %d instead of %d\n", i, histogram(i - 10), reference_hist[i]);
            return false;
        }
    }

    return true;
}

int main(int argc, char **argv) {
    Target target = get_jit_target_from_environment();

    if (target.has_feature(Target::HVX)) {
        if (!test<uint8_t, int16_t>() ||
            !test<uint16_t, uint16_t>() ||
            !test<uint8_t, int32_t>() ||
            !test<uint32_t, uint32_t>()) return 1;
    } else {
        if (!test<float, int>()) return 1;
    }

    printf("Success!\n");
    return 0;
}