File: func_lifetime_2.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 (78 lines) | stat: -rw-r--r-- 1,884 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
#include "Halide.h"
#include <iostream>
#include <stdio.h>

using namespace Halide;

bool validate(const Buffer<int> &im, int add) {
    // Check the result was what we expected
    for (int i = 0; i < im.width(); i++) {
        for (int j = 0; j < im.height(); j++) {
            int correct = i * j + add;
            if (im(i, j) != correct) {
                printf("im[%d, %d] = %d instead of %d\n", i, j, im(i, j), correct);
                return false;
            }
        }
    }
    return true;
}

int main(int argc, char **argv) {

    Var x("x"), y("y"), xi("xi"), yi("yi");

    Func g("g");

    Target target = get_jit_target_from_environment();

    {
        printf("Defining function f...\n");
        Func f("f");

        f(x, y) = x * y + 1;

        if (target.has_gpu_feature()) {
            f.gpu_tile(x, y, xi, yi, 8, 8);
        } else if (target.has_feature(Target::HVX)) {
            f.hexagon().vectorize(x, 32);
        }

        {
            printf("Realizing function f...\n");

            Buffer<int> imf = f.realize({32, 32}, target);
            if (!validate(imf, 1)) {
                return 1;
            }
        }

        printf("Defining function g...\n");

        g(x, y) = x * y + 2;

        if (target.has_gpu_feature()) {
            g.gpu_tile(x, y, xi, yi, 8, 8);
        } else if (target.has_feature(Target::HVX)) {
            g.hexagon().vectorize(x, 32);
        }

        printf("Realizing function g...\n");

        Buffer<int> img1 = g.realize({32, 32}, target);
        if (!validate(img1, 2)) {
            return 1;
        }
    }

    // Try using g again to ensure it is still valid (after f's destruction).
    printf("Realizing function g again...\n");

    Buffer<int> img2 = g.realize({32, 32}, target);
    if (!validate(img2, 2.0f)) {
        return 1;
    }

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