File: extern_reorder_storage.cpp

package info (click to toggle)
halide 14.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 49,124 kB
  • sloc: cpp: 238,722; makefile: 4,303; python: 4,047; java: 1,575; sh: 1,384; pascal: 211; xml: 165; javascript: 43; ansic: 34
file content (60 lines) | stat: -rw-r--r-- 1,553 bytes parent folder | download
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
#include "Halide.h"
#include <stdio.h>

#ifdef _WIN32
#define DLLEXPORT __declspec(dllexport)
#else
#define DLLEXPORT
#endif

// An extern stage that translates.
extern "C" DLLEXPORT int copy_and_check_strides(halide_buffer_t *in, halide_buffer_t *out) {
    if (in->is_bounds_query()) {
        for (int i = 0; i < 2; i++) {
            in->dim[i].min = out->dim[i].min;
            in->dim[i].extent = out->dim[i].extent;
        }
    } else if (!out->is_bounds_query()) {
        // Check that the storage has been reordered.
        assert(out->dim[0].stride > out->dim[1].stride);
        Halide::Runtime::Buffer<uint8_t> out_buf(*out);
        out_buf.copy_from(Halide::Runtime::Buffer<uint8_t>(*in));
    }

    return 0;
}

using namespace Halide;

int main(int argc, char **argv) {
    Var x, y;

    const int W = 30, H = 20;
    Buffer<uint8_t> input_buffer(W, H);
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            input_buffer(j, i) = i + j;
        }
    }

    // Define a pipeline that uses an input image in an extern stage
    // only and do bounds queries.
    ImageParam input(UInt(8), 2);
    Func f, g;

    f.define_extern("copy_and_check_strides", {input}, UInt(8), {x, y});
    g(x, y) = f(x, y);

    f.compute_root().reorder_storage(y, x);

    input.set(input_buffer);
    Buffer<uint8_t> output = g.realize({W, H});
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            assert(output(j, i) == i + j);
        }
    }

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