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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
#include <stdio.h>
#ifdef _WIN32
int main(int argc, char **argv) {
printf("[SKIP] Test requires weak linkage, which is not available on Windows.\n");
return 0;
}
#else
#include "HalideBuffer.h"
#include "HalideRuntime.h"
#include <assert.h>
#include <math.h>
#include <string.h>
#include "acquire_release.h"
#include "gpu_context.h"
using namespace Halide::Runtime;
const int W = 256, H = 256;
#if defined(TEST_OPENCL)
// Just use a global context and queue, created and destroyed by main.
cl_context cl_ctx = nullptr;
cl_command_queue cl_q = nullptr;
// Create the global context. This is just a helper function not called by Halide.
bool init_context() {
return create_opencl_context(cl_ctx, cl_q);
}
void destroy_context() {
destroy_opencl_context(cl_ctx, cl_q);
cl_q = nullptr;
cl_ctx = nullptr;
}
// These functions replace the acquire/release implementation in src/runtime/opencl.cpp.
// Since we don't parallelize access to the GPU in the schedule, we don't need synchronization
// in our implementation of these functions.
extern "C" int halide_acquire_cl_context(void *user_context, cl_context *ctx, cl_command_queue *q) {
printf("Acquired CL context %p\n", cl_ctx);
*ctx = cl_ctx;
*q = cl_q;
return 0;
}
extern "C" int halide_release_cl_context(void *user_context) {
printf("Releasing CL context %p\n", cl_ctx);
return 0;
}
#elif defined(TEST_CUDA)
CUcontext cuda_ctx = nullptr;
bool init_context() {
return create_cuda_context(cuda_ctx);
}
void destroy_context() {
destroy_cuda_context(cuda_ctx);
cuda_ctx = nullptr;
}
// These functions replace the acquire/release implementation in src/runtime/cuda.cpp.
// Since we don't parallelize access to the GPU in the schedule, we don't need synchronization
// in our implementation of these functions.
extern "C" int halide_cuda_acquire_context(void *user_context, CUcontext *ctx, bool create = true) {
printf("Acquired CUDA context %p\n", cuda_ctx);
*ctx = cuda_ctx;
return 0;
}
extern "C" int halide_cuda_release_context(void *user_context) {
printf("Releasing CUDA context %p\n", cuda_ctx);
return 0;
}
#elif defined(TEST_METAL) && defined(__OBJC__)
struct gpu_context {
id<MTLDevice> device;
id<MTLCommandQueue> queue;
} metal_context;
bool init_context() {
return create_metal_context(metal_context.device, metal_context.queue);
}
void destroy_context() {
destroy_metal_context(metal_context.device, metal_context.queue);
metal_context.device = nullptr;
metal_context.queue = nullptr;
}
int halide_metal_acquire_context(void *user_context, id<MTLDevice> *device_ret,
id<MTLCommandQueue> *queue_ret, bool create) {
*device_ret = metal_context.device;
*queue_ret = metal_context.queue;
return 0;
}
int halide_metal_release_context(void *user_context) {
return 0;
}
#elif defined(TEST_WEBGPU)
struct gpu_context {
WGPUInstance instance = nullptr;
WGPUAdapter adapter = nullptr;
WGPUDevice device = nullptr;
WGPUBuffer staging_buffer = nullptr;
} webgpu_context;
bool init_context() {
return create_webgpu_context(&webgpu_context.instance, &webgpu_context.adapter, &webgpu_context.device, &webgpu_context.staging_buffer);
}
void destroy_context() {
destroy_webgpu_context(webgpu_context.instance, webgpu_context.adapter, webgpu_context.device, webgpu_context.staging_buffer);
webgpu_context.instance = nullptr;
webgpu_context.adapter = nullptr;
webgpu_context.device = nullptr;
webgpu_context.staging_buffer = nullptr;
}
extern "C" int halide_webgpu_acquire_context(void *user_context,
WGPUInstance *instance_ret,
WGPUAdapter *adapter_ret,
WGPUDevice *device_ret,
WGPUBuffer *staging_buffer_ret,
bool create) {
*instance_ret = webgpu_context.instance;
*adapter_ret = webgpu_context.adapter;
*device_ret = webgpu_context.device;
*staging_buffer_ret = webgpu_context.staging_buffer;
return 0;
}
extern "C" int halide_webgpu_release_context(void *user_context) {
return 0;
}
#define HAS_MULTIPLE_CONTEXTS true
#else
// Just use the default implementation of acquire/release.
bool init_context() {
printf("Using default implementation of acquire/release\n");
return true;
}
void destroy_context() {
}
#endif
bool run_test() {
// Initialize the runtime specific GPU context.
if (!init_context()) {
return false;
}
// Everything else is a normal Halide program. The GPU runtime will call
// the above acquire/release functions to get the context instead of using
// its own internal context.
Buffer<float, 2> input(W, H);
for (int y = 0; y < input.height(); y++) {
for (int x = 0; x < input.width(); x++) {
input(x, y) = (float)(x * y);
}
}
input.set_host_dirty(true);
Buffer<float, 2> output(W, H);
acquire_release(input, output);
output.copy_to_host();
for (int y = 0; y < output.height(); y++) {
for (int x = 0; x < output.width(); x++) {
if (input(x, y) * 2.0f + 1.0f != output(x, y)) {
printf("Error at (%d, %d): %f != %f\n", x, y, input(x, y) * 2.0f + 1.0f,
output(x, y));
return false;
}
}
}
const halide_device_interface_t *interface = output.raw_buffer()->device_interface;
// We need to free our GPU buffers before destroying the context.
input.device_free();
output.device_free();
if (interface != nullptr) {
halide_device_release(nullptr, interface);
// Free the context we created.
destroy_context();
} else {
printf("Device interface is nullptr.\n");
}
printf("Success!\n");
return true;
}
int main(int argc, char **argv) {
#if defined(TEST_VULKAN)
printf("[SKIP] Vulkan doesn't implement a custom context for this test.\n");
#else
if (!run_test()) {
return 1;
}
if (!run_test()) {
return 1;
}
#endif
return 0;
}
#endif
|