ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/OpenCL/OpenCL.xs
(Generate patch)

Comparing OpenCL/OpenCL.xs (file contents):
Revision 1.69 by root, Sat May 5 02:30:27 2012 UTC vs.
Revision 1.78 by root, Sat May 5 15:11:19 2012 UTC

28#if PREFER_1_1 28#if PREFER_1_1
29 #define CL_USE_DEPRECATED_OPENCL_1_1_APIS 29 #define CL_USE_DEPRECATED_OPENCL_1_1_APIS
30#endif 30#endif
31 31
32#ifdef __APPLE__ 32#ifdef __APPLE__
33 #include <OpenCL/opencl.h> 33 #define CLHDR(name) <OpenCL/name>
34#else 34#else
35 #define CLHDR(name) <CL/name>
36#endif
37
35 #include <CL/opencl.h> 38#include CLHDR(opencl.h)
39
40#ifndef CL_VERSION_1_2
41 #include CLHDR(cl_d3d9.h)
42#endif
43
44#if _WIN32
45 #include CLHDR(cl_d3d10.h)
46 #if CL_VERSION_1_2
47 #include CLHDR<cl_d3d11.h>
48 #endif
49 #include CLHDR<cl_dx9_media_sharing.h.h>
36#endif 50#endif
37 51
38#ifndef CL_VERSION_1_2 52#ifndef CL_VERSION_1_2
39 #undef PREFER_1_1 53 #undef PREFER_1_1
40 #define PREFER_1_1 1 54 #define PREFER_1_1 1
41#endif 55#endif
56
57// make sure all constants we might use are actually defined
58#include "default.h"
42 59
43typedef cl_platform_id OpenCL__Platform; 60typedef cl_platform_id OpenCL__Platform;
44typedef cl_device_id OpenCL__Device; 61typedef cl_device_id OpenCL__Device;
45typedef cl_device_id OpenCL__SubDevice; 62typedef cl_device_id OpenCL__SubDevice;
46typedef cl_context OpenCL__Context; 63typedef cl_context OpenCL__Context;
57typedef cl_kernel OpenCL__Kernel; 74typedef cl_kernel OpenCL__Kernel;
58typedef cl_event OpenCL__Event; 75typedef cl_event OpenCL__Event;
59typedef cl_event OpenCL__UserEvent; 76typedef cl_event OpenCL__UserEvent;
60 77
61typedef struct mapped * OpenCL__Mapped; 78typedef struct mapped * OpenCL__Mapped;
62
63typedef SV *FUTURE;
64 79
65static HV 80static HV
66 *stash_platform, 81 *stash_platform,
67 *stash_device, 82 *stash_device,
68 *stash_subdevice, 83 *stash_subdevice,
177 IV iv; 192 IV iv;
178 const char *name; 193 const char *name;
179 #define const_iv(name) { (IV)CL_ ## name, # name }, 194 #define const_iv(name) { (IV)CL_ ## name, # name },
180} ivstr; 195} ivstr;
181 196
197typedef struct
198{
199 NV nv;
200 const char *name;
201 #define const_nv(name) { (NV)CL_ ## name, # name },
202} nvstr;
203
182static const char * 204static const char *
183iv2str (IV value, const ivstr *base, int count, const char *fallback) 205iv2str (IV value, const ivstr *base, int count, const char *fallback)
184{ 206{
185 int i; 207 int i;
186 static char strbuf [32]; 208 static char strbuf [32];
246#define XPUSH_CLOBJ(stash,id) XPUSHs (new_clobj ((stash), (IV)(id))) 268#define XPUSH_CLOBJ(stash,id) XPUSHs (new_clobj ((stash), (IV)(id)))
247 269
248/* cl objects are either \$iv, or [$iv, ...] */ 270/* cl objects are either \$iv, or [$iv, ...] */
249/* they can be upgraded at runtime to the array form */ 271/* they can be upgraded at runtime to the array form */
250static void * ecb_noinline 272static void * ecb_noinline
251SvCLOBJ (const char *func, const char *svname, SV *sv, const char *pkg) 273SvCLOBJ (CV *cv, const char *svname, SV *sv, const char *pkg)
252{ 274{
253 // sv_derived_from is quite slow :( 275 // sv_derived_from is quite slow :(
254 if (SvROK (sv) && sv_derived_from (sv, pkg)) 276 if (SvROK (sv) && sv_derived_from (sv, pkg))
255 return (void *)SvIV (SvRV (sv)); 277 return (void *)SvIV (SvRV (sv));
256 278
257 croak ("%s: %s is not of type %s", func, svname, pkg); 279 croak ("%s: %s is not of type %s", cv_get_name (cv), svname, pkg);
258} 280}
259 281
260// the "no-inherit" version of the above 282// the "no-inherit" version of the above
261static void * ecb_noinline 283static void * ecb_noinline
262SvCLOBJ_ni (const char *func, const char *svname, SV *sv, HV *stash) 284SvCLOBJ_ni (CV *cv, const char *svname, SV *sv, HV *stash)
263{ 285{
264 if (SvROK (sv) && SvSTASH (SvRV (sv)) == stash) 286 if (SvROK (sv) && SvSTASH (SvRV (sv)) == stash)
265 return (void *)SvIV (SvRV (sv)); 287 return (void *)SvIV (SvRV (sv));
266 288
267 croak ("%s: %s is not of type %s", func, svname, HvNAME (stash)); 289 croak ("%s: %s is not of type %s", cv_get_name (cv), svname, HvNAME (stash));
268} 290}
269 291
270/*****************************************************************************/ 292/*****************************************************************************/
271 293
272static cl_context_properties * ecb_noinline 294static cl_context_properties * ecb_noinline
273SvCONTEXTPROPERTIES (const char *func, const char *svname, SV *sv, cl_context_properties *extra, int extracount) 295SvCONTEXTPROPERTIES (CV *cv, const char *svname, SV *sv, cl_context_properties *extra, int extracount)
274{ 296{
275 if (!sv || !SvOK (sv)) 297 if (!sv || !SvOK (sv))
276 if (extra) 298 if (extra)
277 sv = sv_2mortal (newRV_noinc ((SV *)newAV ())); // slow, but rarely used hopefully 299 sv = sv_2mortal (newRV_noinc ((SV *)newAV ())); // slow, but rarely used hopefully
278 else 300 else
284 int i, len = av_len (av) + 1; 306 int i, len = av_len (av) + 1;
285 cl_context_properties *p = tmpbuf (sizeof (cl_context_properties) * (len + extracount + 1)); 307 cl_context_properties *p = tmpbuf (sizeof (cl_context_properties) * (len + extracount + 1));
286 cl_context_properties *l = p; 308 cl_context_properties *l = p;
287 309
288 if (len & 1) 310 if (len & 1)
289 croak ("%s: %s is not a property list (must contain an even number of elements)", func, svname); 311 croak ("%s: %s is not a property list (must contain an even number of elements)", cv_get_name (cv), svname);
290 312
291 while (extracount--) 313 while (extracount--)
292 *l++ = *extra++; 314 *l++ = *extra++;
293 315
294 for (i = 0; i < len; i += 2) 316 for (i = 0; i < len; i += 2)
299 321
300 switch (t) 322 switch (t)
301 { 323 {
302 case CL_CONTEXT_PLATFORM: 324 case CL_CONTEXT_PLATFORM:
303 if (SvROK (p_sv)) 325 if (SvROK (p_sv))
304 v = (cl_context_properties)SvCLOBJ (func, svname, p_sv, "OpenCL::Platform"); 326 v = (cl_context_properties)SvCLOBJ (cv, svname, p_sv, "OpenCL::Platform");
305 break; 327 break;
306 328
307 case CL_GLX_DISPLAY_KHR: 329 case CL_GLX_DISPLAY_KHR:
308 if (!SvOK (p_sv)) 330 if (!SvOK (p_sv))
309 { 331 {
334 *l = 0; 356 *l = 0;
335 357
336 return p; 358 return p;
337 } 359 }
338 360
339 croak ("%s: %s is not a property list (either undef or [type => value, ...])", func, svname); 361 croak ("%s: %s is not a property list (either undef or [type => value, ...])", cv_get_name (cv), svname);
340} 362}
341 363
364// parse an array of CLOBJ into a void ** array in C - works only for CLOBJs whose representation
365// is a pointer (and only on well-behaved systems).
342static void * ecb_noinline 366static void * ecb_noinline
343object_list (CV *cv, int or_undef, const char *argname, SV *arg, const char *klass, cl_uint *rcount) 367object_list (CV *cv, int or_undef, const char *argname, SV *arg, const char *klass, cl_uint *rcount)
344{ 368{
345 const char *funcname = cv_get_name (cv);
346
347 if (!SvROK (arg) || SvTYPE (SvRV (arg)) != SVt_PVAV) 369 if (!SvROK (arg) || SvTYPE (SvRV (arg)) != SVt_PVAV)
348 croak ("%s: '%s' parameter must be %sa reference to an array of %s objects", 370 croak ("%s: '%s' parameter must be %sa reference to an array of %s objects",
349 funcname, argname, or_undef ? "undef or " : "", klass); 371 cv_get_name (cv), argname, or_undef ? "undef or " : "", klass);
350 372
351 AV *av = (AV *)SvRV (arg); 373 AV *av = (AV *)SvRV (arg);
352 void **list = 0; 374 void **list = 0;
353 cl_uint count = av_len (av) + 1; 375 cl_uint count = av_len (av) + 1;
354 376
355 if (count) 377 if (count)
356 { 378 {
357 list = tmpbuf (sizeof (*list) * count); 379 list = tmpbuf (sizeof (*list) * count);
358 int i; 380 int i;
359 for (i = 0; i < count; ++i) 381 for (i = 0; i < count; ++i)
360 list [i] = SvCLOBJ (funcname, argname, *av_fetch (av, i, 1), klass); 382 list [i] = SvCLOBJ (cv, argname, *av_fetch (av, i, 1), klass);
361 } 383 }
362 384
363 if (!count && !or_undef) 385 if (!count && !or_undef)
364 croak ("%s: '%s' must contain at least one %s object", 386 croak ("%s: '%s' must contain at least one %s object",
365 funcname, argname, klass); 387 cv_get_name (cv), argname, klass);
366 388
367 *rcount = count; 389 *rcount = count;
368 return (void *)list; 390 return (void *)list;
369} 391}
370 392
596 xthread_t id; 618 xthread_t id;
597 thread_create (&id, build_program_thread, arg); 619 thread_create (&id, build_program_thread, arg);
598} 620}
599 621
600/*****************************************************************************/ 622/*****************************************************************************/
623/* mem object destructor notify */
624
625static void ecb_noinline
626eq_destructor_push (void *data1, void *data2, void *data3)
627{
628}
629
630static eq_vtbl eq_destructor_vtbl = { 0, eq_destructor_push };
631
632static void CL_CALLBACK
633eq_destructor_notify (cl_mem memobj, void *user_data)
634{
635 eq_enq (&eq_destructor_vtbl, (SV *)user_data, (void *)memobj, 0, 0);
636}
637
638/*****************************************************************************/
601/* event objects */ 639/* event objects */
602 640
603static void 641static void
604eq_event_push (void *data1, void *data2, void *data3) 642eq_event_push (void *data1, void *data2, void *data3)
605{ 643{
628 clGetImageInfo (img, CL_IMAGE_ROW_PITCH, sizeof (res), &res, 0); 666 clGetImageInfo (img, CL_IMAGE_ROW_PITCH, sizeof (res), &res, 0);
629 return res; 667 return res;
630} 668}
631 669
632static cl_event * ecb_noinline 670static cl_event * ecb_noinline
633event_list (SV **items, cl_uint *rcount, cl_event extra) 671event_list (CV *cv, SV **items, cl_uint *rcount, cl_event extra)
634{ 672{
635 cl_uint count = *rcount; 673 cl_uint count = *rcount;
636 674
637 if (count > 0x7fffffffU) // yeah, it's a hack - the caller might have underflowed 675 if (count > 0x7fffffffU) // yeah, it's a hack - the caller might have underflowed
638 *rcount = count = 0; 676 *rcount = count = 0;
643 cl_event *list = tmpbuf (sizeof (cl_event) * (count + 1)); 681 cl_event *list = tmpbuf (sizeof (cl_event) * (count + 1));
644 int i = 0; 682 int i = 0;
645 683
646 while (count--) 684 while (count--)
647 if (SvOK (items [count])) 685 if (SvOK (items [count]))
648 list [i++] = SvCLOBJ ("clEnqueue", "wait_events", items [count], "OpenCL::Event"); 686 list [i++] = SvCLOBJ (cv, "wait_events", items [count], "OpenCL::Event");
649 687
650 if (extra) 688 if (extra)
651 list [i++] = extra; 689 list [i++] = extra;
652 690
653 *rcount = i; 691 *rcount = i;
655 return i ? list : 0; 693 return i ? list : 0;
656} 694}
657 695
658#define EVENT_LIST(skip) \ 696#define EVENT_LIST(skip) \
659 cl_uint event_list_count = items - (skip); \ 697 cl_uint event_list_count = items - (skip); \
660 cl_event *event_list_ptr = event_list (&ST (skip), &event_list_count, 0) 698 cl_event *event_list_ptr = event_list (cv, &ST (skip), &event_list_count, 0)
661 699
662#define INFO(class) \ 700#define INFO(class) \
663{ \ 701{ \
664 size_t size; \ 702 size_t size; \
665 NEED_SUCCESS (Get ## class ## Info, (self, name, 0, 0, &size)); \ 703 NEED_SUCCESS (Get ## class ## Info, (self, name, 0, 0, &size)); \
747 785
748 mapped->ptr = 0; 786 mapped->ptr = 0;
749} 787}
750 788
751static void 789static void
752mapped_unmap (SV *self, OpenCL__Mapped mapped, cl_command_queue queue, SV **wait_list, cl_uint event_list_count) 790mapped_unmap (CV *cv, SV *self, OpenCL__Mapped mapped, cl_command_queue queue, SV **wait_list, cl_uint event_list_count)
753{ 791{
754 cl_event *event_list_ptr = event_list (wait_list, &event_list_count, mapped->event); 792 cl_event *event_list_ptr = event_list (cv, wait_list, &event_list_count, mapped->event);
755 cl_event ev; 793 cl_event ev;
756 794
757 NEED_SUCCESS (EnqueueUnmapMemObject, (queue, mapped->memobj, mapped->ptr, event_list_count, event_list_ptr, &ev)); 795 NEED_SUCCESS (EnqueueUnmapMemObject, (queue, mapped->memobj, mapped->ptr, event_list_count, event_list_ptr, &ev));
758 796
759 clReleaseEvent (mapped->event); 797 clReleaseEvent (mapped->event);
780 eq_signal_arg = (void*)arg; 818 eq_signal_arg = (void*)arg;
781 819
782BOOT: 820BOOT:
783{ 821{
784 HV *stash = gv_stashpv ("OpenCL", 1); 822 HV *stash = gv_stashpv ("OpenCL", 1);
823
785 static const ivstr *civ, const_iv[] = { 824 static const ivstr *civ, const_iv[] = {
786 { sizeof (cl_char ), "SIZEOF_CHAR" }, 825 { sizeof (cl_char ), "SIZEOF_CHAR" },
787 { sizeof (cl_uchar ), "SIZEOF_UCHAR" }, 826 { sizeof (cl_uchar ), "SIZEOF_UCHAR" },
788 { sizeof (cl_short ), "SIZEOF_SHORT" }, 827 { sizeof (cl_short ), "SIZEOF_SHORT" },
789 { sizeof (cl_ushort), "SIZEOF_USHORT" }, 828 { sizeof (cl_ushort), "SIZEOF_USHORT" },
792 { sizeof (cl_long ), "SIZEOF_LONG" }, 831 { sizeof (cl_long ), "SIZEOF_LONG" },
793 { sizeof (cl_ulong ), "SIZEOF_ULONG" }, 832 { sizeof (cl_ulong ), "SIZEOF_ULONG" },
794 { sizeof (cl_half ), "SIZEOF_HALF" }, 833 { sizeof (cl_half ), "SIZEOF_HALF" },
795 { sizeof (cl_float ), "SIZEOF_FLOAT" }, 834 { sizeof (cl_float ), "SIZEOF_FLOAT" },
796 { sizeof (cl_double), "SIZEOF_DOUBLE" }, 835 { sizeof (cl_double), "SIZEOF_DOUBLE" },
836 { PREFER_1_1 , "PREFER_1_1" },
797#include "constiv.h" 837#include "constiv.h"
798 }; 838 };
799 839
800 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 840 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
801 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); 841 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
842
843 static const nvstr *cnv, const_nv[] = {
844#include "constnv.h"
845 };
846
847 for (cnv = const_nv + sizeof (const_nv) / sizeof (const_nv [0]); cnv > const_nv; cnv--)
848 newCONSTSUB (stash, (char *)cnv[-1].name, newSVnv (cnv[-1].nv));
849
850 newCONSTSUB (stash, "NAN", newSVnv (CL_NAN)); // CL_NAN might be a function call
802 851
803 stash_platform = gv_stashpv ("OpenCL::Platform", GV_ADD); 852 stash_platform = gv_stashpv ("OpenCL::Platform", GV_ADD);
804 stash_device = gv_stashpv ("OpenCL::Device", GV_ADD); 853 stash_device = gv_stashpv ("OpenCL::Device", GV_ADD);
805 stash_subdevice = gv_stashpv ("OpenCL::SubDevice", GV_ADD); 854 stash_subdevice = gv_stashpv ("OpenCL::SubDevice", GV_ADD);
806 stash_context = gv_stashpv ("OpenCL::Context", GV_ADD); 855 stash_context = gv_stashpv ("OpenCL::Context", GV_ADD);
861 CONTEXT_NOTIFY_CALLBACK; 910 CONTEXT_NOTIFY_CALLBACK;
862 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (properties, type, pfn_notify, user_data, &res)); 911 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (properties, type, pfn_notify, user_data, &res));
863 XPUSH_CLOBJ_CONTEXT; 912 XPUSH_CLOBJ_CONTEXT;
864 913
865void 914void
866context (FUTURE properties, FUTURE devices, FUTURE notify) 915context (cl_context_properties *properties, SV *devices, SV *notify = &PL_sv_undef)
867 PPCODE: 916 PPCODE:
868 /* der Gipfel der Kunst */ 917 cl_uint device_count;
918 cl_device_id *device_list = object_list (cv, 0, "devices", devices, "OpenCL::Device", &device_count);
919
920 CONTEXT_NOTIFY_CALLBACK;
921 NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (properties, device_count, device_list, pfn_notify, user_data, &res));
922 XPUSH_CLOBJ_CONTEXT;
869 923
870void 924void
871wait_for_events (...) 925wait_for_events (...)
872 CODE: 926 CODE:
873 EVENT_LIST (0); 927 EVENT_LIST (0);
924 EXTEND (SP, count); 978 EXTEND (SP, count);
925 for (i = 0; i < count; ++i) 979 for (i = 0; i < count; ++i)
926 PUSH_CLOBJ (stash_device, list [i]); 980 PUSH_CLOBJ (stash_device, list [i]);
927 981
928void 982void
929context (OpenCL::Platform self, SV *properties = 0, SV *devices, SV *notify = &PL_sv_undef) 983context (OpenCL::Platform self, SV *properties, SV *devices, SV *notify = &PL_sv_undef)
930 PPCODE: 984 PPCODE:
931 cl_context_properties extra[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)self }; 985 cl_context_properties extra[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)self };
932 cl_context_properties *props = SvCONTEXTPROPERTIES ("OpenCL::Platform::context", "properties", properties, extra, 2); 986 cl_context_properties *props = SvCONTEXTPROPERTIES (cv, "properties", properties, extra, 2);
933 987
934 cl_uint device_count = 0; 988 cl_uint device_count;
935 cl_device_id *device_list = object_list (cv, 0, "devices", devices, "OpenCL::Device", &device_count); 989 cl_device_id *device_list = object_list (cv, 0, "devices", devices, "OpenCL::Device", &device_count);
936 990
937 CONTEXT_NOTIFY_CALLBACK; 991 CONTEXT_NOTIFY_CALLBACK;
938 NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (props, device_count, device_list, pfn_notify, user_data, &res)); 992 NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (props, device_count, device_list, pfn_notify, user_data, &res));
939 XPUSH_CLOBJ_CONTEXT; 993 XPUSH_CLOBJ_CONTEXT;
940 994
941void 995void
942context_from_type (OpenCL::Platform self, SV *properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, SV *notify = &PL_sv_undef) 996context_from_type (OpenCL::Platform self, SV *properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, SV *notify = &PL_sv_undef)
943 PPCODE: 997 PPCODE:
944 cl_context_properties extra[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)self }; 998 cl_context_properties extra[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)self };
945 cl_context_properties *props = SvCONTEXTPROPERTIES ("OpenCL::Platform::context_from_type", "properties", properties, extra, 2); 999 cl_context_properties *props = SvCONTEXTPROPERTIES (cv, "properties", properties, extra, 2);
946 1000
947 CONTEXT_NOTIFY_CALLBACK; 1001 CONTEXT_NOTIFY_CALLBACK;
948 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (props, type, pfn_notify, user_data, &res)); 1002 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (props, type, pfn_notify, user_data, &res));
949 XPUSH_CLOBJ_CONTEXT; 1003 XPUSH_CLOBJ_CONTEXT;
950 1004
1274 const cl_image_desc desc = { 1328 const cl_image_desc desc = {
1275 type, 1329 type,
1276 width, height, depth, 1330 width, height, depth,
1277 array_size, row_pitch, slice_pitch, 1331 array_size, row_pitch, slice_pitch,
1278 num_mip_level, num_samples, 1332 num_mip_level, num_samples,
1279 type == CL_MEM_OBJECT_IMAGE1D_BUFFER ? (cl_mem)SvCLOBJ ("OpenCL::Context::Image", "data", data, "OpenCL::Buffer") : 0 1333 type == CL_MEM_OBJECT_IMAGE1D_BUFFER ? (cl_mem)SvCLOBJ (cv, "data", data, "OpenCL::Buffer") : 0
1280 }; 1334 };
1281 NEED_SUCCESS_ARG (cl_mem mem, CreateImage, (self, flags, &format, &desc, ptr, &res)); 1335 NEED_SUCCESS_ARG (cl_mem mem, CreateImage, (self, flags, &format, &desc, ptr, &res));
1282 HV *stash = stash_image; 1336 HV *stash = stash_image;
1283 switch (type) 1337 switch (type)
1284 { 1338 {
1420 XPUSH_CLOBJ (stash_program, prog); 1474 XPUSH_CLOBJ (stash_program, prog);
1421 1475
1422void 1476void
1423program_with_binary (OpenCL::Context self, SV *devices, SV *binaries) 1477program_with_binary (OpenCL::Context self, SV *devices, SV *binaries)
1424 PPCODE: 1478 PPCODE:
1425 if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV) 1479 cl_uint device_count;
1426 croak ("OpenCL::Context::program_with_binary: devices must be specified as reference to an array of device objects"); 1480 cl_device_id *device_list = object_list (cv, 0, "devices", devices, "OpenCL::Device", &device_count);
1427
1428 devices = SvRV (devices);
1429 1481
1430 if (!SvROK (binaries) || SvTYPE (SvRV (binaries)) != SVt_PVAV) 1482 if (!SvROK (binaries) || SvTYPE (SvRV (binaries)) != SVt_PVAV)
1431 croak ("OpenCL::Context::program_with_binary: binaries must be specified as reference to an array of strings"); 1483 croak ("OpenCL::Context::program_with_binary: binaries must be specified as reference to an array of strings");
1432 1484
1433 binaries = SvRV (binaries); 1485 binaries = SvRV (binaries);
1434 1486
1435 if (av_len ((AV *)devices) != av_len ((AV *)binaries)) 1487 if (device_count != av_len ((AV *)binaries) + 1)
1436 croak ("OpenCL::Context::program_with_binary: differing numbers of devices and binaries are not allowed"); 1488 croak ("OpenCL::Context::program_with_binary: differing numbers of devices and binaries are not allowed");
1437 1489
1438 int count = av_len ((AV *)devices) + 1;
1439 cl_device_id *device_list = tmpbuf (sizeof (*device_list) * count);
1440 size_t *length_list = tmpbuf (sizeof (*length_list) * count); 1490 size_t *length_list = tmpbuf (sizeof (*length_list) * device_count);
1441 const unsigned char **binary_list = tmpbuf (sizeof (*binary_list) * count); 1491 const unsigned char **binary_list = tmpbuf (sizeof (*binary_list) * device_count);
1442 cl_int *status_list = tmpbuf (sizeof (*status_list) * count); 1492 cl_int *status_list = tmpbuf (sizeof (*status_list) * device_count);
1443 1493
1444 int i; 1494 int i;
1445 for (i = 0; i < count; ++i) 1495 for (i = 0; i < device_count; ++i)
1446 { 1496 {
1447 device_list [i] = SvCLOBJ ("OpenCL::Context::program_with_binary", "devices", *av_fetch ((AV *)devices, i, 0), "OpenCL::Device");
1448 STRLEN len; 1497 STRLEN len;
1449 binary_list [i] = (const unsigned char *)SvPVbyte (*av_fetch ((AV *)binaries, i, 0), len); 1498 binary_list [i] = (const unsigned char *)SvPVbyte (*av_fetch ((AV *)binaries, i, 0), len);
1450 length_list [i] = len; 1499 length_list [i] = len;
1451 } 1500 }
1452 1501
1453 NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithBinary, (self, count, device_list, length_list, binary_list, GIMME_V == G_ARRAY ? status_list : 0, &res)); 1502 NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithBinary, (self, device_count, device_list,
1503 length_list, binary_list,
1504 GIMME_V == G_ARRAY ? status_list : 0, &res));
1454 1505
1455 EXTEND (SP, 2); 1506 EXTEND (SP, 2);
1456 PUSH_CLOBJ (stash_program, prog); 1507 PUSH_CLOBJ (stash_program, prog);
1457 1508
1458 if (GIMME_V == G_ARRAY) 1509 if (GIMME_V == G_ARRAY)
1459 { 1510 {
1460 AV *av = newAV (); 1511 AV *av = newAV ();
1461 PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); 1512 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
1462 1513
1463 for (i = count; i--; ) 1514 for (i = device_count; i--; )
1464 av_store (av, i, newSViv (status_list [i])); 1515 av_store (av, i, newSViv (status_list [i]));
1465 } 1516 }
1466 1517
1467#if CL_VERSION_1_2 1518#if CL_VERSION_1_2
1468 1519
1469void 1520void
1470program_with_built_in_kernels (OpenCL::Context self, SV *devices, SV *kernel_names) 1521program_with_built_in_kernels (OpenCL::Context self, SV *devices, SV *kernel_names)
1471 PPCODE: 1522 PPCODE:
1472 cl_uint device_count = 0; 1523 cl_uint device_count;
1473 cl_device_id *device_list = object_list (cv, 0, "devices", devices, "OpenCL::Device", &device_count); 1524 cl_device_id *device_list = object_list (cv, 0, "devices", devices, "OpenCL::Device", &device_count);
1474 1525
1475 NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithBuiltInKernels, (self, device_count, device_list, SvPVbyte_nolen (kernel_names), &res)); 1526 NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithBuiltInKernels, (self, device_count, device_list, SvPVbyte_nolen (kernel_names), &res));
1476 1527
1477 XPUSH_CLOBJ (stash_program, prog); 1528 XPUSH_CLOBJ (stash_program, prog);
1478 1529
1902 XPUSHs (mapped_new (stash_mappedimage, self, img, map_flags, ptr, cb, ev, row_pitch, slice_pitch)); 1953 XPUSHs (mapped_new (stash_mappedimage, self, img, map_flags, ptr, cb, ev, row_pitch, slice_pitch));
1903 1954
1904void 1955void
1905unmap (OpenCL::Queue self, OpenCL::Mapped mapped, ...) 1956unmap (OpenCL::Queue self, OpenCL::Mapped mapped, ...)
1906 PPCODE: 1957 PPCODE:
1907 mapped_unmap (ST (1), mapped, self, &ST (2), items - 2); 1958 mapped_unmap (cv, ST (1), mapped, self, &ST (2), items - 2);
1908 if (GIMME_V != G_VOID) 1959 if (GIMME_V != G_VOID)
1909 { 1960 {
1910 clRetainEvent (mapped->event); 1961 clRetainEvent (mapped->event);
1911 XPUSH_CLOBJ (stash_event, mapped->event); 1962 XPUSH_CLOBJ (stash_event, mapped->event);
1912 } 1963 }
2170void 2221void
2171info (OpenCL::Memory self, cl_mem_info name) 2222info (OpenCL::Memory self, cl_mem_info name)
2172 PPCODE: 2223 PPCODE:
2173 INFO (MemObject) 2224 INFO (MemObject)
2174 2225
2226void
2227destructor_callback (OpenCL::Memory self, SV *notify)
2228 PPCODE:
2229 clSetMemObjectDestructorCallback (self, eq_destructor_notify, SvREFCNT_inc (s_get_cv (notify)));
2230
2175#BEGIN:mem 2231#BEGIN:mem
2176 2232
2177void 2233void
2178type (OpenCL::Memory self) 2234type (OpenCL::Memory self)
2179 PPCODE: 2235 PPCODE:
2459 HE *he; 2515 HE *he;
2460 int i = 0; 2516 int i = 0;
2461 while (he = hv_iternext (hv)) 2517 while (he = hv_iternext (hv))
2462 { 2518 {
2463 header_name [i] = SvPVbyte_nolen (HeSVKEY_force (he)); 2519 header_name [i] = SvPVbyte_nolen (HeSVKEY_force (he));
2464 header_list [i] = SvCLOBJ ("clCompileProgram", "headers", HeVAL (he), "OpenCL::Program"); 2520 header_list [i] = SvCLOBJ (cv, "headers", HeVAL (he), "OpenCL::Program");
2465 ++i; 2521 ++i;
2466 } 2522 }
2467 } 2523 }
2468 2524
2469 void *user_data; 2525 void *user_data;
2508 char *value = tmpbuf (size); 2564 char *value = tmpbuf (size);
2509 NEED_SUCCESS (GetProgramBuildInfo, (self, device, ix, size, value, 0)); 2565 NEED_SUCCESS (GetProgramBuildInfo, (self, device, ix, size, value, 0));
2510 EXTEND (SP, 1); 2566 EXTEND (SP, 1);
2511 const int i = 0; 2567 const int i = 0;
2512 PUSHs (sv_2mortal (newSVpv (value, 0))); 2568 PUSHs (sv_2mortal (newSVpv (value, 0)));
2569
2570void
2571binary_type (OpenCL::Program self, OpenCL::Device device)
2572 PPCODE:
2573 cl_program_binary_type value [1];
2574 NEED_SUCCESS (GetProgramBuildInfo, (self, device, CL_PROGRAM_BINARY_TYPE, sizeof (value), value, 0));
2575 EXTEND (SP, 1);
2576 const int i = 0;
2577 PUSHs (sv_2mortal (newSVuv ((UV)value [i])));
2513 2578
2514#END:program_build 2579#END:program_build
2515 2580
2516void 2581void
2517kernel (OpenCL::Program program, SV *function) 2582kernel (OpenCL::Program program, SV *function)
2684 case 'f': arg.cf = SvNV (sv); size = sizeof (arg.cf); break; 2749 case 'f': arg.cf = SvNV (sv); size = sizeof (arg.cf); break;
2685 case 'd': arg.cd = SvNV (sv); size = sizeof (arg.cd); break; 2750 case 'd': arg.cd = SvNV (sv); size = sizeof (arg.cd); break;
2686 2751
2687 case 'z': nullarg = 1; size = SvIV (sv); break; 2752 case 'z': nullarg = 1; size = SvIV (sv); break;
2688 2753
2689 case 'm': nullarg = !SvOK (sv); arg.cm = SvCLOBJ ("OpenCL::Kernel::setf", "m", sv, "OpenCL::Memory" ); size = sizeof (arg.cm); break; 2754 case 'm': nullarg = !SvOK (sv); arg.cm = SvCLOBJ (cv, "m", sv, "OpenCL::Memory" ); size = sizeof (arg.cm); break;
2690 case 'a': nullarg = !SvOK (sv); arg.ca = SvCLOBJ ("OpenCL::Kernel::setf", "a", sv, "OpenCL::Sampler"); size = sizeof (arg.ca); break; 2755 case 'a': nullarg = !SvOK (sv); arg.ca = SvCLOBJ (cv, "a", sv, "OpenCL::Sampler"); size = sizeof (arg.ca); break;
2691 case 'e': nullarg = !SvOK (sv); arg.ca = SvCLOBJ ("OpenCL::Kernel::setf", "e", sv, "OpenCL::Event" ); size = sizeof (arg.ce); break; 2756 case 'e': nullarg = !SvOK (sv); arg.ca = SvCLOBJ (cv, "e", sv, "OpenCL::Event" ); size = sizeof (arg.ce); break;
2692 2757
2693 default: 2758 default:
2694 croak ("OpenCL::Kernel::setf format character '%c' not supported", type); 2759 croak ("OpenCL::Kernel::setf format character '%c' not supported", type);
2695 } 2760 }
2696 2761
2767 CODE: 2832 CODE:
2768 clSetKernelArg (self, idx, sizeof (value), value ? &value : 0); 2833 clSetKernelArg (self, idx, sizeof (value), value ? &value : 0);
2769 2834
2770void 2835void
2771set_image (OpenCL::Kernel self, cl_uint idx, OpenCL::Image_ornull value) 2836set_image (OpenCL::Kernel self, cl_uint idx, OpenCL::Image_ornull value)
2772 ALIAS:
2773 set_image2d = 0
2774 set_image3d = 0
2775 CODE: 2837 CODE:
2776 clSetKernelArg (self, idx, sizeof (value), value ? &value : 0); 2838 clSetKernelArg (self, idx, sizeof (value), value ? &value : 0);
2777 2839
2778void 2840void
2779set_sampler (OpenCL::Kernel self, cl_uint idx, OpenCL::Sampler value) 2841set_sampler (OpenCL::Kernel self, cl_uint idx, OpenCL::Sampler value)
3075 Safefree (mapped); 3137 Safefree (mapped);
3076 3138
3077void 3139void
3078unmap (OpenCL::Mapped self, ...) 3140unmap (OpenCL::Mapped self, ...)
3079 CODE: 3141 CODE:
3080 mapped_unmap (ST (0), self, self->queue, &ST (1), items - 1); 3142 mapped_unmap (cv, ST (0), self, self->queue, &ST (1), items - 1);
3081 3143
3082bool 3144bool
3083mapped (OpenCL::Mapped self) 3145mapped (OpenCL::Mapped self)
3084 CODE: 3146 CODE:
3085 RETVAL = !!self->ptr; 3147 RETVAL = !!self->ptr;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines