ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/OpenCL/OpenCL.xs
Revision: 1.7
Committed: Thu Nov 17 02:10:39 2011 UTC (12 years, 6 months ago) by root
Branch: MAIN
Changes since 1.6: +6 -4 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     #include <CL/opencl.h>
6    
7     typedef cl_platform_id OpenCL__Platform;
8     typedef cl_device_id OpenCL__Device;
9     typedef cl_context OpenCL__Context;
10     typedef cl_command_queue OpenCL__Queue;
11 root 1.2 typedef cl_mem OpenCL__Memory;
12 root 1.3 typedef cl_mem OpenCL__Buffer;
13     typedef cl_mem OpenCL__Image;
14     typedef cl_mem OpenCL__Image2D;
15     typedef cl_mem OpenCL__Image3D;
16     typedef cl_mem OpenCL__Memory_ornull;
17     typedef cl_mem OpenCL__Buffer_ornull;
18     typedef cl_mem OpenCL__Image_ornull;
19     typedef cl_mem OpenCL__Image2D_ornull;
20     typedef cl_mem OpenCL__Image3D_ornull;
21 root 1.2 typedef cl_sampler OpenCL__Sampler;
22     typedef cl_program OpenCL__Program;
23     typedef cl_kernel OpenCL__Kernel;
24     typedef cl_event OpenCL__Event;
25 root 1.5 typedef cl_event OpenCL__UserEvent;
26    
27 root 1.7 typedef SV *FUTURE;
28    
29 root 1.5 /*****************************************************************************/
30    
31     /* up to two temporary buffers */
32     static void *
33     tmpbuf (size_t size)
34     {
35     static int idx;
36     static void *buf [2];
37     static size_t len [2];
38    
39     idx ^= 1;
40    
41     if (len [idx] < size)
42     {
43     free (buf [idx]);
44     len [idx] = ((size + 31) & ~4095) + 4096 - 32;
45     buf [idx] = malloc (len [idx]);
46     }
47    
48     return buf [idx];
49     }
50    
51     /*****************************************************************************/
52 root 1.1
53 root 1.3 typedef struct
54     {
55 root 1.1 IV iv;
56     const char *name;
57 root 1.3 #define const_iv(name) { (IV)CL_ ## name, # name },
58     } ivstr;
59 root 1.1
60     static const char *
61 root 1.3 iv2str (IV value, const ivstr *base, int count, const char *fallback)
62 root 1.1 {
63     int i;
64 root 1.3 static char strbuf [32];
65    
66     for (i = count; i--; )
67     if (base [i].iv == value)
68     return base [i].name;
69    
70     snprintf (strbuf, sizeof (strbuf), fallback, (int)value);
71    
72     return strbuf;
73     }
74    
75     static const char *
76     enum2str (cl_uint value)
77     {
78     static const ivstr enumstr[] = {
79     #include "enumstr.h"
80     };
81 root 1.1
82 root 1.3 return iv2str (value, enumstr, sizeof (enumstr) / sizeof (enumstr [0]), "ENUM(0x%04x)");
83     }
84 root 1.1
85 root 1.3 static const char *
86     err2str (cl_int err)
87     {
88     static const ivstr errstr[] = {
89     #include "errstr.h"
90     };
91 root 1.1
92 root 1.3 return iv2str (err, errstr, sizeof (errstr) / sizeof (errstr [0]), "ERROR(%d)");
93 root 1.1 }
94    
95 root 1.5 /*****************************************************************************/
96    
97     static cl_int last_error;
98    
99     #define FAIL(name,err) \
100     croak ("cl" # name ": %s", err2str (last_error = err));
101 root 1.1
102     #define NEED_SUCCESS(name,args) \
103     do { \
104 root 1.2 cl_int res = cl ## name args; \
105 root 1.1 \
106     if (res) \
107     FAIL (name, res); \
108     } while (0)
109    
110 root 1.5 /*****************************************************************************/
111    
112 root 1.2 #define NEW_MORTAL_OBJ(class,ptr) sv_setref_pv (sv_newmortal (), class, ptr)
113     #define XPUSH_NEW_OBJ(class,ptr) XPUSHs (NEW_MORTAL_OBJ (class, ptr))
114    
115 root 1.5 static void *
116     SvPTROBJ (const char *func, const char *svname, SV *sv, const char *pkg)
117     {
118     if (SvROK (sv) && sv_derived_from (sv, pkg))
119     return (void *)SvIV (SvRV (sv));
120    
121     croak ("%s: %s is not of type %s", func, svname, pkg);
122     }
123    
124     /*****************************************************************************/
125    
126     static cl_event *
127     event_list (SV **items, int count)
128     {
129     cl_event *list = tmpbuf (sizeof (cl_event) * count);
130    
131     while (count--)
132     list [count] = SvPTROBJ ("clEnqueue", "wait_events", items [count], "OpenCL::Event");
133    
134     return list;
135     }
136    
137     #define EVENT_LIST(items,count) \
138     cl_uint event_list_count = (count); \
139     cl_event *event_list_ptr = event_list (&ST (items), event_list_count)
140 root 1.2
141     #define INFO(class) \
142     { \
143     size_t size; \
144     SV *sv; \
145     \
146     NEED_SUCCESS (Get ## class ## Info, (this, name, 0, 0, &size)); \
147     sv = sv_2mortal (newSV (size)); \
148     SvUPGRADE (sv, SVt_PV); \
149     SvPOK_only (sv); \
150     SvCUR_set (sv, size); \
151     NEED_SUCCESS (Get ## class ## Info, (this, name, size, SvPVX (sv), 0)); \
152     XPUSHs (sv); \
153     }
154    
155 root 1.1 MODULE = OpenCL PACKAGE = OpenCL
156    
157 root 1.2 PROTOTYPES: ENABLE
158    
159 root 1.1 BOOT:
160     {
161     HV *stash = gv_stashpv ("OpenCL", 1);
162 root 1.3 static const ivstr *civ, const_iv[] = {
163 root 1.5 { sizeof (cl_char ), "SIZEOF_CHAR" },
164     { sizeof (cl_uchar ), "SIZEOF_UCHAR" },
165     { sizeof (cl_short ), "SIZEOF_SHORT" },
166     { sizeof (cl_ushort), "SIZEOF_USHORT" },
167     { sizeof (cl_int ), "SIZEOF_INT" },
168     { sizeof (cl_uint ), "SIZEOF_UINT" },
169     { sizeof (cl_long ), "SIZEOF_LONG" },
170     { sizeof (cl_ulong ), "SIZEOF_ULONG" },
171     { sizeof (cl_half ), "SIZEOF_HALF" },
172     { sizeof (cl_float ), "SIZEOF_FLOAT" },
173     { sizeof (cl_double), "SIZEOF_DOUBLE" },
174 root 1.1 #include "constiv.h"
175     };
176     for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
177     newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
178     }
179    
180 root 1.5 cl_int
181     errno ()
182     CODE:
183     errno = last_error;
184    
185 root 1.3 const char *
186     err2str (cl_int err)
187    
188     const char *
189     enum2str (cl_uint value)
190    
191 root 1.1 void
192     platforms ()
193     PPCODE:
194     {
195     cl_platform_id *list;
196     cl_uint count;
197     int i;
198    
199 root 1.2 NEED_SUCCESS (GetPlatformIDs, (0, 0, &count));
200 root 1.4 list = tmpbuf (sizeof (*list) * count);
201 root 1.2 NEED_SUCCESS (GetPlatformIDs, (count, list, 0));
202 root 1.1
203     EXTEND (SP, count);
204     for (i = 0; i < count; ++i)
205 root 1.2 PUSHs (NEW_MORTAL_OBJ ("OpenCL::Platform", list [i]));
206 root 1.1 }
207    
208     void
209 root 1.7 context_from_type (FUTURE properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0)
210 root 1.1 PPCODE:
211     {
212     cl_int res;
213     cl_context ctx = clCreateContextFromType (0, type, 0, 0, &res);
214    
215     if (res)
216 root 1.2 FAIL (CreateContextFromType, res);
217 root 1.1
218 root 1.2 XPUSH_NEW_OBJ ("OpenCL::Context", ctx);
219 root 1.1 }
220    
221 root 1.2 void
222     wait_for_events (...)
223     CODE:
224     {
225     EVENT_LIST (0, items);
226     NEED_SUCCESS (WaitForEvents, (event_list_count, event_list_ptr));
227     }
228    
229     PROTOTYPES: DISABLE
230    
231 root 1.1 MODULE = OpenCL PACKAGE = OpenCL::Platform
232    
233     void
234     info (OpenCL::Platform this, cl_platform_info name)
235     PPCODE:
236 root 1.2 INFO (Platform)
237 root 1.1
238     void
239     devices (OpenCL::Platform this, cl_device_type type = CL_DEVICE_TYPE_ALL)
240     PPCODE:
241     {
242     cl_device_id *list;
243     cl_uint count;
244     int i;
245    
246 root 1.2 NEED_SUCCESS (GetDeviceIDs, (this, type, 0, 0, &count));
247 root 1.4 list = tmpbuf (sizeof (*list) * count);
248 root 1.2 NEED_SUCCESS (GetDeviceIDs, (this, type, count, list, 0));
249 root 1.1
250     EXTEND (SP, count);
251     for (i = 0; i < count; ++i)
252     PUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Device", list [i]));
253     }
254    
255     void
256 root 1.7 context_from_type (OpenCL::Platform this, FUTURE properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0)
257 root 1.1 PPCODE:
258     {
259     cl_int res;
260     cl_context_properties props[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)this, 0 };
261     cl_context ctx = clCreateContextFromType (props, type, 0, 0, &res);
262    
263     if (res)
264 root 1.2 FAIL (CreateContextFromType, res);
265 root 1.1
266 root 1.2 XPUSH_NEW_OBJ ("OpenCL::Context", ctx);
267 root 1.1 }
268    
269     MODULE = OpenCL PACKAGE = OpenCL::Device
270    
271     void
272     info (OpenCL::Device this, cl_device_info name)
273     PPCODE:
274 root 1.2 INFO (Device)
275 root 1.1
276     void
277 root 1.7 context (OpenCL::Device this, FUTURE properties = 0, FUTURE notify = 0)
278 root 1.1 PPCODE:
279     {
280     cl_int res;
281     cl_context ctx = clCreateContext (0, 1, &this, 0, 0, &res);
282    
283     if (res)
284 root 1.2 FAIL (CreateContext, res);
285 root 1.1
286 root 1.2 XPUSH_NEW_OBJ ("OpenCL::Context", ctx);
287 root 1.1 }
288    
289     MODULE = OpenCL PACKAGE = OpenCL::Context
290    
291     void
292     DESTROY (OpenCL::Context context)
293     CODE:
294     clReleaseContext (context);
295    
296     void
297     info (OpenCL::Context this, cl_context_info name)
298     PPCODE:
299 root 1.2 INFO (Context)
300    
301     void
302 root 1.7 queue (OpenCL::Context this, OpenCL::Device device, cl_command_queue_properties properties = 0)
303 root 1.2 PPCODE:
304 root 1.1 {
305 root 1.2 cl_int res;
306 root 1.6 cl_command_queue queue = clCreateCommandQueue (this, device, properties, &res);
307 root 1.2
308     if (res)
309     FAIL (CreateCommandQueue, res);
310    
311     XPUSH_NEW_OBJ ("OpenCL::Queue", queue);
312     }
313    
314     void
315 root 1.5 user_event (OpenCL::Context this)
316     PPCODE:
317     {
318     cl_int res;
319     cl_event ev = clCreateUserEvent (this, &res);
320    
321     if (res)
322     FAIL (CreateUserevent, res);
323    
324     XPUSH_NEW_OBJ ("OpenCL::UserEvent", ev);
325     }
326    
327     void
328 root 1.2 buffer (OpenCL::Context this, cl_mem_flags flags, size_t len)
329     PPCODE:
330     {
331     cl_int res;
332 root 1.3 cl_mem mem;
333    
334     if (flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))
335     croak ("clCreateBuffer: cannot use/copy host ptr when no data is given, use $context->buffer_sv instead?");
336    
337     mem = clCreateBuffer (this, flags, len, 0, &res);
338 root 1.2
339     if (res)
340     FAIL (CreateBuffer, res);
341    
342 root 1.3 XPUSH_NEW_OBJ ("OpenCL::Buffer", mem);
343 root 1.2 }
344    
345     void
346     buffer_sv (OpenCL::Context this, cl_mem_flags flags, SV *data)
347     PPCODE:
348     {
349     STRLEN len;
350     char *ptr = SvPVbyte (data, len);
351     cl_int res;
352 root 1.3 cl_mem mem;
353    
354     if (!(flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR)))
355     croak ("clCreateBuffer: have to specify use or copy host ptr when buffer data is given, use $context->buffer instead?");
356    
357     mem = clCreateBuffer (this, flags, len, ptr, &res);
358 root 1.2
359     if (res)
360     FAIL (CreateBuffer, res);
361    
362 root 1.3 XPUSH_NEW_OBJ ("OpenCL::Buffer", mem);
363     }
364    
365     void
366     image2d (OpenCL::Context this, cl_mem_flags flags, cl_channel_order channel_order, cl_channel_type channel_type, size_t width, size_t height, SV *data)
367     PPCODE:
368     {
369     STRLEN len;
370     char *ptr = SvPVbyte (data, len);
371     const cl_image_format format = { channel_order, channel_type };
372     cl_int res;
373     cl_mem mem = clCreateImage2D (this, flags, &format, width, height, len / height, ptr, &res);
374    
375     if (res)
376     FAIL (CreateImage2D, res);
377    
378     XPUSH_NEW_OBJ ("OpenCL::Image2D", mem);
379     }
380    
381     void
382     image3d (OpenCL::Context this, 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 slice_pitch, SV *data)
383     PPCODE:
384     {
385     STRLEN len;
386     char *ptr = SvPVbyte (data, len);
387     const cl_image_format format = { channel_order, channel_type };
388     cl_int res;
389     cl_mem mem = clCreateImage3D (this, flags, &format, width, height,
390     depth, len / (height * slice_pitch), slice_pitch, ptr, &res);
391    
392     if (res)
393     FAIL (CreateImage3D, res);
394    
395     XPUSH_NEW_OBJ ("OpenCL::Image3D", mem);
396     }
397    
398     void
399     supported_image_formats (OpenCL::Context this, cl_mem_flags flags, cl_mem_object_type image_type)
400     PPCODE:
401     {
402     cl_uint count;
403     cl_image_format *list;
404     int i;
405    
406     NEED_SUCCESS (GetSupportedImageFormats, (this, flags, image_type, 0, 0, &count));
407     Newx (list, count, cl_image_format);
408     NEED_SUCCESS (GetSupportedImageFormats, (this, flags, image_type, count, list, 0));
409    
410     EXTEND (SP, count);
411     for (i = 0; i < count; ++i)
412     {
413     AV *av = newAV ();
414     av_store (av, 1, newSVuv (list [i].image_channel_data_type));
415     av_store (av, 0, newSVuv (list [i].image_channel_order));
416     PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
417     }
418 root 1.2 }
419    
420     void
421     sampler (OpenCL::Context this, cl_bool normalized_coords, cl_addressing_mode addressing_mode, cl_filter_mode filter_mode)
422     PPCODE:
423     {
424     cl_int res;
425     cl_sampler sampler = clCreateSampler (this, normalized_coords, addressing_mode, filter_mode, &res);
426    
427     if (res)
428     FAIL (CreateSampler, res);
429 root 1.1
430 root 1.2 XPUSH_NEW_OBJ ("OpenCL::Sampler", sampler);
431 root 1.1 }
432    
433     void
434 root 1.2 program_with_source (OpenCL::Context this, SV *program)
435 root 1.1 PPCODE:
436     {
437 root 1.2 STRLEN len;
438     size_t len2;
439     const char *ptr = SvPVbyte (program, len);
440 root 1.1 cl_int res;
441 root 1.2 cl_program prog;
442    
443     len2 = len;
444     prog = clCreateProgramWithSource (this, 1, &ptr, &len2, &res);
445 root 1.1
446     if (res)
447 root 1.2 FAIL (CreateProgramWithSource, res);
448 root 1.1
449 root 1.2 XPUSH_NEW_OBJ ("OpenCL::Program", prog);
450 root 1.1 }
451    
452     MODULE = OpenCL PACKAGE = OpenCL::Queue
453    
454     void
455     DESTROY (OpenCL::Queue this)
456     CODE:
457     clReleaseCommandQueue (this);
458    
459     void
460     info (OpenCL::Queue this, cl_command_queue_info name)
461     PPCODE:
462 root 1.2 INFO (CommandQueue)
463    
464     void
465 root 1.3 enqueue_read_buffer (OpenCL::Queue this, OpenCL::Buffer mem, cl_bool blocking, size_t offset, size_t len, SV *data, ...)
466 root 1.2 PPCODE:
467     {
468     cl_event ev = 0;
469     EVENT_LIST (6, items - 6);
470    
471     SvUPGRADE (data, SVt_PV);
472     SvGROW (data, len);
473     SvPOK_only (data);
474     SvCUR_set (data, len);
475     NEED_SUCCESS (EnqueueReadBuffer, (this, mem, blocking, offset, len, SvPVX (data), event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
476    
477     if (ev)
478     XPUSH_NEW_OBJ ("OpenCL::Event", ev);
479     }
480    
481     void
482 root 1.3 enqueue_write_buffer (OpenCL::Queue this, OpenCL::Buffer mem, cl_bool blocking, size_t offset, SV *data, ...)
483 root 1.2 PPCODE:
484     {
485     cl_event ev = 0;
486     STRLEN len;
487     char *ptr = SvPVbyte (data, len);
488     EVENT_LIST (5, items - 5);
489    
490     NEED_SUCCESS (EnqueueReadBuffer, (this, mem, blocking, offset, len, ptr, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
491    
492     if (ev)
493     XPUSH_NEW_OBJ ("OpenCL::Event", ev);
494     }
495    
496     void
497 root 1.3 enqueue_copy_buffer (OpenCL::Queue this, OpenCL::Buffer src, OpenCL::Buffer dst, size_t src_offset, size_t dst_offset, size_t len, ...)
498 root 1.2 PPCODE:
499     {
500     cl_event ev = 0;
501     EVENT_LIST (6, items - 6);
502    
503     NEED_SUCCESS (EnqueueCopyBuffer, (this, src, dst, src_offset, dst_offset, len, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
504    
505     if (ev)
506     XPUSH_NEW_OBJ ("OpenCL::Event", ev);
507     }
508    
509 root 1.3 /*TODO http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueReadBufferRect.html */
510     /*TODO http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueWriteBufferRect.html */
511    
512     void
513     enqueue_read_image (OpenCL::Queue this, 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, ...)
514     PPCODE:
515     {
516     cl_event ev = 0;
517     const size_t src_origin[3] = { src_x, src_y, src_z };
518     const size_t region[3] = { width, height, depth };
519     size_t len = row_pitch * slice_pitch * depth;
520     EVENT_LIST (11, items - 11);
521    
522     SvUPGRADE (data, SVt_PV);
523     SvGROW (data, len);
524     SvPOK_only (data);
525     SvCUR_set (data, len);
526     NEED_SUCCESS (EnqueueReadImage, (this, src, blocking, src_origin, region, row_pitch, slice_pitch, SvPVX (data), event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
527    
528     if (ev)
529     XPUSH_NEW_OBJ ("OpenCL::Event", ev);
530     }
531    
532     void
533     enqueue_write_image (OpenCL::Queue this, 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, SV *data, ...)
534     PPCODE:
535     {
536     cl_event ev = 0;
537     const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
538     const size_t region[3] = { width, height, depth };
539     STRLEN len;
540     char *ptr = SvPVbyte (data, len);
541     size_t slice_pitch = len / (row_pitch * height);
542     EVENT_LIST (11, items - 11);
543    
544     NEED_SUCCESS (EnqueueWriteImage, (this, dst, blocking, dst_origin, region, row_pitch, slice_pitch, SvPVX (data), event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
545    
546     if (ev)
547     XPUSH_NEW_OBJ ("OpenCL::Event", ev);
548     }
549    
550     void
551     enqueue_copy_buffer_rect (OpenCL::Queue this, 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, ...)
552     PPCODE:
553     {
554     cl_event ev = 0;
555     const size_t src_origin[3] = { src_x, src_y, src_z };
556     const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
557     const size_t region[3] = { width, height, depth };
558     EVENT_LIST (16, items - 16);
559    
560     NEED_SUCCESS (EnqueueCopyBufferRect, (this, 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));
561    
562     if (ev)
563     XPUSH_NEW_OBJ ("OpenCL::Event", ev);
564     }
565    
566     void
567     enqueue_copy_buffer_to_image (OpenCL::Queue this, 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, ...)
568     PPCODE:
569     {
570     cl_event ev = 0;
571     const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
572     const size_t region[3] = { width, height, depth };
573     EVENT_LIST (10, items - 10);
574    
575     NEED_SUCCESS (EnqueueCopyBufferToImage, (this, src, dst, src_offset, dst_origin, region, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
576    
577     if (ev)
578     XPUSH_NEW_OBJ ("OpenCL::Event", ev);
579     }
580    
581     void
582     enqueue_copy_image (OpenCL::Queue this, OpenCL::Image 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, ...)
583     PPCODE:
584     {
585     cl_event ev = 0;
586     const size_t src_origin[3] = { src_x, src_y, src_z };
587     const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
588     const size_t region[3] = { width, height, depth };
589     EVENT_LIST (12, items - 12);
590    
591     NEED_SUCCESS (EnqueueCopyImage, (this, src, dst, src_origin, dst_origin, region, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
592    
593     if (ev)
594     XPUSH_NEW_OBJ ("OpenCL::Event", ev);
595     }
596    
597     void
598     enqueue_copy_image_to_buffer (OpenCL::Queue this, 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, ...)
599     PPCODE:
600     {
601     cl_event ev = 0;
602     const size_t src_origin[3] = { src_x, src_y, src_z };
603     const size_t region[3] = { width, height, depth };
604     EVENT_LIST (10, items - 10);
605    
606     NEED_SUCCESS (EnqueueCopyImageToBuffer, (this, src, dst, src_origin, region, dst_offset, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
607    
608     if (ev)
609     XPUSH_NEW_OBJ ("OpenCL::Event", ev);
610     }
611    
612     void
613     enqueue_task (OpenCL::Queue this, OpenCL::Kernel kernel, ...)
614     PPCODE:
615     {
616     cl_event ev = 0;
617     EVENT_LIST (2, items - 2);
618    
619     NEED_SUCCESS (EnqueueTask, (this, kernel, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
620    
621     if (ev)
622     XPUSH_NEW_OBJ ("OpenCL::Event", ev);
623     }
624    
625 root 1.4 void
626     enqueue_nd_range_kernel (OpenCL::Queue this, OpenCL::Kernel kernel, SV *global_work_offset, SV *global_work_size, SV *local_work_size = &PL_sv_undef, ...)
627     PPCODE:
628     {
629     cl_event ev = 0;
630     size_t *gwo = 0, *gws, *lws = 0;
631     int gws_len;
632     size_t *lists;
633     int i;
634 root 1.5 EVENT_LIST (5, items - 5);
635 root 1.4
636     if (!SvROK (global_work_size) || SvTYPE (SvRV (global_work_size)) != SVt_PVAV)
637     croak ("clEnqueueNDRangeKernel: global_work_size must be an array reference");
638    
639     gws_len = AvFILLp (SvRV (global_work_size)) + 1;
640    
641     lists = tmpbuf (sizeof (size_t) * 3 * gws_len);
642    
643     gws = lists + gws_len * 0;
644     for (i = 0; i < gws_len; ++i)
645     gws [i] = SvIV (AvARRAY (SvRV (global_work_size))[i]);
646    
647     if (SvOK (global_work_offset))
648     {
649     if (!SvROK (global_work_offset) || SvTYPE (SvRV (global_work_offset)) != SVt_PVAV)
650     croak ("clEnqueueNDRangeKernel: global_work_offset must be undef or an array reference");
651    
652     if (AvFILLp (SvRV (global_work_size)) + 1 != gws_len)
653     croak ("clEnqueueNDRangeKernel: global_work_offset must be undef or an array of same size as global_work_size");
654    
655     gwo = lists + gws_len * 1;
656     for (i = 0; i < gws_len; ++i)
657     gwo [i] = SvIV (AvARRAY (SvRV (global_work_offset))[i]);
658     }
659    
660     if (SvOK (local_work_size))
661     {
662     if (SvOK (local_work_size) && !SvROK (local_work_size) || SvTYPE (SvRV (local_work_size)) != SVt_PVAV)
663     croak ("clEnqueueNDRangeKernel: global_work_size must be undef or an array reference");
664    
665     if (AvFILLp (SvRV (local_work_size)) + 1 != gws_len)
666     croak ("clEnqueueNDRangeKernel: local_work_local must be undef or an array of same size as global_work_size");
667    
668     lws = lists + gws_len * 2;
669     for (i = 0; i < gws_len; ++i)
670     lws [i] = SvIV (AvARRAY (SvRV (local_work_size))[i]);
671     }
672    
673     NEED_SUCCESS (EnqueueNDRangeKernel, (this, kernel, gws_len, gwo, gws, lws, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
674    
675     if (ev)
676     XPUSH_NEW_OBJ ("OpenCL::Event", ev);
677     }
678 root 1.3
679 root 1.2 void
680     enqueue_marker (OpenCL::Queue this)
681     PPCODE:
682     {
683     cl_event ev;
684     NEED_SUCCESS (EnqueueMarker, (this, &ev));
685     XPUSH_NEW_OBJ ("OpenCL::Event", ev);
686     }
687    
688     void
689     enqueue_wait_for_events (OpenCL::Queue this, ...)
690     CODE:
691     {
692     EVENT_LIST (1, items - 1);
693     NEED_SUCCESS (EnqueueWaitForEvents, (this, event_list_count, event_list_ptr));
694     }
695    
696     void
697     enqueue_barrier (OpenCL::Queue this)
698     CODE:
699     NEED_SUCCESS (EnqueueBarrier, (this));
700    
701 root 1.3 void
702     flush (OpenCL::Queue this)
703     CODE:
704     NEED_SUCCESS (Flush, (this));
705    
706     void
707     finish (OpenCL::Queue this)
708     CODE:
709     NEED_SUCCESS (Finish, (this));
710    
711 root 1.2 MODULE = OpenCL PACKAGE = OpenCL::Memory
712    
713     void
714     DESTROY (OpenCL::Memory this)
715     CODE:
716     clReleaseMemObject (this);
717    
718     void
719     info (OpenCL::Memory this, cl_mem_info name)
720     PPCODE:
721     INFO (MemObject)
722    
723     MODULE = OpenCL PACKAGE = OpenCL::Sampler
724    
725     void
726     DESTROY (OpenCL::Sampler this)
727     CODE:
728     clReleaseSampler (this);
729    
730     void
731     info (OpenCL::Sampler this, cl_sampler_info name)
732     PPCODE:
733     INFO (Sampler)
734    
735     MODULE = OpenCL PACKAGE = OpenCL::Program
736    
737     void
738     DESTROY (OpenCL::Program this)
739     CODE:
740     clReleaseProgram (this);
741    
742     void
743     info (OpenCL::Program this, cl_program_info name)
744     PPCODE:
745     INFO (Program)
746    
747     void
748     build (OpenCL::Program this, OpenCL::Device device, SV *options = &PL_sv_undef)
749     CODE:
750     NEED_SUCCESS (BuildProgram, (this, 1, &device, SvPVbyte_nolen (options), 0, 0));
751    
752     void
753     build_info (OpenCL::Program this, OpenCL::Device device, cl_program_build_info name)
754     PPCODE:
755 root 1.1 {
756     size_t size;
757     SV *sv;
758 root 1.2
759     NEED_SUCCESS (GetProgramBuildInfo, (this, device, name, 0, 0, &size));
760 root 1.1 sv = sv_2mortal (newSV (size));
761     SvUPGRADE (sv, SVt_PV);
762     SvPOK_only (sv);
763     SvCUR_set (sv, size);
764 root 1.2 NEED_SUCCESS (GetProgramBuildInfo, (this, device, name, size, SvPVX (sv), 0));
765 root 1.1 XPUSHs (sv);
766     }
767    
768 root 1.2 void
769     kernel (OpenCL::Program program, SV *function)
770     PPCODE:
771     {
772     cl_int res;
773     cl_kernel kernel = clCreateKernel (program, SvPVbyte_nolen (function), &res);
774    
775     if (res)
776     FAIL (CreateKernel, res);
777    
778     XPUSH_NEW_OBJ ("OpenCL::Kernel", kernel);
779     }
780    
781     MODULE = OpenCL PACKAGE = OpenCL::Kernel
782    
783     void
784     DESTROY (OpenCL::Kernel this)
785     CODE:
786     clReleaseKernel (this);
787    
788     void
789     info (OpenCL::Kernel this, cl_kernel_info name)
790     PPCODE:
791     INFO (Kernel)
792    
793     void
794 root 1.3 set_char (OpenCL::Kernel this, cl_uint idx, cl_char value)
795     CODE:
796     clSetKernelArg (this, idx, sizeof (value), &value);
797    
798     void
799     set_uchar (OpenCL::Kernel this, cl_uint idx, cl_uchar value)
800     CODE:
801     clSetKernelArg (this, idx, sizeof (value), &value);
802    
803     void
804     set_short (OpenCL::Kernel this, cl_uint idx, cl_short value)
805     CODE:
806     clSetKernelArg (this, idx, sizeof (value), &value);
807    
808     void
809     set_ushort (OpenCL::Kernel this, cl_uint idx, cl_ushort value)
810     CODE:
811     clSetKernelArg (this, idx, sizeof (value), &value);
812    
813     void
814     set_int (OpenCL::Kernel this, cl_uint idx, cl_int value)
815     CODE:
816     clSetKernelArg (this, idx, sizeof (value), &value);
817    
818     void
819     set_uint (OpenCL::Kernel this, cl_uint idx, cl_uint value)
820     CODE:
821     clSetKernelArg (this, idx, sizeof (value), &value);
822    
823     void
824     set_long (OpenCL::Kernel this, cl_uint idx, cl_long value)
825     CODE:
826     clSetKernelArg (this, idx, sizeof (value), &value);
827    
828     void
829     set_ulong (OpenCL::Kernel this, cl_uint idx, cl_ulong value)
830     CODE:
831     clSetKernelArg (this, idx, sizeof (value), &value);
832    
833     void
834     set_half (OpenCL::Kernel this, cl_uint idx, cl_half value)
835     CODE:
836     clSetKernelArg (this, idx, sizeof (value), &value);
837    
838     void
839     set_float (OpenCL::Kernel this, cl_uint idx, cl_float value)
840     CODE:
841     clSetKernelArg (this, idx, sizeof (value), &value);
842    
843     void
844 root 1.5 set_double (OpenCL::Kernel this, cl_uint idx, cl_double value)
845     CODE:
846     clSetKernelArg (this, idx, sizeof (value), &value);
847    
848     void
849 root 1.3 set_memory (OpenCL::Kernel this, cl_uint idx, OpenCL::Memory_ornull value)
850     CODE:
851     clSetKernelArg (this, idx, sizeof (value), &value);
852    
853     void
854     set_buffer (OpenCL::Kernel this, cl_uint idx, OpenCL::Buffer_ornull value)
855     CODE:
856     clSetKernelArg (this, idx, sizeof (value), &value);
857    
858     void
859     set_image2d (OpenCL::Kernel this, cl_uint idx, OpenCL::Image2D_ornull value)
860     CODE:
861     clSetKernelArg (this, idx, sizeof (value), &value);
862    
863     void
864     set_image3d (OpenCL::Kernel this, cl_uint idx, OpenCL::Image3D_ornull value)
865     CODE:
866     clSetKernelArg (this, idx, sizeof (value), &value);
867    
868     void
869     set_sampler (OpenCL::Kernel this, cl_uint idx, OpenCL::Sampler value)
870     CODE:
871     clSetKernelArg (this, idx, sizeof (value), &value);
872    
873     void
874     set_event (OpenCL::Kernel this, cl_uint idx, OpenCL::Event value)
875 root 1.2 CODE:
876 root 1.3 clSetKernelArg (this, idx, sizeof (value), &value);
877 root 1.2
878     MODULE = OpenCL PACKAGE = OpenCL::Event
879    
880     void
881     DESTROY (OpenCL::Event this)
882     CODE:
883     clReleaseEvent (this);
884    
885     void
886     info (OpenCL::Event this, cl_event_info name)
887     PPCODE:
888     INFO (Event)
889    
890     void
891     wait (OpenCL::Event this)
892     CODE:
893     clWaitForEvents (1, &this);
894    
895 root 1.5 MODULE = OpenCL PACKAGE = OpenCL::UserEvent
896    
897     void
898     set_status (OpenCL::UserEvent this, cl_int execution_status)
899     CODE:
900     clSetUserEventStatus (this, execution_status);
901