… | |
… | |
22 | typedef cl_program OpenCL__Program; |
22 | typedef cl_program OpenCL__Program; |
23 | typedef cl_kernel OpenCL__Kernel; |
23 | typedef cl_kernel OpenCL__Kernel; |
24 | typedef cl_event OpenCL__Event; |
24 | typedef cl_event OpenCL__Event; |
25 | typedef cl_event OpenCL__UserEvent; |
25 | typedef cl_event OpenCL__UserEvent; |
26 | |
26 | |
|
|
27 | typedef SV *FUTURE; |
|
|
28 | |
27 | /*****************************************************************************/ |
29 | /*****************************************************************************/ |
28 | |
30 | |
29 | /* up to two temporary buffers */ |
31 | /* up to two temporary buffers */ |
30 | static void * |
32 | static void * |
31 | tmpbuf (size_t size) |
33 | tmpbuf (size_t size) |
… | |
… | |
90 | return iv2str (err, errstr, sizeof (errstr) / sizeof (errstr [0]), "ERROR(%d)"); |
92 | return iv2str (err, errstr, sizeof (errstr) / sizeof (errstr [0]), "ERROR(%d)"); |
91 | } |
93 | } |
92 | |
94 | |
93 | /*****************************************************************************/ |
95 | /*****************************************************************************/ |
94 | |
96 | |
95 | static cl_int last_error; |
97 | static cl_int res; |
96 | |
98 | |
97 | #define FAIL(name,err) \ |
99 | #define FAIL(name) \ |
98 | croak ("cl" # name ": %s", err2str (last_error = err)); |
100 | croak ("cl" # name ": %s", err2str (res)); |
99 | |
101 | |
100 | #define NEED_SUCCESS(name,args) \ |
102 | #define NEED_SUCCESS(name,args) \ |
101 | do { \ |
103 | do { \ |
102 | cl_int res = cl ## name args; \ |
104 | res = cl ## name args; \ |
103 | \ |
105 | \ |
104 | if (res) \ |
106 | if (res) \ |
105 | FAIL (name, res); \ |
107 | FAIL (name); \ |
106 | } while (0) |
108 | } while (0) |
|
|
109 | |
|
|
110 | #define NEED_SUCCESS_ARG(retdecl, name, args) \ |
|
|
111 | retdecl = cl ## name args; \ |
|
|
112 | if (res) \ |
|
|
113 | FAIL (name); |
107 | |
114 | |
108 | /*****************************************************************************/ |
115 | /*****************************************************************************/ |
109 | |
116 | |
110 | #define NEW_MORTAL_OBJ(class,ptr) sv_setref_pv (sv_newmortal (), class, ptr) |
117 | #define NEW_MORTAL_OBJ(class,ptr) sv_setref_pv (sv_newmortal (), class, ptr) |
111 | #define XPUSH_NEW_OBJ(class,ptr) XPUSHs (NEW_MORTAL_OBJ (class, ptr)) |
118 | #define XPUSH_NEW_OBJ(class,ptr) XPUSHs (NEW_MORTAL_OBJ (class, ptr)) |
… | |
… | |
138 | |
145 | |
139 | #define INFO(class) \ |
146 | #define INFO(class) \ |
140 | { \ |
147 | { \ |
141 | size_t size; \ |
148 | size_t size; \ |
142 | SV *sv; \ |
149 | SV *sv; \ |
143 | \ |
150 | \ |
144 | NEED_SUCCESS (Get ## class ## Info, (this, name, 0, 0, &size)); \ |
151 | NEED_SUCCESS (Get ## class ## Info, (this, name, 0, 0, &size)); \ |
145 | sv = sv_2mortal (newSV (size)); \ |
152 | sv = sv_2mortal (newSV (size)); \ |
146 | SvUPGRADE (sv, SVt_PV); \ |
153 | SvUPGRADE (sv, SVt_PV); \ |
147 | SvPOK_only (sv); \ |
154 | SvPOK_only (sv); \ |
148 | SvCUR_set (sv, size); \ |
155 | SvCUR_set (sv, size); \ |
… | |
… | |
176 | } |
183 | } |
177 | |
184 | |
178 | cl_int |
185 | cl_int |
179 | errno () |
186 | errno () |
180 | CODE: |
187 | CODE: |
181 | errno = last_error; |
188 | errno = res; |
182 | |
189 | |
183 | const char * |
190 | const char * |
184 | err2str (cl_int err) |
191 | err2str (cl_int err) |
185 | |
192 | |
186 | const char * |
193 | const char * |
… | |
… | |
202 | for (i = 0; i < count; ++i) |
209 | for (i = 0; i < count; ++i) |
203 | PUSHs (NEW_MORTAL_OBJ ("OpenCL::Platform", list [i])); |
210 | PUSHs (NEW_MORTAL_OBJ ("OpenCL::Platform", list [i])); |
204 | } |
211 | } |
205 | |
212 | |
206 | void |
213 | void |
207 | context_from_type_simple (cl_device_type type = CL_DEVICE_TYPE_DEFAULT) |
214 | context_from_type (FUTURE properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0) |
208 | PPCODE: |
215 | PPCODE: |
209 | { |
|
|
210 | cl_int res; |
|
|
211 | cl_context ctx = clCreateContextFromType (0, type, 0, 0, &res); |
216 | NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (0, type, 0, 0, &res)); |
212 | |
|
|
213 | if (res) |
|
|
214 | FAIL (CreateContextFromType, res); |
|
|
215 | |
|
|
216 | XPUSH_NEW_OBJ ("OpenCL::Context", ctx); |
217 | XPUSH_NEW_OBJ ("OpenCL::Context", ctx); |
217 | } |
218 | |
|
|
219 | void |
|
|
220 | context (FUTURE properties, FUTURE devices, FUTURE notify = 0) |
|
|
221 | PPCODE: |
|
|
222 | /* der Gipfel der Kunst */ |
218 | |
223 | |
219 | void |
224 | void |
220 | wait_for_events (...) |
225 | wait_for_events (...) |
221 | CODE: |
226 | CODE: |
222 | { |
227 | { |
… | |
… | |
249 | for (i = 0; i < count; ++i) |
254 | for (i = 0; i < count; ++i) |
250 | PUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Device", list [i])); |
255 | PUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Device", list [i])); |
251 | } |
256 | } |
252 | |
257 | |
253 | void |
258 | void |
|
|
259 | context (OpenCL::Platform this, FUTURE properties, SV *devices, FUTURE notify = 0) |
|
|
260 | PPCODE: |
|
|
261 | if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV) |
|
|
262 | croak ("OpenCL::Platform argument 'device' must be an arrayref with device objects, in call"); |
|
|
263 | |
|
|
264 | AV *av = (SV *)SvRV (devices); |
|
|
265 | cl_uint num_devices = av_len (av) + 1; |
|
|
266 | cl_device_id *device_list = tmpbuf (sizeof (cl_device_id) * num_devices); |
|
|
267 | int i; |
|
|
268 | |
|
|
269 | for (i = num_devices; i--; ) |
|
|
270 | device_list [i] = SvPTROBJ ("clCreateContext", "devices", *av_fetch (av, i, 0), "OpenCL::Device"); |
|
|
271 | |
|
|
272 | NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (0, num_devices, device_list, 0, 0, &res)); |
|
|
273 | XPUSH_NEW_OBJ ("OpenCL::Context", ctx); |
|
|
274 | |
|
|
275 | void |
254 | context_from_type_simple (OpenCL::Platform this, cl_device_type type = CL_DEVICE_TYPE_DEFAULT) |
276 | context_from_type (OpenCL::Platform this, FUTURE properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0) |
255 | PPCODE: |
277 | PPCODE: |
256 | { |
|
|
257 | cl_int res; |
|
|
258 | cl_context_properties props[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)this, 0 }; |
278 | cl_context_properties props[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)this, 0 }; |
259 | cl_context ctx = clCreateContextFromType (props, type, 0, 0, &res); |
279 | NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (props, type, 0, 0, &res)); |
260 | |
|
|
261 | if (res) |
|
|
262 | FAIL (CreateContextFromType, res); |
|
|
263 | |
|
|
264 | XPUSH_NEW_OBJ ("OpenCL::Context", ctx); |
280 | XPUSH_NEW_OBJ ("OpenCL::Context", ctx); |
265 | } |
|
|
266 | |
281 | |
267 | MODULE = OpenCL PACKAGE = OpenCL::Device |
282 | MODULE = OpenCL PACKAGE = OpenCL::Device |
268 | |
283 | |
269 | void |
284 | void |
270 | info (OpenCL::Device this, cl_device_info name) |
285 | info (OpenCL::Device this, cl_device_info name) |
271 | PPCODE: |
286 | PPCODE: |
272 | INFO (Device) |
287 | INFO (Device) |
273 | |
288 | |
274 | void |
|
|
275 | context_simple (OpenCL::Device this) |
|
|
276 | PPCODE: |
|
|
277 | { |
|
|
278 | cl_int res; |
|
|
279 | cl_context ctx = clCreateContext (0, 1, &this, 0, 0, &res); |
|
|
280 | |
|
|
281 | if (res) |
|
|
282 | FAIL (CreateContext, res); |
|
|
283 | |
|
|
284 | XPUSH_NEW_OBJ ("OpenCL::Context", ctx); |
|
|
285 | } |
|
|
286 | |
|
|
287 | MODULE = OpenCL PACKAGE = OpenCL::Context |
289 | MODULE = OpenCL PACKAGE = OpenCL::Context |
288 | |
290 | |
289 | void |
291 | void |
290 | DESTROY (OpenCL::Context context) |
292 | DESTROY (OpenCL::Context context) |
291 | CODE: |
293 | CODE: |
… | |
… | |
295 | info (OpenCL::Context this, cl_context_info name) |
297 | info (OpenCL::Context this, cl_context_info name) |
296 | PPCODE: |
298 | PPCODE: |
297 | INFO (Context) |
299 | INFO (Context) |
298 | |
300 | |
299 | void |
301 | void |
300 | command_queue_simple (OpenCL::Context this, OpenCL::Device device) |
302 | queue (OpenCL::Context this, OpenCL::Device device, cl_command_queue_properties properties = 0) |
301 | PPCODE: |
303 | PPCODE: |
302 | { |
|
|
303 | cl_int res; |
|
|
304 | cl_command_queue queue = clCreateCommandQueue (this, device, 0, &res); |
304 | NEED_SUCCESS_ARG (cl_command_queue queue, CreateCommandQueue, (this, device, properties, &res)); |
305 | |
|
|
306 | if (res) |
|
|
307 | FAIL (CreateCommandQueue, res); |
|
|
308 | |
|
|
309 | XPUSH_NEW_OBJ ("OpenCL::Queue", queue); |
305 | XPUSH_NEW_OBJ ("OpenCL::Queue", queue); |
310 | } |
|
|
311 | |
306 | |
312 | void |
307 | void |
313 | user_event (OpenCL::Context this) |
308 | user_event (OpenCL::Context this) |
314 | PPCODE: |
309 | PPCODE: |
315 | { |
|
|
316 | cl_int res; |
|
|
317 | cl_event ev = clCreateUserEvent (this, &res); |
310 | NEED_SUCCESS_ARG (cl_event ev, CreateUserEvent, (this, &res)); |
318 | |
|
|
319 | if (res) |
|
|
320 | FAIL (CreateUserevent, res); |
|
|
321 | |
|
|
322 | XPUSH_NEW_OBJ ("OpenCL::UserEvent", ev); |
311 | XPUSH_NEW_OBJ ("OpenCL::UserEvent", ev); |
323 | } |
|
|
324 | |
312 | |
325 | void |
313 | void |
326 | buffer (OpenCL::Context this, cl_mem_flags flags, size_t len) |
314 | buffer (OpenCL::Context this, cl_mem_flags flags, size_t len) |
327 | PPCODE: |
315 | PPCODE: |
328 | { |
|
|
329 | cl_int res; |
|
|
330 | cl_mem mem; |
|
|
331 | |
|
|
332 | if (flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR)) |
316 | if (flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR)) |
333 | croak ("clCreateBuffer: cannot use/copy host ptr when no data is given, use $context->buffer_sv instead?"); |
317 | croak ("clCreateBuffer: cannot use/copy host ptr when no data is given, use $context->buffer_sv instead?"); |
334 | |
318 | |
335 | mem = clCreateBuffer (this, flags, len, 0, &res); |
319 | NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (this, flags, len, 0, &res)); |
336 | |
|
|
337 | if (res) |
|
|
338 | FAIL (CreateBuffer, res); |
|
|
339 | |
|
|
340 | XPUSH_NEW_OBJ ("OpenCL::Buffer", mem); |
320 | XPUSH_NEW_OBJ ("OpenCL::Buffer", mem); |
341 | } |
|
|
342 | |
321 | |
343 | void |
322 | void |
344 | buffer_sv (OpenCL::Context this, cl_mem_flags flags, SV *data) |
323 | buffer_sv (OpenCL::Context this, cl_mem_flags flags, SV *data) |
345 | PPCODE: |
324 | PPCODE: |
346 | { |
|
|
347 | STRLEN len; |
325 | STRLEN len; |
348 | char *ptr = SvPVbyte (data, len); |
326 | char *ptr = SvPVbyte (data, len); |
349 | cl_int res; |
|
|
350 | cl_mem mem; |
|
|
351 | |
327 | |
352 | if (!(flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))) |
328 | if (!(flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))) |
353 | croak ("clCreateBuffer: have to specify use or copy host ptr when buffer data is given, use $context->buffer instead?"); |
329 | croak ("clCreateBuffer: have to specify use or copy host ptr when buffer data is given, use $context->buffer instead?"); |
354 | |
330 | |
355 | mem = clCreateBuffer (this, flags, len, ptr, &res); |
331 | NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (this, flags, len, ptr, &res)); |
356 | |
|
|
357 | if (res) |
|
|
358 | FAIL (CreateBuffer, res); |
|
|
359 | |
|
|
360 | XPUSH_NEW_OBJ ("OpenCL::Buffer", mem); |
332 | XPUSH_NEW_OBJ ("OpenCL::Buffer", mem); |
361 | } |
|
|
362 | |
333 | |
363 | void |
334 | void |
364 | image2d (OpenCL::Context this, cl_mem_flags flags, cl_channel_order channel_order, cl_channel_type channel_type, size_t width, size_t height, SV *data) |
335 | image2d (OpenCL::Context this, cl_mem_flags flags, cl_channel_order channel_order, cl_channel_type channel_type, size_t width, size_t height, SV *data) |
365 | PPCODE: |
336 | PPCODE: |
366 | { |
|
|
367 | STRLEN len; |
337 | STRLEN len; |
368 | char *ptr = SvPVbyte (data, len); |
338 | char *ptr = SvPVbyte (data, len); |
369 | const cl_image_format format = { channel_order, channel_type }; |
339 | const cl_image_format format = { channel_order, channel_type }; |
370 | cl_int res; |
|
|
371 | cl_mem mem = clCreateImage2D (this, flags, &format, width, height, len / height, ptr, &res); |
340 | NEED_SUCCESS_ARG (cl_mem mem, CreateImage2D, (this, flags, &format, width, height, len / height, ptr, &res)); |
372 | |
|
|
373 | if (res) |
|
|
374 | FAIL (CreateImage2D, res); |
|
|
375 | |
|
|
376 | XPUSH_NEW_OBJ ("OpenCL::Image2D", mem); |
341 | XPUSH_NEW_OBJ ("OpenCL::Image2D", mem); |
377 | } |
|
|
378 | |
342 | |
379 | void |
343 | void |
380 | image3d (OpenCL::Context this, cl_mem_flags flags, cl_channel_order channel_order, cl_channel_type channel_type, size_t width, size_t height, size_t depth, size_t slice_pitch, SV *data) |
344 | image3d (OpenCL::Context this, cl_mem_flags flags, cl_channel_order channel_order, cl_channel_type channel_type, size_t width, size_t height, size_t depth, size_t slice_pitch, SV *data) |
381 | PPCODE: |
345 | PPCODE: |
382 | { |
|
|
383 | STRLEN len; |
346 | STRLEN len; |
384 | char *ptr = SvPVbyte (data, len); |
347 | char *ptr = SvPVbyte (data, len); |
385 | const cl_image_format format = { channel_order, channel_type }; |
348 | const cl_image_format format = { channel_order, channel_type }; |
386 | cl_int res; |
|
|
387 | cl_mem mem = clCreateImage3D (this, flags, &format, width, height, |
349 | NEED_SUCCESS_ARG (cl_mem mem, CreateImage3D, (this, flags, &format, width, height, |
388 | depth, len / (height * slice_pitch), slice_pitch, ptr, &res); |
350 | depth, len / (height * slice_pitch), slice_pitch, ptr, &res)); |
389 | |
|
|
390 | if (res) |
|
|
391 | FAIL (CreateImage3D, res); |
|
|
392 | |
|
|
393 | XPUSH_NEW_OBJ ("OpenCL::Image3D", mem); |
351 | XPUSH_NEW_OBJ ("OpenCL::Image3D", mem); |
394 | } |
|
|
395 | |
352 | |
396 | void |
353 | void |
397 | supported_image_formats (OpenCL::Context this, cl_mem_flags flags, cl_mem_object_type image_type) |
354 | supported_image_formats (OpenCL::Context this, cl_mem_flags flags, cl_mem_object_type image_type) |
398 | PPCODE: |
355 | PPCODE: |
399 | { |
356 | { |
… | |
… | |
416 | } |
373 | } |
417 | |
374 | |
418 | void |
375 | void |
419 | sampler (OpenCL::Context this, cl_bool normalized_coords, cl_addressing_mode addressing_mode, cl_filter_mode filter_mode) |
376 | sampler (OpenCL::Context this, cl_bool normalized_coords, cl_addressing_mode addressing_mode, cl_filter_mode filter_mode) |
420 | PPCODE: |
377 | PPCODE: |
421 | { |
|
|
422 | cl_int res; |
|
|
423 | cl_sampler sampler = clCreateSampler (this, normalized_coords, addressing_mode, filter_mode, &res); |
378 | NEED_SUCCESS_ARG (cl_sampler sampler, CreateSampler, (this, normalized_coords, addressing_mode, filter_mode, &res)); |
424 | |
|
|
425 | if (res) |
|
|
426 | FAIL (CreateSampler, res); |
|
|
427 | |
|
|
428 | XPUSH_NEW_OBJ ("OpenCL::Sampler", sampler); |
379 | XPUSH_NEW_OBJ ("OpenCL::Sampler", sampler); |
429 | } |
|
|
430 | |
380 | |
431 | void |
381 | void |
432 | program_with_source (OpenCL::Context this, SV *program) |
382 | program_with_source (OpenCL::Context this, SV *program) |
433 | PPCODE: |
383 | PPCODE: |
434 | { |
|
|
435 | STRLEN len; |
384 | STRLEN len; |
436 | size_t len2; |
385 | size_t len2; |
437 | const char *ptr = SvPVbyte (program, len); |
386 | const char *ptr = SvPVbyte (program, len); |
438 | cl_int res; |
|
|
439 | cl_program prog; |
|
|
440 | |
387 | |
441 | len2 = len; |
388 | len2 = len; |
442 | prog = clCreateProgramWithSource (this, 1, &ptr, &len2, &res); |
389 | NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithSource, (this, 1, &ptr, &len2, &res)); |
443 | |
|
|
444 | if (res) |
|
|
445 | FAIL (CreateProgramWithSource, res); |
|
|
446 | |
|
|
447 | XPUSH_NEW_OBJ ("OpenCL::Program", prog); |
390 | XPUSH_NEW_OBJ ("OpenCL::Program", prog); |
448 | } |
|
|
449 | |
391 | |
450 | MODULE = OpenCL PACKAGE = OpenCL::Queue |
392 | MODULE = OpenCL PACKAGE = OpenCL::Queue |
451 | |
393 | |
452 | void |
394 | void |
453 | DESTROY (OpenCL::Queue this) |
395 | DESTROY (OpenCL::Queue this) |
… | |
… | |
764 | } |
706 | } |
765 | |
707 | |
766 | void |
708 | void |
767 | kernel (OpenCL::Program program, SV *function) |
709 | kernel (OpenCL::Program program, SV *function) |
768 | PPCODE: |
710 | PPCODE: |
769 | { |
|
|
770 | cl_int res; |
|
|
771 | cl_kernel kernel = clCreateKernel (program, SvPVbyte_nolen (function), &res); |
711 | NEED_SUCCESS_ARG (cl_kernel kernel, CreateKernel, (program, SvPVbyte_nolen (function), &res)); |
772 | |
|
|
773 | if (res) |
|
|
774 | FAIL (CreateKernel, res); |
|
|
775 | |
|
|
776 | XPUSH_NEW_OBJ ("OpenCL::Kernel", kernel); |
712 | XPUSH_NEW_OBJ ("OpenCL::Kernel", kernel); |
777 | } |
|
|
778 | |
713 | |
779 | MODULE = OpenCL PACKAGE = OpenCL::Kernel |
714 | MODULE = OpenCL PACKAGE = OpenCL::Kernel |
780 | |
715 | |
781 | void |
716 | void |
782 | DESTROY (OpenCL::Kernel this) |
717 | DESTROY (OpenCL::Kernel this) |