File: gpu_texture.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 (152 lines) | stat: -rw-r--r-- 4,944 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
147
148
149
150
151
152
#include "Halide.h"
#include "HalideRuntimeOpenCL.h"
#include <stdio.h>

using namespace Halide;
using namespace Halide::Internal;

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

    if (!t.has_feature(halide_target_feature_opencl)) {
        printf("[SKIP] No OpenCL target enabled.\n");
        return 0;
    }

    const auto *interface = get_device_interface_for_device_api(DeviceAPI::OpenCL);
    assert(interface->compute_capability != nullptr);
    int major, minor;
    int err = interface->compute_capability(nullptr, &major, &minor);
    if (err != 0 || (major == 1 && minor < 2)) {
        printf("[SKIP] OpenCL %d.%d is less than required 1.2.\n", major, minor);
        return 0;
    }

    // Check dynamic allocations into Heap and Texture memory
    for (auto memory_type : {MemoryType::GPUTexture, MemoryType::Heap}) {
        {
            // 1D stores/loads
            Buffer<int> input(100);
            input.fill(10);
            ImageParam param(Int(32), 1, "input");
            param.set(input);
            param.store_in(memory_type);  // check float stores

            Func f("f"), g("g");
            Var x("x"), xi("xi");
            Var y("y");

            f(x) = cast<float>(x);
            g(x) = param(x) + cast<int>(f(2 * x));

            g.gpu_tile(x, xi, 16);

            f.compute_root().store_in(memory_type).gpu_blocks(x);  // store f as integer
            g.output_buffer().store_in(memory_type);

            Buffer<int> out = g.realize({100});
            for (int x = 0; x < 100; x++) {
                int correct = 2 * x + 10;
                if (out(x) != correct) {
                    printf("out[1D][%d](%d) = %d instead of %d\n", (int)memory_type, x, out(x), correct);
                    return 1;
                }
            }
        }
        {
            // 2D stores/loads
            Buffer<int> input(10, 10);
            input.fill(10);
            ImageParam param(Int(32), 2);
            param.set(input);
            param.store_in(memory_type);  // check float stores

            Func f("f"), g("g");
            Var x("x"), xi("xi");
            Var y("y");

            f(x, y) = cast<float>(x + y);
            g(x) = param(x, x) + cast<int>(f(2 * x, x));

            g.gpu_tile(x, xi, 16, TailStrategy::GuardWithIf);

            f.compute_root().store_in(memory_type).gpu_blocks(x, y);  // store f as integer
            g.store_in(memory_type);

            Buffer<int> out = g.realize({10});
            for (int x = 0; x < 10; x++) {
                int correct = 3 * x + 10;
                if (out(x) != correct) {
                    printf("out[2D][%d](%d) = %d instead of %d\n", (int)memory_type, x, out(x), correct);
                    return 1;
                }
            }
        }
        {
            // 3D stores/loads
            Buffer<int> input(10, 10, 10);
            input.fill(10);
            ImageParam param(Int(32), 3);
            param.set(input);
            param.store_in(memory_type);  // check float stores

            Func f("f"), g("g");
            Var x("x"), xi("xi");
            Var y("y"), z("z");

            f(x, y, z) = cast<float>(x + y + z);
            g(x) = param(x, x, x) + cast<int>(f(2 * x, x, x));

            g.gpu_tile(x, xi, 16, TailStrategy::GuardWithIf);

            f.compute_root().store_in(memory_type).gpu_blocks(x, y, z);  // store f as integer

            g.store_in(memory_type);

            Buffer<int> out = g.realize({10});
            for (int x = 0; x < 10; x++) {
                int correct = 4 * x + 10;
                if (out(x) != correct) {
                    printf("out[3D][%d](%d) = %d instead of %d\n", (int)memory_type, x, out(x), correct);
                    return 1;
                }
            }
        }
        {
            // 1D offset
            Buffer<int> input(100);
            input.set_min(5);
            input.fill(10);
            ImageParam param(Int(32), 1);
            param.set(input);
            param.store_in(memory_type);  // check float stores

            Func f("f"), g("g");
            Var x("x"), xi("xi");
            Var y("y");

            f(x) = cast<float>(x);
            g(x) = param(x) + cast<int>(f(2 * x));

            g.gpu_tile(x, xi, 16, TailStrategy::GuardWithIf);

            f.compute_root().store_in(memory_type).gpu_blocks(x);  // store f as integer
            g.store_in(memory_type);

            Buffer<int> out(10);
            out.set_min(10);
            g.realize(out);
            out.copy_to_host();
            for (int x = 10; x < 20; x++) {
                int correct = 2 * x + 10;
                if (out(x) != correct) {
                    printf("out[1D-shift][%d](%d) = %d instead of %d\n", (int)memory_type, x, out(x), correct);
                    return 1;
                }
            }
        }
    }

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