-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcl_utils.py
314 lines (270 loc) · 11.2 KB
/
cl_utils.py
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
""" This file implements a thin wrapper of OpenCL API,
mostly using the awesome library "gpuctypes"
from Geohot https://github.com/tinygrad/gpuctypes
To use the API, first you need to call the create_context function.
This will give you an OpenCL context, an OpenCL device, and an OpenCL command queue
import cl_utils
cl_ctx, cl_device, cl_cmd_queue = cl_utils.create_context()
Next, you can create an OpenCLLibrary using cl_compile
code = ... # some OpenCL code as string
kernel_names = [...] # names of kernels in the code you want to expose
lib = cl_utils.cl_compile(cl_ctx,
cl_device,
cl_cmd_queue,
code,
kernel_names)
Next, you allocate OpenCL memory buffers using clCreateBuffer
import gpuctypes.opencl as cl
import ctypes
status = ctypes.c_int32()
py_x = [0, 0, 0]
x = (ctypes.c_int * len(py_x))(*py_x)
bufx = cl.clCreateBuffer(cl_ctx,
cl.CL_MEM_WRITE_ONLY,
ctypes.sizeof(x),
ctypes.byref(x),
ctypes.byref(status))
cl_utils.cl_check(status.value)
Calling lib.[function_name] gives you an OpenCLKernel object
# f is an OpenCLKernel
f = lib.opencl_func
Calling f(...) launches the OpenCL kernel.
You can use clFinish to wait for the kernel to complete.
f(bufx)
cl.clFinish(cl_cmd_queue)
Finally, use clEnqueueReadBuffer to read from the buffer
cl.clEnqueueReadBuffer(cl_cmd_queue,
bufx,
cl.CL_TRUE,
0,
ctypes.sizeof(x),
ctypes.byref(x),
0,
None,
None)
"""
import gpuctypes.opencl as cl
import ctypes
import os
class OpenCLKernel:
""" An OpenCLKernel wraps around a 1D OpenCL kernel.
See the file-level comment for how it's used.
"""
def __init__(self,
program,
func_name,
cmd_queue):
self.cmd_queue = cmd_queue
status = ctypes.c_int32()
self.kernel = cl.clCreateKernel(program,
bytes(func_name, 'utf-8'),
ctypes.byref(status))
cl_check(status.value)
def __call__(self, *args):
for i, buffer in enumerate(args):
if i == len(args) - 1:
break
cl.clSetKernelArg(self.kernel,
i,
ctypes.sizeof(buffer),
ctypes.byref(buffer))
total_work = ctypes.c_size_t(int(args[-1]))
cl.clEnqueueNDRangeKernel(self.cmd_queue,
self.kernel,
1,
None,
ctypes.byref(total_work),
None,
0,
None,
None)
class OpenCLLibrary:
""" An OpenCLLibrary contains many OpenCL kernels.
See the file-level comment for how it's used.
"""
def __init__(self,
program,
cmd_queue,
func_names):
self.program = program
self.cmd_queue = cmd_queue
for f in func_names:
setattr(self, f, OpenCLKernel(program, f, cmd_queue))
def to_char_p_p(options):
c_options = (ctypes.POINTER(ctypes.c_char) * len(options))()
c_options[:] = [ctypes.cast(ctypes.create_string_buffer(o.encode("utf-8")), ctypes.POINTER(ctypes.c_char)) for o in options]
return c_options
def cl_check(status, info = None):
if status != 0: raise RuntimeError(f'OpenCL Error {status}' + (('\n\n'+info) if info else ''))
def cl_get_platform_name(platform_id):
platform_str_size = ctypes.c_size_t(0)
cl.clGetPlatformInfo(platform_id,
cl.CL_PLATFORM_NAME,
0,
0,
ctypes.byref(platform_str_size))
platform_str = ctypes.create_string_buffer(platform_str_size.value)
cl.clGetPlatformInfo(platform_id,
cl.CL_PLATFORM_NAME,
platform_str_size,
platform_str,
ctypes.POINTER(ctypes.c_size_t)())
return platform_str.value
def cl_get_device_name(device_id):
device_str_size = ctypes.c_size_t(0)
cl.clGetDeviceInfo(device_id,
cl.CL_DEVICE_NAME,
0,
0,
ctypes.byref(device_str_size))
device_str = ctypes.create_string_buffer(device_str_size.value)
cl.clGetDeviceInfo(device_id,
cl.CL_DEVICE_NAME,
device_str_size,
device_str,
ctypes.POINTER(ctypes.c_size_t)())
return device_str.value
def create_context():
""" Returns an OpenCL context, an OpenCL device, and an OpenCL command queue.
It reads from the "OPENCL_CTX" environment variable to make choices.
If the environment variable does not exist, then it interactively asks
the user.
See the file-level comment for how to use this.
"""
# Some of the code is inspired from PyOpenCL https://github.com/inducer/pyopencl/
answers = None
if "OPENCL_CTX" in os.environ:
ctx_spec = os.environ["OPENCL_CTX"]
answers = [int(choice) for choice in ctx_spec.split(":")]
# Get platforms
num_platforms = ctypes.c_uint32()
cl_check(cl.clGetPlatformIDs(0,
ctypes.POINTER(cl.cl_platform_id)(),
ctypes.byref(num_platforms)))
platform_array = (cl.cl_platform_id * num_platforms.value)()
cl_check(cl.clGetPlatformIDs(num_platforms,
platform_array,
ctypes.POINTER(ctypes.c_uint32)()))
assert num_platforms.value > 0, 'didn\'t get platform'
current_answers = []
if answers is not None:
platform = platform_array[answers[0]]
else:
platform_names = [cl_get_platform_name(platform_id) for platform_id in platform_array]
print('Choose platform:')
for i, pf in enumerate(platform_names):
print("[%d] %s" % (i, pf))
answer = input('Choice [0]:')
if not answer:
platform = platform_array[0]
current_answers.append(0)
else:
platform = None
try:
int_choice = int(answer)
except ValueError:
pass
else:
if 0 <= int_choice < len(platform_array):
platform = platform_array[int_choice]
if platform is None:
raise RuntimeError('input did not match any platform')
current_answers.append(int_choice)
# Get devices
num_devices = ctypes.c_uint32()
cl_check(cl.clGetDeviceIDs(platform,
cl.CL_DEVICE_TYPE_ALL,
0,
ctypes.POINTER(cl.cl_device_id)(),
ctypes.byref(num_devices)))
device_array = (cl.cl_device_id * num_devices.value)()
cl_check(cl.clGetDeviceIDs(platform,
cl.CL_DEVICE_TYPE_ALL,
num_devices,
device_array,
ctypes.POINTER(ctypes.c_uint32)()))
assert num_devices.value > 0, 'didn\'t get device'
if answers is not None:
device = device_array[answers[1]]
else:
device_names = [cl_get_device_name(device_id) for device_id in device_array]
print('Choose device:')
for i, dev in enumerate(device_names):
print("[%d] %s" % (i, dev))
answer = input('Choice [0]:')
if not answer:
device = device_array[0]
current_answers.append(0)
else:
device = None
try:
int_choice = int(answer)
except ValueError:
pass
else:
if 0 <= int_choice < len(device_array):
device = device_array[int_choice]
if device is None:
raise RuntimeError('input did not match any device')
current_answers.append(int_choice)
# Create context & command queue
status = ctypes.c_int32()
context = cl.clCreateContext(None,
num_devices,
device_array,
ctypes.cast(None, cl.clCreateContext.argtypes[3]),
None,
ctypes.byref(status))
cl_check(status.value)
assert context is not None
status = ctypes.c_int32()
cmd_queue = cl.clCreateCommandQueue(context,
device,
0,
ctypes.byref(status))
cl_check(status.value)
if answers is None:
print('Set the environment variable OPENCL_CTX=\'%s\' to '
'avoid being asked again.' % ':'.join([str(a) for a in current_answers]))
return context, device, cmd_queue
def cl_compile(context,
device,
cmd_queue,
code,
func_names):
""" Compiles OpenCL programs represented as strings.
Returns an OpenCLLibrary.
See the file-level comment for how to use this.
"""
num_programs = 1
sizes = (ctypes.c_size_t * num_programs)()
sizes[0] = len(code)
status = ctypes.c_int32()
program = cl.clCreateProgramWithSource(context,
num_programs,
to_char_p_p([code]),
sizes,
ctypes.byref(status))
assert program is not None
device_array = (cl.cl_device_id * 1)(device)
status = cl.clBuildProgram(program,
1,
device_array,
None,
ctypes.cast(None, cl.clBuildProgram.argtypes[4]),
None)
if status != 0:
log_size = ctypes.c_size_t()
cl.clGetProgramBuildInfo(program,
device,
cl.CL_PROGRAM_BUILD_LOG,
0, None,
ctypes.byref(log_size))
cl.clGetProgramBuildInfo(program,
device_array[0],
cl.CL_PROGRAM_BUILD_LOG,
log_size.value, mstr := ctypes.create_string_buffer(log_size.value), None)
assert False, ctypes.string_at(mstr, size=log_size.value).decode()
return OpenCLLibrary(program,
cmd_queue,
func_names)