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