… | |
… | |
92 | return iv2str (err, errstr, sizeof (errstr) / sizeof (errstr [0]), "ERROR(%d)"); |
92 | return iv2str (err, errstr, sizeof (errstr) / sizeof (errstr [0]), "ERROR(%d)"); |
93 | } |
93 | } |
94 | |
94 | |
95 | /*****************************************************************************/ |
95 | /*****************************************************************************/ |
96 | |
96 | |
97 | static cl_int last_error; |
97 | static cl_int res; |
98 | |
98 | |
99 | #define FAIL(name,err) \ |
99 | #define FAIL(name) \ |
100 | croak ("cl" # name ": %s", err2str (last_error = err)); |
100 | croak ("cl" # name ": %s", err2str (res)); |
101 | |
101 | |
102 | #define NEED_SUCCESS(name,args) \ |
102 | #define NEED_SUCCESS(name,args) \ |
103 | do { \ |
103 | do { \ |
104 | cl_int res = cl ## name args; \ |
104 | res = cl ## name args; \ |
105 | \ |
105 | \ |
106 | if (res) \ |
106 | if (res) \ |
107 | FAIL (name, res); \ |
107 | FAIL (name); \ |
108 | } 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); |
109 | |
114 | |
110 | /*****************************************************************************/ |
115 | /*****************************************************************************/ |
111 | |
116 | |
112 | #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) |
113 | #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)) |
… | |
… | |
140 | |
145 | |
141 | #define INFO(class) \ |
146 | #define INFO(class) \ |
142 | { \ |
147 | { \ |
143 | size_t size; \ |
148 | size_t size; \ |
144 | SV *sv; \ |
149 | SV *sv; \ |
145 | \ |
150 | \ |
146 | NEED_SUCCESS (Get ## class ## Info, (this, name, 0, 0, &size)); \ |
151 | NEED_SUCCESS (Get ## class ## Info, (this, name, 0, 0, &size)); \ |
147 | sv = sv_2mortal (newSV (size)); \ |
152 | sv = sv_2mortal (newSV (size)); \ |
148 | SvUPGRADE (sv, SVt_PV); \ |
153 | SvUPGRADE (sv, SVt_PV); \ |
149 | SvPOK_only (sv); \ |
154 | SvPOK_only (sv); \ |
150 | SvCUR_set (sv, size); \ |
155 | SvCUR_set (sv, size); \ |
… | |
… | |
178 | } |
183 | } |
179 | |
184 | |
180 | cl_int |
185 | cl_int |
181 | errno () |
186 | errno () |
182 | CODE: |
187 | CODE: |
183 | errno = last_error; |
188 | errno = res; |
184 | |
189 | |
185 | const char * |
190 | const char * |
186 | err2str (cl_int err) |
191 | err2str (cl_int err) |
187 | |
192 | |
188 | const char * |
193 | const char * |
… | |
… | |
206 | } |
211 | } |
207 | |
212 | |
208 | void |
213 | void |
209 | context_from_type (FUTURE properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0) |
214 | context_from_type (FUTURE properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0) |
210 | PPCODE: |
215 | PPCODE: |
211 | { |
|
|
212 | cl_int res; |
|
|
213 | cl_context ctx = clCreateContextFromType (0, type, 0, 0, &res); |
216 | NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (0, type, 0, 0, &res)); |
214 | |
|
|
215 | if (res) |
|
|
216 | FAIL (CreateContextFromType, res); |
|
|
217 | |
|
|
218 | XPUSH_NEW_OBJ ("OpenCL::Context", ctx); |
217 | XPUSH_NEW_OBJ ("OpenCL::Context", ctx); |
219 | } |
218 | |
|
|
219 | void |
|
|
220 | context (FUTURE properties, FUTURE devices, FUTURE notify = 0) |
|
|
221 | PPCODE: |
|
|
222 | /* der Gipfel der Kunst */ |
220 | |
223 | |
221 | void |
224 | void |
222 | wait_for_events (...) |
225 | wait_for_events (...) |
223 | CODE: |
226 | CODE: |
224 | { |
227 | { |
… | |
… | |
251 | for (i = 0; i < count; ++i) |
254 | for (i = 0; i < count; ++i) |
252 | PUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Device", list [i])); |
255 | PUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Device", list [i])); |
253 | } |
256 | } |
254 | |
257 | |
255 | 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 |
256 | context_from_type (OpenCL::Platform this, FUTURE properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0) |
276 | context_from_type (OpenCL::Platform this, FUTURE properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0) |
257 | PPCODE: |
277 | PPCODE: |
258 | { |
|
|
259 | cl_int res; |
|
|
260 | 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 }; |
261 | cl_context ctx = clCreateContextFromType (props, type, 0, 0, &res); |
279 | NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (props, type, 0, 0, &res)); |
262 | |
|
|
263 | if (res) |
|
|
264 | FAIL (CreateContextFromType, res); |
|
|
265 | |
|
|
266 | XPUSH_NEW_OBJ ("OpenCL::Context", ctx); |
280 | XPUSH_NEW_OBJ ("OpenCL::Context", ctx); |
267 | } |
|
|
268 | |
281 | |
269 | MODULE = OpenCL PACKAGE = OpenCL::Device |
282 | MODULE = OpenCL PACKAGE = OpenCL::Device |
270 | |
283 | |
271 | void |
284 | void |
272 | info (OpenCL::Device this, cl_device_info name) |
285 | info (OpenCL::Device this, cl_device_info name) |
273 | PPCODE: |
286 | PPCODE: |
274 | INFO (Device) |
287 | INFO (Device) |
275 | |
288 | |
276 | void |
|
|
277 | context (OpenCL::Device this, FUTURE properties = 0, FUTURE notify = 0) |
|
|
278 | PPCODE: |
|
|
279 | { |
|
|
280 | cl_int res; |
|
|
281 | cl_context ctx = clCreateContext (0, 1, &this, 0, 0, &res); |
|
|
282 | |
|
|
283 | if (res) |
|
|
284 | FAIL (CreateContext, res); |
|
|
285 | |
|
|
286 | XPUSH_NEW_OBJ ("OpenCL::Context", ctx); |
|
|
287 | } |
|
|
288 | |
|
|
289 | MODULE = OpenCL PACKAGE = OpenCL::Context |
289 | MODULE = OpenCL PACKAGE = OpenCL::Context |
290 | |
290 | |
291 | void |
291 | void |
292 | DESTROY (OpenCL::Context context) |
292 | DESTROY (OpenCL::Context context) |
293 | CODE: |
293 | CODE: |
… | |
… | |
299 | INFO (Context) |
299 | INFO (Context) |
300 | |
300 | |
301 | void |
301 | void |
302 | queue (OpenCL::Context this, OpenCL::Device device, cl_command_queue_properties properties = 0) |
302 | queue (OpenCL::Context this, OpenCL::Device device, cl_command_queue_properties properties = 0) |
303 | PPCODE: |
303 | PPCODE: |
304 | { |
|
|
305 | cl_int res; |
|
|
306 | cl_command_queue queue = clCreateCommandQueue (this, device, properties, &res); |
304 | NEED_SUCCESS_ARG (cl_command_queue queue, CreateCommandQueue, (this, device, properties, &res)); |
307 | |
|
|
308 | if (res) |
|
|
309 | FAIL (CreateCommandQueue, res); |
|
|
310 | |
|
|
311 | XPUSH_NEW_OBJ ("OpenCL::Queue", queue); |
305 | XPUSH_NEW_OBJ ("OpenCL::Queue", queue); |
312 | } |
|
|
313 | |
306 | |
314 | void |
307 | void |
315 | user_event (OpenCL::Context this) |
308 | user_event (OpenCL::Context this) |
316 | PPCODE: |
309 | PPCODE: |
317 | { |
|
|
318 | cl_int res; |
|
|
319 | cl_event ev = clCreateUserEvent (this, &res); |
310 | NEED_SUCCESS_ARG (cl_event ev, CreateUserEvent, (this, &res)); |
320 | |
|
|
321 | if (res) |
|
|
322 | FAIL (CreateUserevent, res); |
|
|
323 | |
|
|
324 | XPUSH_NEW_OBJ ("OpenCL::UserEvent", ev); |
311 | XPUSH_NEW_OBJ ("OpenCL::UserEvent", ev); |
325 | } |
|
|
326 | |
312 | |
327 | void |
313 | void |
328 | buffer (OpenCL::Context this, cl_mem_flags flags, size_t len) |
314 | buffer (OpenCL::Context this, cl_mem_flags flags, size_t len) |
329 | PPCODE: |
315 | PPCODE: |
330 | { |
|
|
331 | cl_int res; |
|
|
332 | cl_mem mem; |
|
|
333 | |
|
|
334 | 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)) |
335 | 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?"); |
336 | |
318 | |
337 | mem = clCreateBuffer (this, flags, len, 0, &res); |
319 | NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (this, flags, len, 0, &res)); |
338 | |
|
|
339 | if (res) |
|
|
340 | FAIL (CreateBuffer, res); |
|
|
341 | |
|
|
342 | XPUSH_NEW_OBJ ("OpenCL::Buffer", mem); |
320 | XPUSH_NEW_OBJ ("OpenCL::Buffer", mem); |
343 | } |
|
|
344 | |
321 | |
345 | void |
322 | void |
346 | buffer_sv (OpenCL::Context this, cl_mem_flags flags, SV *data) |
323 | buffer_sv (OpenCL::Context this, cl_mem_flags flags, SV *data) |
347 | PPCODE: |
324 | PPCODE: |
348 | { |
|
|
349 | STRLEN len; |
325 | STRLEN len; |
350 | char *ptr = SvPVbyte (data, len); |
326 | char *ptr = SvPVbyte (data, len); |
351 | cl_int res; |
|
|
352 | cl_mem mem; |
|
|
353 | |
327 | |
354 | 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))) |
355 | 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?"); |
356 | |
330 | |
357 | mem = clCreateBuffer (this, flags, len, ptr, &res); |
331 | NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (this, flags, len, ptr, &res)); |
358 | |
|
|
359 | if (res) |
|
|
360 | FAIL (CreateBuffer, res); |
|
|
361 | |
|
|
362 | XPUSH_NEW_OBJ ("OpenCL::Buffer", mem); |
332 | XPUSH_NEW_OBJ ("OpenCL::Buffer", mem); |
363 | } |
|
|
364 | |
333 | |
365 | void |
334 | void |
366 | 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) |
367 | PPCODE: |
336 | PPCODE: |
368 | { |
|
|
369 | STRLEN len; |
337 | STRLEN len; |
370 | char *ptr = SvPVbyte (data, len); |
338 | char *ptr = SvPVbyte (data, len); |
371 | const cl_image_format format = { channel_order, channel_type }; |
339 | const cl_image_format format = { channel_order, channel_type }; |
372 | cl_int res; |
|
|
373 | 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)); |
374 | |
|
|
375 | if (res) |
|
|
376 | FAIL (CreateImage2D, res); |
|
|
377 | |
|
|
378 | XPUSH_NEW_OBJ ("OpenCL::Image2D", mem); |
341 | XPUSH_NEW_OBJ ("OpenCL::Image2D", mem); |
379 | } |
|
|
380 | |
342 | |
381 | void |
343 | void |
382 | 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) |
383 | PPCODE: |
345 | PPCODE: |
384 | { |
|
|
385 | STRLEN len; |
346 | STRLEN len; |
386 | char *ptr = SvPVbyte (data, len); |
347 | char *ptr = SvPVbyte (data, len); |
387 | const cl_image_format format = { channel_order, channel_type }; |
348 | const cl_image_format format = { channel_order, channel_type }; |
388 | cl_int res; |
|
|
389 | cl_mem mem = clCreateImage3D (this, flags, &format, width, height, |
349 | NEED_SUCCESS_ARG (cl_mem mem, CreateImage3D, (this, flags, &format, width, height, |
390 | depth, len / (height * slice_pitch), slice_pitch, ptr, &res); |
350 | depth, len / (height * slice_pitch), slice_pitch, ptr, &res)); |
391 | |
|
|
392 | if (res) |
|
|
393 | FAIL (CreateImage3D, res); |
|
|
394 | |
|
|
395 | XPUSH_NEW_OBJ ("OpenCL::Image3D", mem); |
351 | XPUSH_NEW_OBJ ("OpenCL::Image3D", mem); |
396 | } |
|
|
397 | |
352 | |
398 | void |
353 | void |
399 | 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) |
400 | PPCODE: |
355 | PPCODE: |
401 | { |
356 | { |
… | |
… | |
418 | } |
373 | } |
419 | |
374 | |
420 | void |
375 | void |
421 | 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) |
422 | PPCODE: |
377 | PPCODE: |
423 | { |
|
|
424 | cl_int res; |
|
|
425 | 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)); |
426 | |
|
|
427 | if (res) |
|
|
428 | FAIL (CreateSampler, res); |
|
|
429 | |
|
|
430 | XPUSH_NEW_OBJ ("OpenCL::Sampler", sampler); |
379 | XPUSH_NEW_OBJ ("OpenCL::Sampler", sampler); |
431 | } |
|
|
432 | |
380 | |
433 | void |
381 | void |
434 | program_with_source (OpenCL::Context this, SV *program) |
382 | program_with_source (OpenCL::Context this, SV *program) |
435 | PPCODE: |
383 | PPCODE: |
436 | { |
|
|
437 | STRLEN len; |
384 | STRLEN len; |
438 | size_t len2; |
385 | size_t len2; |
439 | const char *ptr = SvPVbyte (program, len); |
386 | const char *ptr = SvPVbyte (program, len); |
440 | cl_int res; |
|
|
441 | cl_program prog; |
|
|
442 | |
387 | |
443 | len2 = len; |
388 | len2 = len; |
444 | prog = clCreateProgramWithSource (this, 1, &ptr, &len2, &res); |
389 | NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithSource, (this, 1, &ptr, &len2, &res)); |
445 | |
|
|
446 | if (res) |
|
|
447 | FAIL (CreateProgramWithSource, res); |
|
|
448 | |
|
|
449 | XPUSH_NEW_OBJ ("OpenCL::Program", prog); |
390 | XPUSH_NEW_OBJ ("OpenCL::Program", prog); |
450 | } |
|
|
451 | |
391 | |
452 | MODULE = OpenCL PACKAGE = OpenCL::Queue |
392 | MODULE = OpenCL PACKAGE = OpenCL::Queue |
453 | |
393 | |
454 | void |
394 | void |
455 | DESTROY (OpenCL::Queue this) |
395 | DESTROY (OpenCL::Queue this) |
… | |
… | |
766 | } |
706 | } |
767 | |
707 | |
768 | void |
708 | void |
769 | kernel (OpenCL::Program program, SV *function) |
709 | kernel (OpenCL::Program program, SV *function) |
770 | PPCODE: |
710 | PPCODE: |
771 | { |
|
|
772 | cl_int res; |
|
|
773 | cl_kernel kernel = clCreateKernel (program, SvPVbyte_nolen (function), &res); |
711 | NEED_SUCCESS_ARG (cl_kernel kernel, CreateKernel, (program, SvPVbyte_nolen (function), &res)); |
774 | |
|
|
775 | if (res) |
|
|
776 | FAIL (CreateKernel, res); |
|
|
777 | |
|
|
778 | XPUSH_NEW_OBJ ("OpenCL::Kernel", kernel); |
712 | XPUSH_NEW_OBJ ("OpenCL::Kernel", kernel); |
779 | } |
|
|
780 | |
713 | |
781 | MODULE = OpenCL PACKAGE = OpenCL::Kernel |
714 | MODULE = OpenCL PACKAGE = OpenCL::Kernel |
782 | |
715 | |
783 | void |
716 | void |
784 | DESTROY (OpenCL::Kernel this) |
717 | DESTROY (OpenCL::Kernel this) |