File: tiled_blur_aottest.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 (99 lines) | stat: -rw-r--r-- 2,991 bytes parent folder | download | duplicates (4)
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
#include "HalideBuffer.h"
#include "HalideRuntime.h"
#include <assert.h>
#include <math.h>
#include <stdio.h>

#include "tiled_blur.h"

// defined away to avoid requiring libpng, libjpeg everywhere;
// left in because useful for debugging and profiling.
#define SAVE_IMAGES 0

#if SAVE_IMAGES
#include "halide_image_io.h"
#endif

#define RUN_BENCHMARKS 0
#if RUN_BENCHMARKS
#include "halide_benchmark.h"
#endif

using namespace Halide::Runtime;

const int W = 80, H = 80;

int my_halide_trace(void *user_context, const halide_trace_event_t *ev) {
    if (ev->event == halide_trace_begin_realization) {
        assert(ev->dimensions == 6);
        int min_x = ev->coordinates[0], width = ev->coordinates[1];
        int min_y = ev->coordinates[2], height = ev->coordinates[3];
        int max_x = min_x + width - 1;
        int max_y = min_y + height - 1;
#if !RUN_BENCHMARKS
        printf("Using %d x %d input tile over [%d - %d] x [%d - %d]\n", width, height, min_x, max_x,
               min_y, max_y);
#endif
        assert(min_x >= 0 && min_y >= 0 && max_x < W && max_y < H);

        // The input is large enough that the boundary condition could
        // only ever apply on one side.
        assert(width == 33 || width == 34);
        assert(height == 33 || height == 34);
    }
    return 0;
}

Buffer<uint8_t, 3> buffer_factory_planar(int w, int h, int c) {
    return Buffer<uint8_t, 3>(w, h, c);
}

Buffer<uint8_t, 3> buffer_factory_interleaved(int w, int h, int c) {
    return Buffer<uint8_t, 3>::make_interleaved(w, h, c);
}

void test(Buffer<uint8_t, 3> (*factory)(int w, int h, int c)) {
    Buffer<uint8_t, 3> input = factory(W, H, 3);
    input.for_each_element([&](int x, int y, int c) {
        // Just an arbitrary color pattern with enough variation to notice the brighten + blur
        if (c == 0) {
            input(x, y, c) = (uint8_t)((x % 7) + (y % 3));
        } else if (c == 1) {
            input(x, y, c) = (uint8_t)(x + y);
        } else {
            input(x, y, c) = (uint8_t)((x * 5) + (y * 2));
        }
    });
    Buffer<uint8_t, 3> output = factory(W, H, 3);

    printf("Evaluating output over %d x %d in tiles of size 32 x 32\n", W, H);
    tiled_blur(input, output);

#if RUN_BENCHMARKS
    double t = Halide::Tools::benchmark(10, 100, [&]() {
        tiled_blur(input, output);
    });
    const float megapixels = (W * H) / (1024.f * 1024.f);
    printf("Benchmark: %d %d -> %f mpix/s\n", W, H, megapixels / t);
#endif

#if SAVE_IMAGES
    static int x = 0;
    Halide::Tools::save_image(input, "/tmp/tiled_input" + std::to_string(x) + ".png");
    Halide::Tools::save_image(output, "/tmp/tiled_output" + std::to_string(x) + ".png");
    ++x;
#endif
}

int main(int argc, char **argv) {
    halide_set_custom_trace(&my_halide_trace);

    printf("Testing planar buffer...\n");
    test(buffer_factory_planar);

    printf("Testing interleaved buffer...\n");
    test(buffer_factory_interleaved);

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