ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/OpenCL/OpenCL.xs
Revision: 1.12
Committed: Thu Nov 17 06:22:29 2011 UTC (12 years, 6 months ago) by root
Branch: MAIN
CVS Tags: rel-0_15
Changes since 1.11: +2 -2 lines
Log Message:
0.15

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