ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/OpenCL/OpenCL.xs
Revision: 1.71
Committed: Sat May 5 02:45:38 2012 UTC (12 years ago) by root
Branch: MAIN
Changes since 1.70: +39 -36 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #include "EXTERN.h"
2     #include "perl.h"
3     #include "XSUB.h"
4    
5 root 1.64 #include "ecb.h"//D
6    
7 root 1.51 #define X_STACKSIZE sizeof (void *) * 512 * 1024 // 2-4mb should be enough, really
8     #include "xthread.h"
9     #include "schmorp.h"
10    
11 root 1.30 #ifdef I_DLFCN
12     #include <dlfcn.h>
13     #endif
14    
15 root 1.37 // how stupid is that, the 1.2 header files define CL_VERSION_1_1,
16     // but then fail to define the api functions unless you ALSO define
17     // this. This breaks 100% of the opencl 1.1 apps, for what reason?
18     // after all, the functions are deprecated, not removed.
19     // in addition, you cannot test for this in any future-proof way.
20     // each time a new opencl version comes out, you need to make a new
21     // release.
22     #define CL_USE_DEPRECATED_OPENCL_1_2_APIS /* just guessing, you stupid idiots */
23    
24 root 1.45 #ifndef PREFER_1_1
25 root 1.44 #define PREFER_1_1 1
26     #endif
27    
28     #if PREFER_1_1
29     #define CL_USE_DEPRECATED_OPENCL_1_1_APIS
30     #endif
31    
32 root 1.19 #ifdef __APPLE__
33     #include <OpenCL/opencl.h>
34     #else
35     #include <CL/opencl.h>
36     #endif
37 root 1.1
38 root 1.44 #ifndef CL_VERSION_1_2
39     #undef PREFER_1_1
40     #define PREFER_1_1 1
41 root 1.38 #endif
42    
43 root 1.16 typedef cl_platform_id OpenCL__Platform;
44     typedef cl_device_id OpenCL__Device;
45 root 1.65 typedef cl_device_id OpenCL__SubDevice;
46 root 1.16 typedef cl_context OpenCL__Context;
47     typedef cl_command_queue OpenCL__Queue;
48     typedef cl_mem OpenCL__Memory;
49     typedef cl_mem OpenCL__Buffer;
50 root 1.18 typedef cl_mem OpenCL__BufferObj;
51 root 1.16 typedef cl_mem OpenCL__Image;
52     typedef cl_mem OpenCL__Memory_ornull;
53     typedef cl_mem OpenCL__Buffer_ornull;
54     typedef cl_mem OpenCL__Image_ornull;
55     typedef cl_sampler OpenCL__Sampler;
56     typedef cl_program OpenCL__Program;
57     typedef cl_kernel OpenCL__Kernel;
58     typedef cl_event OpenCL__Event;
59     typedef cl_event OpenCL__UserEvent;
60 root 1.5
61 root 1.61 typedef struct mapped * OpenCL__Mapped;
62    
63     static HV
64     *stash_platform,
65     *stash_device,
66 root 1.65 *stash_subdevice,
67 root 1.61 *stash_context,
68     *stash_queue,
69     *stash_program,
70     *stash_kernel,
71     *stash_sampler,
72     *stash_event,
73     *stash_userevent,
74     *stash_memory,
75     *stash_buffer,
76     *stash_bufferobj,
77     *stash_image,
78     *stash_image1d,
79     *stash_image1darray,
80     *stash_image1dbuffer,
81     *stash_image2d,
82     *stash_image2darray,
83     *stash_image3d,
84     *stash_mapped,
85     *stash_mappedbuffer,
86     *stash_mappedimage;
87    
88 root 1.5 /*****************************************************************************/
89    
90 root 1.30 // name must include a leading underscore
91 root 1.32 // all of this horrors would be unneceesary if somebody wrote a proper OpenGL module
92     // for perl. doh.
93 root 1.30 static void *
94 root 1.32 glsym (const char *name)
95 root 1.30 {
96 root 1.32 void *fun = 0;
97    
98 root 1.30 #if defined I_DLFCN && defined RTLD_DEFAULT
99 root 1.32 fun = dlsym (RTLD_DEFAULT, name + 1);
100     if (!fun) fun = dlsym (RTLD_DEFAULT, name);
101    
102     if (!fun)
103     {
104     static void *libgl;
105     static const char *glso[] = {
106     "libGL.so.1",
107     "libGL.so.3",
108     "libGL.so.4.0",
109     "libGL.so",
110     "/usr/lib/libGL.so",
111     "/usr/X11R6/lib/libGL.1.dylib"
112     };
113     int i;
114    
115     for (i = 0; !libgl && i < sizeof (glso) / sizeof (glso [0]); ++i)
116     {
117     libgl = dlopen (glso [i], RTLD_LAZY);
118     if (libgl)
119     break;
120     }
121    
122     if (libgl)
123     {
124     fun = dlsym (libgl, name + 1);
125     if (!fun) fun = dlsym (libgl, name);
126     }
127     }
128 root 1.30 #endif
129 root 1.32
130     return fun;
131 root 1.30 }
132    
133     /*****************************************************************************/
134    
135 root 1.5 /* up to two temporary buffers */
136 root 1.64 static void * ecb_noinline
137 root 1.5 tmpbuf (size_t size)
138     {
139 root 1.64 enum { buffers = 4 };
140 root 1.5 static int idx;
141 root 1.24 static void *buf [buffers];
142     static size_t len [buffers];
143 root 1.5
144 root 1.37 idx = (idx + 1) % buffers;
145 root 1.5
146     if (len [idx] < size)
147     {
148     free (buf [idx]);
149     len [idx] = ((size + 31) & ~4095) + 4096 - 32;
150     buf [idx] = malloc (len [idx]);
151     }
152    
153     return buf [idx];
154     }
155    
156 root 1.69 static const char * ecb_noinline
157     cv_get_name (CV *cv)
158     {
159     static char fullname [256];
160    
161     GV *gv = CvGV (cv); // gv better be nonzero
162    
163     HV *stash = GvSTASH (gv);
164     const char *hvname = HvNAME_get (stash); // stash also better be nonzero
165     const char *gvname = GvNAME (gv);
166    
167     snprintf (fullname, sizeof (fullname), "%s::%s", hvname, gvname);
168     return fullname;
169     }
170    
171 root 1.5 /*****************************************************************************/
172 root 1.1
173 root 1.3 typedef struct
174     {
175 root 1.1 IV iv;
176     const char *name;
177 root 1.3 #define const_iv(name) { (IV)CL_ ## name, # name },
178     } ivstr;
179 root 1.1
180     static const char *
181 root 1.3 iv2str (IV value, const ivstr *base, int count, const char *fallback)
182 root 1.1 {
183     int i;
184 root 1.3 static char strbuf [32];
185    
186     for (i = count; i--; )
187     if (base [i].iv == value)
188     return base [i].name;
189    
190     snprintf (strbuf, sizeof (strbuf), fallback, (int)value);
191    
192     return strbuf;
193     }
194    
195     static const char *
196     enum2str (cl_uint value)
197     {
198     static const ivstr enumstr[] = {
199     #include "enumstr.h"
200     };
201 root 1.1
202 root 1.3 return iv2str (value, enumstr, sizeof (enumstr) / sizeof (enumstr [0]), "ENUM(0x%04x)");
203     }
204 root 1.1
205 root 1.3 static const char *
206     err2str (cl_int err)
207     {
208     static const ivstr errstr[] = {
209     #include "errstr.h"
210     };
211 root 1.1
212 root 1.3 return iv2str (err, errstr, sizeof (errstr) / sizeof (errstr [0]), "ERROR(%d)");
213 root 1.1 }
214    
215 root 1.5 /*****************************************************************************/
216    
217 root 1.8 static cl_int res;
218 root 1.5
219 root 1.8 #define FAIL(name) \
220     croak ("cl" # name ": %s", err2str (res));
221 root 1.1
222     #define NEED_SUCCESS(name,args) \
223     do { \
224 root 1.8 res = cl ## name args; \
225 root 1.1 \
226     if (res) \
227 root 1.8 FAIL (name); \
228 root 1.1 } while (0)
229    
230 root 1.8 #define NEED_SUCCESS_ARG(retdecl, name, args) \
231     retdecl = cl ## name args; \
232     if (res) \
233     FAIL (name);
234    
235 root 1.5 /*****************************************************************************/
236    
237 root 1.65 static SV *
238     new_clobj (HV *stash, IV id)
239     {
240     return sv_2mortal (sv_bless (newRV_noinc (newSViv (id)), stash));
241     }
242    
243     #define PUSH_CLOBJ(stash,id) PUSHs (new_clobj ((stash), (IV)(id)))
244     #define XPUSH_CLOBJ(stash,id) XPUSHs (new_clobj ((stash), (IV)(id)))
245    
246     /* cl objects are either \$iv, or [$iv, ...] */
247     /* they can be upgraded at runtime to the array form */
248     static void * ecb_noinline
249 root 1.71 SvCLOBJ (CV *cv, const char *svname, SV *sv, const char *pkg)
250 root 1.65 {
251     // sv_derived_from is quite slow :(
252     if (SvROK (sv) && sv_derived_from (sv, pkg))
253     return (void *)SvIV (SvRV (sv));
254    
255 root 1.71 croak ("%s: %s is not of type %s", cv_get_name (cv), svname, pkg);
256 root 1.65 }
257    
258     // the "no-inherit" version of the above
259     static void * ecb_noinline
260 root 1.71 SvCLOBJ_ni (CV *cv, const char *svname, SV *sv, HV *stash)
261 root 1.65 {
262     if (SvROK (sv) && SvSTASH (SvRV (sv)) == stash)
263     return (void *)SvIV (SvRV (sv));
264    
265 root 1.71 croak ("%s: %s is not of type %s", cv_get_name (cv), svname, HvNAME (stash));
266 root 1.65 }
267    
268     /*****************************************************************************/
269    
270 root 1.64 static cl_context_properties * ecb_noinline
271 root 1.71 SvCONTEXTPROPERTIES (CV *cv, const char *svname, SV *sv, cl_context_properties *extra, int extracount)
272 root 1.24 {
273     if (!sv || !SvOK (sv))
274     if (extra)
275     sv = sv_2mortal (newRV_noinc ((SV *)newAV ())); // slow, but rarely used hopefully
276     else
277     return 0;
278    
279     if (SvROK (sv) && SvTYPE (SvRV (sv)) == SVt_PVAV)
280     {
281     AV *av = (AV *)SvRV (sv);
282 root 1.28 int i, len = av_len (av) + 1;
283 root 1.24 cl_context_properties *p = tmpbuf (sizeof (cl_context_properties) * (len + extracount + 1));
284     cl_context_properties *l = p;
285    
286     if (len & 1)
287 root 1.71 croak ("%s: %s is not a property list (must contain an even number of elements)", cv_get_name (cv), svname);
288 root 1.24
289     while (extracount--)
290     *l++ = *extra++;
291    
292 root 1.29 for (i = 0; i < len; i += 2)
293 root 1.24 {
294 root 1.29 cl_context_properties t = SvIV (*av_fetch (av, i , 0));
295     SV *p_sv = *av_fetch (av, i + 1, 0);
296 root 1.32 cl_context_properties v = SvIV (p_sv); // code below can override
297 root 1.24
298     switch (t)
299     {
300 root 1.65 case CL_CONTEXT_PLATFORM:
301     if (SvROK (p_sv))
302 root 1.71 v = (cl_context_properties)SvCLOBJ (cv, svname, p_sv, "OpenCL::Platform");
303 root 1.65 break;
304    
305 root 1.32 case CL_GLX_DISPLAY_KHR:
306     if (!SvOK (p_sv))
307     {
308     void *func = glsym ("_glXGetCurrentDisplay");
309     if (func)
310     v = (cl_context_properties)((void *(*)(void))func)();
311     }
312     break;
313    
314     case CL_GL_CONTEXT_KHR:
315     if (!SvOK (p_sv))
316     {
317     void *func = glsym ("_glXGetCurrentContext");
318     if (func)
319     v = (cl_context_properties)((void *(*)(void))func)();
320     }
321     break;
322    
323 root 1.24 default:
324     /* unknown property, treat as int */
325     break;
326     }
327    
328     *l++ = t;
329     *l++ = v;
330     }
331    
332     *l = 0;
333    
334     return p;
335     }
336    
337 root 1.71 croak ("%s: %s is not a property list (either undef or [type => value, ...])", cv_get_name (cv), svname);
338 root 1.24 }
339    
340 root 1.71 // parse an array of CLOBJ into a void ** array in C - works only for CLOBJs whose representation
341     // is a pointer (and only on well-behaved systems).
342 root 1.69 static void * ecb_noinline
343     object_list (CV *cv, int or_undef, const char *argname, SV *arg, const char *klass, cl_uint *rcount)
344     {
345     if (!SvROK (arg) || SvTYPE (SvRV (arg)) != SVt_PVAV)
346     croak ("%s: '%s' parameter must be %sa reference to an array of %s objects",
347 root 1.71 cv_get_name (cv), argname, or_undef ? "undef or " : "", klass);
348 root 1.69
349     AV *av = (AV *)SvRV (arg);
350     void **list = 0;
351     cl_uint count = av_len (av) + 1;
352    
353     if (count)
354     {
355     list = tmpbuf (sizeof (*list) * count);
356     int i;
357     for (i = 0; i < count; ++i)
358 root 1.71 list [i] = SvCLOBJ (cv, argname, *av_fetch (av, i, 1), klass);
359 root 1.69 }
360    
361     if (!count && !or_undef)
362     croak ("%s: '%s' must contain at least one %s object",
363 root 1.71 cv_get_name (cv), argname, klass);
364 root 1.69
365     *rcount = count;
366     return (void *)list;
367     }
368    
369 root 1.24 /*****************************************************************************/
370 root 1.51 /* callback stuff */
371    
372     /* default context callback, log to stderr */
373     static void CL_CALLBACK
374     context_default_notify (const char *msg, const void *info, size_t cb, void *data)
375     {
376     fprintf (stderr, "OpenCL Context Notify: %s\n", msg);
377     }
378    
379     typedef struct
380     {
381     int free_cb;
382     void (*push)(void *data1, void *data2, void *data3);
383     } eq_vtbl;
384    
385     typedef struct eq_item
386     {
387     struct eq_item *next;
388     eq_vtbl *vtbl;
389     SV *cb;
390     void *data1, *data2, *data3;
391     } eq_item;
392    
393     static void (*eq_signal_func)(void *signal_arg, int value);
394     static void *eq_signal_arg;
395     static xmutex_t eq_lock = X_MUTEX_INIT;
396     static eq_item *eq_head, *eq_tail;
397    
398 root 1.64 static void ecb_noinline
399 root 1.51 eq_enq (eq_vtbl *vtbl, SV *cb, void *data1, void *data2, void *data3)
400     {
401     eq_item *item = malloc (sizeof (eq_item));
402    
403     item->next = 0;
404     item->vtbl = vtbl;
405     item->cb = cb;
406     item->data1 = data1;
407     item->data2 = data2;
408     item->data3 = data3;
409    
410     X_LOCK (eq_lock);
411    
412     *(eq_head ? &eq_tail->next : &eq_head) = item;
413     eq_tail = item;
414    
415     X_UNLOCK (eq_lock);
416    
417     eq_signal_func (eq_signal_arg, 0);
418     }
419    
420     static eq_item *
421     eq_dec (void)
422     {
423     eq_item *res;
424    
425     X_LOCK (eq_lock);
426    
427     res = eq_head;
428     if (res)
429     eq_head = res->next;
430    
431     X_UNLOCK (eq_lock);
432    
433     return res;
434     }
435    
436     static void
437     eq_poll (void)
438     {
439     eq_item *item;
440    
441     while ((item = eq_dec ()))
442     {
443     ENTER;
444     SAVETMPS;
445    
446     dSP;
447     PUSHMARK (SP);
448     EXTEND (SP, 2);
449    
450     if (item->vtbl->free_cb)
451     sv_2mortal (item->cb);
452    
453     PUTBACK;
454     item->vtbl->push (item->data1, item->data2, item->data3);
455    
456     SV *cb = item->cb;
457     free (item);
458    
459     call_sv (cb, G_DISCARD | G_VOID);
460    
461     FREETMPS;
462     LEAVE;
463     }
464     }
465    
466     static void
467     eq_poll_interrupt (pTHX_ void *c_arg, int value)
468     {
469     eq_poll ();
470     }
471    
472 root 1.52 /*****************************************************************************/
473 root 1.51 /* context notify */
474    
475 root 1.64 static void ecb_noinline
476 root 1.51 eq_context_push (void *data1, void *data2, void *data3)
477     {
478     dSP;
479     PUSHs (sv_2mortal (newSVpv (data1, 0)));
480     PUSHs (sv_2mortal (newSVpvn (data2, (STRLEN)data3)));
481     PUTBACK;
482 root 1.52
483     free (data1);
484     free (data2);
485 root 1.51 }
486    
487     static eq_vtbl eq_context_vtbl = { 0, eq_context_push };
488    
489 root 1.52 static void CL_CALLBACK
490     eq_context_notify (const char *msg, const void *pvt, size_t cb, void *user_data)
491     {
492     void *pvt_copy = malloc (cb);
493     memcpy (pvt_copy, pvt, cb);
494     eq_enq (&eq_context_vtbl, user_data, strdup (msg), pvt_copy, (void *)cb);
495     }
496    
497     #define CONTEXT_NOTIFY_CALLBACK \
498     void (CL_CALLBACK *pfn_notify)(const char *, const void *, size_t, void *) = context_default_notify; \
499     void *user_data = 0; \
500     \
501     if (SvOK (notify)) \
502     { \
503     pfn_notify = eq_context_notify; \
504     user_data = s_get_cv (notify); \
505     }
506    
507 root 1.64 static SV * ecb_noinline
508 root 1.52 new_clobj_context (cl_context ctx, void *user_data)
509     {
510 root 1.61 SV *sv = new_clobj (stash_context, (IV)ctx);
511 root 1.52
512     if (user_data)
513     sv_magicext (SvRV (sv), user_data, PERL_MAGIC_ext, 0, 0, 0);
514    
515     return sv;
516     }
517    
518     #define XPUSH_CLOBJ_CONTEXT XPUSHs (new_clobj_context (ctx, user_data));
519    
520     /*****************************************************************************/
521 root 1.51 /* build/compile/link notify */
522    
523     static void
524     eq_program_push (void *data1, void *data2, void *data3)
525     {
526     dSP;
527 root 1.61 PUSH_CLOBJ (stash_program, data1);
528 root 1.51 PUTBACK;
529     }
530    
531     static eq_vtbl eq_program_vtbl = { 1, eq_program_push };
532    
533     static void CL_CALLBACK
534     eq_program_notify (cl_program program, void *user_data)
535     {
536 root 1.69 clRetainProgram (program);
537    
538 root 1.51 eq_enq (&eq_program_vtbl, user_data, (void *)program, 0, 0);
539     }
540    
541 root 1.69 typedef void (CL_CALLBACK *program_callback)(cl_program program, void *user_data);
542    
543     static program_callback ecb_noinline
544     make_program_callback (SV *notify, void **ruser_data)
545     {
546     if (SvOK (notify))
547     {
548     *ruser_data = SvREFCNT_inc (s_get_cv (notify));
549     return eq_program_notify;
550     }
551     else
552     {
553     *ruser_data = 0;
554     return 0;
555     }
556     }
557    
558 root 1.51 struct build_args
559     {
560     cl_program program;
561     char *options;
562     void *user_data;
563     cl_uint num_devices;
564     };
565    
566     X_THREAD_PROC (build_program_thread)
567     {
568     struct build_args *arg = thr_arg;
569    
570     clBuildProgram (arg->program, arg->num_devices, arg->num_devices ? (void *)(arg + 1) : 0, arg->options, 0, 0);
571    
572     if (arg->user_data)
573     eq_program_notify (arg->program, arg->user_data);
574     else
575     clReleaseProgram (arg->program);
576    
577     free (arg->options);
578     free (arg);
579 root 1.64
580     return 0;
581 root 1.51 }
582    
583     static void
584     build_program_async (cl_program program, cl_uint num_devices, const cl_device_id *device_list, const char *options, void *user_data)
585     {
586     struct build_args *arg = malloc (sizeof (struct build_args) + sizeof (*device_list) * num_devices);
587    
588     arg->program = program;
589     arg->options = strdup (options);
590     arg->user_data = user_data;
591     arg->num_devices = num_devices;
592     memcpy (arg + 1, device_list, sizeof (*device_list) * num_devices);
593    
594     xthread_t id;
595     thread_create (&id, build_program_thread, arg);
596     }
597    
598 root 1.52 /*****************************************************************************/
599 root 1.51 /* event objects */
600    
601     static void
602     eq_event_push (void *data1, void *data2, void *data3)
603     {
604     dSP;
605 root 1.61 PUSH_CLOBJ (stash_event, data1);
606 root 1.51 PUSHs (sv_2mortal (newSViv ((IV)data2)));
607     PUTBACK;
608     }
609    
610     static eq_vtbl eq_event_vtbl = { 1, eq_event_push };
611    
612     static void CL_CALLBACK
613     eq_event_notify (cl_event event, cl_int event_command_exec_status, void *user_data)
614     {
615     clRetainEvent (event);
616 root 1.52 eq_enq (&eq_event_vtbl, user_data, (void *)event, (void *)(IV)event_command_exec_status, 0);
617 root 1.51 }
618    
619     /*****************************************************************************/
620 root 1.62 /* utilities for XS code */
621    
622     static size_t
623     img_row_pitch (cl_mem img)
624     {
625     size_t res;
626     clGetImageInfo (img, CL_IMAGE_ROW_PITCH, sizeof (res), &res, 0);
627     return res;
628     }
629    
630 root 1.64 static cl_event * ecb_noinline
631 root 1.71 event_list (CV *cv, SV **items, cl_uint *rcount, cl_event extra)
632 root 1.62 {
633     cl_uint count = *rcount;
634    
635 root 1.64 if (count > 0x7fffffffU) // yeah, it's a hack - the caller might have underflowed
636     *rcount = count = 0;
637 root 1.62
638     if (!count && !extra)
639     return 0;
640    
641     cl_event *list = tmpbuf (sizeof (cl_event) * (count + 1));
642     int i = 0;
643    
644     while (count--)
645     if (SvOK (items [count]))
646 root 1.71 list [i++] = SvCLOBJ (cv, "wait_events", items [count], "OpenCL::Event");
647 root 1.62
648     if (extra)
649     list [i++] = extra;
650    
651     *rcount = i;
652    
653     return i ? list : 0;
654     }
655    
656     #define EVENT_LIST(skip) \
657     cl_uint event_list_count = items - (skip); \
658 root 1.71 cl_event *event_list_ptr = event_list (cv, &ST (skip), &event_list_count, 0)
659 root 1.62
660     #define INFO(class) \
661     { \
662     size_t size; \
663     NEED_SUCCESS (Get ## class ## Info, (self, name, 0, 0, &size)); \
664     SV *sv = sv_2mortal (newSV (size)); \
665     SvUPGRADE (sv, SVt_PV); \
666     SvPOK_only (sv); \
667     SvCUR_set (sv, size); \
668     NEED_SUCCESS (Get ## class ## Info, (self, name, size, SvPVX (sv), 0)); \
669     XPUSHs (sv); \
670     }
671    
672     /*****************************************************************************/
673 root 1.61 /* mapped_xxx */
674    
675     static OpenCL__Mapped
676     SvMAPPED (SV *sv)
677     {
678     // no typechecking atm., keep your fingers crossed
679     return (OpenCL__Mapped)SvMAGIC (SvRV (sv))->mg_ptr;
680     }
681    
682     struct mapped
683     {
684     cl_command_queue queue;
685     cl_mem memobj;
686     void *ptr;
687     size_t cb;
688     cl_event event;
689     size_t row_pitch;
690     size_t slice_pitch;
691     };
692    
693     static SV *
694     mapped_new (HV *stash, cl_command_queue queue, cl_mem memobj, cl_map_flags flags, void *ptr, size_t cb, cl_event ev, size_t row_pitch, size_t slice_pitch)
695     {
696     SV *data = newSV (0);
697     SvUPGRADE (data, SVt_PVMG);
698    
699     OpenCL__Mapped mapped;
700     New (0, mapped, 1, struct mapped);
701    
702     clRetainCommandQueue (queue);
703    
704     mapped->queue = queue;
705     mapped->memobj = memobj;
706     mapped->ptr = ptr;
707     mapped->cb = cb;
708     mapped->event = ev;
709     mapped->row_pitch = row_pitch;
710     mapped->slice_pitch = slice_pitch;
711    
712     sv_magicext (data, 0, PERL_MAGIC_ext, 0, (char *)mapped, 0);
713    
714     if (SvLEN (data))
715     Safefree (data);
716    
717     SvPVX (data) = (char *)ptr;
718     SvCUR_set (data, cb);
719     SvLEN_set (data, 0);
720     SvPOK_only (data);
721    
722 root 1.62 SV *obj = sv_2mortal (sv_bless (newRV_noinc (data), stash));
723    
724     if (!(flags & CL_MAP_WRITE))
725     SvREADONLY_on (data);
726    
727     return obj;
728 root 1.61 }
729    
730     static void
731     mapped_detach (SV *sv, OpenCL__Mapped mapped)
732     {
733     SV *data = SvRV (sv);
734    
735 root 1.62 // the next check checks both before AND after detach, where SvPVX should STILL be 0
736 root 1.61 if (SvPVX (data) != (char *)mapped->ptr)
737     warn ("FATAL: OpenCL memory mapped scalar changed location, detected");
738     else
739     {
740     SvREADONLY_off (data);
741     SvCUR_set (data, 0);
742     SvPVX (data) = 0;
743     SvOK_off (data);
744     }
745    
746     mapped->ptr = 0;
747     }
748    
749 root 1.62 static void
750 root 1.71 mapped_unmap (CV *cv, SV *self, OpenCL__Mapped mapped, cl_command_queue queue, SV **wait_list, cl_uint event_list_count)
751 root 1.11 {
752 root 1.71 cl_event *event_list_ptr = event_list (cv, wait_list, &event_list_count, mapped->event);
753 root 1.62 cl_event ev;
754 root 1.11
755 root 1.62 NEED_SUCCESS (EnqueueUnmapMemObject, (queue, mapped->memobj, mapped->ptr, event_list_count, event_list_ptr, &ev));
756 root 1.35
757 root 1.62 clReleaseEvent (mapped->event);
758     mapped->event = ev;
759 root 1.61
760 root 1.62 mapped_detach (self, mapped);
761 root 1.5 }
762    
763 root 1.62 /*****************************************************************************/
764 root 1.2
765 root 1.1 MODULE = OpenCL PACKAGE = OpenCL
766    
767 root 1.2 PROTOTYPES: ENABLE
768    
769 root 1.51 void
770     poll ()
771     CODE:
772     eq_poll ();
773    
774     void
775     _eq_initialise (IV func, IV arg)
776     CODE:
777     eq_signal_func = (void (*)(void *, int))func;
778     eq_signal_arg = (void*)arg;
779    
780 root 1.1 BOOT:
781     {
782 root 1.24 HV *stash = gv_stashpv ("OpenCL", 1);
783     static const ivstr *civ, const_iv[] = {
784     { sizeof (cl_char ), "SIZEOF_CHAR" },
785     { sizeof (cl_uchar ), "SIZEOF_UCHAR" },
786     { sizeof (cl_short ), "SIZEOF_SHORT" },
787     { sizeof (cl_ushort), "SIZEOF_USHORT" },
788     { sizeof (cl_int ), "SIZEOF_INT" },
789     { sizeof (cl_uint ), "SIZEOF_UINT" },
790     { sizeof (cl_long ), "SIZEOF_LONG" },
791     { sizeof (cl_ulong ), "SIZEOF_ULONG" },
792     { sizeof (cl_half ), "SIZEOF_HALF" },
793     { sizeof (cl_float ), "SIZEOF_FLOAT" },
794     { sizeof (cl_double), "SIZEOF_DOUBLE" },
795 root 1.1 #include "constiv.h"
796 root 1.24 };
797 root 1.51
798 root 1.24 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
799     newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
800 root 1.51
801 root 1.61 stash_platform = gv_stashpv ("OpenCL::Platform", GV_ADD);
802     stash_device = gv_stashpv ("OpenCL::Device", GV_ADD);
803 root 1.65 stash_subdevice = gv_stashpv ("OpenCL::SubDevice", GV_ADD);
804 root 1.61 stash_context = gv_stashpv ("OpenCL::Context", GV_ADD);
805     stash_queue = gv_stashpv ("OpenCL::Queue", GV_ADD);
806     stash_program = gv_stashpv ("OpenCL::Program", GV_ADD);
807     stash_kernel = gv_stashpv ("OpenCL::Kernel", GV_ADD);
808     stash_sampler = gv_stashpv ("OpenCL::Sampler", GV_ADD);
809     stash_event = gv_stashpv ("OpenCL::Event", GV_ADD);
810     stash_userevent = gv_stashpv ("OpenCL::UserEvent", GV_ADD);
811     stash_memory = gv_stashpv ("OpenCL::Memory", GV_ADD);
812     stash_buffer = gv_stashpv ("OpenCL::Buffer", GV_ADD);
813     stash_bufferobj = gv_stashpv ("OpenCL::BufferObj", GV_ADD);
814     stash_image = gv_stashpv ("OpenCL::Image", GV_ADD);
815     stash_image1d = gv_stashpv ("OpenCL::Image1D", GV_ADD);
816     stash_image1darray = gv_stashpv ("OpenCL::Image1DArray", GV_ADD);
817     stash_image1dbuffer = gv_stashpv ("OpenCL::Image1DBuffer", GV_ADD);
818     stash_image2d = gv_stashpv ("OpenCL::Image2D", GV_ADD);
819     stash_image2darray = gv_stashpv ("OpenCL::Image2DArray", GV_ADD);
820     stash_image3d = gv_stashpv ("OpenCL::Image3D", GV_ADD);
821     stash_mapped = gv_stashpv ("OpenCL::Mapped", GV_ADD);
822     stash_mappedbuffer = gv_stashpv ("OpenCL::MappedBuffer", GV_ADD);
823     stash_mappedimage = gv_stashpv ("OpenCL::MappedImage", GV_ADD);
824    
825 root 1.51 sv_setiv (perl_get_sv ("OpenCL::POLL_FUNC", TRUE), (IV)eq_poll_interrupt);
826 root 1.1 }
827    
828 root 1.5 cl_int
829     errno ()
830     CODE:
831 root 1.37 RETVAL = res;
832     OUTPUT:
833     RETVAL
834 root 1.5
835 root 1.3 const char *
836 root 1.57 err2str (cl_int err = res)
837 root 1.3
838     const char *
839     enum2str (cl_uint value)
840    
841 root 1.1 void
842     platforms ()
843     PPCODE:
844     cl_platform_id *list;
845     cl_uint count;
846     int i;
847    
848 root 1.2 NEED_SUCCESS (GetPlatformIDs, (0, 0, &count));
849 root 1.4 list = tmpbuf (sizeof (*list) * count);
850 root 1.2 NEED_SUCCESS (GetPlatformIDs, (count, list, 0));
851 root 1.1
852     EXTEND (SP, count);
853     for (i = 0; i < count; ++i)
854 root 1.65 PUSH_CLOBJ (stash_platform, list [i]);
855 root 1.1
856     void
857 root 1.52 context_from_type (cl_context_properties *properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, SV *notify = &PL_sv_undef)
858 root 1.1 PPCODE:
859 root 1.52 CONTEXT_NOTIFY_CALLBACK;
860 root 1.64 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (properties, type, pfn_notify, user_data, &res));
861 root 1.52 XPUSH_CLOBJ_CONTEXT;
862 root 1.37
863 root 1.8 void
864 root 1.71 context (cl_context_properties *properties, SV *devices, SV *notify = &PL_sv_undef)
865 root 1.8 PPCODE:
866 root 1.71 cl_uint device_count;
867     cl_device_id *device_list = object_list (cv, 0, "devices", devices, "OpenCL::Device", &device_count);
868    
869     CONTEXT_NOTIFY_CALLBACK;
870     NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (properties, device_count, device_list, pfn_notify, user_data, &res));
871     XPUSH_CLOBJ_CONTEXT;
872 root 1.1
873 root 1.2 void
874     wait_for_events (...)
875     CODE:
876 root 1.61 EVENT_LIST (0);
877 root 1.2 NEED_SUCCESS (WaitForEvents, (event_list_count, event_list_ptr));
878    
879     PROTOTYPES: DISABLE
880    
881 root 1.1 MODULE = OpenCL PACKAGE = OpenCL::Platform
882    
883     void
884 root 1.22 info (OpenCL::Platform self, cl_platform_info name)
885 root 1.1 PPCODE:
886 root 1.2 INFO (Platform)
887 root 1.1
888 root 1.47 void
889     unload_compiler (OpenCL::Platform self)
890     CODE:
891     #if CL_VERSION_1_2
892     clUnloadPlatformCompiler (self);
893     #endif
894    
895 root 1.13 #BEGIN:platform
896    
897     void
898 root 1.22 profile (OpenCL::Platform self)
899 root 1.16 ALIAS:
900     profile = CL_PLATFORM_PROFILE
901     version = CL_PLATFORM_VERSION
902     name = CL_PLATFORM_NAME
903     vendor = CL_PLATFORM_VENDOR
904     extensions = CL_PLATFORM_EXTENSIONS
905 root 1.14 PPCODE:
906     size_t size;
907 root 1.22 NEED_SUCCESS (GetPlatformInfo, (self, ix, 0, 0, &size));
908 root 1.14 char *value = tmpbuf (size);
909 root 1.22 NEED_SUCCESS (GetPlatformInfo, (self, ix, size, value, 0));
910 root 1.16 EXTEND (SP, 1);
911     const int i = 0;
912 root 1.14 PUSHs (sv_2mortal (newSVpv (value, 0)));
913 root 1.13
914     #END:platform
915    
916 root 1.1 void
917 root 1.22 devices (OpenCL::Platform self, cl_device_type type = CL_DEVICE_TYPE_ALL)
918 root 1.1 PPCODE:
919     cl_device_id *list;
920     cl_uint count;
921     int i;
922    
923 root 1.22 NEED_SUCCESS (GetDeviceIDs, (self, type, 0, 0, &count));
924 root 1.4 list = tmpbuf (sizeof (*list) * count);
925 root 1.22 NEED_SUCCESS (GetDeviceIDs, (self, type, count, list, 0));
926 root 1.1
927     EXTEND (SP, count);
928     for (i = 0; i < count; ++i)
929 root 1.61 PUSH_CLOBJ (stash_device, list [i]);
930 root 1.1
931     void
932 root 1.71 context (OpenCL::Platform self, SV *properties, SV *devices, SV *notify = &PL_sv_undef)
933 root 1.8 PPCODE:
934 root 1.64 cl_context_properties extra[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)self };
935 root 1.71 cl_context_properties *props = SvCONTEXTPROPERTIES (cv, "properties", properties, extra, 2);
936 root 1.64
937 root 1.71 cl_uint device_count;
938     cl_device_id *device_list = object_list (cv, 0, "devices", devices, "OpenCL::Device", &device_count);
939 root 1.8
940 root 1.52 CONTEXT_NOTIFY_CALLBACK;
941 root 1.69 NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (props, device_count, device_list, pfn_notify, user_data, &res));
942 root 1.52 XPUSH_CLOBJ_CONTEXT;
943 root 1.8
944     void
945 root 1.52 context_from_type (OpenCL::Platform self, SV *properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, SV *notify = &PL_sv_undef)
946 root 1.1 PPCODE:
947 root 1.24 cl_context_properties extra[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)self };
948 root 1.71 cl_context_properties *props = SvCONTEXTPROPERTIES (cv, "properties", properties, extra, 2);
949 root 1.52
950     CONTEXT_NOTIFY_CALLBACK;
951 root 1.64 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (props, type, pfn_notify, user_data, &res));
952 root 1.52 XPUSH_CLOBJ_CONTEXT;
953 root 1.1
954 root 1.16 MODULE = OpenCL PACKAGE = OpenCL::Device
955 root 1.14
956     void
957 root 1.22 info (OpenCL::Device self, cl_device_info name)
958 root 1.16 PPCODE:
959     INFO (Device)
960 root 1.14
961 root 1.65 #if CL_VERSION_1_2
962    
963     void
964     sub_devices (OpenCL::Device self, SV *properties)
965     PPCODE:
966     if (!SvROK (properties) || SvTYPE (SvRV (properties)) != SVt_PVAV)
967     croak ("OpenCL::Device::sub_devices: properties must be specified as reference to an array of property-value pairs");
968    
969     properties = SvRV (properties);
970    
971     cl_uint count = av_len ((AV *)properties) + 1;
972     cl_device_partition_property *props = tmpbuf (sizeof (*props) * count + 1);
973    
974     int i;
975     for (i = 0; i < count; ++i)
976     props [i] = (cl_device_partition_property)SvIV (*av_fetch ((AV *)properties, i, 0));
977    
978     props [count] = 0;
979    
980     cl_uint num_devices;
981     NEED_SUCCESS (CreateSubDevices, (self, props, 0, 0, &num_devices));
982     cl_device_id *list = tmpbuf (sizeof (*list) * num_devices);
983     NEED_SUCCESS (CreateSubDevices, (self, props, num_devices, list, 0));
984    
985     EXTEND (SP, num_devices);
986     for (i = 0; i < count; ++i)
987     PUSH_CLOBJ (stash_subdevice, list [i]);
988    
989     #endif
990    
991 root 1.16 #BEGIN:device
992 root 1.14
993     void
994 root 1.22 type (OpenCL::Device self)
995 root 1.14 PPCODE:
996 root 1.16 cl_device_type value [1];
997 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, CL_DEVICE_TYPE, sizeof (value), value, 0));
998 root 1.16 EXTEND (SP, 1);
999     const int i = 0;
1000 root 1.61 PUSHs (sv_2mortal (newSVuv (value [i])));
1001 root 1.14
1002     void
1003 root 1.22 vendor_id (OpenCL::Device self)
1004 root 1.16 ALIAS:
1005     vendor_id = CL_DEVICE_VENDOR_ID
1006     max_compute_units = CL_DEVICE_MAX_COMPUTE_UNITS
1007     max_work_item_dimensions = CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS
1008     preferred_vector_width_char = CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR
1009     preferred_vector_width_short = CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT
1010     preferred_vector_width_int = CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT
1011     preferred_vector_width_long = CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG
1012     preferred_vector_width_float = CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT
1013     preferred_vector_width_double = CL_DEVICE_PREFERRED_VECTOR_WIDTH_DOUBLE
1014     max_clock_frequency = CL_DEVICE_MAX_CLOCK_FREQUENCY
1015     max_read_image_args = CL_DEVICE_MAX_READ_IMAGE_ARGS
1016     max_write_image_args = CL_DEVICE_MAX_WRITE_IMAGE_ARGS
1017     image_support = CL_DEVICE_IMAGE_SUPPORT
1018     max_samplers = CL_DEVICE_MAX_SAMPLERS
1019     mem_base_addr_align = CL_DEVICE_MEM_BASE_ADDR_ALIGN
1020     min_data_type_align_size = CL_DEVICE_MIN_DATA_TYPE_ALIGN_SIZE
1021     global_mem_cacheline_size = CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE
1022     max_constant_args = CL_DEVICE_MAX_CONSTANT_ARGS
1023     preferred_vector_width_half = CL_DEVICE_PREFERRED_VECTOR_WIDTH_HALF
1024     native_vector_width_char = CL_DEVICE_NATIVE_VECTOR_WIDTH_CHAR
1025     native_vector_width_short = CL_DEVICE_NATIVE_VECTOR_WIDTH_SHORT
1026     native_vector_width_int = CL_DEVICE_NATIVE_VECTOR_WIDTH_INT
1027     native_vector_width_long = CL_DEVICE_NATIVE_VECTOR_WIDTH_LONG
1028     native_vector_width_float = CL_DEVICE_NATIVE_VECTOR_WIDTH_FLOAT
1029     native_vector_width_double = CL_DEVICE_NATIVE_VECTOR_WIDTH_DOUBLE
1030     native_vector_width_half = CL_DEVICE_NATIVE_VECTOR_WIDTH_HALF
1031 root 1.41 reference_count_ext = CL_DEVICE_REFERENCE_COUNT_EXT
1032 root 1.14 PPCODE:
1033     cl_uint value [1];
1034 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, ix, sizeof (value), value, 0));
1035 root 1.14 EXTEND (SP, 1);
1036     const int i = 0;
1037     PUSHs (sv_2mortal (newSVuv (value [i])));
1038    
1039     void
1040 root 1.22 max_work_group_size (OpenCL::Device self)
1041 root 1.16 ALIAS:
1042     max_work_group_size = CL_DEVICE_MAX_WORK_GROUP_SIZE
1043     image2d_max_width = CL_DEVICE_IMAGE2D_MAX_WIDTH
1044     image2d_max_height = CL_DEVICE_IMAGE2D_MAX_HEIGHT
1045     image3d_max_width = CL_DEVICE_IMAGE3D_MAX_WIDTH
1046     image3d_max_height = CL_DEVICE_IMAGE3D_MAX_HEIGHT
1047     image3d_max_depth = CL_DEVICE_IMAGE3D_MAX_DEPTH
1048     max_parameter_size = CL_DEVICE_MAX_PARAMETER_SIZE
1049     profiling_timer_resolution = CL_DEVICE_PROFILING_TIMER_RESOLUTION
1050 root 1.14 PPCODE:
1051 root 1.16 size_t value [1];
1052 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, ix, sizeof (value), value, 0));
1053 root 1.14 EXTEND (SP, 1);
1054     const int i = 0;
1055     PUSHs (sv_2mortal (newSVuv (value [i])));
1056    
1057     void
1058 root 1.22 max_work_item_sizes (OpenCL::Device self)
1059 root 1.14 PPCODE:
1060 root 1.16 size_t size;
1061 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, CL_DEVICE_MAX_WORK_ITEM_SIZES, 0, 0, &size));
1062 root 1.16 size_t *value = tmpbuf (size);
1063 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, CL_DEVICE_MAX_WORK_ITEM_SIZES, size, value, 0));
1064 root 1.16 int i, n = size / sizeof (*value);
1065     EXTEND (SP, n);
1066     for (i = 0; i < n; ++i)
1067 root 1.14 PUSHs (sv_2mortal (newSVuv (value [i])));
1068    
1069     void
1070 root 1.22 address_bits (OpenCL::Device self)
1071 root 1.14 PPCODE:
1072 root 1.16 cl_bitfield value [1];
1073 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, CL_DEVICE_ADDRESS_BITS, sizeof (value), value, 0));
1074 root 1.14 EXTEND (SP, 1);
1075     const int i = 0;
1076     PUSHs (sv_2mortal (newSVuv (value [i])));
1077    
1078     void
1079 root 1.22 max_mem_alloc_size (OpenCL::Device self)
1080 root 1.16 ALIAS:
1081     max_mem_alloc_size = CL_DEVICE_MAX_MEM_ALLOC_SIZE
1082     global_mem_cache_size = CL_DEVICE_GLOBAL_MEM_CACHE_SIZE
1083     global_mem_size = CL_DEVICE_GLOBAL_MEM_SIZE
1084     max_constant_buffer_size = CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE
1085     local_mem_size = CL_DEVICE_LOCAL_MEM_SIZE
1086 root 1.14 PPCODE:
1087 root 1.16 cl_ulong value [1];
1088 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, ix, sizeof (value), value, 0));
1089 root 1.14 EXTEND (SP, 1);
1090     const int i = 0;
1091     PUSHs (sv_2mortal (newSVuv (value [i])));
1092    
1093     void
1094 root 1.22 single_fp_config (OpenCL::Device self)
1095 root 1.16 ALIAS:
1096     single_fp_config = CL_DEVICE_SINGLE_FP_CONFIG
1097     double_fp_config = CL_DEVICE_DOUBLE_FP_CONFIG
1098     half_fp_config = CL_DEVICE_HALF_FP_CONFIG
1099 root 1.14 PPCODE:
1100 root 1.16 cl_device_fp_config value [1];
1101 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, ix, sizeof (value), value, 0));
1102 root 1.14 EXTEND (SP, 1);
1103     const int i = 0;
1104     PUSHs (sv_2mortal (newSVuv (value [i])));
1105    
1106     void
1107 root 1.22 global_mem_cache_type (OpenCL::Device self)
1108 root 1.14 PPCODE:
1109 root 1.16 cl_device_mem_cache_type value [1];
1110 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, CL_DEVICE_GLOBAL_MEM_CACHE_TYPE, sizeof (value), value, 0));
1111 root 1.14 EXTEND (SP, 1);
1112     const int i = 0;
1113     PUSHs (sv_2mortal (newSVuv (value [i])));
1114    
1115     void
1116 root 1.22 local_mem_type (OpenCL::Device self)
1117 root 1.14 PPCODE:
1118 root 1.16 cl_device_local_mem_type value [1];
1119 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, CL_DEVICE_LOCAL_MEM_TYPE, sizeof (value), value, 0));
1120 root 1.14 EXTEND (SP, 1);
1121     const int i = 0;
1122     PUSHs (sv_2mortal (newSVuv (value [i])));
1123    
1124     void
1125 root 1.22 error_correction_support (OpenCL::Device self)
1126 root 1.16 ALIAS:
1127     error_correction_support = CL_DEVICE_ERROR_CORRECTION_SUPPORT
1128     endian_little = CL_DEVICE_ENDIAN_LITTLE
1129     available = CL_DEVICE_AVAILABLE
1130     compiler_available = CL_DEVICE_COMPILER_AVAILABLE
1131     host_unified_memory = CL_DEVICE_HOST_UNIFIED_MEMORY
1132 root 1.14 PPCODE:
1133 root 1.16 cl_bool value [1];
1134 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, ix, sizeof (value), value, 0));
1135 root 1.14 EXTEND (SP, 1);
1136     const int i = 0;
1137 root 1.16 PUSHs (sv_2mortal (value [i] ? &PL_sv_yes : &PL_sv_no));
1138 root 1.14
1139     void
1140 root 1.22 execution_capabilities (OpenCL::Device self)
1141 root 1.14 PPCODE:
1142 root 1.16 cl_device_exec_capabilities value [1];
1143 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, CL_DEVICE_EXECUTION_CAPABILITIES, sizeof (value), value, 0));
1144 root 1.14 EXTEND (SP, 1);
1145     const int i = 0;
1146     PUSHs (sv_2mortal (newSVuv (value [i])));
1147    
1148     void
1149 root 1.22 properties (OpenCL::Device self)
1150 root 1.14 PPCODE:
1151 root 1.16 cl_command_queue_properties value [1];
1152 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, CL_DEVICE_QUEUE_PROPERTIES, sizeof (value), value, 0));
1153 root 1.14 EXTEND (SP, 1);
1154     const int i = 0;
1155 root 1.61 PUSHs (sv_2mortal (newSVuv (value [i])));
1156 root 1.14
1157     void
1158 root 1.22 platform (OpenCL::Device self)
1159 root 1.14 PPCODE:
1160 root 1.16 cl_platform_id value [1];
1161 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, CL_DEVICE_PLATFORM, sizeof (value), value, 0));
1162 root 1.14 EXTEND (SP, 1);
1163     const int i = 0;
1164     {
1165 root 1.61 PUSH_CLOBJ (stash_platform, value [i]);
1166 root 1.14 }
1167    
1168     void
1169 root 1.22 name (OpenCL::Device self)
1170 root 1.16 ALIAS:
1171     name = CL_DEVICE_NAME
1172     vendor = CL_DEVICE_VENDOR
1173     driver_version = CL_DRIVER_VERSION
1174     profile = CL_DEVICE_PROFILE
1175     version = CL_DEVICE_VERSION
1176     extensions = CL_DEVICE_EXTENSIONS
1177 root 1.14 PPCODE:
1178     size_t size;
1179 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, ix, 0, 0, &size));
1180 root 1.16 char *value = tmpbuf (size);
1181 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, ix, size, value, 0));
1182 root 1.16 EXTEND (SP, 1);
1183     const int i = 0;
1184     PUSHs (sv_2mortal (newSVpv (value, 0)));
1185 root 1.14
1186     void
1187 root 1.22 parent_device_ext (OpenCL::Device self)
1188 root 1.14 PPCODE:
1189 root 1.16 cl_device_id value [1];
1190 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, CL_DEVICE_PARENT_DEVICE_EXT, sizeof (value), value, 0));
1191 root 1.14 EXTEND (SP, 1);
1192     const int i = 0;
1193 root 1.16 {
1194 root 1.61 PUSH_CLOBJ (stash_device, value [i]);
1195 root 1.16 }
1196 root 1.14
1197     void
1198 root 1.22 partition_types_ext (OpenCL::Device self)
1199 root 1.16 ALIAS:
1200     partition_types_ext = CL_DEVICE_PARTITION_TYPES_EXT
1201     affinity_domains_ext = CL_DEVICE_AFFINITY_DOMAINS_EXT
1202     partition_style_ext = CL_DEVICE_PARTITION_STYLE_EXT
1203 root 1.14 PPCODE:
1204     size_t size;
1205 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, ix, 0, 0, &size));
1206 root 1.14 cl_device_partition_property_ext *value = tmpbuf (size);
1207 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, ix, size, value, 0));
1208 root 1.15 int i, n = size / sizeof (*value);
1209 root 1.14 EXTEND (SP, n);
1210     for (i = 0; i < n; ++i)
1211     PUSHs (sv_2mortal (newSVuv (value [i])));
1212    
1213     #END:device
1214    
1215 root 1.65 MODULE = OpenCL PACKAGE = OpenCL::SubDevice
1216    
1217     #if CL_VERSION_1_2
1218    
1219     void
1220     DESTROY (OpenCL::SubDevice self)
1221     CODE:
1222     clReleaseDevice (self);
1223    
1224     #endif
1225    
1226 root 1.1 MODULE = OpenCL PACKAGE = OpenCL::Context
1227    
1228     void
1229 root 1.65 DESTROY (OpenCL::Context self)
1230 root 1.1 CODE:
1231 root 1.65 clReleaseContext (self);
1232 root 1.1
1233     void
1234 root 1.22 info (OpenCL::Context self, cl_context_info name)
1235 root 1.1 PPCODE:
1236 root 1.2 INFO (Context)
1237    
1238     void
1239 root 1.22 queue (OpenCL::Context self, OpenCL::Device device, cl_command_queue_properties properties = 0)
1240 root 1.2 PPCODE:
1241 root 1.23 NEED_SUCCESS_ARG (cl_command_queue queue, CreateCommandQueue, (self, device, properties, &res));
1242 root 1.61 XPUSH_CLOBJ (stash_queue, queue);
1243 root 1.2
1244     void
1245 root 1.22 user_event (OpenCL::Context self)
1246 root 1.5 PPCODE:
1247 root 1.23 NEED_SUCCESS_ARG (cl_event ev, CreateUserEvent, (self, &res));
1248 root 1.61 XPUSH_CLOBJ (stash_userevent, ev);
1249 root 1.5
1250     void
1251 root 1.22 buffer (OpenCL::Context self, cl_mem_flags flags, size_t len)
1252 root 1.2 PPCODE:
1253 root 1.3 if (flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))
1254 root 1.27 croak ("OpenCL::Context::buffer: cannot use/copy host ptr when no data is given, use $context->buffer_sv instead?");
1255 root 1.3
1256 root 1.22 NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (self, flags, len, 0, &res));
1257 root 1.61 XPUSH_CLOBJ (stash_bufferobj, mem);
1258 root 1.2
1259     void
1260 root 1.22 buffer_sv (OpenCL::Context self, cl_mem_flags flags, SV *data)
1261 root 1.2 PPCODE:
1262     STRLEN len;
1263 root 1.21 char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0;
1264 root 1.3 if (!(flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR)))
1265 root 1.27 croak ("OpenCL::Context::buffer_sv: you have to specify use or copy host ptr when buffer data is given, use $context->buffer instead?");
1266 root 1.22 NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (self, flags, len, ptr, &res));
1267 root 1.61 XPUSH_CLOBJ (stash_bufferobj, mem);
1268 root 1.3
1269 root 1.42 #if CL_VERSION_1_2
1270    
1271     void
1272 root 1.55 image (OpenCL::Context self, cl_mem_flags flags, cl_channel_order channel_order, cl_channel_type channel_type, cl_mem_object_type type, size_t width, size_t height, size_t depth = 0, size_t array_size = 0, size_t row_pitch = 0, size_t slice_pitch = 0, cl_uint num_mip_level = 0, cl_uint num_samples = 0, SV *data = &PL_sv_undef)
1273 root 1.42 PPCODE:
1274     STRLEN len;
1275     char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0;
1276     const cl_image_format format = { channel_order, channel_type };
1277     const cl_image_desc desc = {
1278     type,
1279     width, height, depth,
1280     array_size, row_pitch, slice_pitch,
1281     num_mip_level, num_samples,
1282 root 1.71 type == CL_MEM_OBJECT_IMAGE1D_BUFFER ? (cl_mem)SvCLOBJ (cv, "data", data, "OpenCL::Buffer") : 0
1283 root 1.42 };
1284     NEED_SUCCESS_ARG (cl_mem mem, CreateImage, (self, flags, &format, &desc, ptr, &res));
1285 root 1.61 HV *stash = stash_image;
1286 root 1.42 switch (type)
1287     {
1288 root 1.61 case CL_MEM_OBJECT_IMAGE1D_BUFFER: stash = stash_image1dbuffer; break;
1289     case CL_MEM_OBJECT_IMAGE1D: stash = stash_image1d; break;
1290     case CL_MEM_OBJECT_IMAGE1D_ARRAY: stash = stash_image2darray; break;
1291     case CL_MEM_OBJECT_IMAGE2D: stash = stash_image2d; break;
1292     case CL_MEM_OBJECT_IMAGE2D_ARRAY: stash = stash_image2darray; break;
1293     case CL_MEM_OBJECT_IMAGE3D: stash = stash_image3d; break;
1294 root 1.42 }
1295 root 1.61 XPUSH_CLOBJ (stash, mem);
1296 root 1.42
1297     #endif
1298    
1299 root 1.3 void
1300 root 1.22 image2d (OpenCL::Context self, cl_mem_flags flags, cl_channel_order channel_order, cl_channel_type channel_type, size_t width, size_t height, size_t row_pitch = 0, SV *data = &PL_sv_undef)
1301 root 1.3 PPCODE:
1302     STRLEN len;
1303 root 1.21 char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0;
1304 root 1.3 const cl_image_format format = { channel_order, channel_type };
1305 root 1.44 #if PREFER_1_1
1306     NEED_SUCCESS_ARG (cl_mem mem, CreateImage2D, (self, flags, &format, width, height, row_pitch, ptr, &res));
1307     #else
1308 root 1.42 const cl_image_desc desc = { CL_MEM_OBJECT_IMAGE2D, width, height, 0, 0, row_pitch, 0, 0, 0, 0 };
1309     NEED_SUCCESS_ARG (cl_mem mem, CreateImage, (self, flags, &format, &desc, ptr, &res));
1310     #endif
1311 root 1.61 XPUSH_CLOBJ (stash_image2d, mem);
1312 root 1.3
1313     void
1314 root 1.22 image3d (OpenCL::Context self, cl_mem_flags flags, cl_channel_order channel_order, cl_channel_type channel_type, size_t width, size_t height, size_t depth, size_t row_pitch = 0, size_t slice_pitch = 0, SV *data = &PL_sv_undef)
1315 root 1.3 PPCODE:
1316     STRLEN len;
1317 root 1.21 char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0;
1318 root 1.3 const cl_image_format format = { channel_order, channel_type };
1319 root 1.44 #if PREFER_1_1
1320     NEED_SUCCESS_ARG (cl_mem mem, CreateImage3D, (self, flags, &format, width, height, depth, row_pitch, slice_pitch, ptr, &res));
1321     #else
1322 root 1.42 const cl_image_desc desc = { CL_MEM_OBJECT_IMAGE3D, width, height, depth, 0, row_pitch, slice_pitch, 0, 0, 0 };
1323     NEED_SUCCESS_ARG (cl_mem mem, CreateImage, (self, flags, &format, &desc, ptr, &res));
1324     #endif
1325 root 1.61 XPUSH_CLOBJ (stash_image3d, mem);
1326 root 1.3
1327 root 1.25 #if cl_apple_gl_sharing || cl_khr_gl_sharing
1328    
1329     void
1330     gl_buffer (OpenCL::Context self, cl_mem_flags flags, cl_GLuint bufobj)
1331     PPCODE:
1332     NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLBuffer, (self, flags, bufobj, &res));
1333 root 1.61 XPUSH_CLOBJ (stash_bufferobj, mem);
1334 root 1.25
1335     void
1336 root 1.40 gl_renderbuffer (OpenCL::Context self, cl_mem_flags flags, cl_GLuint renderbuffer)
1337 root 1.25 PPCODE:
1338 root 1.40 NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLRenderbuffer, (self, flags, renderbuffer, &res));
1339 root 1.61 XPUSH_CLOBJ (stash_image2d, mem);
1340 root 1.25
1341 root 1.39 #if CL_VERSION_1_2
1342    
1343     void
1344     gl_texture (OpenCL::Context self, cl_mem_flags flags, cl_GLenum target, cl_GLint miplevel, cl_GLuint texture)
1345 root 1.43 ALIAS:
1346 root 1.39 PPCODE:
1347 root 1.43 NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture, (self, flags, target, miplevel, texture, &res));
1348     cl_gl_object_type type;
1349     NEED_SUCCESS (GetGLObjectInfo, (mem, &type, 0)); // TODO: use target instead?
1350 root 1.61 HV *stash = stash_memory;
1351 root 1.42 switch (type)
1352 root 1.39 {
1353 root 1.61 case CL_GL_OBJECT_TEXTURE_BUFFER: stash = stash_image1dbuffer; break;
1354     case CL_GL_OBJECT_TEXTURE1D: stash = stash_image1d; break;
1355     case CL_GL_OBJECT_TEXTURE1D_ARRAY: stash = stash_image2darray; break;
1356     case CL_GL_OBJECT_TEXTURE2D: stash = stash_image2d; break;
1357     case CL_GL_OBJECT_TEXTURE2D_ARRAY: stash = stash_image2darray; break;
1358     case CL_GL_OBJECT_TEXTURE3D: stash = stash_image3d; break;
1359 root 1.39 }
1360 root 1.61 XPUSH_CLOBJ (stash, mem);
1361 root 1.39
1362 root 1.44 #endif
1363 root 1.40
1364 root 1.25 void
1365 root 1.40 gl_texture2d (OpenCL::Context self, cl_mem_flags flags, cl_GLenum target, cl_GLint miplevel, cl_GLuint texture)
1366 root 1.25 PPCODE:
1367 root 1.44 #if PREFER_1_1
1368 root 1.40 NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture2D, (self, flags, target, miplevel, texture, &res));
1369 root 1.44 #else
1370     NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture , (self, flags, target, miplevel, texture, &res));
1371     #endif
1372 root 1.61 XPUSH_CLOBJ (stash_image2d, mem);
1373 root 1.25
1374 root 1.40 void
1375     gl_texture3d (OpenCL::Context self, cl_mem_flags flags, cl_GLenum target, cl_GLint miplevel, cl_GLuint texture)
1376     PPCODE:
1377 root 1.44 #if PREFER_1_1
1378 root 1.40 NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture3D, (self, flags, target, miplevel, texture, &res));
1379 root 1.44 #else
1380     NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture , (self, flags, target, miplevel, texture, &res));
1381     #endif
1382 root 1.61 XPUSH_CLOBJ (stash_image3d, mem);
1383 root 1.40
1384     #endif
1385    
1386 root 1.3 void
1387 root 1.22 supported_image_formats (OpenCL::Context self, cl_mem_flags flags, cl_mem_object_type image_type)
1388 root 1.3 PPCODE:
1389     {
1390     cl_uint count;
1391     cl_image_format *list;
1392     int i;
1393    
1394 root 1.23 NEED_SUCCESS (GetSupportedImageFormats, (self, flags, image_type, 0, 0, &count));
1395 root 1.3 Newx (list, count, cl_image_format);
1396 root 1.23 NEED_SUCCESS (GetSupportedImageFormats, (self, flags, image_type, count, list, 0));
1397 root 1.3
1398     EXTEND (SP, count);
1399     for (i = 0; i < count; ++i)
1400     {
1401     AV *av = newAV ();
1402     av_store (av, 1, newSVuv (list [i].image_channel_data_type));
1403     av_store (av, 0, newSVuv (list [i].image_channel_order));
1404     PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
1405     }
1406 root 1.2 }
1407    
1408     void
1409 root 1.22 sampler (OpenCL::Context self, cl_bool normalized_coords, cl_addressing_mode addressing_mode, cl_filter_mode filter_mode)
1410 root 1.2 PPCODE:
1411 root 1.23 NEED_SUCCESS_ARG (cl_sampler sampler, CreateSampler, (self, normalized_coords, addressing_mode, filter_mode, &res));
1412 root 1.61 XPUSH_CLOBJ (stash_sampler, sampler);
1413 root 1.1
1414     void
1415 root 1.22 program_with_source (OpenCL::Context self, SV *program)
1416 root 1.1 PPCODE:
1417 root 1.2 STRLEN len;
1418     size_t len2;
1419     const char *ptr = SvPVbyte (program, len);
1420    
1421     len2 = len;
1422 root 1.23 NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithSource, (self, 1, &ptr, &len2, &res));
1423 root 1.61 XPUSH_CLOBJ (stash_program, prog);
1424 root 1.1
1425 root 1.64 void
1426     program_with_binary (OpenCL::Context self, SV *devices, SV *binaries)
1427     PPCODE:
1428 root 1.70 cl_uint device_count;
1429     cl_device_id *device_list = object_list (cv, 0, "devices", devices, "OpenCL::Device", &device_count);
1430 root 1.64
1431     if (!SvROK (binaries) || SvTYPE (SvRV (binaries)) != SVt_PVAV)
1432     croak ("OpenCL::Context::program_with_binary: binaries must be specified as reference to an array of strings");
1433    
1434     binaries = SvRV (binaries);
1435    
1436 root 1.70 if (device_count != av_len ((AV *)binaries) + 1)
1437 root 1.64 croak ("OpenCL::Context::program_with_binary: differing numbers of devices and binaries are not allowed");
1438    
1439 root 1.70 size_t *length_list = tmpbuf (sizeof (*length_list) * device_count);
1440     const unsigned char **binary_list = tmpbuf (sizeof (*binary_list) * device_count);
1441     cl_int *status_list = tmpbuf (sizeof (*status_list) * device_count);
1442 root 1.64
1443     int i;
1444 root 1.70 for (i = 0; i < device_count; ++i)
1445 root 1.64 {
1446     STRLEN len;
1447     binary_list [i] = (const unsigned char *)SvPVbyte (*av_fetch ((AV *)binaries, i, 0), len);
1448     length_list [i] = len;
1449     }
1450    
1451 root 1.70 NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithBinary, (self, device_count, device_list,
1452     length_list, binary_list,
1453     GIMME_V == G_ARRAY ? status_list : 0, &res));
1454 root 1.64
1455     EXTEND (SP, 2);
1456     PUSH_CLOBJ (stash_program, prog);
1457    
1458     if (GIMME_V == G_ARRAY)
1459     {
1460     AV *av = newAV ();
1461     PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
1462    
1463 root 1.70 for (i = device_count; i--; )
1464 root 1.64 av_store (av, i, newSViv (status_list [i]));
1465     }
1466    
1467 root 1.65 #if CL_VERSION_1_2
1468    
1469     void
1470     program_with_built_in_kernels (OpenCL::Context self, SV *devices, SV *kernel_names)
1471     PPCODE:
1472 root 1.71 cl_uint device_count;
1473     cl_device_id *device_list = object_list (cv, 0, "devices", devices, "OpenCL::Device", &device_count);
1474 root 1.69
1475     NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithBuiltInKernels, (self, device_count, device_list, SvPVbyte_nolen (kernel_names), &res));
1476    
1477     XPUSH_CLOBJ (stash_program, prog);
1478    
1479     void
1480     link_program (OpenCL::Context self, SV *devices, SV *options, SV *programs, SV *notify = &PL_sv_undef)
1481     CODE:
1482     cl_uint device_count = 0;
1483     cl_device_id *device_list = 0;
1484 root 1.65
1485 root 1.69 if (SvOK (devices))
1486     device_list = object_list (cv, 1, "devices", devices, "OpenCL::Device", &device_count);
1487 root 1.65
1488 root 1.69 cl_uint program_count;
1489     cl_program *program_list = object_list (cv, 0, "programs", programs, "OpenCL::Program", &program_count);
1490 root 1.65
1491 root 1.69 void *user_data;
1492     program_callback pfn_notify = make_program_callback (notify, &user_data);
1493 root 1.65
1494 root 1.69 NEED_SUCCESS_ARG (cl_program prog, LinkProgram, (self, device_count, device_list, SvPVbyte_nolen (options),
1495     program_count, program_list, pfn_notify, user_data, &res));
1496 root 1.65
1497     XPUSH_CLOBJ (stash_program, prog);
1498    
1499     #endif
1500    
1501 root 1.13 #BEGIN:context
1502    
1503 root 1.14 void
1504 root 1.22 reference_count (OpenCL::Context self)
1505 root 1.16 ALIAS:
1506     reference_count = CL_CONTEXT_REFERENCE_COUNT
1507     num_devices = CL_CONTEXT_NUM_DEVICES
1508 root 1.14 PPCODE:
1509     cl_uint value [1];
1510 root 1.22 NEED_SUCCESS (GetContextInfo, (self, ix, sizeof (value), value, 0));
1511 root 1.14 EXTEND (SP, 1);
1512     const int i = 0;
1513     PUSHs (sv_2mortal (newSVuv (value [i])));
1514    
1515     void
1516 root 1.22 devices (OpenCL::Context self)
1517 root 1.14 PPCODE:
1518     size_t size;
1519 root 1.22 NEED_SUCCESS (GetContextInfo, (self, CL_CONTEXT_DEVICES, 0, 0, &size));
1520 root 1.14 cl_device_id *value = tmpbuf (size);
1521 root 1.22 NEED_SUCCESS (GetContextInfo, (self, CL_CONTEXT_DEVICES, size, value, 0));
1522 root 1.15 int i, n = size / sizeof (*value);
1523 root 1.14 EXTEND (SP, n);
1524     for (i = 0; i < n; ++i)
1525     {
1526 root 1.61 PUSH_CLOBJ (stash_device, value [i]);
1527 root 1.14 }
1528    
1529     void
1530 root 1.22 properties (OpenCL::Context self)
1531 root 1.14 PPCODE:
1532     size_t size;
1533 root 1.22 NEED_SUCCESS (GetContextInfo, (self, CL_CONTEXT_PROPERTIES, 0, 0, &size));
1534 root 1.14 cl_context_properties *value = tmpbuf (size);
1535 root 1.22 NEED_SUCCESS (GetContextInfo, (self, CL_CONTEXT_PROPERTIES, size, value, 0));
1536 root 1.15 int i, n = size / sizeof (*value);
1537 root 1.14 EXTEND (SP, n);
1538     for (i = 0; i < n; ++i)
1539     PUSHs (sv_2mortal (newSVuv ((UV)value [i])));
1540    
1541 root 1.13 #END:context
1542    
1543 root 1.1 MODULE = OpenCL PACKAGE = OpenCL::Queue
1544    
1545     void
1546 root 1.22 DESTROY (OpenCL::Queue self)
1547 root 1.1 CODE:
1548 root 1.22 clReleaseCommandQueue (self);
1549 root 1.1
1550     void
1551 root 1.55 read_buffer (OpenCL::Queue self, OpenCL::Buffer mem, cl_bool blocking, size_t offset, size_t len, SV *data, ...)
1552     ALIAS:
1553     enqueue_read_buffer = 0
1554 root 1.2 PPCODE:
1555 root 1.61 EVENT_LIST (6);
1556 root 1.2
1557     SvUPGRADE (data, SVt_PV);
1558     SvGROW (data, len);
1559     SvPOK_only (data);
1560     SvCUR_set (data, len);
1561 root 1.69
1562     cl_event ev = 0;
1563 root 1.22 NEED_SUCCESS (EnqueueReadBuffer, (self, mem, blocking, offset, len, SvPVX (data), event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1564 root 1.2
1565     if (ev)
1566 root 1.61 XPUSH_CLOBJ (stash_event, ev);
1567 root 1.2
1568     void
1569 root 1.55 write_buffer (OpenCL::Queue self, OpenCL::Buffer mem, cl_bool blocking, size_t offset, SV *data, ...)
1570     ALIAS:
1571     enqueue_write_buffer = 0
1572 root 1.2 PPCODE:
1573 root 1.69 EVENT_LIST (5);
1574    
1575 root 1.2 STRLEN len;
1576     char *ptr = SvPVbyte (data, len);
1577    
1578 root 1.69 cl_event ev = 0;
1579 root 1.34 NEED_SUCCESS (EnqueueWriteBuffer, (self, mem, blocking, offset, len, ptr, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1580 root 1.2
1581     if (ev)
1582 root 1.61 XPUSH_CLOBJ (stash_event, ev);
1583 root 1.2
1584 root 1.48 #if CL_VERSION_1_2
1585    
1586     void
1587 root 1.55 fill_buffer (OpenCL::Queue self, OpenCL::Buffer mem, SV *data, size_t offset, size_t size, ...)
1588     ALIAS:
1589     enqueue_fill_buffer = 0
1590 root 1.48 PPCODE:
1591 root 1.69 EVENT_LIST (5);
1592    
1593 root 1.48 STRLEN len;
1594     char *ptr = SvPVbyte (data, len);
1595    
1596 root 1.69 cl_event ev = 0;
1597 root 1.48 NEED_SUCCESS (EnqueueFillBuffer, (self, mem, ptr, len, offset, size, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1598    
1599     if (ev)
1600 root 1.61 XPUSH_CLOBJ (stash_event, ev);
1601 root 1.48
1602     void
1603 root 1.55 fill_image (OpenCL::Queue self, OpenCL::Image img, NV r, NV g, NV b, NV a, size_t x, size_t y, size_t z, size_t width, size_t height, size_t depth, ...)
1604     ALIAS:
1605     enqueue_fill_image = 0
1606 root 1.48 PPCODE:
1607 root 1.69 EVENT_LIST (12);
1608    
1609 root 1.48 const size_t origin [3] = { x, y, z };
1610     const size_t region [3] = { width, height, depth };
1611    
1612     const cl_float c_f [4] = { r, g, b, a };
1613     const cl_uint c_u [4] = { r, g, b, a };
1614     const cl_int c_s [4] = { r, g, b, a };
1615     const void *c_fus [3] = { &c_f, &c_u, &c_s };
1616     static const char fus [] = { 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 1, 1, 1, 0, 0 };
1617     cl_image_format format;
1618 root 1.50 NEED_SUCCESS (GetImageInfo, (img, CL_IMAGE_FORMAT, sizeof (format), &format, 0));
1619 root 1.48 assert (sizeof (fus) == CL_FLOAT + 1 - CL_SNORM_INT8);
1620     if (format.image_channel_data_type < CL_SNORM_INT8 || CL_FLOAT < format.image_channel_data_type)
1621     croak ("enqueue_fill_image: image has unsupported channel type, only opencl 1.2 channel types supported.");
1622    
1623 root 1.69 cl_event ev = 0;
1624 root 1.55 NEED_SUCCESS (EnqueueFillImage, (self, img, c_fus [fus [format.image_channel_data_type - CL_SNORM_INT8]],
1625 root 1.48 origin, region, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1626    
1627     if (ev)
1628 root 1.61 XPUSH_CLOBJ (stash_event, ev);
1629 root 1.48
1630     #endif
1631    
1632 root 1.2 void
1633 root 1.55 copy_buffer (OpenCL::Queue self, OpenCL::Buffer src, OpenCL::Buffer dst, size_t src_offset, size_t dst_offset, size_t len, ...)
1634     ALIAS:
1635     enqueue_copy_buffer = 0
1636 root 1.2 PPCODE:
1637 root 1.61 EVENT_LIST (6);
1638 root 1.2
1639 root 1.69 cl_event ev = 0;
1640 root 1.22 NEED_SUCCESS (EnqueueCopyBuffer, (self, src, dst, src_offset, dst_offset, len, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1641 root 1.2
1642     if (ev)
1643 root 1.61 XPUSH_CLOBJ (stash_event, ev);
1644 root 1.2
1645 root 1.3 void
1646 root 1.55 read_buffer_rect (OpenCL::Queue self, OpenCL::Memory buf, cl_bool blocking, size_t buf_x, size_t buf_y, size_t buf_z, size_t host_x, size_t host_y, size_t host_z, size_t width, size_t height, size_t depth, size_t buf_row_pitch, size_t buf_slice_pitch, size_t host_row_pitch, size_t host_slice_pitch, SV *data, ...)
1647     ALIAS:
1648     enqueue_read_buffer_rect = 0
1649 root 1.17 PPCODE:
1650 root 1.69 EVENT_LIST (17);
1651    
1652 root 1.17 const size_t buf_origin [3] = { buf_x , buf_y , buf_z };
1653     const size_t host_origin[3] = { host_x, host_y, host_z };
1654     const size_t region[3] = { width, height, depth };
1655    
1656     if (!buf_row_pitch)
1657     buf_row_pitch = region [0];
1658    
1659     if (!buf_slice_pitch)
1660     buf_slice_pitch = region [1] * buf_row_pitch;
1661    
1662     if (!host_row_pitch)
1663     host_row_pitch = region [0];
1664    
1665     if (!host_slice_pitch)
1666     host_slice_pitch = region [1] * host_row_pitch;
1667    
1668     size_t len = host_row_pitch * host_slice_pitch * region [2];
1669    
1670     SvUPGRADE (data, SVt_PV);
1671     SvGROW (data, len);
1672     SvPOK_only (data);
1673     SvCUR_set (data, len);
1674 root 1.69
1675     cl_event ev = 0;
1676 root 1.22 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));
1677 root 1.17
1678     if (ev)
1679 root 1.61 XPUSH_CLOBJ (stash_event, ev);
1680 root 1.17
1681     void
1682 root 1.55 write_buffer_rect (OpenCL::Queue self, OpenCL::Memory buf, cl_bool blocking, size_t buf_x, size_t buf_y, size_t buf_z, size_t host_x, size_t host_y, size_t host_z, size_t width, size_t height, size_t depth, size_t buf_row_pitch, size_t buf_slice_pitch, size_t host_row_pitch, size_t host_slice_pitch, SV *data, ...)
1683     ALIAS:
1684     enqueue_write_buffer_rect = 0
1685 root 1.17 PPCODE:
1686 root 1.69 EVENT_LIST (17);
1687    
1688 root 1.17 const size_t buf_origin [3] = { buf_x , buf_y , buf_z };
1689     const size_t host_origin[3] = { host_x, host_y, host_z };
1690     const size_t region[3] = { width, height, depth };
1691     STRLEN len;
1692     char *ptr = SvPVbyte (data, len);
1693    
1694     if (!buf_row_pitch)
1695     buf_row_pitch = region [0];
1696    
1697     if (!buf_slice_pitch)
1698     buf_slice_pitch = region [1] * buf_row_pitch;
1699    
1700     if (!host_row_pitch)
1701     host_row_pitch = region [0];
1702    
1703     if (!host_slice_pitch)
1704     host_slice_pitch = region [1] * host_row_pitch;
1705    
1706     size_t min_len = host_row_pitch * host_slice_pitch * region [2];
1707    
1708     if (len < min_len)
1709     croak ("clEnqueueWriteImage: data string is shorter than what would be transferred");
1710    
1711 root 1.69 cl_event ev = 0;
1712 root 1.37 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));
1713 root 1.17
1714     if (ev)
1715 root 1.61 XPUSH_CLOBJ (stash_event, ev);
1716 root 1.17
1717     void
1718 root 1.55 copy_buffer_rect (OpenCL::Queue self, OpenCL::Buffer src, OpenCL::Buffer dst, size_t src_x, size_t src_y, size_t src_z, size_t dst_x, size_t dst_y, size_t dst_z, size_t width, size_t height, size_t depth, size_t src_row_pitch, size_t src_slice_pitch, size_t dst_row_pitch, size_t dst_slice_pitch, ...)
1719     ALIAS:
1720     enqueue_copy_buffer_rect = 0
1721 root 1.18 PPCODE:
1722 root 1.69 EVENT_LIST (16);
1723    
1724 root 1.18 const size_t src_origin[3] = { src_x, src_y, src_z };
1725     const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
1726     const size_t region[3] = { width, height, depth };
1727    
1728 root 1.69 cl_event ev = 0;
1729 root 1.22 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));
1730 root 1.18
1731     if (ev)
1732 root 1.61 XPUSH_CLOBJ (stash_event, ev);
1733 root 1.18
1734     void
1735 root 1.55 read_image (OpenCL::Queue self, OpenCL::Image src, cl_bool blocking, size_t src_x, size_t src_y, size_t src_z, size_t width, size_t height, size_t depth, size_t row_pitch, size_t slice_pitch, SV *data, ...)
1736     ALIAS:
1737     enqueue_read_image = 0
1738 root 1.3 PPCODE:
1739 root 1.69 EVENT_LIST (12);
1740    
1741 root 1.3 const size_t src_origin[3] = { src_x, src_y, src_z };
1742     const size_t region[3] = { width, height, depth };
1743 root 1.10
1744 root 1.11 if (!row_pitch)
1745     row_pitch = img_row_pitch (src);
1746    
1747     if (depth > 1 && !slice_pitch)
1748     slice_pitch = row_pitch * height;
1749    
1750     size_t len = slice_pitch ? slice_pitch * depth : row_pitch * height;
1751 root 1.3
1752     SvUPGRADE (data, SVt_PV);
1753     SvGROW (data, len);
1754     SvPOK_only (data);
1755     SvCUR_set (data, len);
1756 root 1.69
1757     cl_event ev = 0;
1758 root 1.22 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));
1759 root 1.3
1760     if (ev)
1761 root 1.61 XPUSH_CLOBJ (stash_event, ev);
1762 root 1.3
1763     void
1764 root 1.55 write_image (OpenCL::Queue self, OpenCL::Image dst, cl_bool blocking, size_t dst_x, size_t dst_y, size_t dst_z, size_t width, size_t height, size_t depth, size_t row_pitch, size_t slice_pitch, SV *data, ...)
1765     ALIAS:
1766     enqueue_write_image = 0
1767 root 1.3 PPCODE:
1768 root 1.69 EVENT_LIST (12);
1769    
1770 root 1.3 const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
1771     const size_t region[3] = { width, height, depth };
1772     STRLEN len;
1773     char *ptr = SvPVbyte (data, len);
1774    
1775 root 1.11 if (!row_pitch)
1776     row_pitch = img_row_pitch (dst);
1777    
1778     if (depth > 1 && !slice_pitch)
1779     slice_pitch = row_pitch * height;
1780    
1781     size_t min_len = slice_pitch ? slice_pitch * depth : row_pitch * height;
1782    
1783     if (len < min_len)
1784     croak ("clEnqueueWriteImage: data string is shorter than what would be transferred");
1785    
1786 root 1.69 cl_event ev = 0;
1787 root 1.37 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));
1788 root 1.3
1789     if (ev)
1790 root 1.61 XPUSH_CLOBJ (stash_event, ev);
1791 root 1.3
1792     void
1793 root 1.55 copy_image (OpenCL::Queue self, OpenCL::Image src, OpenCL::Image dst, size_t src_x, size_t src_y, size_t src_z, size_t dst_x, size_t dst_y, size_t dst_z, size_t width, size_t height, size_t depth, ...)
1794     ALIAS:
1795     enqueue_copy_image = 0
1796 root 1.3 PPCODE:
1797 root 1.69 EVENT_LIST (12);
1798    
1799 root 1.3 const size_t src_origin[3] = { src_x, src_y, src_z };
1800     const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
1801     const size_t region[3] = { width, height, depth };
1802    
1803 root 1.69 cl_event ev = 0;
1804 root 1.22 NEED_SUCCESS (EnqueueCopyImage, (self, src, dst, src_origin, dst_origin, region, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1805 root 1.3
1806     if (ev)
1807 root 1.61 XPUSH_CLOBJ (stash_event, ev);
1808 root 1.3
1809     void
1810 root 1.55 copy_image_to_buffer (OpenCL::Queue self, OpenCL::Image src, OpenCL::Buffer dst, size_t src_x, size_t src_y, size_t src_z, size_t width, size_t height, size_t depth, size_t dst_offset, ...)
1811     ALIAS:
1812     enqueue_copy_image_to_buffer = 0
1813 root 1.3 PPCODE:
1814 root 1.69 EVENT_LIST (10);
1815    
1816 root 1.61 const size_t src_origin[3] = { src_x, src_y, src_z };
1817     const size_t region [3] = { width, height, depth };
1818 root 1.3
1819 root 1.69 cl_event ev = 0;
1820 root 1.22 NEED_SUCCESS (EnqueueCopyImageToBuffer, (self, src, dst, src_origin, region, dst_offset, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1821 root 1.3
1822     if (ev)
1823 root 1.61 XPUSH_CLOBJ (stash_event, ev);
1824 root 1.3
1825     void
1826 root 1.55 copy_buffer_to_image (OpenCL::Queue self, OpenCL::Buffer src, OpenCL::Image dst, size_t src_offset, size_t dst_x, size_t dst_y, size_t dst_z, size_t width, size_t height, size_t depth, ...)
1827     ALIAS:
1828     enqueue_copy_buffer_to_image = 0
1829 root 1.3 PPCODE:
1830 root 1.69 EVENT_LIST (10);
1831    
1832 root 1.61 const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
1833     const size_t region [3] = { width, height, depth };
1834 root 1.3
1835 root 1.69 cl_event ev = 0;
1836 root 1.22 NEED_SUCCESS (EnqueueCopyBufferToImage, (self, src, dst, src_offset, dst_origin, region, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1837 root 1.3
1838     if (ev)
1839 root 1.61 XPUSH_CLOBJ (stash_event, ev);
1840    
1841     void
1842 root 1.64 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, ...)
1843 root 1.61 ALIAS:
1844     enqueue_map_buffer = 0
1845     PPCODE:
1846 root 1.62 EVENT_LIST (6);
1847 root 1.69
1848 root 1.64 size_t cb = SvIV (cb_);
1849    
1850     if (!SvOK (cb_))
1851     {
1852     NEED_SUCCESS (GetMemObjectInfo, (buf, CL_MEM_SIZE, sizeof (cb), &cb, 0));
1853     cb -= offset;
1854     }
1855 root 1.61
1856 root 1.69 cl_event ev;
1857 root 1.61 NEED_SUCCESS_ARG (void *ptr, EnqueueMapBuffer, (self, buf, blocking, map_flags, offset, cb, event_list_count, event_list_ptr, &ev, &res));
1858     XPUSHs (mapped_new (stash_mappedbuffer, self, buf, map_flags, ptr, cb, ev, 0, 0));
1859    
1860     void
1861 root 1.64 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, ...)
1862 root 1.61 ALIAS:
1863     enqueue_map_image = 0
1864     PPCODE:
1865 root 1.64 size_t width = SvIV (width_);
1866     if (!SvOK (width_))
1867     {
1868     NEED_SUCCESS (GetImageInfo, (img, CL_IMAGE_WIDTH, sizeof (width), &width, 0));
1869     width -= x;
1870     }
1871    
1872     size_t height = SvIV (width_);
1873     if (!SvOK (height_))
1874     {
1875     NEED_SUCCESS (GetImageInfo, (img, CL_IMAGE_HEIGHT, sizeof (height), &height, 0));
1876     height -= y;
1877     }
1878    
1879     size_t depth = SvIV (width_);
1880     if (!SvOK (depth_))
1881     {
1882     NEED_SUCCESS (GetImageInfo, (img, CL_IMAGE_DEPTH, sizeof (depth), &depth, 0));
1883     depth -= z;
1884    
1885     // stupid opencl returns 0 for depth, but requires 1 for 2d images
1886     if (!depth)
1887     depth = 1;
1888     }
1889    
1890 root 1.61 const size_t origin[3] = { x, y, z };
1891     const size_t region[3] = { width, height, depth };
1892     size_t row_pitch, slice_pitch;
1893 root 1.62 EVENT_LIST (10);
1894 root 1.61
1895 root 1.64 cl_event ev;
1896 root 1.61 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));
1897    
1898     size_t cb = slice_pitch ? slice_pitch * region [2]
1899     : row_pitch ? row_pitch * region [1]
1900     : region [0];
1901    
1902     XPUSHs (mapped_new (stash_mappedimage, self, img, map_flags, ptr, cb, ev, row_pitch, slice_pitch));
1903    
1904     void
1905     unmap (OpenCL::Queue self, OpenCL::Mapped mapped, ...)
1906     PPCODE:
1907 root 1.71 mapped_unmap (cv, ST (1), mapped, self, &ST (2), items - 2);
1908 root 1.61 if (GIMME_V != G_VOID)
1909 root 1.62 {
1910     clRetainEvent (mapped->event);
1911     XPUSH_CLOBJ (stash_event, mapped->event);
1912     }
1913 root 1.3
1914     void
1915 root 1.55 task (OpenCL::Queue self, OpenCL::Kernel kernel, ...)
1916     ALIAS:
1917     enqueue_task = 0
1918 root 1.3 PPCODE:
1919 root 1.61 EVENT_LIST (2);
1920 root 1.3
1921 root 1.69 cl_event ev = 0;
1922 root 1.22 NEED_SUCCESS (EnqueueTask, (self, kernel, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1923 root 1.3
1924     if (ev)
1925 root 1.61 XPUSH_CLOBJ (stash_event, ev);
1926 root 1.3
1927 root 1.4 void
1928 root 1.55 nd_range_kernel (OpenCL::Queue self, OpenCL::Kernel kernel, SV *global_work_offset, SV *global_work_size, SV *local_work_size = &PL_sv_undef, ...)
1929     ALIAS:
1930     enqueue_nd_range_kernel = 0
1931 root 1.4 PPCODE:
1932 root 1.69 EVENT_LIST (5);
1933    
1934 root 1.4 size_t *gwo = 0, *gws, *lws = 0;
1935     int gws_len;
1936     size_t *lists;
1937     int i;
1938    
1939     if (!SvROK (global_work_size) || SvTYPE (SvRV (global_work_size)) != SVt_PVAV)
1940     croak ("clEnqueueNDRangeKernel: global_work_size must be an array reference");
1941    
1942     gws_len = AvFILLp (SvRV (global_work_size)) + 1;
1943    
1944     lists = tmpbuf (sizeof (size_t) * 3 * gws_len);
1945    
1946     gws = lists + gws_len * 0;
1947     for (i = 0; i < gws_len; ++i)
1948 root 1.58 {
1949     gws [i] = SvIV (AvARRAY (SvRV (global_work_size))[i]);
1950     // at least nvidia crashes for 0-sized work group sizes, work around
1951     if (!gws [i])
1952     croak ("clEnqueueNDRangeKernel: global_work_size[%d] is zero, must be non-zero", i);
1953     }
1954 root 1.4
1955     if (SvOK (global_work_offset))
1956     {
1957     if (!SvROK (global_work_offset) || SvTYPE (SvRV (global_work_offset)) != SVt_PVAV)
1958     croak ("clEnqueueNDRangeKernel: global_work_offset must be undef or an array reference");
1959    
1960     if (AvFILLp (SvRV (global_work_size)) + 1 != gws_len)
1961     croak ("clEnqueueNDRangeKernel: global_work_offset must be undef or an array of same size as global_work_size");
1962    
1963     gwo = lists + gws_len * 1;
1964     for (i = 0; i < gws_len; ++i)
1965     gwo [i] = SvIV (AvARRAY (SvRV (global_work_offset))[i]);
1966     }
1967    
1968     if (SvOK (local_work_size))
1969     {
1970 root 1.37 if ((SvOK (local_work_size) && !SvROK (local_work_size)) || SvTYPE (SvRV (local_work_size)) != SVt_PVAV)
1971 root 1.58 croak ("clEnqueueNDRangeKernel: local_work_size must be undef or an array reference");
1972 root 1.4
1973     if (AvFILLp (SvRV (local_work_size)) + 1 != gws_len)
1974     croak ("clEnqueueNDRangeKernel: local_work_local must be undef or an array of same size as global_work_size");
1975    
1976     lws = lists + gws_len * 2;
1977     for (i = 0; i < gws_len; ++i)
1978 root 1.58 {
1979     lws [i] = SvIV (AvARRAY (SvRV (local_work_size))[i]);
1980     // at least nvidia crashes for 0-sized work group sizes, work around
1981     if (!lws [i])
1982     croak ("clEnqueueNDRangeKernel: local_work_size[%d] is zero, must be non-zero", i);
1983     }
1984 root 1.4 }
1985    
1986 root 1.69 cl_event ev = 0;
1987 root 1.22 NEED_SUCCESS (EnqueueNDRangeKernel, (self, kernel, gws_len, gwo, gws, lws, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1988 root 1.4
1989     if (ev)
1990 root 1.61 XPUSH_CLOBJ (stash_event, ev);
1991 root 1.3
1992 root 1.65 #if CL_VERSION_1_2
1993    
1994     void
1995     migrate_mem_objects (OpenCL::Queue self, SV *objects, cl_mem_migration_flags flags, ...)
1996     ALIAS:
1997     enqueue_migrate_mem_objects = 0
1998     PPCODE:
1999     EVENT_LIST (3);
2000    
2001 root 1.69 cl_uint obj_count;
2002     cl_mem *obj_list = object_list (cv, 0, "objects", objects, "OpenCL::Memory", &obj_count);
2003 root 1.65
2004     cl_event ev = 0;
2005 root 1.69 NEED_SUCCESS (EnqueueMigrateMemObjects, (self, obj_count, obj_list, flags, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
2006 root 1.65
2007     if (ev)
2008     XPUSH_CLOBJ (stash_event, ev);
2009    
2010     #endif
2011    
2012 root 1.25 #if cl_apple_gl_sharing || cl_khr_gl_sharing
2013    
2014     void
2015 root 1.55 acquire_gl_objects (OpenCL::Queue self, SV *objects, ...)
2016     ALIAS:
2017 root 1.60 release_gl_objects = 1
2018 root 1.55 enqueue_acquire_gl_objects = 0
2019 root 1.27 enqueue_release_gl_objects = 1
2020 root 1.36 PPCODE:
2021 root 1.69 EVENT_LIST (2);
2022    
2023     cl_uint obj_count;
2024     cl_mem *obj_list = object_list (cv, 0, "objects", objects, "OpenCL::Memory", &obj_count);
2025 root 1.27
2026 root 1.25 cl_event ev = 0;
2027    
2028 root 1.27 if (ix)
2029 root 1.69 NEED_SUCCESS (EnqueueReleaseGLObjects, (self, obj_count, obj_list, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
2030 root 1.27 else
2031 root 1.69 NEED_SUCCESS (EnqueueAcquireGLObjects, (self, obj_count, obj_list, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
2032 root 1.25
2033     if (ev)
2034 root 1.61 XPUSH_CLOBJ (stash_event, ev);
2035 root 1.25
2036     #endif
2037    
2038 root 1.2 void
2039 root 1.55 wait_for_events (OpenCL::Queue self, ...)
2040     ALIAS:
2041     enqueue_wait_for_events = 0
2042 root 1.2 CODE:
2043 root 1.61 EVENT_LIST (1);
2044 root 1.47 #if PREFER_1_1
2045 root 1.22 NEED_SUCCESS (EnqueueWaitForEvents, (self, event_list_count, event_list_ptr));
2046 root 1.47 #else
2047     NEED_SUCCESS (EnqueueBarrierWithWaitList, (self, event_list_count, event_list_ptr, 0));
2048 root 1.38 #endif
2049    
2050     void
2051 root 1.55 marker (OpenCL::Queue self, ...)
2052     ALIAS:
2053     enqueue_marker = 0
2054 root 1.38 PPCODE:
2055 root 1.69 EVENT_LIST (1);
2056 root 1.38 cl_event ev = 0;
2057 root 1.45 #if PREFER_1_1
2058 root 1.47 if (!event_list_count)
2059     NEED_SUCCESS (EnqueueMarker, (self, GIMME_V != G_VOID ? &ev : 0));
2060     else
2061 root 1.46 #if CL_VERSION_1_2
2062     NEED_SUCCESS (EnqueueMarkerWithWaitList, (self, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
2063     #else
2064 root 1.50 {
2065     NEED_SUCCESS (EnqueueWaitForEvents, (self, event_list_count, event_list_ptr)); // also a barrier
2066     NEED_SUCCESS (EnqueueMarker, (self, GIMME_V != G_VOID ? &ev : 0));
2067     }
2068 root 1.46 #endif
2069 root 1.45 #else
2070     NEED_SUCCESS (EnqueueMarkerWithWaitList, (self, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
2071 root 1.38 #endif
2072     if (ev)
2073 root 1.61 XPUSH_CLOBJ (stash_event, ev);
2074 root 1.38
2075 root 1.2 void
2076 root 1.55 barrier (OpenCL::Queue self, ...)
2077     ALIAS:
2078     enqueue_barrier = 0
2079 root 1.38 PPCODE:
2080 root 1.69 EVENT_LIST (1);
2081 root 1.38 cl_event ev = 0;
2082 root 1.45 #if PREFER_1_1
2083 root 1.47 if (!event_list_count && GIMME_V == G_VOID)
2084     NEED_SUCCESS (EnqueueBarrier, (self));
2085     else
2086 root 1.46 #if CL_VERSION_1_2
2087     NEED_SUCCESS (EnqueueBarrierWithWaitList, (self, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
2088     #else
2089 root 1.47 {
2090     if (event_list_count)
2091 root 1.50 NEED_SUCCESS (EnqueueWaitForEvents, (self, event_list_count, event_list_ptr));
2092 root 1.47
2093     if (GIMME_V != G_VOID)
2094     NEED_SUCCESS (EnqueueMarker, (self, &ev));
2095     }
2096 root 1.46 #endif
2097 root 1.45 #else
2098 root 1.46 NEED_SUCCESS (EnqueueBarrierWithWaitList, (self, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
2099 root 1.37 #endif
2100 root 1.38 if (ev)
2101 root 1.61 XPUSH_CLOBJ (stash_event, ev);
2102 root 1.37
2103 root 1.3 void
2104 root 1.22 flush (OpenCL::Queue self)
2105 root 1.3 CODE:
2106 root 1.22 NEED_SUCCESS (Flush, (self));
2107 root 1.3
2108     void
2109 root 1.22 finish (OpenCL::Queue self)
2110 root 1.3 CODE:
2111 root 1.22 NEED_SUCCESS (Finish, (self));
2112 root 1.3
2113 root 1.14 void
2114 root 1.22 info (OpenCL::Queue self, cl_command_queue_info name)
2115 root 1.14 PPCODE:
2116     INFO (CommandQueue)
2117    
2118     #BEGIN:command_queue
2119    
2120     void
2121 root 1.22 context (OpenCL::Queue self)
2122 root 1.14 PPCODE:
2123     cl_context value [1];
2124 root 1.22 NEED_SUCCESS (GetCommandQueueInfo, (self, CL_QUEUE_CONTEXT, sizeof (value), value, 0));
2125 root 1.14 EXTEND (SP, 1);
2126     const int i = 0;
2127     {
2128     NEED_SUCCESS (RetainContext, (value [i]));
2129 root 1.61 PUSH_CLOBJ (stash_context, value [i]);
2130 root 1.14 }
2131    
2132     void
2133 root 1.22 device (OpenCL::Queue self)
2134 root 1.14 PPCODE:
2135     cl_device_id value [1];
2136 root 1.22 NEED_SUCCESS (GetCommandQueueInfo, (self, CL_QUEUE_DEVICE, sizeof (value), value, 0));
2137 root 1.14 EXTEND (SP, 1);
2138     const int i = 0;
2139     {
2140 root 1.61 PUSH_CLOBJ (stash_device, value [i]);
2141 root 1.14 }
2142    
2143     void
2144 root 1.22 reference_count (OpenCL::Queue self)
2145 root 1.14 PPCODE:
2146     cl_uint value [1];
2147 root 1.22 NEED_SUCCESS (GetCommandQueueInfo, (self, CL_QUEUE_REFERENCE_COUNT, sizeof (value), value, 0));
2148 root 1.14 EXTEND (SP, 1);
2149     const int i = 0;
2150     PUSHs (sv_2mortal (newSVuv (value [i])));
2151    
2152     void
2153 root 1.22 properties (OpenCL::Queue self)
2154 root 1.14 PPCODE:
2155     cl_command_queue_properties value [1];
2156 root 1.22 NEED_SUCCESS (GetCommandQueueInfo, (self, CL_QUEUE_PROPERTIES, sizeof (value), value, 0));
2157 root 1.14 EXTEND (SP, 1);
2158     const int i = 0;
2159 root 1.61 PUSHs (sv_2mortal (newSVuv (value [i])));
2160 root 1.14
2161     #END:command_queue
2162    
2163 root 1.2 MODULE = OpenCL PACKAGE = OpenCL::Memory
2164    
2165     void
2166 root 1.22 DESTROY (OpenCL::Memory self)
2167 root 1.2 CODE:
2168 root 1.22 clReleaseMemObject (self);
2169 root 1.2
2170     void
2171 root 1.22 info (OpenCL::Memory self, cl_mem_info name)
2172 root 1.2 PPCODE:
2173     INFO (MemObject)
2174    
2175 root 1.14 #BEGIN:mem
2176    
2177     void
2178 root 1.22 type (OpenCL::Memory self)
2179 root 1.14 PPCODE:
2180     cl_mem_object_type value [1];
2181 root 1.22 NEED_SUCCESS (GetMemObjectInfo, (self, CL_MEM_TYPE, sizeof (value), value, 0));
2182 root 1.14 EXTEND (SP, 1);
2183     const int i = 0;
2184 root 1.61 PUSHs (sv_2mortal (newSVuv (value [i])));
2185 root 1.14
2186     void
2187 root 1.22 flags (OpenCL::Memory self)
2188 root 1.14 PPCODE:
2189     cl_mem_flags value [1];
2190 root 1.22 NEED_SUCCESS (GetMemObjectInfo, (self, CL_MEM_FLAGS, sizeof (value), value, 0));
2191 root 1.14 EXTEND (SP, 1);
2192     const int i = 0;
2193 root 1.61 PUSHs (sv_2mortal (newSVuv (value [i])));
2194 root 1.14
2195     void
2196 root 1.22 size (OpenCL::Memory self)
2197 root 1.16 ALIAS:
2198     size = CL_MEM_SIZE
2199     offset = CL_MEM_OFFSET
2200 root 1.14 PPCODE:
2201     size_t value [1];
2202 root 1.22 NEED_SUCCESS (GetMemObjectInfo, (self, ix, sizeof (value), value, 0));
2203 root 1.14 EXTEND (SP, 1);
2204     const int i = 0;
2205     PUSHs (sv_2mortal (newSVuv (value [i])));
2206    
2207     void
2208 root 1.22 host_ptr (OpenCL::Memory self)
2209 root 1.14 PPCODE:
2210     void * value [1];
2211 root 1.22 NEED_SUCCESS (GetMemObjectInfo, (self, CL_MEM_HOST_PTR, sizeof (value), value, 0));
2212 root 1.14 EXTEND (SP, 1);
2213     const int i = 0;
2214     PUSHs (sv_2mortal (newSVuv ((IV)(intptr_t)value [i])));
2215    
2216     void
2217 root 1.22 map_count (OpenCL::Memory self)
2218 root 1.16 ALIAS:
2219     map_count = CL_MEM_MAP_COUNT
2220     reference_count = CL_MEM_REFERENCE_COUNT
2221 root 1.14 PPCODE:
2222     cl_uint value [1];
2223 root 1.22 NEED_SUCCESS (GetMemObjectInfo, (self, ix, sizeof (value), value, 0));
2224 root 1.14 EXTEND (SP, 1);
2225     const int i = 0;
2226     PUSHs (sv_2mortal (newSVuv (value [i])));
2227    
2228     void
2229 root 1.22 context (OpenCL::Memory self)
2230 root 1.14 PPCODE:
2231     cl_context value [1];
2232 root 1.22 NEED_SUCCESS (GetMemObjectInfo, (self, CL_MEM_CONTEXT, sizeof (value), value, 0));
2233 root 1.14 EXTEND (SP, 1);
2234     const int i = 0;
2235     {
2236     NEED_SUCCESS (RetainContext, (value [i]));
2237 root 1.61 PUSH_CLOBJ (stash_context, value [i]);
2238 root 1.14 }
2239    
2240     void
2241 root 1.22 associated_memobject (OpenCL::Memory self)
2242 root 1.14 PPCODE:
2243     cl_mem value [1];
2244 root 1.22 NEED_SUCCESS (GetMemObjectInfo, (self, CL_MEM_ASSOCIATED_MEMOBJECT, sizeof (value), value, 0));
2245 root 1.14 EXTEND (SP, 1);
2246     const int i = 0;
2247     {
2248     NEED_SUCCESS (RetainMemObject, (value [i]));
2249 root 1.61 PUSH_CLOBJ (stash_memory, value [i]);
2250 root 1.14 }
2251    
2252     #END:mem
2253    
2254 root 1.26 #if cl_apple_gl_sharing || cl_khr_gl_sharing
2255    
2256     void
2257     gl_object_info (OpenCL::Memory self)
2258     PPCODE:
2259     cl_gl_object_type type;
2260     cl_GLuint name;
2261 root 1.31 NEED_SUCCESS (GetGLObjectInfo, (self, &type, &name));
2262 root 1.26 EXTEND (SP, 2);
2263     PUSHs (sv_2mortal (newSVuv (type)));
2264     PUSHs (sv_2mortal (newSVuv (name)));
2265    
2266     #endif
2267    
2268 root 1.18 MODULE = OpenCL PACKAGE = OpenCL::BufferObj
2269    
2270     void
2271 root 1.22 sub_buffer_region (OpenCL::BufferObj self, cl_mem_flags flags, size_t origin, size_t size)
2272 root 1.18 PPCODE:
2273     if (flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR | CL_MEM_ALLOC_HOST_PTR))
2274     croak ("clCreateSubBuffer: cannot use/copy/alloc host ptr, doesn't make sense, check your flags!");
2275    
2276     cl_buffer_region crdata = { origin, size };
2277    
2278 root 1.22 NEED_SUCCESS_ARG (cl_mem mem, CreateSubBuffer, (self, flags, CL_BUFFER_CREATE_TYPE_REGION, &crdata, &res));
2279 root 1.61 XPUSH_CLOBJ (stash_buffer, mem);
2280 root 1.18
2281 root 1.13 MODULE = OpenCL PACKAGE = OpenCL::Image
2282    
2283     void
2284 root 1.22 image_info (OpenCL::Image self, cl_image_info name)
2285 root 1.13 PPCODE:
2286     INFO (Image)
2287    
2288 root 1.49 void
2289     format (OpenCL::Image self)
2290     PPCODE:
2291     cl_image_format format;
2292     NEED_SUCCESS (GetImageInfo, (self, CL_IMAGE_FORMAT, sizeof (format), &format, 0));
2293     EXTEND (SP, 2);
2294     PUSHs (sv_2mortal (newSVuv (format.image_channel_order)));
2295     PUSHs (sv_2mortal (newSVuv (format.image_channel_data_type)));
2296    
2297 root 1.14 #BEGIN:image
2298    
2299     void
2300 root 1.22 element_size (OpenCL::Image self)
2301 root 1.16 ALIAS:
2302     element_size = CL_IMAGE_ELEMENT_SIZE
2303     row_pitch = CL_IMAGE_ROW_PITCH
2304     slice_pitch = CL_IMAGE_SLICE_PITCH
2305     width = CL_IMAGE_WIDTH
2306     height = CL_IMAGE_HEIGHT
2307     depth = CL_IMAGE_DEPTH
2308 root 1.14 PPCODE:
2309     size_t value [1];
2310 root 1.22 NEED_SUCCESS (GetImageInfo, (self, ix, sizeof (value), value, 0));
2311 root 1.14 EXTEND (SP, 1);
2312     const int i = 0;
2313     PUSHs (sv_2mortal (newSVuv (value [i])));
2314    
2315     #END:image
2316    
2317 root 1.26 #if cl_apple_gl_sharing || cl_khr_gl_sharing
2318    
2319     #BEGIN:gl_texture
2320    
2321     void
2322     target (OpenCL::Image self)
2323     PPCODE:
2324     cl_GLenum value [1];
2325 root 1.31 NEED_SUCCESS (GetGLTextureInfo, (self, CL_GL_TEXTURE_TARGET, sizeof (value), value, 0));
2326 root 1.26 EXTEND (SP, 1);
2327     const int i = 0;
2328     PUSHs (sv_2mortal (newSVuv (value [i])));
2329    
2330     void
2331     gl_mipmap_level (OpenCL::Image self)
2332     PPCODE:
2333     cl_GLint value [1];
2334 root 1.31 NEED_SUCCESS (GetGLTextureInfo, (self, CL_GL_MIPMAP_LEVEL, sizeof (value), value, 0));
2335 root 1.26 EXTEND (SP, 1);
2336     const int i = 0;
2337     PUSHs (sv_2mortal (newSViv (value [i])));
2338    
2339     #END:gl_texture
2340    
2341     #endif
2342    
2343 root 1.2 MODULE = OpenCL PACKAGE = OpenCL::Sampler
2344    
2345     void
2346 root 1.22 DESTROY (OpenCL::Sampler self)
2347 root 1.2 CODE:
2348 root 1.22 clReleaseSampler (self);
2349 root 1.2
2350     void
2351 root 1.22 info (OpenCL::Sampler self, cl_sampler_info name)
2352 root 1.2 PPCODE:
2353     INFO (Sampler)
2354    
2355 root 1.14 #BEGIN:sampler
2356    
2357     void
2358 root 1.22 reference_count (OpenCL::Sampler self)
2359 root 1.14 PPCODE:
2360     cl_uint value [1];
2361 root 1.22 NEED_SUCCESS (GetSamplerInfo, (self, CL_SAMPLER_REFERENCE_COUNT, sizeof (value), value, 0));
2362 root 1.14 EXTEND (SP, 1);
2363     const int i = 0;
2364     PUSHs (sv_2mortal (newSVuv (value [i])));
2365    
2366     void
2367 root 1.22 context (OpenCL::Sampler self)
2368 root 1.14 PPCODE:
2369     cl_context value [1];
2370 root 1.22 NEED_SUCCESS (GetSamplerInfo, (self, CL_SAMPLER_CONTEXT, sizeof (value), value, 0));
2371 root 1.14 EXTEND (SP, 1);
2372     const int i = 0;
2373     {
2374     NEED_SUCCESS (RetainContext, (value [i]));
2375 root 1.61 PUSH_CLOBJ (stash_context, value [i]);
2376 root 1.14 }
2377    
2378     void
2379 root 1.22 normalized_coords (OpenCL::Sampler self)
2380 root 1.14 PPCODE:
2381     cl_addressing_mode value [1];
2382 root 1.22 NEED_SUCCESS (GetSamplerInfo, (self, CL_SAMPLER_NORMALIZED_COORDS, sizeof (value), value, 0));
2383 root 1.14 EXTEND (SP, 1);
2384     const int i = 0;
2385 root 1.61 PUSHs (sv_2mortal (newSVuv (value [i])));
2386 root 1.14
2387     void
2388 root 1.22 addressing_mode (OpenCL::Sampler self)
2389 root 1.14 PPCODE:
2390     cl_filter_mode value [1];
2391 root 1.22 NEED_SUCCESS (GetSamplerInfo, (self, CL_SAMPLER_ADDRESSING_MODE, sizeof (value), value, 0));
2392 root 1.14 EXTEND (SP, 1);
2393     const int i = 0;
2394 root 1.61 PUSHs (sv_2mortal (newSVuv (value [i])));
2395 root 1.14
2396     void
2397 root 1.22 filter_mode (OpenCL::Sampler self)
2398 root 1.14 PPCODE:
2399     cl_bool value [1];
2400 root 1.22 NEED_SUCCESS (GetSamplerInfo, (self, CL_SAMPLER_FILTER_MODE, sizeof (value), value, 0));
2401 root 1.14 EXTEND (SP, 1);
2402     const int i = 0;
2403     PUSHs (sv_2mortal (value [i] ? &PL_sv_yes : &PL_sv_no));
2404    
2405     #END:sampler
2406    
2407 root 1.2 MODULE = OpenCL PACKAGE = OpenCL::Program
2408    
2409     void
2410 root 1.22 DESTROY (OpenCL::Program self)
2411 root 1.2 CODE:
2412 root 1.22 clReleaseProgram (self);
2413 root 1.2
2414     void
2415 root 1.51 build (OpenCL::Program self, SV *devices = &PL_sv_undef, SV *options = &PL_sv_undef, SV *notify = &PL_sv_undef)
2416     ALIAS:
2417     build_async = 1
2418 root 1.2 CODE:
2419 root 1.69 cl_uint device_count = 0;
2420     cl_device_id *device_list = 0;
2421 root 1.51
2422     if (SvOK (devices))
2423 root 1.69 device_list = object_list (cv, 1, "devices", devices, "OpenCL::Device", &device_count);
2424    
2425     void *user_data;
2426     program_callback pfn_notify = make_program_callback (notify, &user_data);
2427    
2428     if (ix)
2429     build_program_async (self, device_count, device_list, SvPVbyte_nolen (options), user_data);
2430     else
2431     NEED_SUCCESS (BuildProgram, (self, device_count, device_list, SvPVbyte_nolen (options), pfn_notify, user_data));
2432    
2433     #if CL_VERSION_1_2
2434    
2435     void
2436     compile (OpenCL::Program self, SV *devices, SV *options = &PL_sv_undef, SV *headers = &PL_sv_undef, SV *notify = &PL_sv_undef)
2437     CODE:
2438     cl_uint device_count = 0;
2439     cl_device_id *device_list = 0;
2440    
2441     if (SvOK (devices))
2442     device_list = object_list (cv, 1, "devices", devices, "OpenCL::Device", &device_count);
2443    
2444     cl_uint header_count = 0;
2445     cl_program *header_list = 0;
2446     const char **header_name = 0;
2447    
2448     if (SvOK (headers))
2449     {
2450     if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVHV)
2451     croak ("clCompileProgram: headers must be undef or a hashref of name => OpenCL::Program pairs");
2452 root 1.51
2453 root 1.69 HV *hv = (HV *)SvRV (devices);
2454 root 1.51
2455 root 1.69 header_count = hv_iterinit (hv);
2456     header_list = tmpbuf (sizeof (*header_list) * header_count);
2457     header_name = tmpbuf (sizeof (*header_name) * header_count);
2458    
2459     HE *he;
2460     int i = 0;
2461     while (he = hv_iternext (hv))
2462 root 1.51 {
2463 root 1.69 header_name [i] = SvPVbyte_nolen (HeSVKEY_force (he));
2464 root 1.71 header_list [i] = SvCLOBJ (cv, "headers", HeVAL (he), "OpenCL::Program");
2465 root 1.69 ++i;
2466 root 1.51 }
2467     }
2468    
2469 root 1.69 void *user_data;
2470     program_callback pfn_notify = make_program_callback (notify, &user_data);
2471    
2472     NEED_SUCCESS (CompileProgram, (self, device_count, device_list, SvPVbyte_nolen (options),
2473     header_count, header_list, header_name, pfn_notify, user_data));
2474 root 1.51
2475 root 1.69 #endif
2476 root 1.2
2477     void
2478 root 1.22 build_info (OpenCL::Program self, OpenCL::Device device, cl_program_build_info name)
2479 root 1.2 PPCODE:
2480 root 1.23 size_t size;
2481     NEED_SUCCESS (GetProgramBuildInfo, (self, device, name, 0, 0, &size));
2482 root 1.10 SV *sv = sv_2mortal (newSV (size));
2483 root 1.1 SvUPGRADE (sv, SVt_PV);
2484     SvPOK_only (sv);
2485     SvCUR_set (sv, size);
2486 root 1.23 NEED_SUCCESS (GetProgramBuildInfo, (self, device, name, size, SvPVX (sv), 0));
2487 root 1.1 XPUSHs (sv);
2488    
2489 root 1.14 #BEGIN:program_build
2490    
2491     void
2492 root 1.22 build_status (OpenCL::Program self, OpenCL::Device device)
2493 root 1.14 PPCODE:
2494     cl_build_status value [1];
2495 root 1.22 NEED_SUCCESS (GetProgramBuildInfo, (self, device, CL_PROGRAM_BUILD_STATUS, sizeof (value), value, 0));
2496 root 1.14 EXTEND (SP, 1);
2497     const int i = 0;
2498     PUSHs (sv_2mortal (newSViv (value [i])));
2499    
2500     void
2501 root 1.22 build_options (OpenCL::Program self, OpenCL::Device device)
2502 root 1.16 ALIAS:
2503     build_options = CL_PROGRAM_BUILD_OPTIONS
2504     build_log = CL_PROGRAM_BUILD_LOG
2505 root 1.14 PPCODE:
2506     size_t size;
2507 root 1.22 NEED_SUCCESS (GetProgramBuildInfo, (self, device, ix, 0, 0, &size));
2508 root 1.14 char *value = tmpbuf (size);
2509 root 1.22 NEED_SUCCESS (GetProgramBuildInfo, (self, device, ix, size, value, 0));
2510 root 1.16 EXTEND (SP, 1);
2511     const int i = 0;
2512 root 1.14 PUSHs (sv_2mortal (newSVpv (value, 0)));
2513    
2514     #END:program_build
2515    
2516 root 1.2 void
2517     kernel (OpenCL::Program program, SV *function)
2518     PPCODE:
2519 root 1.23 NEED_SUCCESS_ARG (cl_kernel kernel, CreateKernel, (program, SvPVbyte_nolen (function), &res));
2520 root 1.61 XPUSH_CLOBJ (stash_kernel, kernel);
2521 root 1.2
2522 root 1.14 void
2523 root 1.47 kernels_in_program (OpenCL::Program program)
2524     PPCODE:
2525     cl_uint num_kernels;
2526     NEED_SUCCESS (CreateKernelsInProgram, (program, 0, 0, &num_kernels));
2527     cl_kernel *kernels = tmpbuf (sizeof (cl_kernel) * num_kernels);
2528     NEED_SUCCESS (CreateKernelsInProgram, (program, num_kernels, kernels, 0));
2529    
2530     int i;
2531     EXTEND (SP, num_kernels);
2532     for (i = 0; i < num_kernels; ++i)
2533 root 1.61 PUSH_CLOBJ (stash_kernel, kernels [i]);
2534 root 1.47
2535     void
2536 root 1.22 info (OpenCL::Program self, cl_program_info name)
2537 root 1.14 PPCODE:
2538     INFO (Program)
2539    
2540 root 1.15 void
2541 root 1.22 binaries (OpenCL::Program self)
2542 root 1.15 PPCODE:
2543     cl_uint n, i;
2544     size_t size;
2545    
2546 root 1.22 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_NUM_DEVICES , sizeof (n) , &n , 0));
2547 root 1.15 if (!n) XSRETURN_EMPTY;
2548    
2549     size_t *sizes = tmpbuf (sizeof (*sizes) * n);
2550 root 1.22 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_BINARY_SIZES, sizeof (*sizes) * n, sizes, &size));
2551 root 1.15 if (size != sizeof (*sizes) * n) XSRETURN_EMPTY;
2552     unsigned char **ptrs = tmpbuf (sizeof (*ptrs) * n);
2553    
2554     EXTEND (SP, n);
2555     for (i = 0; i < n; ++i)
2556     {
2557     SV *sv = sv_2mortal (newSV (sizes [i]));
2558     SvUPGRADE (sv, SVt_PV);
2559     SvPOK_only (sv);
2560     SvCUR_set (sv, sizes [i]);
2561 root 1.37 ptrs [i] = (void *)SvPVX (sv);
2562 root 1.15 PUSHs (sv);
2563     }
2564    
2565 root 1.22 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_BINARIES , sizeof (*ptrs ) * n, ptrs , &size));
2566 root 1.15 if (size != sizeof (*ptrs) * n) XSRETURN_EMPTY;
2567    
2568 root 1.14 #BEGIN:program
2569    
2570     void
2571 root 1.22 reference_count (OpenCL::Program self)
2572 root 1.16 ALIAS:
2573     reference_count = CL_PROGRAM_REFERENCE_COUNT
2574     num_devices = CL_PROGRAM_NUM_DEVICES
2575 root 1.14 PPCODE:
2576     cl_uint value [1];
2577 root 1.22 NEED_SUCCESS (GetProgramInfo, (self, ix, sizeof (value), value, 0));
2578 root 1.14 EXTEND (SP, 1);
2579     const int i = 0;
2580     PUSHs (sv_2mortal (newSVuv (value [i])));
2581    
2582     void
2583 root 1.22 context (OpenCL::Program self)
2584 root 1.14 PPCODE:
2585     cl_context value [1];
2586 root 1.22 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_CONTEXT, sizeof (value), value, 0));
2587 root 1.14 EXTEND (SP, 1);
2588     const int i = 0;
2589     {
2590     NEED_SUCCESS (RetainContext, (value [i]));
2591 root 1.61 PUSH_CLOBJ (stash_context, value [i]);
2592 root 1.14 }
2593    
2594     void
2595 root 1.22 devices (OpenCL::Program self)
2596 root 1.14 PPCODE:
2597     size_t size;
2598 root 1.22 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_DEVICES, 0, 0, &size));
2599 root 1.14 cl_device_id *value = tmpbuf (size);
2600 root 1.22 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_DEVICES, size, value, 0));
2601 root 1.15 int i, n = size / sizeof (*value);
2602 root 1.14 EXTEND (SP, n);
2603     for (i = 0; i < n; ++i)
2604     {
2605 root 1.61 PUSH_CLOBJ (stash_device, value [i]);
2606 root 1.14 }
2607    
2608     void
2609 root 1.22 source (OpenCL::Program self)
2610 root 1.14 PPCODE:
2611     size_t size;
2612 root 1.22 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_SOURCE, 0, 0, &size));
2613 root 1.14 char *value = tmpbuf (size);
2614 root 1.22 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_SOURCE, size, value, 0));
2615 root 1.16 EXTEND (SP, 1);
2616     const int i = 0;
2617 root 1.14 PUSHs (sv_2mortal (newSVpv (value, 0)));
2618    
2619     void
2620 root 1.22 binary_sizes (OpenCL::Program self)
2621 root 1.14 PPCODE:
2622     size_t size;
2623 root 1.22 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_BINARY_SIZES, 0, 0, &size));
2624 root 1.14 size_t *value = tmpbuf (size);
2625 root 1.22 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_BINARY_SIZES, size, value, 0));
2626 root 1.15 int i, n = size / sizeof (*value);
2627 root 1.14 EXTEND (SP, n);
2628     for (i = 0; i < n; ++i)
2629     PUSHs (sv_2mortal (newSVuv (value [i])));
2630    
2631     #END:program
2632    
2633 root 1.2 MODULE = OpenCL PACKAGE = OpenCL::Kernel
2634    
2635     void
2636 root 1.22 DESTROY (OpenCL::Kernel self)
2637 root 1.2 CODE:
2638 root 1.22 clReleaseKernel (self);
2639 root 1.2
2640     void
2641 root 1.56 setf (OpenCL::Kernel self, const char *format, ...)
2642     CODE:
2643     int i;
2644     for (i = 2; ; ++i)
2645     {
2646     while (*format == ' ')
2647     ++format;
2648    
2649     char type = *format++;
2650    
2651     if (!type)
2652     break;
2653    
2654     if (i >= items)
2655     croak ("OpenCL::Kernel::setf format string too long (not enough arguments)");
2656    
2657     SV *sv = ST (i);
2658    
2659     union
2660     {
2661     cl_char cc; cl_uchar cC; cl_short cs; cl_ushort cS;
2662     cl_int ci; cl_uint cI; cl_long cl; cl_ulong cL;
2663     cl_half ch; cl_float cf; cl_double cd;
2664     cl_mem cm;
2665     cl_sampler ca;
2666     size_t cz;
2667     cl_event ce;
2668     } arg;
2669     size_t size;
2670 root 1.59 int nullarg = 0;
2671 root 1.56
2672     switch (type)
2673     {
2674     case 'c': arg.cc = SvIV (sv); size = sizeof (arg.cc); break;
2675     case 'C': arg.cC = SvUV (sv); size = sizeof (arg.cC); break;
2676     case 's': arg.cs = SvIV (sv); size = sizeof (arg.cs); break;
2677     case 'S': arg.cS = SvUV (sv); size = sizeof (arg.cS); break;
2678     case 'i': arg.ci = SvIV (sv); size = sizeof (arg.ci); break;
2679     case 'I': arg.cI = SvUV (sv); size = sizeof (arg.cI); break;
2680     case 'l': arg.cl = SvIV (sv); size = sizeof (arg.cl); break;
2681     case 'L': arg.cL = SvUV (sv); size = sizeof (arg.cL); break;
2682    
2683     case 'h': arg.ch = SvUV (sv); size = sizeof (arg.ch); break;
2684     case 'f': arg.cf = SvNV (sv); size = sizeof (arg.cf); break;
2685     case 'd': arg.cd = SvNV (sv); size = sizeof (arg.cd); break;
2686    
2687 root 1.59 case 'z': nullarg = 1; size = SvIV (sv); break;
2688    
2689 root 1.71 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 (cv, "a", sv, "OpenCL::Sampler"); size = sizeof (arg.ca); break;
2691     case 'e': nullarg = !SvOK (sv); arg.ca = SvCLOBJ (cv, "e", sv, "OpenCL::Event" ); size = sizeof (arg.ce); break;
2692 root 1.56
2693     default:
2694     croak ("OpenCL::Kernel::setf format character '%c' not supported", type);
2695     }
2696    
2697 root 1.59 res = clSetKernelArg (self, i - 2, size, nullarg ? 0 : &arg);
2698     if (res)
2699     croak ("OpenCL::Kernel::setf kernel parameter '%c' (#%d): %s", type, i - 2, err2str (res));
2700 root 1.56 }
2701    
2702     if (i != items)
2703     croak ("OpenCL::Kernel::setf format string too short (too many arguments)");
2704    
2705     void
2706 root 1.22 set_char (OpenCL::Kernel self, cl_uint idx, cl_char value)
2707 root 1.3 CODE:
2708 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2709 root 1.3
2710     void
2711 root 1.22 set_uchar (OpenCL::Kernel self, cl_uint idx, cl_uchar value)
2712 root 1.3 CODE:
2713 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2714 root 1.3
2715     void
2716 root 1.22 set_short (OpenCL::Kernel self, cl_uint idx, cl_short value)
2717 root 1.3 CODE:
2718 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2719 root 1.3
2720     void
2721 root 1.22 set_ushort (OpenCL::Kernel self, cl_uint idx, cl_ushort value)
2722 root 1.3 CODE:
2723 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2724 root 1.3
2725     void
2726 root 1.22 set_int (OpenCL::Kernel self, cl_uint idx, cl_int value)
2727 root 1.3 CODE:
2728 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2729 root 1.3
2730     void
2731 root 1.22 set_uint (OpenCL::Kernel self, cl_uint idx, cl_uint value)
2732 root 1.3 CODE:
2733 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2734 root 1.3
2735     void
2736 root 1.22 set_long (OpenCL::Kernel self, cl_uint idx, cl_long value)
2737 root 1.3 CODE:
2738 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2739 root 1.3
2740     void
2741 root 1.22 set_ulong (OpenCL::Kernel self, cl_uint idx, cl_ulong value)
2742 root 1.3 CODE:
2743 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2744 root 1.3
2745     void
2746 root 1.22 set_half (OpenCL::Kernel self, cl_uint idx, cl_half value)
2747 root 1.3 CODE:
2748 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2749 root 1.3
2750     void
2751 root 1.22 set_float (OpenCL::Kernel self, cl_uint idx, cl_float value)
2752 root 1.3 CODE:
2753 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2754 root 1.3
2755     void
2756 root 1.22 set_double (OpenCL::Kernel self, cl_uint idx, cl_double value)
2757 root 1.5 CODE:
2758 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2759 root 1.5
2760     void
2761 root 1.22 set_memory (OpenCL::Kernel self, cl_uint idx, OpenCL::Memory_ornull value)
2762 root 1.3 CODE:
2763 root 1.59 clSetKernelArg (self, idx, sizeof (value), value ? &value : 0);
2764 root 1.3
2765     void
2766 root 1.22 set_buffer (OpenCL::Kernel self, cl_uint idx, OpenCL::Buffer_ornull value)
2767 root 1.3 CODE:
2768 root 1.59 clSetKernelArg (self, idx, sizeof (value), value ? &value : 0);
2769 root 1.3
2770     void
2771 root 1.54 set_image (OpenCL::Kernel self, cl_uint idx, OpenCL::Image_ornull value)
2772     ALIAS:
2773     set_image2d = 0
2774     set_image3d = 0
2775 root 1.3 CODE:
2776 root 1.59 clSetKernelArg (self, idx, sizeof (value), value ? &value : 0);
2777 root 1.3
2778     void
2779 root 1.22 set_sampler (OpenCL::Kernel self, cl_uint idx, OpenCL::Sampler value)
2780 root 1.3 CODE:
2781 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2782 root 1.3
2783     void
2784 root 1.33 set_local (OpenCL::Kernel self, cl_uint idx, size_t size)
2785     CODE:
2786     clSetKernelArg (self, idx, size, 0);
2787    
2788     void
2789 root 1.22 set_event (OpenCL::Kernel self, cl_uint idx, OpenCL::Event value)
2790 root 1.2 CODE:
2791 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2792 root 1.2
2793 root 1.14 void
2794 root 1.22 info (OpenCL::Kernel self, cl_kernel_info name)
2795 root 1.14 PPCODE:
2796     INFO (Kernel)
2797    
2798     #BEGIN:kernel
2799    
2800     void
2801 root 1.22 function_name (OpenCL::Kernel self)
2802 root 1.14 PPCODE:
2803     size_t size;
2804 root 1.22 NEED_SUCCESS (GetKernelInfo, (self, CL_KERNEL_FUNCTION_NAME, 0, 0, &size));
2805 root 1.14 char *value = tmpbuf (size);
2806 root 1.22 NEED_SUCCESS (GetKernelInfo, (self, CL_KERNEL_FUNCTION_NAME, size, value, 0));
2807 root 1.16 EXTEND (SP, 1);
2808     const int i = 0;
2809 root 1.14 PUSHs (sv_2mortal (newSVpv (value, 0)));
2810    
2811     void
2812 root 1.22 num_args (OpenCL::Kernel self)
2813 root 1.16 ALIAS:
2814     num_args = CL_KERNEL_NUM_ARGS
2815     reference_count = CL_KERNEL_REFERENCE_COUNT
2816 root 1.14 PPCODE:
2817     cl_uint value [1];
2818 root 1.22 NEED_SUCCESS (GetKernelInfo, (self, ix, sizeof (value), value, 0));
2819 root 1.14 EXTEND (SP, 1);
2820     const int i = 0;
2821     PUSHs (sv_2mortal (newSVuv (value [i])));
2822    
2823     void
2824 root 1.22 context (OpenCL::Kernel self)
2825 root 1.14 PPCODE:
2826     cl_context value [1];
2827 root 1.22 NEED_SUCCESS (GetKernelInfo, (self, CL_KERNEL_CONTEXT, sizeof (value), value, 0));
2828 root 1.14 EXTEND (SP, 1);
2829     const int i = 0;
2830     {
2831     NEED_SUCCESS (RetainContext, (value [i]));
2832 root 1.61 PUSH_CLOBJ (stash_context, value [i]);
2833 root 1.14 }
2834    
2835     void
2836 root 1.22 program (OpenCL::Kernel self)
2837 root 1.14 PPCODE:
2838     cl_program value [1];
2839 root 1.22 NEED_SUCCESS (GetKernelInfo, (self, CL_KERNEL_PROGRAM, sizeof (value), value, 0));
2840 root 1.14 EXTEND (SP, 1);
2841     const int i = 0;
2842     {
2843     NEED_SUCCESS (RetainProgram, (value [i]));
2844 root 1.61 PUSH_CLOBJ (stash_program, value [i]);
2845 root 1.14 }
2846    
2847     #END:kernel
2848    
2849     void
2850 root 1.22 work_group_info (OpenCL::Kernel self, OpenCL::Device device, cl_kernel_work_group_info name)
2851 root 1.14 PPCODE:
2852 root 1.22 size_t size;
2853     NEED_SUCCESS (GetKernelWorkGroupInfo, (self, device, name, 0, 0, &size));
2854 root 1.14 SV *sv = sv_2mortal (newSV (size));
2855     SvUPGRADE (sv, SVt_PV);
2856     SvPOK_only (sv);
2857     SvCUR_set (sv, size);
2858 root 1.22 NEED_SUCCESS (GetKernelWorkGroupInfo, (self, device, name, size, SvPVX (sv), 0));
2859 root 1.14 XPUSHs (sv);
2860    
2861     #BEGIN:kernel_work_group
2862    
2863     void
2864 root 1.22 work_group_size (OpenCL::Kernel self, OpenCL::Device device)
2865 root 1.16 ALIAS:
2866     work_group_size = CL_KERNEL_WORK_GROUP_SIZE
2867     preferred_work_group_size_multiple = CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE
2868 root 1.14 PPCODE:
2869     size_t value [1];
2870 root 1.22 NEED_SUCCESS (GetKernelWorkGroupInfo, (self, device, ix, sizeof (value), value, 0));
2871 root 1.14 EXTEND (SP, 1);
2872     const int i = 0;
2873     PUSHs (sv_2mortal (newSVuv (value [i])));
2874    
2875     void
2876 root 1.22 compile_work_group_size (OpenCL::Kernel self, OpenCL::Device device)
2877 root 1.14 PPCODE:
2878     size_t size;
2879 root 1.22 NEED_SUCCESS (GetKernelWorkGroupInfo, (self, device, CL_KERNEL_COMPILE_WORK_GROUP_SIZE, 0, 0, &size));
2880 root 1.14 size_t *value = tmpbuf (size);
2881 root 1.22 NEED_SUCCESS (GetKernelWorkGroupInfo, (self, device, CL_KERNEL_COMPILE_WORK_GROUP_SIZE, size, value, 0));
2882 root 1.15 int i, n = size / sizeof (*value);
2883 root 1.14 EXTEND (SP, n);
2884     for (i = 0; i < n; ++i)
2885     PUSHs (sv_2mortal (newSVuv (value [i])));
2886    
2887     void
2888 root 1.22 local_mem_size (OpenCL::Kernel self, OpenCL::Device device)
2889 root 1.16 ALIAS:
2890     local_mem_size = CL_KERNEL_LOCAL_MEM_SIZE
2891     private_mem_size = CL_KERNEL_PRIVATE_MEM_SIZE
2892 root 1.14 PPCODE:
2893     cl_ulong value [1];
2894 root 1.22 NEED_SUCCESS (GetKernelWorkGroupInfo, (self, device, ix, sizeof (value), value, 0));
2895 root 1.14 EXTEND (SP, 1);
2896     const int i = 0;
2897     PUSHs (sv_2mortal (newSVuv (value [i])));
2898    
2899     #END:kernel_work_group
2900    
2901 root 1.66 #if CL_VERSION_1_2
2902    
2903     void
2904     arg_info (OpenCL::Kernel self, cl_uint idx, cl_kernel_arg_info name)
2905     PPCODE:
2906     size_t size;
2907 root 1.67 NEED_SUCCESS (GetKernelArgInfo, (self, idx, name, 0, 0, &size));
2908 root 1.66 SV *sv = sv_2mortal (newSV (size));
2909     SvUPGRADE (sv, SVt_PV);
2910     SvPOK_only (sv);
2911     SvCUR_set (sv, size);
2912 root 1.67 NEED_SUCCESS (GetKernelArgInfo, (self, idx, name, size, SvPVX (sv), 0));
2913 root 1.66 XPUSHs (sv);
2914    
2915     #BEGIN:kernel_arg
2916    
2917 root 1.68 void
2918     arg_address_qualifier (OpenCL::Kernel self, cl_uint idx)
2919     PPCODE:
2920     cl_kernel_arg_address_qualifier value [1];
2921     NEED_SUCCESS (GetKernelArgInfo, (self, idx, CL_KERNEL_ARG_ADDRESS_QUALIFIER, sizeof (value), value, 0));
2922     EXTEND (SP, 1);
2923     const int i = 0;
2924     PUSHs (sv_2mortal (newSVuv (value [i])));
2925    
2926     void
2927     arg_access_qualifier (OpenCL::Kernel self, cl_uint idx)
2928     PPCODE:
2929     cl_kernel_arg_access_qualifier value [1];
2930     NEED_SUCCESS (GetKernelArgInfo, (self, idx, CL_KERNEL_ARG_ACCESS_QUALIFIER, sizeof (value), value, 0));
2931     EXTEND (SP, 1);
2932     const int i = 0;
2933     PUSHs (sv_2mortal (newSVuv (value [i])));
2934    
2935     void
2936     arg_type_name (OpenCL::Kernel self, cl_uint idx)
2937     ALIAS:
2938     arg_type_name = CL_KERNEL_ARG_TYPE_NAME
2939     arg_name = CL_KERNEL_ARG_NAME
2940     PPCODE:
2941     size_t size;
2942     NEED_SUCCESS (GetKernelArgInfo, (self, idx, ix, 0, 0, &size));
2943     char *value = tmpbuf (size);
2944     NEED_SUCCESS (GetKernelArgInfo, (self, idx, ix, size, value, 0));
2945     EXTEND (SP, 1);
2946     const int i = 0;
2947     PUSHs (sv_2mortal (newSVpv (value, 0)));
2948    
2949     void
2950     arg_type_qualifier (OpenCL::Kernel self, cl_uint idx)
2951     PPCODE:
2952     cl_kernel_arg_type_qualifier value [1];
2953     NEED_SUCCESS (GetKernelArgInfo, (self, idx, CL_KERNEL_ARG_TYPE_QUALIFIER, sizeof (value), value, 0));
2954     EXTEND (SP, 1);
2955     const int i = 0;
2956     PUSHs (sv_2mortal (newSVuv (value [i])));
2957    
2958 root 1.66 #END:kernel_arg
2959    
2960     #endif
2961    
2962 root 1.2 MODULE = OpenCL PACKAGE = OpenCL::Event
2963    
2964     void
2965 root 1.22 DESTROY (OpenCL::Event self)
2966 root 1.2 CODE:
2967 root 1.22 clReleaseEvent (self);
2968 root 1.2
2969     void
2970 root 1.22 wait (OpenCL::Event self)
2971 root 1.14 CODE:
2972 root 1.22 clWaitForEvents (1, &self);
2973 root 1.14
2974     void
2975 root 1.51 cb (OpenCL::Event self, cl_int command_exec_callback_type, SV *cb)
2976     CODE:
2977     clSetEventCallback (self, command_exec_callback_type, eq_event_notify, SvREFCNT_inc (s_get_cv (cb)));
2978    
2979     void
2980 root 1.22 info (OpenCL::Event self, cl_event_info name)
2981 root 1.2 PPCODE:
2982     INFO (Event)
2983    
2984 root 1.14 #BEGIN:event
2985    
2986     void
2987 root 1.22 command_queue (OpenCL::Event self)
2988 root 1.14 PPCODE:
2989     cl_command_queue value [1];
2990 root 1.22 NEED_SUCCESS (GetEventInfo, (self, CL_EVENT_COMMAND_QUEUE, sizeof (value), value, 0));
2991 root 1.14 EXTEND (SP, 1);
2992     const int i = 0;
2993     {
2994     NEED_SUCCESS (RetainCommandQueue, (value [i]));
2995 root 1.61 PUSH_CLOBJ (stash_queue, value [i]);
2996 root 1.14 }
2997    
2998     void
2999 root 1.22 command_type (OpenCL::Event self)
3000 root 1.14 PPCODE:
3001     cl_command_type value [1];
3002 root 1.22 NEED_SUCCESS (GetEventInfo, (self, CL_EVENT_COMMAND_TYPE, sizeof (value), value, 0));
3003 root 1.14 EXTEND (SP, 1);
3004     const int i = 0;
3005     PUSHs (sv_2mortal (newSVuv (value [i])));
3006    
3007     void
3008 root 1.22 reference_count (OpenCL::Event self)
3009 root 1.16 ALIAS:
3010     reference_count = CL_EVENT_REFERENCE_COUNT
3011     command_execution_status = CL_EVENT_COMMAND_EXECUTION_STATUS
3012 root 1.14 PPCODE:
3013     cl_uint value [1];
3014 root 1.22 NEED_SUCCESS (GetEventInfo, (self, ix, sizeof (value), value, 0));
3015 root 1.14 EXTEND (SP, 1);
3016     const int i = 0;
3017     PUSHs (sv_2mortal (newSVuv (value [i])));
3018    
3019     void
3020 root 1.22 context (OpenCL::Event self)
3021 root 1.14 PPCODE:
3022     cl_context value [1];
3023 root 1.22 NEED_SUCCESS (GetEventInfo, (self, CL_EVENT_CONTEXT, sizeof (value), value, 0));
3024 root 1.14 EXTEND (SP, 1);
3025     const int i = 0;
3026     {
3027     NEED_SUCCESS (RetainContext, (value [i]));
3028 root 1.61 PUSH_CLOBJ (stash_context, value [i]);
3029 root 1.14 }
3030    
3031     #END:event
3032    
3033 root 1.2 void
3034 root 1.22 profiling_info (OpenCL::Event self, cl_profiling_info name)
3035 root 1.13 PPCODE:
3036     INFO (EventProfiling)
3037    
3038 root 1.14 #BEGIN:profiling
3039    
3040 root 1.13 void
3041 root 1.22 profiling_command_queued (OpenCL::Event self)
3042 root 1.16 ALIAS:
3043     profiling_command_queued = CL_PROFILING_COMMAND_QUEUED
3044     profiling_command_submit = CL_PROFILING_COMMAND_SUBMIT
3045     profiling_command_start = CL_PROFILING_COMMAND_START
3046     profiling_command_end = CL_PROFILING_COMMAND_END
3047 root 1.14 PPCODE:
3048     cl_ulong value [1];
3049 root 1.22 NEED_SUCCESS (GetEventProfilingInfo, (self, ix, sizeof (value), value, 0));
3050 root 1.14 EXTEND (SP, 1);
3051     const int i = 0;
3052     PUSHs (sv_2mortal (newSVuv (value [i])));
3053    
3054     #END:profiling
3055 root 1.2
3056 root 1.5 MODULE = OpenCL PACKAGE = OpenCL::UserEvent
3057    
3058     void
3059 root 1.22 set_status (OpenCL::UserEvent self, cl_int execution_status)
3060 root 1.5 CODE:
3061 root 1.22 clSetUserEventStatus (self, execution_status);
3062 root 1.5
3063 root 1.61 MODULE = OpenCL PACKAGE = OpenCL::Mapped
3064    
3065     void
3066     DESTROY (SV *self)
3067     CODE:
3068     OpenCL__Mapped mapped = SvMAPPED (self);
3069    
3070     clEnqueueUnmapMemObject (mapped->queue, mapped->memobj, mapped->ptr, 1, &mapped->event, 0);
3071     mapped_detach (self, mapped);
3072    
3073     clReleaseCommandQueue (mapped->queue);
3074 root 1.62 clReleaseEvent (mapped->event);
3075 root 1.61 Safefree (mapped);
3076    
3077 root 1.62 void
3078     unmap (OpenCL::Mapped self, ...)
3079     CODE:
3080 root 1.71 mapped_unmap (cv, ST (0), self, self->queue, &ST (1), items - 1);
3081 root 1.62
3082 root 1.61 bool
3083     mapped (OpenCL::Mapped self)
3084     CODE:
3085     RETVAL = !!self->ptr;
3086     OUTPUT:
3087     RETVAL
3088    
3089     void
3090     wait (OpenCL::Mapped self)
3091     PPCODE:
3092     if (self->event)
3093     NEED_SUCCESS (WaitForEvents, (1, &self->event));
3094    
3095     void
3096     event (OpenCL::Mapped self)
3097     PPCODE:
3098     if (!self->event)
3099     XSRETURN_UNDEF;
3100    
3101     clRetainEvent (self->event);
3102     XPUSH_CLOBJ (stash_event, self->event);
3103    
3104     size_t
3105     size (OpenCL::Mapped self)
3106     CODE:
3107     RETVAL = self->cb;
3108     OUTPUT:
3109     RETVAL
3110    
3111     IV
3112     ptr (OpenCL::Mapped self)
3113     CODE:
3114     RETVAL = PTR2IV (self->ptr);
3115     OUTPUT:
3116     RETVAL
3117    
3118 root 1.63 void
3119     set (OpenCL::Mapped self, size_t offset, SV *data)
3120     CODE:
3121     STRLEN len;
3122     const char *ptr = SvPVbyte (data, len);
3123    
3124 root 1.64 if (offset + len > self->cb)
3125 root 1.63 croak ("OpenCL::Mapped::set out of bound condition detected");
3126    
3127     memcpy (offset + (char *)self->ptr, ptr, len);
3128    
3129 root 1.61 MODULE = OpenCL PACKAGE = OpenCL::MappedBuffer
3130    
3131     MODULE = OpenCL PACKAGE = OpenCL::MappedImage
3132    
3133     IV
3134     row_pitch (OpenCL::Mapped self)
3135     ALIAS:
3136     slice_pitch = 1
3137     CODE:
3138     RETVAL = ix ? self->slice_pitch : self->row_pitch;
3139     OUTPUT:
3140     RETVAL
3141    
3142    
3143