--- OpenCL/OpenCL.xs 2012/05/03 23:30:08 1.64 +++ OpenCL/OpenCL.xs 2012/05/05 15:11:19 1.78 @@ -30,9 +30,23 @@ #endif #ifdef __APPLE__ - #include + #define CLHDR(name) #else - #include + #define CLHDR(name) +#endif + +#include CLHDR(opencl.h) + +#ifndef CL_VERSION_1_2 + #include CLHDR(cl_d3d9.h) +#endif + +#if _WIN32 + #include CLHDR(cl_d3d10.h) + #if CL_VERSION_1_2 + #include CLHDR + #endif + #include CLHDR #endif #ifndef CL_VERSION_1_2 @@ -40,8 +54,12 @@ #define PREFER_1_1 1 #endif +// make sure all constants we might use are actually defined +#include "default.h" + typedef cl_platform_id OpenCL__Platform; typedef cl_device_id OpenCL__Device; +typedef cl_device_id OpenCL__SubDevice; typedef cl_context OpenCL__Context; typedef cl_command_queue OpenCL__Queue; typedef cl_mem OpenCL__Memory; @@ -59,11 +77,10 @@ typedef struct mapped * OpenCL__Mapped; -typedef SV *FUTURE; - static HV *stash_platform, *stash_device, + *stash_subdevice, *stash_context, *stash_queue, *stash_program, @@ -153,6 +170,21 @@ return buf [idx]; } +static const char * ecb_noinline +cv_get_name (CV *cv) +{ + static char fullname [256]; + + GV *gv = CvGV (cv); // gv better be nonzero + + HV *stash = GvSTASH (gv); + const char *hvname = HvNAME_get (stash); // stash also better be nonzero + const char *gvname = GvNAME (gv); + + snprintf (fullname, sizeof (fullname), "%s::%s", hvname, gvname); + return fullname; +} + /*****************************************************************************/ typedef struct @@ -162,6 +194,13 @@ #define const_iv(name) { (IV)CL_ ## name, # name }, } ivstr; +typedef struct +{ + NV nv; + const char *name; + #define const_nv(name) { (NV)CL_ ## name, # name }, +} nvstr; + static const char * iv2str (IV value, const ivstr *base, int count, const char *fallback) { @@ -219,8 +258,41 @@ /*****************************************************************************/ +static SV * +new_clobj (HV *stash, IV id) +{ + return sv_2mortal (sv_bless (newRV_noinc (newSViv (id)), stash)); +} + +#define PUSH_CLOBJ(stash,id) PUSHs (new_clobj ((stash), (IV)(id))) +#define XPUSH_CLOBJ(stash,id) XPUSHs (new_clobj ((stash), (IV)(id))) + +/* cl objects are either \$iv, or [$iv, ...] */ +/* they can be upgraded at runtime to the array form */ +static void * ecb_noinline +SvCLOBJ (CV *cv, const char *svname, SV *sv, const char *pkg) +{ + // sv_derived_from is quite slow :( + if (SvROK (sv) && sv_derived_from (sv, pkg)) + return (void *)SvIV (SvRV (sv)); + + croak ("%s: %s is not of type %s", cv_get_name (cv), svname, pkg); +} + +// the "no-inherit" version of the above +static void * ecb_noinline +SvCLOBJ_ni (CV *cv, const char *svname, SV *sv, HV *stash) +{ + if (SvROK (sv) && SvSTASH (SvRV (sv)) == stash) + return (void *)SvIV (SvRV (sv)); + + croak ("%s: %s is not of type %s", cv_get_name (cv), svname, HvNAME (stash)); +} + +/*****************************************************************************/ + static cl_context_properties * ecb_noinline -SvCONTEXTPROPERTIES (const char *func, const char *svname, SV *sv, cl_context_properties *extra, int extracount) +SvCONTEXTPROPERTIES (CV *cv, const char *svname, SV *sv, cl_context_properties *extra, int extracount) { if (!sv || !SvOK (sv)) if (extra) @@ -236,7 +308,7 @@ cl_context_properties *l = p; if (len & 1) - croak ("%s: %s is not a property list (must be even number of elements)", func, svname); + croak ("%s: %s is not a property list (must contain an even number of elements)", cv_get_name (cv), svname); while (extracount--) *l++ = *extra++; @@ -249,6 +321,11 @@ switch (t) { + case CL_CONTEXT_PLATFORM: + if (SvROK (p_sv)) + v = (cl_context_properties)SvCLOBJ (cv, svname, p_sv, "OpenCL::Platform"); + break; + case CL_GLX_DISPLAY_KHR: if (!SvOK (p_sv)) { @@ -281,40 +358,36 @@ return p; } - croak ("%s: %s is not a property list (either undef or [type => value, ...])", func, svname); -} - -/*****************************************************************************/ - -static SV * -new_clobj (HV *stash, IV id) -{ - return sv_2mortal (sv_bless (newRV_noinc (newSViv (id)), stash)); + croak ("%s: %s is not a property list (either undef or [type => value, ...])", cv_get_name (cv), svname); } -#define PUSH_CLOBJ(stash,id) PUSHs (new_clobj ((stash), (IV)(id))) -#define XPUSH_CLOBJ(stash,id) XPUSHs (new_clobj ((stash), (IV)(id))) - -/* cl objects are either \$iv, or [$iv, ...] */ -/* they can be upgraded at runtime to the array form */ +// parse an array of CLOBJ into a void ** array in C - works only for CLOBJs whose representation +// is a pointer (and only on well-behaved systems). static void * ecb_noinline -SvCLOBJ (const char *func, const char *svname, SV *sv, const char *pkg) +object_list (CV *cv, int or_undef, const char *argname, SV *arg, const char *klass, cl_uint *rcount) { - // sv_derived_from is quite slow :( - if (SvROK (sv) && sv_derived_from (sv, pkg)) - return (void *)SvIV (SvRV (sv)); + if (!SvROK (arg) || SvTYPE (SvRV (arg)) != SVt_PVAV) + croak ("%s: '%s' parameter must be %sa reference to an array of %s objects", + cv_get_name (cv), argname, or_undef ? "undef or " : "", klass); - croak ("%s: %s is not of type %s", func, svname, pkg); -} + AV *av = (AV *)SvRV (arg); + void **list = 0; + cl_uint count = av_len (av) + 1; -// the "no-inherit" version of the above -static void * ecb_noinline -SvCLOBJ_ni (const char *func, const char *svname, SV *sv, HV *stash) -{ - if (SvROK (sv) && SvSTASH (SvRV (sv)) == stash) - return (void *)SvIV (SvRV (sv)); + if (count) + { + list = tmpbuf (sizeof (*list) * count); + int i; + for (i = 0; i < count; ++i) + list [i] = SvCLOBJ (cv, argname, *av_fetch (av, i, 1), klass); + } - croak ("%s: %s is not of type %s", func, svname, HvNAME (stash)); + if (!count && !or_undef) + croak ("%s: '%s' must contain at least one %s object", + cv_get_name (cv), argname, klass); + + *rcount = count; + return (void *)list; } /*****************************************************************************/ @@ -484,9 +557,28 @@ static void CL_CALLBACK eq_program_notify (cl_program program, void *user_data) { + clRetainProgram (program); + eq_enq (&eq_program_vtbl, user_data, (void *)program, 0, 0); } +typedef void (CL_CALLBACK *program_callback)(cl_program program, void *user_data); + +static program_callback ecb_noinline +make_program_callback (SV *notify, void **ruser_data) +{ + if (SvOK (notify)) + { + *ruser_data = SvREFCNT_inc (s_get_cv (notify)); + return eq_program_notify; + } + else + { + *ruser_data = 0; + return 0; + } +} + struct build_args { cl_program program; @@ -528,6 +620,22 @@ } /*****************************************************************************/ +/* mem object destructor notify */ + +static void ecb_noinline +eq_destructor_push (void *data1, void *data2, void *data3) +{ +} + +static eq_vtbl eq_destructor_vtbl = { 0, eq_destructor_push }; + +static void CL_CALLBACK +eq_destructor_notify (cl_mem memobj, void *user_data) +{ + eq_enq (&eq_destructor_vtbl, (SV *)user_data, (void *)memobj, 0, 0); +} + +/*****************************************************************************/ /* event objects */ static void @@ -560,7 +668,7 @@ } static cl_event * ecb_noinline -event_list (SV **items, cl_uint *rcount, cl_event extra) +event_list (CV *cv, SV **items, cl_uint *rcount, cl_event extra) { cl_uint count = *rcount; @@ -575,7 +683,7 @@ while (count--) if (SvOK (items [count])) - list [i++] = SvCLOBJ ("clEnqueue", "wait_events", items [count], "OpenCL::Event"); + list [i++] = SvCLOBJ (cv, "wait_events", items [count], "OpenCL::Event"); if (extra) list [i++] = extra; @@ -587,7 +695,7 @@ #define EVENT_LIST(skip) \ cl_uint event_list_count = items - (skip); \ - cl_event *event_list_ptr = event_list (&ST (skip), &event_list_count, 0) + cl_event *event_list_ptr = event_list (cv, &ST (skip), &event_list_count, 0) #define INFO(class) \ { \ @@ -679,9 +787,9 @@ } static void -mapped_unmap (SV *self, OpenCL__Mapped mapped, cl_command_queue queue, SV **wait_list, cl_uint event_list_count) +mapped_unmap (CV *cv, SV *self, OpenCL__Mapped mapped, cl_command_queue queue, SV **wait_list, cl_uint event_list_count) { - cl_event *event_list_ptr = event_list (wait_list, &event_list_count, mapped->event); + cl_event *event_list_ptr = event_list (cv, wait_list, &event_list_count, mapped->event); cl_event ev; NEED_SUCCESS (EnqueueUnmapMemObject, (queue, mapped->memobj, mapped->ptr, event_list_count, event_list_ptr, &ev)); @@ -712,6 +820,7 @@ BOOT: { HV *stash = gv_stashpv ("OpenCL", 1); + static const ivstr *civ, const_iv[] = { { sizeof (cl_char ), "SIZEOF_CHAR" }, { sizeof (cl_uchar ), "SIZEOF_UCHAR" }, @@ -724,14 +833,25 @@ { sizeof (cl_half ), "SIZEOF_HALF" }, { sizeof (cl_float ), "SIZEOF_FLOAT" }, { sizeof (cl_double), "SIZEOF_DOUBLE" }, + { PREFER_1_1 , "PREFER_1_1" }, #include "constiv.h" }; for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); + static const nvstr *cnv, const_nv[] = { +#include "constnv.h" + }; + + for (cnv = const_nv + sizeof (const_nv) / sizeof (const_nv [0]); cnv > const_nv; cnv--) + newCONSTSUB (stash, (char *)cnv[-1].name, newSVnv (cnv[-1].nv)); + + newCONSTSUB (stash, "NAN", newSVnv (CL_NAN)); // CL_NAN might be a function call + stash_platform = gv_stashpv ("OpenCL::Platform", GV_ADD); stash_device = gv_stashpv ("OpenCL::Device", GV_ADD); + stash_subdevice = gv_stashpv ("OpenCL::SubDevice", GV_ADD); stash_context = gv_stashpv ("OpenCL::Context", GV_ADD); stash_queue = gv_stashpv ("OpenCL::Queue", GV_ADD); stash_program = gv_stashpv ("OpenCL::Program", GV_ADD); @@ -782,7 +902,7 @@ EXTEND (SP, count); for (i = 0; i < count; ++i) - PUSH_CLOBJ (stash_platform, (IV)list [i]); + PUSH_CLOBJ (stash_platform, list [i]); void context_from_type (cl_context_properties *properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, SV *notify = &PL_sv_undef) @@ -792,9 +912,14 @@ XPUSH_CLOBJ_CONTEXT; void -context (FUTURE properties, FUTURE devices, FUTURE notify) +context (cl_context_properties *properties, SV *devices, SV *notify = &PL_sv_undef) PPCODE: - /* der Gipfel der Kunst */ + cl_uint device_count; + cl_device_id *device_list = object_list (cv, 0, "devices", devices, "OpenCL::Device", &device_count); + + CONTEXT_NOTIFY_CALLBACK; + NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (properties, device_count, device_list, pfn_notify, user_data, &res)); + XPUSH_CLOBJ_CONTEXT; void wait_for_events (...) @@ -855,31 +980,23 @@ PUSH_CLOBJ (stash_device, list [i]); void -context (OpenCL::Platform self, SV *properties = 0, SV *devices, SV *notify = &PL_sv_undef) +context (OpenCL::Platform self, SV *properties, SV *devices, SV *notify = &PL_sv_undef) PPCODE: - if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV) - croak ("OpenCL::Platform::context argument 'device' must be an arrayref with device objects, in call"); - cl_context_properties extra[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)self }; - cl_context_properties *props = SvCONTEXTPROPERTIES ("OpenCL::Platform::context", "properties", properties, extra, 2); + cl_context_properties *props = SvCONTEXTPROPERTIES (cv, "properties", properties, extra, 2); - AV *av = (AV *)SvRV (devices); - cl_uint num_devices = av_len (av) + 1; - cl_device_id *device_list = tmpbuf (sizeof (cl_device_id) * num_devices); - - int i; - for (i = num_devices; i--; ) - device_list [i] = SvCLOBJ ("clCreateContext", "devices", *av_fetch (av, i, 0), "OpenCL::Device"); + cl_uint device_count; + cl_device_id *device_list = object_list (cv, 0, "devices", devices, "OpenCL::Device", &device_count); CONTEXT_NOTIFY_CALLBACK; - NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (props, num_devices, device_list, pfn_notify, user_data, &res)); + NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (props, device_count, device_list, pfn_notify, user_data, &res)); XPUSH_CLOBJ_CONTEXT; void context_from_type (OpenCL::Platform self, SV *properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, SV *notify = &PL_sv_undef) PPCODE: cl_context_properties extra[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)self }; - cl_context_properties *props = SvCONTEXTPROPERTIES ("OpenCL::Platform::context_from_type", "properties", properties, extra, 2); + cl_context_properties *props = SvCONTEXTPROPERTIES (cv, "properties", properties, extra, 2); CONTEXT_NOTIFY_CALLBACK; NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (props, type, pfn_notify, user_data, &res)); @@ -892,6 +1009,36 @@ PPCODE: INFO (Device) +#if CL_VERSION_1_2 + +void +sub_devices (OpenCL::Device self, SV *properties) + PPCODE: + if (!SvROK (properties) || SvTYPE (SvRV (properties)) != SVt_PVAV) + croak ("OpenCL::Device::sub_devices: properties must be specified as reference to an array of property-value pairs"); + + properties = SvRV (properties); + + cl_uint count = av_len ((AV *)properties) + 1; + cl_device_partition_property *props = tmpbuf (sizeof (*props) * count + 1); + + int i; + for (i = 0; i < count; ++i) + props [i] = (cl_device_partition_property)SvIV (*av_fetch ((AV *)properties, i, 0)); + + props [count] = 0; + + cl_uint num_devices; + NEED_SUCCESS (CreateSubDevices, (self, props, 0, 0, &num_devices)); + cl_device_id *list = tmpbuf (sizeof (*list) * num_devices); + NEED_SUCCESS (CreateSubDevices, (self, props, num_devices, list, 0)); + + EXTEND (SP, num_devices); + for (i = 0; i < count; ++i) + PUSH_CLOBJ (stash_subdevice, list [i]); + +#endif + #BEGIN:device void @@ -1116,12 +1263,23 @@ #END:device +MODULE = OpenCL PACKAGE = OpenCL::SubDevice + +#if CL_VERSION_1_2 + +void +DESTROY (OpenCL::SubDevice self) + CODE: + clReleaseDevice (self); + +#endif + MODULE = OpenCL PACKAGE = OpenCL::Context void -DESTROY (OpenCL::Context context) +DESTROY (OpenCL::Context self) CODE: - clReleaseContext (context); + clReleaseContext (self); void info (OpenCL::Context self, cl_context_info name) @@ -1172,7 +1330,7 @@ width, height, depth, array_size, row_pitch, slice_pitch, num_mip_level, num_samples, - type == CL_MEM_OBJECT_IMAGE1D_BUFFER ? (cl_mem)SvCLOBJ ("OpenCL::Context::Image", "data", data, "OpenCL::Buffer") : 0 + type == CL_MEM_OBJECT_IMAGE1D_BUFFER ? (cl_mem)SvCLOBJ (cv, "data", data, "OpenCL::Buffer") : 0 }; NEED_SUCCESS_ARG (cl_mem mem, CreateImage, (self, flags, &format, &desc, ptr, &res)); HV *stash = stash_image; @@ -1318,35 +1476,32 @@ void program_with_binary (OpenCL::Context self, SV *devices, SV *binaries) PPCODE: - if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV) - croak ("OpenCL::Context::program_with_binary: devices must be specified as reference to an array of device objects"); - - devices = SvRV (devices); + cl_uint device_count; + cl_device_id *device_list = object_list (cv, 0, "devices", devices, "OpenCL::Device", &device_count); if (!SvROK (binaries) || SvTYPE (SvRV (binaries)) != SVt_PVAV) croak ("OpenCL::Context::program_with_binary: binaries must be specified as reference to an array of strings"); binaries = SvRV (binaries); - if (av_len ((AV *)devices) != av_len ((AV *)binaries)) + if (device_count != av_len ((AV *)binaries) + 1) croak ("OpenCL::Context::program_with_binary: differing numbers of devices and binaries are not allowed"); - int count = av_len ((AV *)devices) + 1; - cl_device_id *device_list = tmpbuf (sizeof (*device_list) * count); - size_t *length_list = tmpbuf (sizeof (*length_list) * count); - const unsigned char **binary_list = tmpbuf (sizeof (*binary_list) * count); - cl_int *status_list = tmpbuf (sizeof (*status_list) * count); + size_t *length_list = tmpbuf (sizeof (*length_list) * device_count); + const unsigned char **binary_list = tmpbuf (sizeof (*binary_list) * device_count); + cl_int *status_list = tmpbuf (sizeof (*status_list) * device_count); int i; - for (i = 0; i < count; ++i) + for (i = 0; i < device_count; ++i) { - device_list [i] = SvCLOBJ ("OpenCL::Context::program_with_binary", "devices", *av_fetch ((AV *)devices, i, 0), "OpenCL::Device"); STRLEN len; binary_list [i] = (const unsigned char *)SvPVbyte (*av_fetch ((AV *)binaries, i, 0), len); length_list [i] = len; } - NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithBinary, (self, count, device_list, length_list, binary_list, GIMME_V == G_ARRAY ? status_list : 0, &res)); + NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithBinary, (self, device_count, device_list, + length_list, binary_list, + GIMME_V == G_ARRAY ? status_list : 0, &res)); EXTEND (SP, 2); PUSH_CLOBJ (stash_program, prog); @@ -1356,10 +1511,44 @@ AV *av = newAV (); PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); - for (i = count; i--; ) + for (i = device_count; i--; ) av_store (av, i, newSViv (status_list [i])); } +#if CL_VERSION_1_2 + +void +program_with_built_in_kernels (OpenCL::Context self, SV *devices, SV *kernel_names) + PPCODE: + cl_uint device_count; + cl_device_id *device_list = object_list (cv, 0, "devices", devices, "OpenCL::Device", &device_count); + + NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithBuiltInKernels, (self, device_count, device_list, SvPVbyte_nolen (kernel_names), &res)); + + XPUSH_CLOBJ (stash_program, prog); + +void +link_program (OpenCL::Context self, SV *devices, SV *options, SV *programs, SV *notify = &PL_sv_undef) + CODE: + cl_uint device_count = 0; + cl_device_id *device_list = 0; + + if (SvOK (devices)) + device_list = object_list (cv, 1, "devices", devices, "OpenCL::Device", &device_count); + + cl_uint program_count; + cl_program *program_list = object_list (cv, 0, "programs", programs, "OpenCL::Program", &program_count); + + void *user_data; + program_callback pfn_notify = make_program_callback (notify, &user_data); + + NEED_SUCCESS_ARG (cl_program prog, LinkProgram, (self, device_count, device_list, SvPVbyte_nolen (options), + program_count, program_list, pfn_notify, user_data, &res)); + + XPUSH_CLOBJ (stash_program, prog); + +#endif + #BEGIN:context void @@ -1414,13 +1603,14 @@ ALIAS: enqueue_read_buffer = 0 PPCODE: - cl_event ev = 0; EVENT_LIST (6); SvUPGRADE (data, SVt_PV); SvGROW (data, len); SvPOK_only (data); SvCUR_set (data, len); + + cl_event ev = 0; NEED_SUCCESS (EnqueueReadBuffer, (self, mem, blocking, offset, len, SvPVX (data), event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); if (ev) @@ -1431,11 +1621,12 @@ ALIAS: enqueue_write_buffer = 0 PPCODE: - cl_event ev = 0; + EVENT_LIST (5); + STRLEN len; char *ptr = SvPVbyte (data, len); - EVENT_LIST (5); + cl_event ev = 0; NEED_SUCCESS (EnqueueWriteBuffer, (self, mem, blocking, offset, len, ptr, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); if (ev) @@ -1448,11 +1639,12 @@ ALIAS: enqueue_fill_buffer = 0 PPCODE: - cl_event ev = 0; + EVENT_LIST (5); + STRLEN len; char *ptr = SvPVbyte (data, len); - EVENT_LIST (5); + cl_event ev = 0; NEED_SUCCESS (EnqueueFillBuffer, (self, mem, ptr, len, offset, size, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); if (ev) @@ -1463,10 +1655,10 @@ ALIAS: enqueue_fill_image = 0 PPCODE: - cl_event ev = 0; + EVENT_LIST (12); + const size_t origin [3] = { x, y, z }; const size_t region [3] = { width, height, depth }; - EVENT_LIST (12); const cl_float c_f [4] = { r, g, b, a }; const cl_uint c_u [4] = { r, g, b, a }; @@ -1479,6 +1671,7 @@ if (format.image_channel_data_type < CL_SNORM_INT8 || CL_FLOAT < format.image_channel_data_type) croak ("enqueue_fill_image: image has unsupported channel type, only opencl 1.2 channel types supported."); + cl_event ev = 0; NEED_SUCCESS (EnqueueFillImage, (self, img, c_fus [fus [format.image_channel_data_type - CL_SNORM_INT8]], origin, region, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); @@ -1492,9 +1685,9 @@ ALIAS: enqueue_copy_buffer = 0 PPCODE: - cl_event ev = 0; EVENT_LIST (6); + cl_event ev = 0; NEED_SUCCESS (EnqueueCopyBuffer, (self, src, dst, src_offset, dst_offset, len, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); if (ev) @@ -1505,11 +1698,11 @@ ALIAS: enqueue_read_buffer_rect = 0 PPCODE: - cl_event ev = 0; + EVENT_LIST (17); + const size_t buf_origin [3] = { buf_x , buf_y , buf_z }; const size_t host_origin[3] = { host_x, host_y, host_z }; const size_t region[3] = { width, height, depth }; - EVENT_LIST (17); if (!buf_row_pitch) buf_row_pitch = region [0]; @@ -1529,6 +1722,8 @@ SvGROW (data, len); SvPOK_only (data); SvCUR_set (data, len); + + cl_event ev = 0; NEED_SUCCESS (EnqueueReadBufferRect, (self, buf, blocking, buf_origin, host_origin, region, buf_row_pitch, buf_slice_pitch, host_row_pitch, host_slice_pitch, SvPVX (data), event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); if (ev) @@ -1539,13 +1734,13 @@ ALIAS: enqueue_write_buffer_rect = 0 PPCODE: - cl_event ev = 0; + EVENT_LIST (17); + const size_t buf_origin [3] = { buf_x , buf_y , buf_z }; const size_t host_origin[3] = { host_x, host_y, host_z }; const size_t region[3] = { width, height, depth }; STRLEN len; char *ptr = SvPVbyte (data, len); - EVENT_LIST (17); if (!buf_row_pitch) buf_row_pitch = region [0]; @@ -1564,6 +1759,7 @@ if (len < min_len) croak ("clEnqueueWriteImage: data string is shorter than what would be transferred"); + cl_event ev = 0; NEED_SUCCESS (EnqueueWriteBufferRect, (self, buf, blocking, buf_origin, host_origin, region, buf_row_pitch, buf_slice_pitch, host_row_pitch, host_slice_pitch, ptr, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); if (ev) @@ -1574,12 +1770,13 @@ ALIAS: enqueue_copy_buffer_rect = 0 PPCODE: - cl_event ev = 0; + EVENT_LIST (16); + const size_t src_origin[3] = { src_x, src_y, src_z }; const size_t dst_origin[3] = { dst_x, dst_y, dst_z }; const size_t region[3] = { width, height, depth }; - EVENT_LIST (16); + cl_event ev = 0; NEED_SUCCESS (EnqueueCopyBufferRect, (self, src, dst, src_origin, dst_origin, region, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); if (ev) @@ -1590,10 +1787,10 @@ ALIAS: enqueue_read_image = 0 PPCODE: - cl_event ev = 0; + EVENT_LIST (12); + const size_t src_origin[3] = { src_x, src_y, src_z }; const size_t region[3] = { width, height, depth }; - EVENT_LIST (12); if (!row_pitch) row_pitch = img_row_pitch (src); @@ -1607,6 +1804,8 @@ SvGROW (data, len); SvPOK_only (data); SvCUR_set (data, len); + + cl_event ev = 0; NEED_SUCCESS (EnqueueReadImage, (self, src, blocking, src_origin, region, row_pitch, slice_pitch, SvPVX (data), event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); if (ev) @@ -1617,12 +1816,12 @@ ALIAS: enqueue_write_image = 0 PPCODE: - cl_event ev = 0; + EVENT_LIST (12); + const size_t dst_origin[3] = { dst_x, dst_y, dst_z }; const size_t region[3] = { width, height, depth }; STRLEN len; char *ptr = SvPVbyte (data, len); - EVENT_LIST (12); if (!row_pitch) row_pitch = img_row_pitch (dst); @@ -1635,6 +1834,7 @@ if (len < min_len) croak ("clEnqueueWriteImage: data string is shorter than what would be transferred"); + cl_event ev = 0; NEED_SUCCESS (EnqueueWriteImage, (self, dst, blocking, dst_origin, region, row_pitch, slice_pitch, ptr, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); if (ev) @@ -1645,12 +1845,13 @@ ALIAS: enqueue_copy_image = 0 PPCODE: - cl_event ev = 0; + EVENT_LIST (12); + const size_t src_origin[3] = { src_x, src_y, src_z }; const size_t dst_origin[3] = { dst_x, dst_y, dst_z }; const size_t region[3] = { width, height, depth }; - EVENT_LIST (12); + cl_event ev = 0; NEED_SUCCESS (EnqueueCopyImage, (self, src, dst, src_origin, dst_origin, region, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); if (ev) @@ -1661,11 +1862,12 @@ ALIAS: enqueue_copy_image_to_buffer = 0 PPCODE: - cl_event ev = 0; + EVENT_LIST (10); + const size_t src_origin[3] = { src_x, src_y, src_z }; const size_t region [3] = { width, height, depth }; - EVENT_LIST (10); + cl_event ev = 0; NEED_SUCCESS (EnqueueCopyImageToBuffer, (self, src, dst, src_origin, region, dst_offset, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); if (ev) @@ -1676,11 +1878,12 @@ ALIAS: enqueue_copy_buffer_to_image = 0 PPCODE: - cl_event ev = 0; + EVENT_LIST (10); + const size_t dst_origin[3] = { dst_x, dst_y, dst_z }; const size_t region [3] = { width, height, depth }; - EVENT_LIST (10); + cl_event ev = 0; NEED_SUCCESS (EnqueueCopyBufferToImage, (self, src, dst, src_offset, dst_origin, region, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); if (ev) @@ -1691,8 +1894,8 @@ ALIAS: enqueue_map_buffer = 0 PPCODE: - cl_event ev; EVENT_LIST (6); + size_t cb = SvIV (cb_); if (!SvOK (cb_)) @@ -1701,6 +1904,7 @@ cb -= offset; } + cl_event ev; NEED_SUCCESS_ARG (void *ptr, EnqueueMapBuffer, (self, buf, blocking, map_flags, offset, cb, event_list_count, event_list_ptr, &ev, &res)); XPUSHs (mapped_new (stash_mappedbuffer, self, buf, map_flags, ptr, cb, ev, 0, 0)); @@ -1751,7 +1955,7 @@ void unmap (OpenCL::Queue self, OpenCL::Mapped mapped, ...) PPCODE: - mapped_unmap (ST (1), mapped, self, &ST (2), items - 2); + mapped_unmap (cv, ST (1), mapped, self, &ST (2), items - 2); if (GIMME_V != G_VOID) { clRetainEvent (mapped->event); @@ -1763,9 +1967,9 @@ ALIAS: enqueue_task = 0 PPCODE: - cl_event ev = 0; EVENT_LIST (2); + cl_event ev = 0; NEED_SUCCESS (EnqueueTask, (self, kernel, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); if (ev) @@ -1776,12 +1980,12 @@ ALIAS: enqueue_nd_range_kernel = 0 PPCODE: - cl_event ev = 0; + EVENT_LIST (5); + size_t *gwo = 0, *gws, *lws = 0; int gws_len; size_t *lists; int i; - EVENT_LIST (5); if (!SvROK (global_work_size) || SvTYPE (SvRV (global_work_size)) != SVt_PVAV) croak ("clEnqueueNDRangeKernel: global_work_size must be an array reference"); @@ -1830,11 +2034,32 @@ } } + cl_event ev = 0; NEED_SUCCESS (EnqueueNDRangeKernel, (self, kernel, gws_len, gwo, gws, lws, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); if (ev) XPUSH_CLOBJ (stash_event, ev); +#if CL_VERSION_1_2 + +void +migrate_mem_objects (OpenCL::Queue self, SV *objects, cl_mem_migration_flags flags, ...) + ALIAS: + enqueue_migrate_mem_objects = 0 + PPCODE: + EVENT_LIST (3); + + cl_uint obj_count; + cl_mem *obj_list = object_list (cv, 0, "objects", objects, "OpenCL::Memory", &obj_count); + + cl_event ev = 0; + NEED_SUCCESS (EnqueueMigrateMemObjects, (self, obj_count, obj_list, flags, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); + + if (ev) + XPUSH_CLOBJ (stash_event, ev); + +#endif + #if cl_apple_gl_sharing || cl_khr_gl_sharing void @@ -1844,23 +2069,17 @@ enqueue_acquire_gl_objects = 0 enqueue_release_gl_objects = 1 PPCODE: - if (!SvROK (objects) || SvTYPE (SvRV (objects)) != SVt_PVAV) - croak ("OpenCL::Queue::enqueue_acquire/release_gl_objects argument 'objects' must be an arrayref with memory objects, in call"); - - cl_event ev = 0; EVENT_LIST (2); - AV *av = (AV *)SvRV (objects); - cl_uint num_objects = av_len (av) + 1; - cl_mem *object_list = tmpbuf (sizeof (cl_mem) * num_objects); - int i; - for (i = num_objects; i--; ) - object_list [i] = SvCLOBJ ("OpenCL::Queue::enqueue_acquire/release_gl_objects", "objects", *av_fetch (av, i, 0), "OpenCL::Memory"); + cl_uint obj_count; + cl_mem *obj_list = object_list (cv, 0, "objects", objects, "OpenCL::Memory", &obj_count); + + cl_event ev = 0; if (ix) - NEED_SUCCESS (EnqueueReleaseGLObjects, (self, num_objects, object_list, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); + NEED_SUCCESS (EnqueueReleaseGLObjects, (self, obj_count, obj_list, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); else - NEED_SUCCESS (EnqueueAcquireGLObjects, (self, num_objects, object_list, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); + NEED_SUCCESS (EnqueueAcquireGLObjects, (self, obj_count, obj_list, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); if (ev) XPUSH_CLOBJ (stash_event, ev); @@ -1884,8 +2103,8 @@ ALIAS: enqueue_marker = 0 PPCODE: - cl_event ev = 0; EVENT_LIST (1); + cl_event ev = 0; #if PREFER_1_1 if (!event_list_count) NEED_SUCCESS (EnqueueMarker, (self, GIMME_V != G_VOID ? &ev : 0)); @@ -1909,8 +2128,8 @@ ALIAS: enqueue_barrier = 0 PPCODE: - cl_event ev = 0; EVENT_LIST (1); + cl_event ev = 0; #if PREFER_1_1 if (!event_list_count && GIMME_V == G_VOID) NEED_SUCCESS (EnqueueBarrier, (self)); @@ -2004,6 +2223,11 @@ PPCODE: INFO (MemObject) +void +destructor_callback (OpenCL::Memory self, SV *notify) + PPCODE: + clSetMemObjectDestructorCallback (self, eq_destructor_notify, SvREFCNT_inc (s_get_cv (notify))); + #BEGIN:mem void @@ -2248,39 +2472,63 @@ ALIAS: build_async = 1 CODE: - void (CL_CALLBACK *pfn_notify)(cl_program program, void *user_data) = 0; - void *user_data = 0; - cl_uint num_devices = 0; - cl_device_id *device_list = 0; + cl_uint device_count = 0; + cl_device_id *device_list = 0; if (SvOK (devices)) - { - if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV) - croak ("clProgramBuild: devices must be undef or an array of OpenCL::Device objects."); + device_list = object_list (cv, 1, "devices", devices, "OpenCL::Device", &device_count); + + void *user_data; + program_callback pfn_notify = make_program_callback (notify, &user_data); + + if (ix) + build_program_async (self, device_count, device_list, SvPVbyte_nolen (options), user_data); + else + NEED_SUCCESS (BuildProgram, (self, device_count, device_list, SvPVbyte_nolen (options), pfn_notify, user_data)); + +#if CL_VERSION_1_2 + +void +compile (OpenCL::Program self, SV *devices, SV *options = &PL_sv_undef, SV *headers = &PL_sv_undef, SV *notify = &PL_sv_undef) + CODE: + cl_uint device_count = 0; + cl_device_id *device_list = 0; - AV *av = (AV *)SvRV (devices); - num_devices = av_len (av) + 1; + if (SvOK (devices)) + device_list = object_list (cv, 1, "devices", devices, "OpenCL::Device", &device_count); + + cl_uint header_count = 0; + cl_program *header_list = 0; + const char **header_name = 0; + + if (SvOK (headers)) + { + if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVHV) + croak ("clCompileProgram: headers must be undef or a hashref of name => OpenCL::Program pairs"); + + HV *hv = (HV *)SvRV (devices); - if (num_devices) + header_count = hv_iterinit (hv); + header_list = tmpbuf (sizeof (*header_list) * header_count); + header_name = tmpbuf (sizeof (*header_name) * header_count); + + HE *he; + int i = 0; + while (he = hv_iternext (hv)) { - device_list = tmpbuf (sizeof (*device_list) * num_devices); - int count; - for (count = 0; count < num_devices; ++count) - device_list [count] = SvCLOBJ ("clBuildProgram", "devices", *av_fetch (av, count, 1), "OpenCL::Device"); + header_name [i] = SvPVbyte_nolen (HeSVKEY_force (he)); + header_list [i] = SvCLOBJ (cv, "headers", HeVAL (he), "OpenCL::Program"); + ++i; } } - if (SvOK (notify)) - { - NEED_SUCCESS (RetainProgram, (self)); - pfn_notify = eq_program_notify; - user_data = SvREFCNT_inc (s_get_cv (notify)); - } + void *user_data; + program_callback pfn_notify = make_program_callback (notify, &user_data); - if (ix) - build_program_async (self, num_devices, device_list, SvPVbyte_nolen (options), user_data); - else - NEED_SUCCESS (BuildProgram, (self, num_devices, device_list, SvPVbyte_nolen (options), pfn_notify, user_data)); + NEED_SUCCESS (CompileProgram, (self, device_count, device_list, SvPVbyte_nolen (options), + header_count, header_list, header_name, pfn_notify, user_data)); + +#endif void build_info (OpenCL::Program self, OpenCL::Device device, cl_program_build_info name) @@ -2319,6 +2567,15 @@ const int i = 0; PUSHs (sv_2mortal (newSVpv (value, 0))); +void +binary_type (OpenCL::Program self, OpenCL::Device device) + PPCODE: + cl_program_binary_type value [1]; + NEED_SUCCESS (GetProgramBuildInfo, (self, device, CL_PROGRAM_BINARY_TYPE, sizeof (value), value, 0)); + EXTEND (SP, 1); + const int i = 0; + PUSHs (sv_2mortal (newSVuv ((UV)value [i]))); + #END:program_build void @@ -2494,9 +2751,9 @@ case 'z': nullarg = 1; size = SvIV (sv); break; - case 'm': nullarg = !SvOK (sv); arg.cm = SvCLOBJ ("OpenCL::Kernel::setf", "m", sv, "OpenCL::Memory" ); size = sizeof (arg.cm); break; - case 'a': nullarg = !SvOK (sv); arg.ca = SvCLOBJ ("OpenCL::Kernel::setf", "a", sv, "OpenCL::Sampler"); size = sizeof (arg.ca); break; - case 'e': nullarg = !SvOK (sv); arg.ca = SvCLOBJ ("OpenCL::Kernel::setf", "e", sv, "OpenCL::Event" ); size = sizeof (arg.ce); break; + case 'm': nullarg = !SvOK (sv); arg.cm = SvCLOBJ (cv, "m", sv, "OpenCL::Memory" ); size = sizeof (arg.cm); break; + case 'a': nullarg = !SvOK (sv); arg.ca = SvCLOBJ (cv, "a", sv, "OpenCL::Sampler"); size = sizeof (arg.ca); break; + case 'e': nullarg = !SvOK (sv); arg.ca = SvCLOBJ (cv, "e", sv, "OpenCL::Event" ); size = sizeof (arg.ce); break; default: croak ("OpenCL::Kernel::setf format character '%c' not supported", type); @@ -2577,9 +2834,6 @@ void set_image (OpenCL::Kernel self, cl_uint idx, OpenCL::Image_ornull value) - ALIAS: - set_image2d = 0 - set_image3d = 0 CODE: clSetKernelArg (self, idx, sizeof (value), value ? &value : 0); @@ -2706,6 +2960,67 @@ #END:kernel_work_group +#if CL_VERSION_1_2 + +void +arg_info (OpenCL::Kernel self, cl_uint idx, cl_kernel_arg_info name) + PPCODE: + size_t size; + NEED_SUCCESS (GetKernelArgInfo, (self, idx, name, 0, 0, &size)); + SV *sv = sv_2mortal (newSV (size)); + SvUPGRADE (sv, SVt_PV); + SvPOK_only (sv); + SvCUR_set (sv, size); + NEED_SUCCESS (GetKernelArgInfo, (self, idx, name, size, SvPVX (sv), 0)); + XPUSHs (sv); + +#BEGIN:kernel_arg + +void +arg_address_qualifier (OpenCL::Kernel self, cl_uint idx) + PPCODE: + cl_kernel_arg_address_qualifier value [1]; + NEED_SUCCESS (GetKernelArgInfo, (self, idx, CL_KERNEL_ARG_ADDRESS_QUALIFIER, sizeof (value), value, 0)); + EXTEND (SP, 1); + const int i = 0; + PUSHs (sv_2mortal (newSVuv (value [i]))); + +void +arg_access_qualifier (OpenCL::Kernel self, cl_uint idx) + PPCODE: + cl_kernel_arg_access_qualifier value [1]; + NEED_SUCCESS (GetKernelArgInfo, (self, idx, CL_KERNEL_ARG_ACCESS_QUALIFIER, sizeof (value), value, 0)); + EXTEND (SP, 1); + const int i = 0; + PUSHs (sv_2mortal (newSVuv (value [i]))); + +void +arg_type_name (OpenCL::Kernel self, cl_uint idx) + ALIAS: + arg_type_name = CL_KERNEL_ARG_TYPE_NAME + arg_name = CL_KERNEL_ARG_NAME + PPCODE: + size_t size; + NEED_SUCCESS (GetKernelArgInfo, (self, idx, ix, 0, 0, &size)); + char *value = tmpbuf (size); + NEED_SUCCESS (GetKernelArgInfo, (self, idx, ix, size, value, 0)); + EXTEND (SP, 1); + const int i = 0; + PUSHs (sv_2mortal (newSVpv (value, 0))); + +void +arg_type_qualifier (OpenCL::Kernel self, cl_uint idx) + PPCODE: + cl_kernel_arg_type_qualifier value [1]; + NEED_SUCCESS (GetKernelArgInfo, (self, idx, CL_KERNEL_ARG_TYPE_QUALIFIER, sizeof (value), value, 0)); + EXTEND (SP, 1); + const int i = 0; + PUSHs (sv_2mortal (newSVuv (value [i]))); + +#END:kernel_arg + +#endif + MODULE = OpenCL PACKAGE = OpenCL::Event void @@ -2824,7 +3139,7 @@ void unmap (OpenCL::Mapped self, ...) CODE: - mapped_unmap (ST (0), self, self->queue, &ST (1), items - 1); + mapped_unmap (cv, ST (0), self, self->queue, &ST (1), items - 1); bool mapped (OpenCL::Mapped self)