Halide  19.0.0
Halide compiler and libraries
gpu_context.h
Go to the documentation of this file.
1 #if defined(TEST_OPENCL)
2 // Implement OpenCL custom context.
3 
4 #define CL_TARGET_OPENCL_VERSION 120
5 #define CL_USE_DEPRECATED_OPENCL_1_2_APIS
6 #ifdef __APPLE__
7 #include <OpenCL/cl.h>
8 #else
9 #include <CL/cl.h>
10 #endif
11 
12 // Create the global context. This is just a helper function not called by Halide.
13 inline bool create_opencl_context(cl_context &cl_ctx, cl_command_queue &cl_q) {
14  cl_int err = 0;
15 
16  const cl_uint maxPlatforms = 4;
17  cl_platform_id platforms[maxPlatforms];
18  cl_uint platformCount = 0;
19 
20  err = clGetPlatformIDs(maxPlatforms, platforms, &platformCount);
21  if (err != CL_SUCCESS) {
22  printf("clGetPlatformIDs failed (%d)\n", err);
23  return false;
24  }
25 
26  cl_platform_id platform = nullptr;
27 
28  if (platformCount > 0) {
29  platform = platforms[0];
30  }
31  if (platform == nullptr) {
32  printf("Failed to get platform\n");
33  return false;
34  }
35 
37 
38  // Make sure we have a device
39  const cl_uint maxDevices = 4;
40  cl_device_id devices[maxDevices];
41  cl_uint deviceCount = 0;
42  err = clGetDeviceIDs(platform, device_type, maxDevices, devices, &deviceCount);
43  if (err != CL_SUCCESS) {
44  printf("clGetDeviceIDs failed (%d)\n", err);
45  return false;
46  }
47  if (deviceCount == 0) {
48  printf("Failed to get device\n");
49  return false;
50  }
51 
52  cl_device_id dev = devices[deviceCount - 1];
53 
54  // Create context and command queue.
56  0};
57  cl_ctx = clCreateContext(properties, 1, &dev, nullptr, nullptr, &err);
58  if (err != CL_SUCCESS) {
59  printf("clCreateContext failed (%d)\n", err);
60  return false;
61  }
62 
63  cl_q = clCreateCommandQueue(cl_ctx, dev, 0, &err);
64  if (err != CL_SUCCESS) {
65  printf("clCreateCommandQueue failed (%d)\n", err);
66  return false;
67  }
68  return true;
69 }
70 
71 inline void destroy_opencl_context(cl_context cl_ctx, cl_command_queue cl_q) {
72  clReleaseCommandQueue(cl_q);
73  clReleaseContext(cl_ctx);
74 }
75 
76 #elif defined(TEST_CUDA)
77 // Implement CUDA custom context.
78 #include <cuda.h>
79 
80 inline bool create_cuda_context(CUcontext &cuda_ctx) {
81  // Initialize CUDA
82  CUresult err = cuInit(0);
83  if (err != CUDA_SUCCESS) {
84  printf("cuInit failed (%d)\n", err);
85  return false;
86  }
87 
88  // Make sure we have a device
89  int deviceCount = 0;
90  err = cuDeviceGetCount(&deviceCount);
91  if (err != CUDA_SUCCESS) {
92  printf("cuGetDeviceCount failed (%d)\n", err);
93  return false;
94  }
95  if (deviceCount <= 0) {
96  printf("No CUDA devices available\n");
97  return false;
98  }
99 
100  CUdevice dev;
101  // Get device
102  CUresult status;
103  // Try to get a device >0 first, since 0 should be our display device
104  // For now, don't try devices > 2 to maintain compatibility with previous behavior.
105  if (deviceCount > 2) deviceCount = 2;
106  for (int id = deviceCount - 1; id >= 0; id--) {
107  status = cuDeviceGet(&dev, id);
108  if (status == CUDA_SUCCESS) break;
109  }
110 
111  if (status != CUDA_SUCCESS) {
112  printf("Failed to get CUDA device\n");
113  return status;
114  }
115 
116  // Create context
117  err = cuCtxCreate(&cuda_ctx, 0, dev);
118  if (err != CUDA_SUCCESS) {
119  printf("cuCtxCreate failed (%d)\n", err);
120  return false;
121  }
122 
123  return true;
124 }
125 
126 inline void destroy_cuda_context(CUcontext cuda_ctx) {
127  cuCtxDestroy(cuda_ctx);
128 }
129 
130 #elif defined(TEST_METAL) && defined(__OBJC__)
131 #include <Metal/MTLCommandQueue.h>
132 #include <Metal/MTLDevice.h>
133 
134 inline bool create_metal_context(id<MTLDevice> &device, id<MTLCommandQueue> &queue) {
135  device = MTLCreateSystemDefaultDevice();
136  if (device == nullptr) {
137  NSArray<id<MTLDevice>> *devices = MTLCopyAllDevices();
138  if (devices != nullptr) {
139  device = devices[0];
140  }
141  }
142  if (device == nullptr) {
143  printf("Failed to find Metal device.\n");
144  return false;
145  }
146  queue = [device newCommandQueue];
147  if (queue == nullptr) {
148  printf("Failed to create Metal command queue.\n");
149  return false;
150  }
151  return true;
152 }
153 
154 inline void destroy_metal_context(id<MTLDevice> device, id<MTLCommandQueue> queue) {
155  [queue release];
156  [device release];
157 }
158 
159 #elif defined(TEST_WEBGPU)
160 
161 #if defined(__EMSCRIPTEN__)
162 #include <webgpu/webgpu_cpp.h>
163 #else
164 #include "mini_webgpu.h"
165 #endif
166 
167 extern "C" {
168 // TODO: Remove all of this when wgpuInstanceProcessEvents() is supported.
169 // See https://github.com/halide/Halide/issues/7248
170 #ifdef WITH_DAWN_NATIVE
171 // From <unistd.h>, used to spin-lock while waiting for device initialization.
172 int usleep(uint32_t);
173 #else
174 // Defined by Emscripten, and used to yield execution to asynchronous Javascript
175 // work in combination with Emscripten's "Asyncify" mechanism.
176 void emscripten_sleep(unsigned int ms);
177 #endif
178 }
179 
180 inline bool create_webgpu_context(WGPUInstance *instance_out, WGPUAdapter *adapter_out, WGPUDevice *device_out, WGPUBuffer *staging_buffer_out) {
181  struct Results {
182  WGPUInstance instance = nullptr;
183  WGPUAdapter adapter = nullptr;
184  WGPUDevice device = nullptr;
185  WGPUBuffer staging_buffer = nullptr;
186  bool success = true;
187  } results;
188 
189  results.instance = wgpuCreateInstance(nullptr);
190 
191  auto request_adapter_callback = [](WGPURequestAdapterStatus status, WGPUAdapter adapter, char const *message, void *userdata) {
192  auto *results = (Results *)userdata;
193 
194  if (status != WGPURequestAdapterStatus_Success) {
195  results->success = false;
196  return;
197  }
198  results->adapter = adapter;
199 
200  // Use the defaults for most limits.
201  WGPURequiredLimits requestedLimits{};
202  requestedLimits.nextInChain = nullptr;
203  memset(&requestedLimits.limits, 0xFF, sizeof(WGPULimits));
204 
205  // TODO: Enable for Emscripten when wgpuAdapterGetLimits is supported.
206  // See https://github.com/halide/Halide/issues/7248
207 #ifdef WITH_DAWN_NATIVE
208  WGPUSupportedLimits supportedLimits{};
209  supportedLimits.nextInChain = nullptr;
210  if (!wgpuAdapterGetLimits(adapter, &supportedLimits)) {
211  results->success = false;
212  return;
213  } else {
214  // Raise the limits on buffer size and workgroup storage size.
215  requestedLimits.limits.maxBufferSize = supportedLimits.limits.maxBufferSize;
216  requestedLimits.limits.maxStorageBufferBindingSize = supportedLimits.limits.maxStorageBufferBindingSize;
217  requestedLimits.limits.maxComputeWorkgroupStorageSize = supportedLimits.limits.maxComputeWorkgroupStorageSize;
218  }
219 #endif
220 
221  auto device_lost_callback = [](WGPUDeviceLostReason reason,
222  char const *message,
223  void *userdata) {
224  // Apparently this should not be treated as a fatal error
225  if (reason == WGPUDeviceLostReason_Destroyed) {
226  return;
227  }
228  fprintf(stderr, "WGPU Device Lost: %d %s", (int)reason, message);
229  abort();
230  };
231 
232  WGPUDeviceDescriptor desc{};
233  desc.nextInChain = nullptr;
234  desc.label = nullptr;
235  desc.requiredFeatureCount = 0;
236  desc.requiredFeatures = nullptr;
237  desc.requiredLimits = &requestedLimits;
238  desc.deviceLostCallback = device_lost_callback;
239 
240  auto request_device_callback = [](WGPURequestDeviceStatus status,
241  WGPUDevice device,
242  char const *message,
243  void *userdata) {
244  auto *results = (Results *)userdata;
245  if (status != WGPURequestDeviceStatus_Success) {
246  results->success = false;
247  return;
248  }
249  results->device = device;
250 
251  // Create a staging buffer for transfers.
252  constexpr int kStagingBufferSize = 4 * 1024 * 1024;
253  WGPUBufferDescriptor desc{};
254  desc.nextInChain = nullptr;
255  desc.label = nullptr;
257  desc.size = kStagingBufferSize;
258  desc.mappedAtCreation = false;
259  results->staging_buffer = wgpuDeviceCreateBuffer(device, &desc);
260  if (results->staging_buffer == nullptr) {
261  results->success = false;
262  return;
263  }
264  };
265 
266  wgpuAdapterRequestDevice(adapter, &desc, request_device_callback, userdata);
267  };
268 
269  wgpuInstanceRequestAdapter(results.instance, nullptr, request_adapter_callback, &results);
270 
271  // Wait for device initialization to complete.
272  while (!results.device && results.success) {
273  // TODO: Use wgpuInstanceProcessEvents() when it is supported.
274  // See https://github.com/halide/Halide/issues/7248
275 #ifndef WITH_DAWN_NATIVE
276  emscripten_sleep(10);
277 #else
278  usleep(1000);
279 #endif
280  }
281 
282  *instance_out = results.instance;
283  *adapter_out = results.adapter;
284  *device_out = results.device;
285  *staging_buffer_out = results.staging_buffer;
286  return results.success;
287 }
288 
289 inline void destroy_webgpu_context(WGPUInstance instance, WGPUAdapter adapter, WGPUDevice device, WGPUBuffer staging_buffer) {
290  wgpuBufferRelease(staging_buffer);
291  wgpuDeviceRelease(device);
292  wgpuAdapterRelease(adapter);
293  wgpuInstanceRelease(instance);
294 }
295 
296 #endif
struct _cl_platform_id * cl_platform_id
Definition: mini_cl.h:55
intptr_t cl_context_properties
Definition: mini_cl.h:78
int32_t cl_int
Definition: mini_cl.h:44
cl_bitfield cl_device_type
Definition: mini_cl.h:67
uint32_t cl_uint
Definition: mini_cl.h:45
#define CL_DEVICE_TYPE_ALL
Definition: mini_cl.h:218
#define CL_CONTEXT_PLATFORM
Definition: mini_cl.h:330
#define CL_SUCCESS
Definition: mini_cl.h:133
struct _cl_context * cl_context
Definition: mini_cl.h:57
struct _cl_device_id * cl_device_id
Definition: mini_cl.h:56
struct _cl_command_queue * cl_command_queue
Definition: mini_cl.h:58
WGPURequestAdapterStatus
Definition: mini_webgpu.h:589
@ WGPURequestAdapterStatus_Success
Definition: mini_webgpu.h:590
WGPUDeviceLostReason
Definition: mini_webgpu.h:421
@ WGPUDeviceLostReason_Destroyed
Definition: mini_webgpu.h:423
@ WGPUBufferUsage_MapRead
Definition: mini_webgpu.h:924
@ WGPUBufferUsage_CopyDst
Definition: mini_webgpu.h:927
WGPU_EXPORT void wgpuDeviceRelease(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE
WGPU_EXPORT void wgpuInstanceRelease(WGPUInstance instance) WGPU_FUNCTION_ATTRIBUTE
WGPURequestDeviceStatus
Definition: mini_webgpu.h:598
@ WGPURequestDeviceStatus_Success
Definition: mini_webgpu.h:599
WGPU_EXPORT WGPUInstance wgpuCreateInstance(WGPU_NULLABLE WGPUInstanceDescriptor const *descriptor) WGPU_FUNCTION_ATTRIBUTE
WGPU_EXPORT void wgpuAdapterRequestDevice(WGPUAdapter adapter, WGPU_NULLABLE WGPUDeviceDescriptor const *descriptor, WGPURequestDeviceCallback callback, void *userdata) WGPU_FUNCTION_ATTRIBUTE
WGPU_EXPORT WGPUBuffer wgpuDeviceCreateBuffer(WGPUDevice device, WGPUBufferDescriptor const *descriptor) WGPU_FUNCTION_ATTRIBUTE
WGPU_EXPORT void wgpuInstanceRequestAdapter(WGPUInstance instance, WGPU_NULLABLE WGPURequestAdapterOptions const *options, WGPURequestAdapterCallback callback, void *userdata) WGPU_FUNCTION_ATTRIBUTE
WGPU_EXPORT void wgpuBufferRelease(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE
WGPU_EXPORT WGPUBool wgpuAdapterGetLimits(WGPUAdapter adapter, WGPUSupportedLimits *limits) WGPU_FUNCTION_ATTRIBUTE
WGPU_EXPORT void wgpuAdapterRelease(WGPUAdapter adapter) WGPU_FUNCTION_ATTRIBUTE
struct CUctx_st * CUcontext
CUDA context.
Definition: mini_cuda.h:22
void * memset(void *s, int val, size_t n)
unsigned __INT32_TYPE__ uint32_t
void abort()
WGPUChainedStruct const * nextInChain
Definition: mini_webgpu.h:1058
WGPUChainedStruct const * nextInChain
Definition: mini_webgpu.h:1974
WGPUChainedStruct const * nextInChain
Definition: mini_webgpu.h:1898
WGPUChainedStructOut * nextInChain
Definition: mini_webgpu.h:1928