1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
|
|
4 | |
|
|
5 | #include "ecb.h"//D |
4 | |
6 | |
5 | #define X_STACKSIZE sizeof (void *) * 512 * 1024 // 2-4mb should be enough, really |
7 | #define X_STACKSIZE sizeof (void *) * 512 * 1024 // 2-4mb should be enough, really |
6 | #include "xthread.h" |
8 | #include "xthread.h" |
7 | #include "schmorp.h" |
9 | #include "schmorp.h" |
8 | |
10 | |
… | |
… | |
38 | #define PREFER_1_1 1 |
40 | #define PREFER_1_1 1 |
39 | #endif |
41 | #endif |
40 | |
42 | |
41 | typedef cl_platform_id OpenCL__Platform; |
43 | typedef cl_platform_id OpenCL__Platform; |
42 | typedef cl_device_id OpenCL__Device; |
44 | typedef cl_device_id OpenCL__Device; |
|
|
45 | typedef cl_device_id OpenCL__SubDevice; |
43 | typedef cl_context OpenCL__Context; |
46 | typedef cl_context OpenCL__Context; |
44 | typedef cl_command_queue OpenCL__Queue; |
47 | typedef cl_command_queue OpenCL__Queue; |
45 | typedef cl_mem OpenCL__Memory; |
48 | typedef cl_mem OpenCL__Memory; |
46 | typedef cl_mem OpenCL__Buffer; |
49 | typedef cl_mem OpenCL__Buffer; |
47 | typedef cl_mem OpenCL__BufferObj; |
50 | typedef cl_mem OpenCL__BufferObj; |
48 | typedef cl_mem OpenCL__Image; |
51 | typedef cl_mem OpenCL__Image; |
49 | typedef cl_mem OpenCL__Image2D; |
|
|
50 | typedef cl_mem OpenCL__Image3D; |
|
|
51 | typedef cl_mem OpenCL__Memory_ornull; |
52 | typedef cl_mem OpenCL__Memory_ornull; |
52 | typedef cl_mem OpenCL__Buffer_ornull; |
53 | typedef cl_mem OpenCL__Buffer_ornull; |
53 | typedef cl_mem OpenCL__Image_ornull; |
54 | typedef cl_mem OpenCL__Image_ornull; |
54 | typedef cl_mem OpenCL__Image2D_ornull; |
|
|
55 | typedef cl_mem OpenCL__Image3D_ornull; |
|
|
56 | typedef cl_sampler OpenCL__Sampler; |
55 | typedef cl_sampler OpenCL__Sampler; |
57 | typedef cl_program OpenCL__Program; |
56 | typedef cl_program OpenCL__Program; |
58 | typedef cl_kernel OpenCL__Kernel; |
57 | typedef cl_kernel OpenCL__Kernel; |
59 | typedef cl_event OpenCL__Event; |
58 | typedef cl_event OpenCL__Event; |
60 | typedef cl_event OpenCL__UserEvent; |
59 | typedef cl_event OpenCL__UserEvent; |
… | |
… | |
64 | typedef SV *FUTURE; |
63 | typedef SV *FUTURE; |
65 | |
64 | |
66 | static HV |
65 | static HV |
67 | *stash_platform, |
66 | *stash_platform, |
68 | *stash_device, |
67 | *stash_device, |
|
|
68 | *stash_subdevice, |
69 | *stash_context, |
69 | *stash_context, |
70 | *stash_queue, |
70 | *stash_queue, |
71 | *stash_program, |
71 | *stash_program, |
72 | *stash_kernel, |
72 | *stash_kernel, |
73 | *stash_sampler, |
73 | *stash_sampler, |
… | |
… | |
133 | } |
133 | } |
134 | |
134 | |
135 | /*****************************************************************************/ |
135 | /*****************************************************************************/ |
136 | |
136 | |
137 | /* up to two temporary buffers */ |
137 | /* up to two temporary buffers */ |
138 | static void * |
138 | static void * ecb_noinline |
139 | tmpbuf (size_t size) |
139 | tmpbuf (size_t size) |
140 | { |
140 | { |
141 | enum { buffers = 3 }; |
141 | enum { buffers = 4 }; |
142 | static int idx; |
142 | static int idx; |
143 | static void *buf [buffers]; |
143 | static void *buf [buffers]; |
144 | static size_t len [buffers]; |
144 | static size_t len [buffers]; |
145 | |
145 | |
146 | idx = (idx + 1) % buffers; |
146 | idx = (idx + 1) % buffers; |
… | |
… | |
219 | if (res) \ |
219 | if (res) \ |
220 | FAIL (name); |
220 | FAIL (name); |
221 | |
221 | |
222 | /*****************************************************************************/ |
222 | /*****************************************************************************/ |
223 | |
223 | |
|
|
224 | static SV * |
|
|
225 | new_clobj (HV *stash, IV id) |
|
|
226 | { |
|
|
227 | return sv_2mortal (sv_bless (newRV_noinc (newSViv (id)), stash)); |
|
|
228 | } |
|
|
229 | |
|
|
230 | #define PUSH_CLOBJ(stash,id) PUSHs (new_clobj ((stash), (IV)(id))) |
|
|
231 | #define XPUSH_CLOBJ(stash,id) XPUSHs (new_clobj ((stash), (IV)(id))) |
|
|
232 | |
|
|
233 | /* cl objects are either \$iv, or [$iv, ...] */ |
|
|
234 | /* they can be upgraded at runtime to the array form */ |
|
|
235 | static void * ecb_noinline |
|
|
236 | SvCLOBJ (const char *func, const char *svname, SV *sv, const char *pkg) |
|
|
237 | { |
|
|
238 | // sv_derived_from is quite slow :( |
|
|
239 | if (SvROK (sv) && sv_derived_from (sv, pkg)) |
|
|
240 | return (void *)SvIV (SvRV (sv)); |
|
|
241 | |
|
|
242 | croak ("%s: %s is not of type %s", func, svname, pkg); |
|
|
243 | } |
|
|
244 | |
|
|
245 | // the "no-inherit" version of the above |
|
|
246 | static void * ecb_noinline |
|
|
247 | SvCLOBJ_ni (const char *func, const char *svname, SV *sv, HV *stash) |
|
|
248 | { |
|
|
249 | if (SvROK (sv) && SvSTASH (SvRV (sv)) == stash) |
|
|
250 | return (void *)SvIV (SvRV (sv)); |
|
|
251 | |
|
|
252 | croak ("%s: %s is not of type %s", func, svname, HvNAME (stash)); |
|
|
253 | } |
|
|
254 | |
|
|
255 | /*****************************************************************************/ |
|
|
256 | |
224 | static cl_context_properties * |
257 | static cl_context_properties * ecb_noinline |
225 | SvCONTEXTPROPERTIES (const char *func, const char *svname, SV *sv, cl_context_properties *extra, int extracount) |
258 | SvCONTEXTPROPERTIES (const char *func, const char *svname, SV *sv, cl_context_properties *extra, int extracount) |
226 | { |
259 | { |
227 | if (!sv || !SvOK (sv)) |
260 | if (!sv || !SvOK (sv)) |
228 | if (extra) |
261 | if (extra) |
229 | sv = sv_2mortal (newRV_noinc ((SV *)newAV ())); // slow, but rarely used hopefully |
262 | sv = sv_2mortal (newRV_noinc ((SV *)newAV ())); // slow, but rarely used hopefully |
… | |
… | |
236 | int i, len = av_len (av) + 1; |
269 | int i, len = av_len (av) + 1; |
237 | cl_context_properties *p = tmpbuf (sizeof (cl_context_properties) * (len + extracount + 1)); |
270 | cl_context_properties *p = tmpbuf (sizeof (cl_context_properties) * (len + extracount + 1)); |
238 | cl_context_properties *l = p; |
271 | cl_context_properties *l = p; |
239 | |
272 | |
240 | if (len & 1) |
273 | if (len & 1) |
241 | croak ("%s: %s is not a property list (must be even number of elements)", func, svname); |
274 | croak ("%s: %s is not a property list (must contain an even number of elements)", func, svname); |
242 | |
275 | |
243 | while (extracount--) |
276 | while (extracount--) |
244 | *l++ = *extra++; |
277 | *l++ = *extra++; |
245 | |
278 | |
246 | for (i = 0; i < len; i += 2) |
279 | for (i = 0; i < len; i += 2) |
… | |
… | |
249 | SV *p_sv = *av_fetch (av, i + 1, 0); |
282 | SV *p_sv = *av_fetch (av, i + 1, 0); |
250 | cl_context_properties v = SvIV (p_sv); // code below can override |
283 | cl_context_properties v = SvIV (p_sv); // code below can override |
251 | |
284 | |
252 | switch (t) |
285 | switch (t) |
253 | { |
286 | { |
|
|
287 | case CL_CONTEXT_PLATFORM: |
|
|
288 | if (SvROK (p_sv)) |
|
|
289 | v = (cl_context_properties)SvCLOBJ (func, svname, p_sv, "OpenCL::Platform"); |
|
|
290 | break; |
|
|
291 | |
254 | case CL_GLX_DISPLAY_KHR: |
292 | case CL_GLX_DISPLAY_KHR: |
255 | if (!SvOK (p_sv)) |
293 | if (!SvOK (p_sv)) |
256 | { |
294 | { |
257 | void *func = glsym ("_glXGetCurrentDisplay"); |
295 | void *func = glsym ("_glXGetCurrentDisplay"); |
258 | if (func) |
296 | if (func) |
… | |
… | |
285 | |
323 | |
286 | croak ("%s: %s is not a property list (either undef or [type => value, ...])", func, svname); |
324 | croak ("%s: %s is not a property list (either undef or [type => value, ...])", func, svname); |
287 | } |
325 | } |
288 | |
326 | |
289 | /*****************************************************************************/ |
327 | /*****************************************************************************/ |
290 | |
|
|
291 | static SV * |
|
|
292 | new_clobj (HV *stash, IV id) |
|
|
293 | { |
|
|
294 | return sv_2mortal (sv_bless (newRV_noinc (newSViv (id)), stash)); |
|
|
295 | } |
|
|
296 | |
|
|
297 | #define PUSH_CLOBJ(stash,id) PUSHs (new_clobj ((stash), (IV)(id))) |
|
|
298 | #define XPUSH_CLOBJ(stash,id) XPUSHs (new_clobj ((stash), (IV)(id))) |
|
|
299 | |
|
|
300 | /* cl objects are either \$iv, or [$iv, ...] */ |
|
|
301 | /* they can be upgraded at runtime to the array form */ |
|
|
302 | static void * |
|
|
303 | SvCLOBJ (const char *func, const char *svname, SV *sv, const char *pkg) |
|
|
304 | { |
|
|
305 | if (SvROK (sv) && sv_derived_from (sv, pkg)) |
|
|
306 | return (void *)SvIV (SvRV (sv)); |
|
|
307 | |
|
|
308 | croak ("%s: %s is not of type %s", func, svname, pkg); |
|
|
309 | } |
|
|
310 | |
|
|
311 | /*****************************************************************************/ |
|
|
312 | /* callback stuff */ |
328 | /* callback stuff */ |
313 | |
329 | |
314 | /* default context callback, log to stderr */ |
330 | /* default context callback, log to stderr */ |
315 | static void CL_CALLBACK |
331 | static void CL_CALLBACK |
316 | context_default_notify (const char *msg, const void *info, size_t cb, void *data) |
332 | context_default_notify (const char *msg, const void *info, size_t cb, void *data) |
… | |
… | |
335 | static void (*eq_signal_func)(void *signal_arg, int value); |
351 | static void (*eq_signal_func)(void *signal_arg, int value); |
336 | static void *eq_signal_arg; |
352 | static void *eq_signal_arg; |
337 | static xmutex_t eq_lock = X_MUTEX_INIT; |
353 | static xmutex_t eq_lock = X_MUTEX_INIT; |
338 | static eq_item *eq_head, *eq_tail; |
354 | static eq_item *eq_head, *eq_tail; |
339 | |
355 | |
340 | static void |
356 | static void ecb_noinline |
341 | eq_enq (eq_vtbl *vtbl, SV *cb, void *data1, void *data2, void *data3) |
357 | eq_enq (eq_vtbl *vtbl, SV *cb, void *data1, void *data2, void *data3) |
342 | { |
358 | { |
343 | eq_item *item = malloc (sizeof (eq_item)); |
359 | eq_item *item = malloc (sizeof (eq_item)); |
344 | |
360 | |
345 | item->next = 0; |
361 | item->next = 0; |
… | |
… | |
412 | } |
428 | } |
413 | |
429 | |
414 | /*****************************************************************************/ |
430 | /*****************************************************************************/ |
415 | /* context notify */ |
431 | /* context notify */ |
416 | |
432 | |
417 | static void |
433 | static void ecb_noinline |
418 | eq_context_push (void *data1, void *data2, void *data3) |
434 | eq_context_push (void *data1, void *data2, void *data3) |
419 | { |
435 | { |
420 | dSP; |
436 | dSP; |
421 | PUSHs (sv_2mortal (newSVpv (data1, 0))); |
437 | PUSHs (sv_2mortal (newSVpv (data1, 0))); |
422 | PUSHs (sv_2mortal (newSVpvn (data2, (STRLEN)data3))); |
438 | PUSHs (sv_2mortal (newSVpvn (data2, (STRLEN)data3))); |
… | |
… | |
444 | { \ |
460 | { \ |
445 | pfn_notify = eq_context_notify; \ |
461 | pfn_notify = eq_context_notify; \ |
446 | user_data = s_get_cv (notify); \ |
462 | user_data = s_get_cv (notify); \ |
447 | } |
463 | } |
448 | |
464 | |
449 | static SV * |
465 | static SV * ecb_noinline |
450 | new_clobj_context (cl_context ctx, void *user_data) |
466 | new_clobj_context (cl_context ctx, void *user_data) |
451 | { |
467 | { |
452 | SV *sv = new_clobj (stash_context, (IV)ctx); |
468 | SV *sv = new_clobj (stash_context, (IV)ctx); |
453 | |
469 | |
454 | if (user_data) |
470 | if (user_data) |
… | |
… | |
497 | else |
513 | else |
498 | clReleaseProgram (arg->program); |
514 | clReleaseProgram (arg->program); |
499 | |
515 | |
500 | free (arg->options); |
516 | free (arg->options); |
501 | free (arg); |
517 | free (arg); |
|
|
518 | |
|
|
519 | return 0; |
502 | } |
520 | } |
503 | |
521 | |
504 | static void |
522 | static void |
505 | build_program_async (cl_program program, cl_uint num_devices, const cl_device_id *device_list, const char *options, void *user_data) |
523 | build_program_async (cl_program program, cl_uint num_devices, const cl_device_id *device_list, const char *options, void *user_data) |
506 | { |
524 | { |
… | |
… | |
546 | size_t res; |
564 | size_t res; |
547 | clGetImageInfo (img, CL_IMAGE_ROW_PITCH, sizeof (res), &res, 0); |
565 | clGetImageInfo (img, CL_IMAGE_ROW_PITCH, sizeof (res), &res, 0); |
548 | return res; |
566 | return res; |
549 | } |
567 | } |
550 | |
568 | |
551 | static cl_event * |
569 | static cl_event * ecb_noinline |
552 | event_list (SV **items, cl_uint *rcount, cl_event extra) |
570 | event_list (SV **items, cl_uint *rcount, cl_event extra) |
553 | { |
571 | { |
554 | cl_uint count = *rcount; |
572 | cl_uint count = *rcount; |
555 | |
573 | |
556 | if (count > 0x7fffffff) // yeha, it's a hack - the caller might have underflowed |
574 | if (count > 0x7fffffffU) // yeah, it's a hack - the caller might have underflowed |
557 | count = 0; |
575 | *rcount = count = 0; |
558 | |
576 | |
559 | if (!count && !extra) |
577 | if (!count && !extra) |
560 | return 0; |
578 | return 0; |
561 | |
579 | |
562 | cl_event *list = tmpbuf (sizeof (cl_event) * (count + 1)); |
580 | cl_event *list = tmpbuf (sizeof (cl_event) * (count + 1)); |
… | |
… | |
719 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
737 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
720 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
738 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
721 | |
739 | |
722 | stash_platform = gv_stashpv ("OpenCL::Platform", GV_ADD); |
740 | stash_platform = gv_stashpv ("OpenCL::Platform", GV_ADD); |
723 | stash_device = gv_stashpv ("OpenCL::Device", GV_ADD); |
741 | stash_device = gv_stashpv ("OpenCL::Device", GV_ADD); |
|
|
742 | stash_subdevice = gv_stashpv ("OpenCL::SubDevice", GV_ADD); |
724 | stash_context = gv_stashpv ("OpenCL::Context", GV_ADD); |
743 | stash_context = gv_stashpv ("OpenCL::Context", GV_ADD); |
725 | stash_queue = gv_stashpv ("OpenCL::Queue", GV_ADD); |
744 | stash_queue = gv_stashpv ("OpenCL::Queue", GV_ADD); |
726 | stash_program = gv_stashpv ("OpenCL::Program", GV_ADD); |
745 | stash_program = gv_stashpv ("OpenCL::Program", GV_ADD); |
727 | stash_kernel = gv_stashpv ("OpenCL::Kernel", GV_ADD); |
746 | stash_kernel = gv_stashpv ("OpenCL::Kernel", GV_ADD); |
728 | stash_sampler = gv_stashpv ("OpenCL::Sampler", GV_ADD); |
747 | stash_sampler = gv_stashpv ("OpenCL::Sampler", GV_ADD); |
… | |
… | |
769 | list = tmpbuf (sizeof (*list) * count); |
788 | list = tmpbuf (sizeof (*list) * count); |
770 | NEED_SUCCESS (GetPlatformIDs, (count, list, 0)); |
789 | NEED_SUCCESS (GetPlatformIDs, (count, list, 0)); |
771 | |
790 | |
772 | EXTEND (SP, count); |
791 | EXTEND (SP, count); |
773 | for (i = 0; i < count; ++i) |
792 | for (i = 0; i < count; ++i) |
774 | PUSH_CLOBJ (stash_platform, (IV)list [i]); |
793 | PUSH_CLOBJ (stash_platform, list [i]); |
775 | |
794 | |
776 | void |
795 | void |
777 | context_from_type (cl_context_properties *properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, SV *notify = &PL_sv_undef) |
796 | context_from_type (cl_context_properties *properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, SV *notify = &PL_sv_undef) |
778 | PPCODE: |
797 | PPCODE: |
779 | CONTEXT_NOTIFY_CALLBACK; |
798 | CONTEXT_NOTIFY_CALLBACK; |
780 | NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (properties, type, 0, 0, &res)); |
799 | NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (properties, type, pfn_notify, user_data, &res)); |
781 | XPUSH_CLOBJ_CONTEXT; |
800 | XPUSH_CLOBJ_CONTEXT; |
782 | |
801 | |
783 | void |
802 | void |
784 | context (FUTURE properties, FUTURE devices, FUTURE notify) |
803 | context (FUTURE properties, FUTURE devices, FUTURE notify) |
785 | PPCODE: |
804 | PPCODE: |
… | |
… | |
842 | EXTEND (SP, count); |
861 | EXTEND (SP, count); |
843 | for (i = 0; i < count; ++i) |
862 | for (i = 0; i < count; ++i) |
844 | PUSH_CLOBJ (stash_device, list [i]); |
863 | PUSH_CLOBJ (stash_device, list [i]); |
845 | |
864 | |
846 | void |
865 | void |
847 | context (OpenCL::Platform self, cl_context_properties *properties, SV *devices, SV *notify = &PL_sv_undef) |
866 | context (OpenCL::Platform self, SV *properties = 0, SV *devices, SV *notify = &PL_sv_undef) |
848 | PPCODE: |
867 | PPCODE: |
849 | if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV) |
868 | if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV) |
850 | croak ("OpenCL::Platform::context argument 'device' must be an arrayref with device objects, in call"); |
869 | croak ("OpenCL::Platform::context argument 'device' must be an arrayref with device objects, in call"); |
|
|
870 | |
|
|
871 | cl_context_properties extra[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)self }; |
|
|
872 | cl_context_properties *props = SvCONTEXTPROPERTIES ("OpenCL::Platform::context", "properties", properties, extra, 2); |
851 | |
873 | |
852 | AV *av = (AV *)SvRV (devices); |
874 | AV *av = (AV *)SvRV (devices); |
853 | cl_uint num_devices = av_len (av) + 1; |
875 | cl_uint num_devices = av_len (av) + 1; |
854 | cl_device_id *device_list = tmpbuf (sizeof (cl_device_id) * num_devices); |
876 | cl_device_id *device_list = tmpbuf (sizeof (cl_device_id) * num_devices); |
855 | |
877 | |
856 | int i; |
878 | int i; |
857 | for (i = num_devices; i--; ) |
879 | for (i = num_devices; i--; ) |
858 | device_list [i] = SvCLOBJ ("clCreateContext", "devices", *av_fetch (av, i, 0), "OpenCL::Device"); |
880 | device_list [i] = SvCLOBJ ("clCreateContext", "devices", *av_fetch (av, i, 0), "OpenCL::Device"); |
859 | |
881 | |
860 | CONTEXT_NOTIFY_CALLBACK; |
882 | CONTEXT_NOTIFY_CALLBACK; |
861 | NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (properties, num_devices, device_list, pfn_notify, user_data, &res)); |
883 | NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (props, num_devices, device_list, pfn_notify, user_data, &res)); |
862 | XPUSH_CLOBJ_CONTEXT; |
884 | XPUSH_CLOBJ_CONTEXT; |
863 | |
885 | |
864 | void |
886 | void |
865 | context_from_type (OpenCL::Platform self, SV *properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, SV *notify = &PL_sv_undef) |
887 | context_from_type (OpenCL::Platform self, SV *properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, SV *notify = &PL_sv_undef) |
866 | PPCODE: |
888 | PPCODE: |
867 | cl_context_properties extra[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)self }; |
889 | cl_context_properties extra[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)self }; |
868 | cl_context_properties *props = SvCONTEXTPROPERTIES ("OpenCL::Platform::context_from_type", "properties", properties, extra, 2); |
890 | cl_context_properties *props = SvCONTEXTPROPERTIES ("OpenCL::Platform::context_from_type", "properties", properties, extra, 2); |
869 | |
891 | |
870 | CONTEXT_NOTIFY_CALLBACK; |
892 | CONTEXT_NOTIFY_CALLBACK; |
871 | NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (props, type, 0, 0, &res)); |
893 | NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (props, type, pfn_notify, user_data, &res)); |
872 | XPUSH_CLOBJ_CONTEXT; |
894 | XPUSH_CLOBJ_CONTEXT; |
873 | |
895 | |
874 | MODULE = OpenCL PACKAGE = OpenCL::Device |
896 | MODULE = OpenCL PACKAGE = OpenCL::Device |
875 | |
897 | |
876 | void |
898 | void |
877 | info (OpenCL::Device self, cl_device_info name) |
899 | info (OpenCL::Device self, cl_device_info name) |
878 | PPCODE: |
900 | PPCODE: |
879 | INFO (Device) |
901 | INFO (Device) |
|
|
902 | |
|
|
903 | #if CL_VERSION_1_2 |
|
|
904 | |
|
|
905 | void |
|
|
906 | sub_devices (OpenCL::Device self, SV *properties) |
|
|
907 | PPCODE: |
|
|
908 | if (!SvROK (properties) || SvTYPE (SvRV (properties)) != SVt_PVAV) |
|
|
909 | croak ("OpenCL::Device::sub_devices: properties must be specified as reference to an array of property-value pairs"); |
|
|
910 | |
|
|
911 | properties = SvRV (properties); |
|
|
912 | |
|
|
913 | cl_uint count = av_len ((AV *)properties) + 1; |
|
|
914 | cl_device_partition_property *props = tmpbuf (sizeof (*props) * count + 1); |
|
|
915 | |
|
|
916 | int i; |
|
|
917 | for (i = 0; i < count; ++i) |
|
|
918 | props [i] = (cl_device_partition_property)SvIV (*av_fetch ((AV *)properties, i, 0)); |
|
|
919 | |
|
|
920 | props [count] = 0; |
|
|
921 | |
|
|
922 | cl_uint num_devices; |
|
|
923 | NEED_SUCCESS (CreateSubDevices, (self, props, 0, 0, &num_devices)); |
|
|
924 | cl_device_id *list = tmpbuf (sizeof (*list) * num_devices); |
|
|
925 | NEED_SUCCESS (CreateSubDevices, (self, props, num_devices, list, 0)); |
|
|
926 | |
|
|
927 | EXTEND (SP, num_devices); |
|
|
928 | for (i = 0; i < count; ++i) |
|
|
929 | PUSH_CLOBJ (stash_subdevice, list [i]); |
|
|
930 | |
|
|
931 | #endif |
880 | |
932 | |
881 | #BEGIN:device |
933 | #BEGIN:device |
882 | |
934 | |
883 | void |
935 | void |
884 | type (OpenCL::Device self) |
936 | type (OpenCL::Device self) |
… | |
… | |
1100 | for (i = 0; i < n; ++i) |
1152 | for (i = 0; i < n; ++i) |
1101 | PUSHs (sv_2mortal (newSVuv (value [i]))); |
1153 | PUSHs (sv_2mortal (newSVuv (value [i]))); |
1102 | |
1154 | |
1103 | #END:device |
1155 | #END:device |
1104 | |
1156 | |
|
|
1157 | MODULE = OpenCL PACKAGE = OpenCL::SubDevice |
|
|
1158 | |
|
|
1159 | #if CL_VERSION_1_2 |
|
|
1160 | |
|
|
1161 | void |
|
|
1162 | DESTROY (OpenCL::SubDevice self) |
|
|
1163 | CODE: |
|
|
1164 | clReleaseDevice (self); |
|
|
1165 | |
|
|
1166 | #endif |
|
|
1167 | |
1105 | MODULE = OpenCL PACKAGE = OpenCL::Context |
1168 | MODULE = OpenCL PACKAGE = OpenCL::Context |
1106 | |
1169 | |
1107 | void |
1170 | void |
1108 | DESTROY (OpenCL::Context context) |
1171 | DESTROY (OpenCL::Context self) |
1109 | CODE: |
1172 | CODE: |
1110 | clReleaseContext (context); |
1173 | clReleaseContext (self); |
1111 | |
1174 | |
1112 | void |
1175 | void |
1113 | info (OpenCL::Context self, cl_context_info name) |
1176 | info (OpenCL::Context self, cl_context_info name) |
1114 | PPCODE: |
1177 | PPCODE: |
1115 | INFO (Context) |
1178 | INFO (Context) |
… | |
… | |
1299 | |
1362 | |
1300 | len2 = len; |
1363 | len2 = len; |
1301 | NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithSource, (self, 1, &ptr, &len2, &res)); |
1364 | NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithSource, (self, 1, &ptr, &len2, &res)); |
1302 | XPUSH_CLOBJ (stash_program, prog); |
1365 | XPUSH_CLOBJ (stash_program, prog); |
1303 | |
1366 | |
|
|
1367 | void |
|
|
1368 | program_with_binary (OpenCL::Context self, SV *devices, SV *binaries) |
|
|
1369 | PPCODE: |
|
|
1370 | if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV) |
|
|
1371 | croak ("OpenCL::Context::program_with_binary: devices must be specified as reference to an array of device objects"); |
|
|
1372 | |
|
|
1373 | devices = SvRV (devices); |
|
|
1374 | |
|
|
1375 | if (!SvROK (binaries) || SvTYPE (SvRV (binaries)) != SVt_PVAV) |
|
|
1376 | croak ("OpenCL::Context::program_with_binary: binaries must be specified as reference to an array of strings"); |
|
|
1377 | |
|
|
1378 | binaries = SvRV (binaries); |
|
|
1379 | |
|
|
1380 | if (av_len ((AV *)devices) != av_len ((AV *)binaries)) |
|
|
1381 | croak ("OpenCL::Context::program_with_binary: differing numbers of devices and binaries are not allowed"); |
|
|
1382 | |
|
|
1383 | int count = av_len ((AV *)devices) + 1; |
|
|
1384 | cl_device_id *device_list = tmpbuf (sizeof (*device_list) * count); |
|
|
1385 | size_t *length_list = tmpbuf (sizeof (*length_list) * count); |
|
|
1386 | const unsigned char **binary_list = tmpbuf (sizeof (*binary_list) * count); |
|
|
1387 | cl_int *status_list = tmpbuf (sizeof (*status_list) * count); |
|
|
1388 | |
|
|
1389 | int i; |
|
|
1390 | for (i = 0; i < count; ++i) |
|
|
1391 | { |
|
|
1392 | device_list [i] = SvCLOBJ ("OpenCL::Context::program_with_binary", "devices", *av_fetch ((AV *)devices, i, 0), "OpenCL::Device"); |
|
|
1393 | STRLEN len; |
|
|
1394 | binary_list [i] = (const unsigned char *)SvPVbyte (*av_fetch ((AV *)binaries, i, 0), len); |
|
|
1395 | length_list [i] = len; |
|
|
1396 | } |
|
|
1397 | |
|
|
1398 | NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithBinary, (self, count, device_list, length_list, binary_list, GIMME_V == G_ARRAY ? status_list : 0, &res)); |
|
|
1399 | |
|
|
1400 | EXTEND (SP, 2); |
|
|
1401 | PUSH_CLOBJ (stash_program, prog); |
|
|
1402 | |
|
|
1403 | if (GIMME_V == G_ARRAY) |
|
|
1404 | { |
|
|
1405 | AV *av = newAV (); |
|
|
1406 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
1407 | |
|
|
1408 | for (i = count; i--; ) |
|
|
1409 | av_store (av, i, newSViv (status_list [i])); |
|
|
1410 | } |
|
|
1411 | |
|
|
1412 | #if CL_VERSION_1_2 |
|
|
1413 | |
|
|
1414 | void |
|
|
1415 | program_with_built_in_kernels (OpenCL::Context self, SV *devices, SV *kernel_names) |
|
|
1416 | PPCODE: |
|
|
1417 | if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV) |
|
|
1418 | croak ("OpenCL::Context::program_with_built_in_kernels: devices must be specified as reference to an array of device objects"); |
|
|
1419 | |
|
|
1420 | devices = SvRV (devices); |
|
|
1421 | |
|
|
1422 | int count = av_len ((AV *)devices) + 1; |
|
|
1423 | cl_device_id *device_list = tmpbuf (sizeof (*device_list) * count); |
|
|
1424 | |
|
|
1425 | int i; |
|
|
1426 | for (i = 0; i < count; ++i) |
|
|
1427 | device_list [i] = SvCLOBJ ("OpenCL::Context::program_with_built_in_kernels", "devices", *av_fetch ((AV *)devices, i, 0), "OpenCL::Device"); |
|
|
1428 | |
|
|
1429 | NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithBuiltInKernels, (self, count, device_list, SvPVbyte_nolen (kernel_names), &res)); |
|
|
1430 | |
|
|
1431 | XPUSH_CLOBJ (stash_program, prog); |
|
|
1432 | |
|
|
1433 | #endif |
|
|
1434 | |
1304 | #BEGIN:context |
1435 | #BEGIN:context |
1305 | |
1436 | |
1306 | void |
1437 | void |
1307 | reference_count (OpenCL::Context self) |
1438 | reference_count (OpenCL::Context self) |
1308 | ALIAS: |
1439 | ALIAS: |
… | |
… | |
1403 | fill_image (OpenCL::Queue self, OpenCL::Image img, NV r, NV g, NV b, NV a, size_t x, size_t y, size_t z, size_t width, size_t height, size_t depth, ...) |
1534 | fill_image (OpenCL::Queue self, OpenCL::Image img, NV r, NV g, NV b, NV a, size_t x, size_t y, size_t z, size_t width, size_t height, size_t depth, ...) |
1404 | ALIAS: |
1535 | ALIAS: |
1405 | enqueue_fill_image = 0 |
1536 | enqueue_fill_image = 0 |
1406 | PPCODE: |
1537 | PPCODE: |
1407 | cl_event ev = 0; |
1538 | cl_event ev = 0; |
1408 | STRLEN len; |
|
|
1409 | const size_t origin [3] = { x, y, z }; |
1539 | const size_t origin [3] = { x, y, z }; |
1410 | const size_t region [3] = { width, height, depth }; |
1540 | const size_t region [3] = { width, height, depth }; |
1411 | EVENT_LIST (12); |
1541 | EVENT_LIST (12); |
1412 | |
1542 | |
1413 | const cl_float c_f [4] = { r, g, b, a }; |
1543 | const cl_float c_f [4] = { r, g, b, a }; |
… | |
… | |
1627 | |
1757 | |
1628 | if (ev) |
1758 | if (ev) |
1629 | XPUSH_CLOBJ (stash_event, ev); |
1759 | XPUSH_CLOBJ (stash_event, ev); |
1630 | |
1760 | |
1631 | void |
1761 | void |
1632 | map_buffer (OpenCL::Queue self, OpenCL::Buffer buf, cl_bool blocking = 1, cl_map_flags map_flags = CL_MAP_READ | CL_MAP_WRITE, size_t offset = 0, size_t cb = 0, ...) |
1762 | map_buffer (OpenCL::Queue self, OpenCL::Buffer buf, cl_bool blocking = 1, cl_map_flags map_flags = CL_MAP_READ | CL_MAP_WRITE, size_t offset = 0, SV *cb_ = &PL_sv_undef, ...) |
1633 | ALIAS: |
1763 | ALIAS: |
1634 | enqueue_map_buffer = 0 |
1764 | enqueue_map_buffer = 0 |
1635 | PPCODE: |
1765 | PPCODE: |
1636 | cl_event ev; |
1766 | cl_event ev; |
1637 | EVENT_LIST (6); |
1767 | EVENT_LIST (6); |
|
|
1768 | size_t cb = SvIV (cb_); |
|
|
1769 | |
|
|
1770 | if (!SvOK (cb_)) |
|
|
1771 | { |
|
|
1772 | NEED_SUCCESS (GetMemObjectInfo, (buf, CL_MEM_SIZE, sizeof (cb), &cb, 0)); |
|
|
1773 | cb -= offset; |
|
|
1774 | } |
1638 | |
1775 | |
1639 | NEED_SUCCESS_ARG (void *ptr, EnqueueMapBuffer, (self, buf, blocking, map_flags, offset, cb, event_list_count, event_list_ptr, &ev, &res)); |
1776 | NEED_SUCCESS_ARG (void *ptr, EnqueueMapBuffer, (self, buf, blocking, map_flags, offset, cb, event_list_count, event_list_ptr, &ev, &res)); |
1640 | XPUSHs (mapped_new (stash_mappedbuffer, self, buf, map_flags, ptr, cb, ev, 0, 0)); |
1777 | XPUSHs (mapped_new (stash_mappedbuffer, self, buf, map_flags, ptr, cb, ev, 0, 0)); |
1641 | |
1778 | |
1642 | void |
1779 | void |
1643 | map_image (OpenCL::Queue self, OpenCL::Image img, cl_bool blocking = 1, cl_map_flags map_flags = CL_MAP_READ | CL_MAP_WRITE, size_t x = 0, size_t y = 0, size_t z = 0, size_t width = 0, size_t height = 0, size_t depth = 0, ...) |
1780 | map_image (OpenCL::Queue self, OpenCL::Image img, cl_bool blocking = 1, cl_map_flags map_flags = CL_MAP_READ | CL_MAP_WRITE, size_t x = 0, size_t y = 0, size_t z = 0, SV *width_ = &PL_sv_undef, SV *height_ = &PL_sv_undef, SV *depth_ = &PL_sv_undef, ...) |
1644 | ALIAS: |
1781 | ALIAS: |
1645 | enqueue_map_image = 0 |
1782 | enqueue_map_image = 0 |
1646 | PPCODE: |
1783 | PPCODE: |
1647 | cl_event ev; |
1784 | size_t width = SvIV (width_); |
|
|
1785 | if (!SvOK (width_)) |
|
|
1786 | { |
|
|
1787 | NEED_SUCCESS (GetImageInfo, (img, CL_IMAGE_WIDTH, sizeof (width), &width, 0)); |
|
|
1788 | width -= x; |
|
|
1789 | } |
|
|
1790 | |
|
|
1791 | size_t height = SvIV (width_); |
|
|
1792 | if (!SvOK (height_)) |
|
|
1793 | { |
|
|
1794 | NEED_SUCCESS (GetImageInfo, (img, CL_IMAGE_HEIGHT, sizeof (height), &height, 0)); |
|
|
1795 | height -= y; |
|
|
1796 | } |
|
|
1797 | |
|
|
1798 | size_t depth = SvIV (width_); |
|
|
1799 | if (!SvOK (depth_)) |
|
|
1800 | { |
|
|
1801 | NEED_SUCCESS (GetImageInfo, (img, CL_IMAGE_DEPTH, sizeof (depth), &depth, 0)); |
|
|
1802 | depth -= z; |
|
|
1803 | |
|
|
1804 | // stupid opencl returns 0 for depth, but requires 1 for 2d images |
|
|
1805 | if (!depth) |
|
|
1806 | depth = 1; |
|
|
1807 | } |
|
|
1808 | |
1648 | const size_t origin[3] = { x, y, z }; |
1809 | const size_t origin[3] = { x, y, z }; |
1649 | const size_t region[3] = { width, height, depth }; |
1810 | const size_t region[3] = { width, height, depth }; |
1650 | size_t row_pitch, slice_pitch; |
1811 | size_t row_pitch, slice_pitch; |
1651 | EVENT_LIST (10); |
1812 | EVENT_LIST (10); |
1652 | |
1813 | |
|
|
1814 | cl_event ev; |
1653 | NEED_SUCCESS_ARG (void *ptr, EnqueueMapImage, (self, img, blocking, map_flags, origin, region, &row_pitch, &slice_pitch, event_list_count, event_list_ptr, &ev, &res)); |
1815 | NEED_SUCCESS_ARG (void *ptr, EnqueueMapImage, (self, img, blocking, map_flags, origin, region, &row_pitch, &slice_pitch, event_list_count, event_list_ptr, &ev, &res)); |
1654 | |
1816 | |
1655 | size_t cb = slice_pitch ? slice_pitch * region [2] |
1817 | size_t cb = slice_pitch ? slice_pitch * region [2] |
1656 | : row_pitch ? row_pitch * region [1] |
1818 | : row_pitch ? row_pitch * region [1] |
1657 | : region [0]; |
1819 | : region [0]; |
… | |
… | |
1743 | NEED_SUCCESS (EnqueueNDRangeKernel, (self, kernel, gws_len, gwo, gws, lws, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); |
1905 | NEED_SUCCESS (EnqueueNDRangeKernel, (self, kernel, gws_len, gwo, gws, lws, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); |
1744 | |
1906 | |
1745 | if (ev) |
1907 | if (ev) |
1746 | XPUSH_CLOBJ (stash_event, ev); |
1908 | XPUSH_CLOBJ (stash_event, ev); |
1747 | |
1909 | |
|
|
1910 | #if CL_VERSION_1_2 |
|
|
1911 | |
|
|
1912 | void |
|
|
1913 | migrate_mem_objects (OpenCL::Queue self, SV *objects, cl_mem_migration_flags flags, ...) |
|
|
1914 | ALIAS: |
|
|
1915 | enqueue_migrate_mem_objects = 0 |
|
|
1916 | PPCODE: |
|
|
1917 | EVENT_LIST (3); |
|
|
1918 | |
|
|
1919 | if (!SvROK (objects) || SvTYPE (SvRV (objects)) != SVt_PVAV) |
|
|
1920 | croak ("OpenCL::Queue::migrate_mem_objects: objects must be an array reference with OpenCL::Memory objects"); |
|
|
1921 | |
|
|
1922 | objects = SvRV (objects); |
|
|
1923 | |
|
|
1924 | cl_uint object_count = av_len ((AV *)objects) + 1; |
|
|
1925 | cl_mem *object_list = tmpbuf (sizeof (*object_list) * object_count); |
|
|
1926 | |
|
|
1927 | int i; |
|
|
1928 | for (i = object_count; i--; ) |
|
|
1929 | object_list [i] = SvCLOBJ ("OpenCL::Queue::migrate_mem_objects", "objects", *av_fetch ((AV *)objects, i, 0), "OpenCL::Memory"); |
|
|
1930 | |
|
|
1931 | cl_event ev = 0; |
|
|
1932 | NEED_SUCCESS (EnqueueMigrateMemObjects, (self, object_count, object_list, flags, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); |
|
|
1933 | |
|
|
1934 | if (ev) |
|
|
1935 | XPUSH_CLOBJ (stash_event, ev); |
|
|
1936 | |
|
|
1937 | #endif |
|
|
1938 | |
1748 | #if cl_apple_gl_sharing || cl_khr_gl_sharing |
1939 | #if cl_apple_gl_sharing || cl_khr_gl_sharing |
1749 | |
1940 | |
1750 | void |
1941 | void |
1751 | acquire_gl_objects (OpenCL::Queue self, SV *objects, ...) |
1942 | acquire_gl_objects (OpenCL::Queue self, SV *objects, ...) |
1752 | ALIAS: |
1943 | ALIAS: |
… | |
… | |
1760 | cl_event ev = 0; |
1951 | cl_event ev = 0; |
1761 | EVENT_LIST (2); |
1952 | EVENT_LIST (2); |
1762 | AV *av = (AV *)SvRV (objects); |
1953 | AV *av = (AV *)SvRV (objects); |
1763 | cl_uint num_objects = av_len (av) + 1; |
1954 | cl_uint num_objects = av_len (av) + 1; |
1764 | cl_mem *object_list = tmpbuf (sizeof (cl_mem) * num_objects); |
1955 | cl_mem *object_list = tmpbuf (sizeof (cl_mem) * num_objects); |
|
|
1956 | |
1765 | int i; |
1957 | int i; |
1766 | |
|
|
1767 | for (i = num_objects; i--; ) |
1958 | for (i = num_objects; i--; ) |
1768 | object_list [i] = SvCLOBJ ("OpenCL::Queue::enqueue_acquire/release_gl_objects", "objects", *av_fetch (av, i, 0), "OpenCL::Memory"); |
1959 | object_list [i] = SvCLOBJ ("OpenCL::Queue::acquire/release_gl_objects", "objects", *av_fetch (av, i, 0), "OpenCL::Memory"); |
1769 | |
1960 | |
1770 | if (ix) |
1961 | if (ix) |
1771 | NEED_SUCCESS (EnqueueReleaseGLObjects, (self, num_objects, object_list, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); |
1962 | NEED_SUCCESS (EnqueueReleaseGLObjects, (self, num_objects, object_list, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); |
1772 | else |
1963 | else |
1773 | NEED_SUCCESS (EnqueueAcquireGLObjects, (self, num_objects, object_list, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); |
1964 | NEED_SUCCESS (EnqueueAcquireGLObjects, (self, num_objects, object_list, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); |
… | |
… | |
2614 | const int i = 0; |
2805 | const int i = 0; |
2615 | PUSHs (sv_2mortal (newSVuv (value [i]))); |
2806 | PUSHs (sv_2mortal (newSVuv (value [i]))); |
2616 | |
2807 | |
2617 | #END:kernel_work_group |
2808 | #END:kernel_work_group |
2618 | |
2809 | |
|
|
2810 | #if CL_VERSION_1_2 |
|
|
2811 | |
|
|
2812 | void |
|
|
2813 | arg_info (OpenCL::Kernel self, cl_uint idx, cl_kernel_arg_info name) |
|
|
2814 | PPCODE: |
|
|
2815 | size_t size; |
|
|
2816 | NEED_SUCCESS (GetKernelArgInfo, (self, name, 0, 0, &size)); |
|
|
2817 | SV *sv = sv_2mortal (newSV (size)); |
|
|
2818 | SvUPGRADE (sv, SVt_PV); |
|
|
2819 | SvPOK_only (sv); |
|
|
2820 | SvCUR_set (sv, size); |
|
|
2821 | NEED_SUCCESS (GetKernelArgInfo, (self, name, size, SvPVX (sv), 0)); |
|
|
2822 | XPUSHs (sv); |
|
|
2823 | |
|
|
2824 | #BEGIN:kernel_arg |
|
|
2825 | |
|
|
2826 | #END:kernel_arg |
|
|
2827 | |
|
|
2828 | #endif |
|
|
2829 | |
2619 | MODULE = OpenCL PACKAGE = OpenCL::Event |
2830 | MODULE = OpenCL PACKAGE = OpenCL::Event |
2620 | |
2831 | |
2621 | void |
2832 | void |
2622 | DESTROY (OpenCL::Event self) |
2833 | DESTROY (OpenCL::Event self) |
2623 | CODE: |
2834 | CODE: |
… | |
… | |
2776 | set (OpenCL::Mapped self, size_t offset, SV *data) |
2987 | set (OpenCL::Mapped self, size_t offset, SV *data) |
2777 | CODE: |
2988 | CODE: |
2778 | STRLEN len; |
2989 | STRLEN len; |
2779 | const char *ptr = SvPVbyte (data, len); |
2990 | const char *ptr = SvPVbyte (data, len); |
2780 | |
2991 | |
2781 | if (offset < 0 || offset + len > self->cb) |
2992 | if (offset + len > self->cb) |
2782 | croak ("OpenCL::Mapped::set out of bound condition detected"); |
2993 | croak ("OpenCL::Mapped::set out of bound condition detected"); |
2783 | |
2994 | |
2784 | memcpy (offset + (char *)self->ptr, ptr, len); |
2995 | memcpy (offset + (char *)self->ptr, ptr, len); |
2785 | |
2996 | |
2786 | MODULE = OpenCL PACKAGE = OpenCL::MappedBuffer |
2997 | MODULE = OpenCL PACKAGE = OpenCL::MappedBuffer |