File: device_crop.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 (146 lines) | stat: -rw-r--r-- 4,340 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
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
#include "Halide.h"
#include "HalideBuffer.h"

using namespace Halide;

Halide::Runtime::Buffer<int32_t> make_gpu_buffer(bool hexagon_rpc) {
    Var x, y;
    Func f;
    f(x, y) = x + y * 256;

    if (hexagon_rpc) {
        f.hexagon();
    } else {
        Var xi, yi;
        f.gpu_tile(x, y, xi, yi, 8, 8);
    }

    Buffer<int32_t> result = f.realize({128, 128});
    return *result.get();
}

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

    bool hexagon_rpc = (target.arch != Target::Hexagon) &&
                       target.has_feature(Target::HVX);

    if (!hexagon_rpc && !target.has_gpu_feature()) {
        printf("[SKIP] No GPU target enabled.\n");
        return 0;
    }

    printf("Test in-place cropping.\n");
    {
        Halide::Runtime::Buffer<int32_t> gpu_buf = make_gpu_buffer(hexagon_rpc);
        assert(gpu_buf.raw_buffer()->device_interface != nullptr);

        gpu_buf.crop({{32, 64}, {32, 64}});
        assert(gpu_buf.raw_buffer()->device_interface != nullptr);

        gpu_buf.copy_to_host();
        for (int i = 0; i < 64; i++) {
            for (int j = 0; j < 64; j++) {
                assert(gpu_buf(32 + i, 32 + j) == (i + 32) + 256 * (j + 32));
            }
        }
    }

    printf("Test nondestructive cropping.\n");
    {
        Halide::Runtime::Buffer<int32_t> gpu_buf = make_gpu_buffer(hexagon_rpc);
        assert(gpu_buf.raw_buffer()->device_interface != nullptr);

        Halide::Runtime::Buffer<int32_t> cropped = gpu_buf.cropped({{32, 64}, {32, 64}});
        assert(cropped.raw_buffer()->device_interface != nullptr);

        cropped.copy_to_host();
        for (int i = 0; i < 64; i++) {
            for (int j = 0; j < 64; j++) {
                assert(cropped(32 + i, 32 + j) == (i + 32) + 256 * (j + 32));
            }
        }
    }

    printf("Test crop of a crop\n");
    {
        Halide::Runtime::Buffer<int32_t> gpu_buf = make_gpu_buffer(hexagon_rpc);
        assert(gpu_buf.raw_buffer()->device_interface != nullptr);

        Halide::Runtime::Buffer<int32_t> cropped = gpu_buf.cropped({{32, 64}, {32, 64}});
        assert(cropped.raw_buffer()->device_interface != nullptr);

        Halide::Runtime::Buffer<int32_t> cropped2 = cropped.cropped({{40, 16}, {40, 16}});
        assert(cropped2.raw_buffer()->device_interface != nullptr);

        cropped.copy_to_host();
        for (int i = 0; i < 64; i++) {
            for (int j = 0; j < 64; j++) {
                assert(cropped(32 + i, 32 + j) == (i + 32) + 256 * (j + 32));
            }
        }

        cropped2.copy_to_host();
        for (int i = 0; i < 16; i++) {
            for (int j = 0; j < 16; j++) {
                assert(cropped2(40 + i, 40 + j) == (i + 40) + 256 * (j + 40));
            }
        }
    }

    printf("Test parent going out of scope before crop.\n");
    {
        Halide::Runtime::Buffer<int32_t> cropped;

        {
            Halide::Runtime::Buffer<int32_t> gpu_buf = make_gpu_buffer(hexagon_rpc);
            assert(gpu_buf.raw_buffer()->device_interface != nullptr);

            cropped = gpu_buf.cropped({{32, 64}, {32, 64}});
            assert(cropped.raw_buffer()->device_interface != nullptr);
        }

        cropped.copy_to_host();
        for (int i = 0; i < 64; i++) {
            for (int j = 0; j < 64; j++) {
                assert(cropped(32 + i, 32 + j) == (i + 32) + 256 * (j + 32));
            }
        }
    }

    printf("Test realizing to/from crop.\n");
    {
        ImageParam in(Int(32), 2);
        Var x, y;
        Func f;
        f(x, y) = in(x, y) + 42;

        Var xi, yi;
        if (hexagon_rpc) {
            f.hexagon();
        } else {
            f.gpu_tile(x, y, xi, yi, 8, 8);
        }

        Halide::Buffer<int32_t> gpu_input = make_gpu_buffer(hexagon_rpc);
        Halide::Buffer<int32_t> gpu_output = make_gpu_buffer(hexagon_rpc);

        gpu_input.crop({{64, 64}, {64, 64}});
        gpu_output.crop({{64, 64}, {64, 64}});

        in.set(gpu_input);

        f.realize(gpu_output, target);

        gpu_output.copy_to_host();
        for (int i = 0; i < 64; i++) {
            for (int j = 0; j < 64; j++) {
                assert(gpu_output(64 + i, 64 + j) == (i + 64) + 256 * (j + 64) + 42);
            }
        }
    }

    printf("Success!\n");

    return 0;
}