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

File Contents

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