File: convolution_multiple_kernels.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 (67 lines) | stat: -rw-r--r-- 2,076 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
#include "Halide.h"
#include <stdio.h>

using namespace Halide;

int main(int argc, char **argv) {

    // int W = 64*3, H = 64*3;
    const int W = 64, H = 16;

    Buffer<uint16_t> in(W, H);
    for (int y = 0; y < H; y++) {
        for (int x = 0; x < W; x++) {
            in(x, y) = rand() & 0xff;
        }
    }

    Var x("x"), y("y");

    Func input("input");
    input(x, y) = in(clamp(x, 0, W - 1), clamp(y, 0, H - 1));
    input.compute_root();

    // The kernels in this test are just simple box blurs.
    Func box1, box2;
    box1(x, y) = cast<uint16_t>(1);
    // Make this other box uint32 so its buffer is a different size.
    box2(x, y) = cast<uint32_t>(2);
    // Compute the kernels outside of blur. If blur is scheduled on the GPU,
    // the buffers for these Funcs should be passed as constant memory
    // if possible.
    box1.compute_root();
    box2.compute_root();

    // Compute the sum of the convolution of the image with both kernels.
    Func blur("blur");
    RDom r(-1, 3, -1, 3);
    blur(x, y) = sum(box1(r.x, r.y) * input(x + r.x, y + r.y)) +
                 sum(cast<uint16_t>(box2(r.x, r.y)) * input(x + r.x, y + r.y));

    Target target = get_jit_target_from_environment();
    if (target.has_gpu_feature()) {
        Var xi("xi"), yi("yi");
        blur.gpu_tile(x, y, xi, yi, 16, 16);
    } else if (target.has_feature(Target::HVX)) {
        blur.hexagon().vectorize(x, 64);
    }

    Buffer<uint16_t> out = blur.realize({W, H}, target);

    for (int y = 2; y < H - 2; y++) {
        for (int x = 2; x < W - 2; x++) {
            uint16_t correct = (in(x - 1, y - 1) + in(x, y - 1) + in(x + 1, y - 1) +
                                in(x - 1, y) + in(x, y) + in(x + 1, y) +
                                in(x - 1, y + 1) + in(x, y + 1) + in(x + 1, y + 1)) *
                               3;

            if (out(x, y) != correct) {
                printf("out(%d, %d) = %d instead of %d\n", x, y, out(x, y), correct);
                return 1;
            }
        }
    }

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