--- OpenCL/OpenCL.xs 2012/05/01 16:37:23 1.61 +++ OpenCL/OpenCL.xs 2012/05/05 15:11:19 1.78 @@ -2,6 +2,8 @@ #include "perl.h" #include "XSUB.h" +#include "ecb.h"//D + #define X_STACKSIZE sizeof (void *) * 512 * 1024 // 2-4mb should be enough, really #include "xthread.h" #include "schmorp.h" @@ -28,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 @@ -38,21 +54,21 @@ #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; typedef cl_mem OpenCL__Buffer; typedef cl_mem OpenCL__BufferObj; typedef cl_mem OpenCL__Image; -typedef cl_mem OpenCL__Image2D; -typedef cl_mem OpenCL__Image3D; typedef cl_mem OpenCL__Memory_ornull; typedef cl_mem OpenCL__Buffer_ornull; typedef cl_mem OpenCL__Image_ornull; -typedef cl_mem OpenCL__Image2D_ornull; -typedef cl_mem OpenCL__Image3D_ornull; typedef cl_sampler OpenCL__Sampler; typedef cl_program OpenCL__Program; typedef cl_kernel OpenCL__Kernel; @@ -61,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, @@ -135,10 +150,10 @@ /*****************************************************************************/ /* up to two temporary buffers */ -static void * +static void * ecb_noinline tmpbuf (size_t size) { - enum { buffers = 3 }; + enum { buffers = 4 }; static int idx; static void *buf [buffers]; static size_t len [buffers]; @@ -155,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 @@ -164,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) { @@ -221,8 +258,41 @@ /*****************************************************************************/ -static cl_context_properties * -SvCONTEXTPROPERTIES (const char *func, const char *svname, SV *sv, cl_context_properties *extra, int extracount) +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 (CV *cv, const char *svname, SV *sv, cl_context_properties *extra, int extracount) { if (!sv || !SvOK (sv)) if (extra) @@ -238,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++; @@ -251,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)) { @@ -283,29 +358,36 @@ return p; } - croak ("%s: %s is not a property list (either undef or [type => value, ...])", func, svname); + croak ("%s: %s is not a property list (either undef or [type => value, ...])", cv_get_name (cv), svname); } -/*****************************************************************************/ - -static SV * -new_clobj (HV *stash, IV id) +// 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 +object_list (CV *cv, int or_undef, const char *argname, SV *arg, const char *klass, cl_uint *rcount) { - return sv_2mortal (sv_bless (newRV_noinc (newSViv (id)), stash)); -} + 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); -#define PUSH_CLOBJ(stash,id) PUSHs (new_clobj ((stash), (IV)(id))) -#define XPUSH_CLOBJ(stash,id) XPUSHs (new_clobj ((stash), (IV)(id))) + AV *av = (AV *)SvRV (arg); + void **list = 0; + cl_uint count = av_len (av) + 1; -/* cl objects are either \$iv, or [$iv, ...] */ -/* they can be upgraded at runtime to the array form */ -static void * -SvCLOBJ (const char *func, const char *svname, SV *sv, const char *pkg) -{ - if (SvROK (sv) && sv_derived_from (sv, pkg)) - 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); + } + + if (!count && !or_undef) + croak ("%s: '%s' must contain at least one %s object", + cv_get_name (cv), argname, klass); - croak ("%s: %s is not of type %s", func, svname, pkg); + *rcount = count; + return (void *)list; } /*****************************************************************************/ @@ -337,7 +419,7 @@ static xmutex_t eq_lock = X_MUTEX_INIT; static eq_item *eq_head, *eq_tail; -static void +static void ecb_noinline eq_enq (eq_vtbl *vtbl, SV *cb, void *data1, void *data2, void *data3) { eq_item *item = malloc (sizeof (eq_item)); @@ -414,7 +496,7 @@ /*****************************************************************************/ /* context notify */ -static void +static void ecb_noinline eq_context_push (void *data1, void *data2, void *data3) { dSP; @@ -446,7 +528,7 @@ user_data = s_get_cv (notify); \ } -static SV * +static SV * ecb_noinline new_clobj_context (cl_context ctx, void *user_data) { SV *sv = new_clobj (stash_context, (IV)ctx); @@ -475,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; @@ -499,6 +600,8 @@ free (arg->options); free (arg); + + return 0; } static void @@ -517,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 @@ -538,6 +657,59 @@ } /*****************************************************************************/ +/* utilities for XS code */ + +static size_t +img_row_pitch (cl_mem img) +{ + size_t res; + clGetImageInfo (img, CL_IMAGE_ROW_PITCH, sizeof (res), &res, 0); + return res; +} + +static cl_event * ecb_noinline +event_list (CV *cv, SV **items, cl_uint *rcount, cl_event extra) +{ + cl_uint count = *rcount; + + if (count > 0x7fffffffU) // yeah, it's a hack - the caller might have underflowed + *rcount = count = 0; + + if (!count && !extra) + return 0; + + cl_event *list = tmpbuf (sizeof (cl_event) * (count + 1)); + int i = 0; + + while (count--) + if (SvOK (items [count])) + list [i++] = SvCLOBJ (cv, "wait_events", items [count], "OpenCL::Event"); + + if (extra) + list [i++] = extra; + + *rcount = i; + + return i ? list : 0; +} + +#define EVENT_LIST(skip) \ + cl_uint event_list_count = items - (skip); \ + cl_event *event_list_ptr = event_list (cv, &ST (skip), &event_list_count, 0) + +#define INFO(class) \ +{ \ + size_t size; \ + NEED_SUCCESS (Get ## class ## Info, (self, name, 0, 0, &size)); \ + SV *sv = sv_2mortal (newSV (size)); \ + SvUPGRADE (sv, SVt_PV); \ + SvPOK_only (sv); \ + SvCUR_set (sv, size); \ + NEED_SUCCESS (Get ## class ## Info, (self, name, size, SvPVX (sv), 0)); \ + XPUSHs (sv); \ +} + +/*****************************************************************************/ /* mapped_xxx */ static OpenCL__Mapped @@ -579,9 +751,6 @@ sv_magicext (data, 0, PERL_MAGIC_ext, 0, (char *)mapped, 0); - if (!(flags & CL_MAP_WRITE)) - SvREADONLY_on (data); - if (SvLEN (data)) Safefree (data); @@ -590,7 +759,12 @@ SvLEN_set (data, 0); SvPOK_only (data); - return sv_2mortal (sv_bless (newRV_noinc (data), stash)); + SV *obj = sv_2mortal (sv_bless (newRV_noinc (data), stash)); + + if (!(flags & CL_MAP_WRITE)) + SvREADONLY_on (data); + + return obj; } static void @@ -598,6 +772,7 @@ { SV *data = SvRV (sv); + // the next check checks both before AND after detach, where SvPVX should STILL be 0 if (SvPVX (data) != (char *)mapped->ptr) warn ("FATAL: OpenCL memory mapped scalar changed location, detected"); else @@ -611,57 +786,21 @@ mapped->ptr = 0; } -/*****************************************************************************/ - -static size_t -img_row_pitch (cl_mem img) -{ - size_t res; - clGetImageInfo (img, CL_IMAGE_ROW_PITCH, sizeof (res), &res, 0); - return res; -} - -static cl_event * -event_list (SV **items, cl_uint *rcount, cl_event extra) +static void +mapped_unmap (CV *cv, SV *self, OpenCL__Mapped mapped, cl_command_queue queue, SV **wait_list, cl_uint event_list_count) { - cl_uint count = *rcount; - - if (!count && !extra) - return 0; - - cl_event *list = tmpbuf (sizeof (cl_event) * (count + 1)); - int i = 0; + cl_event *event_list_ptr = event_list (cv, wait_list, &event_list_count, mapped->event); + cl_event ev; - while (count) - { - --count; - if (SvOK (items [count])) - list [i++] = SvCLOBJ ("clEnqueue", "wait_events", items [count], "OpenCL::Event"); - } + NEED_SUCCESS (EnqueueUnmapMemObject, (queue, mapped->memobj, mapped->ptr, event_list_count, event_list_ptr, &ev)); - if (extra) - list [i++] = extra; - - *rcount = i; + clReleaseEvent (mapped->event); + mapped->event = ev; - return i ? list : 0; + mapped_detach (self, mapped); } -#define EVENT_LIST(skip) \ - cl_uint event_list_count = items - skip; \ - cl_event *event_list_ptr = event_list (&ST (skip), &event_list_count, 0) - -#define INFO(class) \ -{ \ - size_t size; \ - NEED_SUCCESS (Get ## class ## Info, (self, name, 0, 0, &size)); \ - SV *sv = sv_2mortal (newSV (size)); \ - SvUPGRADE (sv, SVt_PV); \ - SvPOK_only (sv); \ - SvCUR_set (sv, size); \ - NEED_SUCCESS (Get ## class ## Info, (self, name, size, SvPVX (sv), 0)); \ - XPUSHs (sv); \ -} +/*****************************************************************************/ MODULE = OpenCL PACKAGE = OpenCL @@ -681,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" }, @@ -693,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); @@ -751,19 +902,24 @@ 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) PPCODE: CONTEXT_NOTIFY_CALLBACK; - NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (properties, type, 0, 0, &res)); + NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (properties, type, pfn_notify, user_data, &res)); 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 (...) @@ -824,31 +980,26 @@ PUSH_CLOBJ (stash_device, list [i]); void -context (OpenCL::Platform self, cl_context_properties *properties, 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 (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, (properties, 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, 0, 0, &res)); + NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (props, type, pfn_notify, user_data, &res)); XPUSH_CLOBJ_CONTEXT; MODULE = OpenCL PACKAGE = OpenCL::Device @@ -858,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 @@ -1082,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) @@ -1138,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; @@ -1281,6 +1473,82 @@ NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithSource, (self, 1, &ptr, &len2, &res)); XPUSH_CLOBJ (stash_program, prog); +void +program_with_binary (OpenCL::Context self, SV *devices, SV *binaries) + PPCODE: + 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 (device_count != av_len ((AV *)binaries) + 1) + croak ("OpenCL::Context::program_with_binary: differing numbers of devices and binaries are not allowed"); + + 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 < device_count; ++i) + { + 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, device_count, device_list, + length_list, binary_list, + GIMME_V == G_ARRAY ? status_list : 0, &res)); + + EXTEND (SP, 2); + PUSH_CLOBJ (stash_program, prog); + + if (GIMME_V == G_ARRAY) + { + AV *av = newAV (); + PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); + + 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 @@ -1335,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) @@ -1352,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) @@ -1369,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) @@ -1384,11 +1655,10 @@ ALIAS: enqueue_fill_image = 0 PPCODE: - cl_event ev = 0; - STRLEN len; + 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 }; @@ -1401,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)); @@ -1414,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) @@ -1427,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]; @@ -1451,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) @@ -1461,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]; @@ -1486,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) @@ -1496,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) @@ -1512,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); @@ -1529,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) @@ -1539,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); @@ -1557,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) @@ -1567,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) @@ -1583,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) @@ -1598,38 +1878,72 @@ 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) XPUSH_CLOBJ (stash_event, ev); void -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, ...) +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, ...) ALIAS: enqueue_map_buffer = 0 PPCODE: - cl_event ev; - EVENT_LIST (7); + EVENT_LIST (6); + size_t cb = SvIV (cb_); + + if (!SvOK (cb_)) + { + NEED_SUCCESS (GetMemObjectInfo, (buf, CL_MEM_SIZE, sizeof (cb), &cb, 0)); + 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)); void -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, ...) +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, ...) ALIAS: enqueue_map_image = 0 PPCODE: - cl_event ev; + size_t width = SvIV (width_); + if (!SvOK (width_)) + { + NEED_SUCCESS (GetImageInfo, (img, CL_IMAGE_WIDTH, sizeof (width), &width, 0)); + width -= x; + } + + size_t height = SvIV (width_); + if (!SvOK (height_)) + { + NEED_SUCCESS (GetImageInfo, (img, CL_IMAGE_HEIGHT, sizeof (height), &height, 0)); + height -= y; + } + + size_t depth = SvIV (width_); + if (!SvOK (depth_)) + { + NEED_SUCCESS (GetImageInfo, (img, CL_IMAGE_DEPTH, sizeof (depth), &depth, 0)); + depth -= z; + + // stupid opencl returns 0 for depth, but requires 1 for 2d images + if (!depth) + depth = 1; + } + const size_t origin[3] = { x, y, z }; const size_t region[3] = { width, height, depth }; size_t row_pitch, slice_pitch; - EVENT_LIST (11); + EVENT_LIST (10); + cl_event ev; 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)); size_t cb = slice_pitch ? slice_pitch * region [2] @@ -1641,23 +1955,21 @@ void unmap (OpenCL::Queue self, OpenCL::Mapped mapped, ...) PPCODE: - cl_uint event_list_count = items - 2; - cl_event *event_list_ptr = event_list (&ST (2), &event_list_count, mapped->event); - - NEED_SUCCESS (EnqueueUnmapMemObject, (self, mapped->memobj, mapped->ptr, event_list_count, event_list_ptr, &mapped->event)); - mapped_detach (ST (1), mapped); - + mapped_unmap (cv, ST (1), mapped, self, &ST (2), items - 2); if (GIMME_V != G_VOID) - XPUSH_CLOBJ (stash_event, mapped->event); + { + clRetainEvent (mapped->event); + XPUSH_CLOBJ (stash_event, mapped->event); + } void task (OpenCL::Queue self, OpenCL::Kernel kernel, ...) 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) @@ -1668,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"); @@ -1722,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 @@ -1736,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); @@ -1776,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)); @@ -1801,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)); @@ -1896,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 @@ -2140,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"); - if (num_devices) + HV *hv = (HV *)SvRV (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) @@ -2211,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 @@ -2386,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); @@ -2469,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); @@ -2598,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 @@ -2710,12 +3133,14 @@ mapped_detach (self, mapped); clReleaseCommandQueue (mapped->queue); - - if (mapped->event) - clReleaseEvent (mapped->event); - + clReleaseEvent (mapped->event); Safefree (mapped); +void +unmap (OpenCL::Mapped self, ...) + CODE: + mapped_unmap (cv, ST (0), self, self->queue, &ST (1), items - 1); + bool mapped (OpenCL::Mapped self) CODE: @@ -2752,6 +3177,17 @@ OUTPUT: RETVAL +void +set (OpenCL::Mapped self, size_t offset, SV *data) + CODE: + STRLEN len; + const char *ptr = SvPVbyte (data, len); + + if (offset + len > self->cb) + croak ("OpenCL::Mapped::set out of bound condition detected"); + + memcpy (offset + (char *)self->ptr, ptr, len); + MODULE = OpenCL PACKAGE = OpenCL::MappedBuffer MODULE = OpenCL PACKAGE = OpenCL::MappedImage