File: partition_loops.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 (63 lines) | stat: -rw-r--r-- 2,138 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
#include "Halide.h"
#include <stdio.h>
using namespace Halide;

int main(int argc, char *argv[]) {
    {
        Buffer<uint8_t> input(1024, 1024, 3);

        for (int c = 0; c < input.channels(); c++) {
            for (int y = 0; y < input.height(); y++) {
                for (int x = 0; x < input.width(); x++) {
                    input(x, y, c) = x + y + c;
                }
            }
        }

        Var x("x"), y("y"), c("c");

        Func clamped_input = Halide::BoundaryConditions::repeat_edge(input);

        // One of the possible conditions for partitioning loop 'f.s0.x' is
        // ((f.s0.x + g[0]) <= 1023) which depends on 'g'. Since 'g' is
        // only allocated inside f.s0.x, partition loops should not use this
        // condition to compute the epilogue/prologue.
        Func f("f"), g("g"), h("h");
        g(x, y, c) = x + y + c;
        g(x, y, 0) = x;
        h(x, y) = clamped_input(x + g(x, y, 0), y, 2);
        f(x, y, c) = select(h(x, y) < x + y, x + y, y + c);

        f.compute_root();

        Func output("output");
        output(x, y, c) = cast<float>(f(x, y, c));
        Buffer<float> im = output.realize({1024, 1024, 3});

        for (int y = 0; y < input.height(); y++) {
            for (int x = 0; x < input.width(); x++) {
                for (int c = 0; c < input.channels(); c++) {
                    float correct = (input(std::min(2 * x, input.width() - 1), y, 2) < x + y) ? x + y : y + c;
                    if (im(x, y, c) != correct) {
                        printf("im(%d, %d, %d) = %f instead of %f\n",
                               x, y, c, im(x, y, c), correct);
                        return 1;
                    }
                }
            }
        }
    }

    // A loop partitioning bug from https://github.com/halide/Halide/issues/7742
    {
        Var x, y, x_outer, x_inner, y_x_inner_fused;
        Func f;
        f(x, y) = x + y;
        f.split(x, x_outer, x_inner, 2, TailStrategy::PredicateStores).fuse(y, x_inner, y_x_inner_fused);
        Pipeline p(f);
        p.realize({100, 100});
    }

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