File: callable_generator.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 (247 lines) | stat: -rw-r--r-- 7,032 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "Halide.h"
#include <iostream>
#include <stdio.h>

using namespace Halide;

namespace {

void check(int r) {
    assert(r == 0);
}

bool custom_malloc_called = false;
bool custom_free_called = false;

void *my_malloc(JITUserContext *user_context, size_t x) {
    custom_malloc_called = true;
    void *orig = malloc(x + 32);
    void *ptr = (void *)((((size_t)orig + 32) >> 5) << 5);
    ((void **)ptr)[-1] = orig;
    return ptr;
}

void my_free(JITUserContext *user_context, void *ptr) {
    custom_free_called = true;
    free(((void **)ptr)[-1]);
}

int call_counter = 0;
extern "C" HALIDE_EXPORT_SYMBOL float my_extern_func(int x, float y) {
    call_counter++;
    return x * y;
}
HalideExtern_2(float, my_extern_func, int, float);

}  // namespace

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

    {
        class TestGen1 : public Generator<TestGen1> {
        public:
            Input<Buffer<uint8_t, 2>> img_{"img"};
            Input<int32_t> int_{"int"};
            Input<float> float_{"float"};

            Output<Buffer<uint8_t, 2>> out_{"out"};

            void generate() {
                Var x("x"), y("y");
                out_(x, y) = img_(x, y) + cast<uint8_t>(int_ / float_);
            }
        };

        Buffer<uint8_t> in1(10, 10);
        Buffer<uint8_t> in2(10, 10);

        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                in1(i, j) = i + j * 10;
                in2(i, j) = i * 10 + j;
            }
        }

        auto gen = TestGen1::create(context);
        Callable c = gen->compile_to_callable();

        Buffer<uint8_t> out1(10, 10);
        check(c(in1, 42, 1.0f, out1));

        Buffer<uint8_t> out2(10, 10);
        check(c(in2, 22, 2.0f, out2));

        Buffer<uint8_t> out3(10, 10);
        check(c(in1, 12, 1.0f, out3));

        Buffer<uint8_t> out4(10, 10);
        check(c(in2, 16, 1.0f, out4));

        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                assert(out1(i, j) == i + j * 10 + 42);
                assert(out2(i, j) == i * 10 + j + 11);
                assert(out3(i, j) == i + j * 10 + 12);
                assert(out4(i, j) == i * 10 + j + 16);
            }
        }

        // Test bounds inference
        Buffer<uint8_t> in_bounds(nullptr, 1, 1);
        Buffer<uint8_t> out_bounds(nullptr, 20, 20);

        check(c(in_bounds, 42, 1.0f, out_bounds));

        assert(in_bounds.defined());
        assert(in_bounds.dim(0).extent() == 20);
        assert(in_bounds.dim(1).extent() == 20);
        assert(in1.dim(0).extent() == 10);
        assert(in1.dim(1).extent() == 10);
    }

    // Override Halide's malloc and free (except under wasm),
    // make sure that Callable freezes the values
    if (t.arch != Target::WebAssembly) {
        custom_malloc_called = false;
        custom_free_called = false;

        class TestGen2 : public Generator<TestGen2> {
        public:
            Output<Buffer<int, 1>> out_{"out"};

            void generate() {
                Var x("x");

                Func f;
                f(x) = x;

                out_(x) = f(x);

                f.compute_root();
            }
        };

        JITHandlers my_jit_handlers;
        my_jit_handlers.custom_malloc = my_malloc;
        my_jit_handlers.custom_free = my_free;

        auto gen = TestGen2::create(context);
        Callable c = gen->compile_to_callable(&my_jit_handlers);

        Buffer<int> im(100000);
        check(c(im));

        assert(custom_malloc_called);
        assert(custom_free_called);
    }

    // Check that Param<void*> works with Callables
    if (t.arch != Target::WebAssembly) {
        class TestGen3 : public Generator<TestGen3> {
        public:
            GeneratorParam<bool> vectorize_{"vectorize", false};

            Input<void *> handle_{"handle"};
            Output<Buffer<uint64_t, 1>> out_{"out"};

            void generate() {
                Var x("x");

                out_(x) = reinterpret<uint64_t>(handle_);
                if (vectorize_) {
                    out_.vectorize(x, 4);
                }
            }
        };

        auto gen_1 = TestGen3::create(context);
        gen_1->vectorize_.set(false);

        auto gen_2 = TestGen3::create(context);
        gen_2->vectorize_.set(true);

        Callable c1 = gen_1->compile_to_callable();
        Callable c2 = gen_2->compile_to_callable();

        int foo = 0;

        Buffer<uint64_t> out1(4);
        // Create a dummy JITUserContext here just to test that
        // passing one explicitly works correctly.
        JITUserContext empty;
        check(c1(&empty, &foo, out1));

        Buffer<uint64_t> out2(4);
        check(c2(&foo, out2));

        uint64_t correct = (uint64_t)((uintptr_t)(&foo));

        for (int x = 0; x < out1.width(); x++) {
            if (out1(x) != correct) {
                printf("out1(%d) = %llu instead of %llu\n",
                       x,
                       (long long unsigned)out1(x),
                       (long long unsigned)correct);
                exit(1);
            }
            if (out2(x) != correct) {
                printf("out2(%d) = %llu instead of %llu\n",
                       x,
                       (long long unsigned)out2(x),
                       (long long unsigned)correct);
                exit(1);
            }
        }
    }

    // Check that JITExterns works with Callables
    if (t.arch != Target::WebAssembly) {
        call_counter = 0;

        class TestGen4 : public Generator<TestGen4> {
        public:
            Output<Buffer<float, 2>> out_{"out"};

            void generate() {
                Func f;
                f.define_extern("extern_func", {user_context_value()}, Float(32), 2);

                Var x("x"), y("y");
                out_(x, y) = f(x, y);
            }
        };

        Var x, y;
        Func monitor;
        monitor(x, y) = my_extern_func(x, cast<float>(y));
        const std::map<std::string, JITExtern> my_jit_externs = {
            {"extern_func", JITExtern{monitor}}};

        auto gen = TestGen4::create(context);
        Callable c = gen->compile_to_callable(nullptr, &my_jit_externs);

        Buffer<float> imf(32, 32);
        check(c(imf));

        // Check the result was what we expected
        for (int i = 0; i < 32; i++) {
            for (int j = 0; j < 32; j++) {
                float correct = (float)(i * j);
                float delta = imf(i, j) - correct;
                if (delta < -0.001 || delta > 0.001) {
                    printf("imf[%d, %d] = %f instead of %f\n", i, j, imf(i, j), correct);
                    exit(1);
                }
            }
        }

        if (call_counter != 32 * 32) {
            printf("In pipeline_set_jit_externs_func, my_func was called %d times instead of %d\n", call_counter, 32 * 32);
            exit(1);
        }
    }

    printf("Success!\n");
}