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