File: gpu_only_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 (107 lines) | stat: -rw-r--r-- 2,892 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
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "HalideBuffer.h"
#include "HalideRuntime.h"
#include <assert.h>
#include <math.h>
#include <stdio.h>
#if defined(TEST_OPENCL)
#include "HalideRuntimeOpenCL.h"
#elif defined(TEST_CUDA)
#include "HalideRuntimeCuda.h"
#elif defined(TEST_METAL)
#include "HalideRuntimeMetal.h"
#elif defined(TEST_WEBGPU)
#include "HalideRuntimeWebGPU.h"
#endif

#include "gpu_only.h"
using namespace Halide::Runtime;

#if defined(TEST_OPENCL)

#if !defined(HALIDE_RUNTIME_OPENCL)
#error "TEST_OPENCL defined but HALIDE_RUNTIME_OPENCL not defined"
#endif

#elif defined(TEST_CUDA)

#if !defined(HALIDE_RUNTIME_CUDA)
#error "TEST_CUDA defined but HALIDE_RUNTIME_CUDA not defined"
#endif

#elif defined(TEST_WEBGPU)

#if !defined(HALIDE_RUNTIME_WEBGPU)
#error "TEST_WEBGPU defined but HALIDE_RUNTIME_WEBGPU not defined"
#endif

#else

#if defined(HALIDE_RUNTIME_OPENCL)
#error "TEST_OPENCL not defined but HALIDE_RUNTIME_OPENCL defined"
#endif
#if defined(HALIDE_RUNTIME_CUDA)
#error "TEST_CUDA not defined but HALIDE_RUNTIME_CUDA defined"
#endif
#if defined(HALIDE_RUNTIME_WEBGPU)
#error "TEST_WEBGPU not defined but HALIDE_RUNTIME_WEBGPU defined"
#endif

#endif

int main(int argc, char **argv) {
#if defined(TEST_OPENCL) || defined(TEST_CUDA) || defined(TEST_METAL) || defined(TEST_WEBGPU)
    const int W = 32, H = 32;
    Buffer<int, 2> input(W, H);
    for (int y = 0; y < input.height(); y++) {
        for (int x = 0; x < input.width(); x++) {
            input(x, y) = x + y;
        }
    }

    // Explicitly copy data to the GPU.
    const halide_device_interface_t *interface = nullptr;
#if defined(TEST_OPENCL)
    interface = halide_opencl_device_interface();
#elif defined(TEST_CUDA)
    interface = halide_cuda_device_interface();
#elif defined(TEST_METAL)
    interface = halide_metal_device_interface();
#elif defined(TEST_WEBGPU)
    interface = halide_webgpu_device_interface();
#endif

    Buffer<int, 2> output(W, H);

    input.set_host_dirty();
    input.copy_to_device(interface);
    output.device_malloc(interface);

    // Create halide_buffer_ts without host pointers.
    halide_buffer_t input_no_host = *((halide_buffer_t *)input);
    input_no_host.host = nullptr;

    halide_buffer_t output_no_host = *((halide_buffer_t *)output);
    output_no_host.host = (uint8_t *)nullptr;

    gpu_only(&input_no_host, &output_no_host);

    // Restore the host pointer and copy to host.
    output_no_host.host = (uint8_t *)output.data();
    halide_copy_to_host(nullptr, &output_no_host);

    // Verify output.
    for (int y = 0; y < H; y++) {
        for (int x = 0; x < W; x++) {
            if (input(x, y) * 2 != output(x, y)) {
                printf("Error at %d, %d: %d != %d\n", x, y, input(x, y), output(x, y));
                return 1;
            }
        }
    }

    printf("Success!\n");
#else
    printf("[SKIP] No GPU target enabled.\n");
#endif
    return 0;
}