File: gpu_data_flows.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 (94 lines) | stat: -rw-r--r-- 2,430 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
#include "Halide.h"
#include <stdio.h>

using namespace Halide;

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

    Target target = get_jit_target_from_environment();

    // We want to test all possible data flows for a buffer:

    // input -> host
    // input -> dev
    // host -> host
    // host -> dev
    // dev -> host
    // dev -> dev
    // dev -> output
    // host -> output

    // We can't really test the last two in the same routine, so we'll
    // run two routines.

    {
        // Pipeline 1 will do input -> host -> dev -> host -> output
        ImageParam in(Int(32), 1);

        Func f, g, out;
        Var x, xi;
        f(x) = in(x) + 1;
        g(x) = f(x) * 2;
        out(x) = g(x) + 3;

        f.compute_root();
        if (target.has_gpu_feature()) {
            g.compute_root().gpu_tile(x, xi, 16);
        } else if (target.has_feature(Target::HVX)) {
            g.compute_root().hexagon();
        }
        out.compute_root();

        Buffer<int> input(1024);
        lambda(x, x * 17 + 83).realize(input);
        in.set(input);

        Buffer<int> output1(1024);
        out.realize(output1);
        output1.copy_to_host();

        for (int x = 0; x < 1024; x++) {
            int correct = (input(x) + 1) * 2 + 3;
            if (output1(x) != correct) {
                printf("output1(%d) = %d instead of %d\n", x, output1(x), correct);
                return 1;
            }
        }
    }

    {
        // Pipeline 2 will do input -> dev -> dev -> output
        ImageParam in(Int(32), 1);
        Func f, out;
        Var x, xi;
        f(x) = in(x) + 1;
        out(x) = f(x) * 2;

        if (target.has_gpu_feature()) {
            f.compute_root().gpu_tile(x, xi, 16);
            out.compute_root().gpu_tile(x, xi, 16);
        } else if (target.has_feature(Target::HVX)) {
            f.compute_root().hexagon();
            out.compute_root().hexagon();
        }

        Buffer<int> input(1024);
        lambda(x, x * 17 + 83).realize(input);
        in.set(input);

        Buffer<int> output2(1024);
        out.realize(output2);
        output2.copy_to_host();

        for (int x = 0; x < 1024; x++) {
            int correct = (input(x) + 1) * 2;
            if (output2(x) != correct) {
                printf("output2(%d) = %d instead of %d\n", x, output2(x), correct);
                return 1;
            }
        }
    }

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