Halide  12.0.1
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 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 
36  cl_device_type device_type = CL_DEVICE_TYPE_ALL;
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 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 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 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 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 void destroy_metal_context(id<MTLDevice> device, id<MTLCommandQueue> queue) {
155  [queue release];
156  [device release];
157 }
158 
159 #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
struct CUctx_st * CUcontext
CUDA context.
Definition: mini_cuda.h:22