ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/OpenCL/OpenCL.xs
Revision: 1.13
Committed: Sun Nov 20 01:09:48 2011 UTC (12 years, 6 months ago) by root
Branch: MAIN
Changes since 1.12: +97 -0 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #include "EXTERN.h"
2     #include "perl.h"
3     #include "XSUB.h"
4    
5     #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 root 1.13 #BEGIN:platform
244    
245     void
246     profile (OpenCL::Platform this)
247     PPCODE:
248     size_t size;
249     int i, n;
250     NEED_SUCCESS (GetPlatformInfo, (this, CL_PLATFORM_PROFILE, 0, 0, &size));
251     char *value = tmpbuf (size);
252     NEED_SUCCESS (GetPlatformInfo, (this, CL_PLATFORM_PROFILE, size, value, 0));
253     n = 1;
254     EXTEND (SP, n);
255     for (i = 0; i < n; ++i)
256     PUSHs (sv_2mortal (newSVpvn (value, size)));
257    
258     void
259     version (OpenCL::Platform this)
260     PPCODE:
261     size_t size;
262     int i, n;
263     NEED_SUCCESS (GetPlatformInfo, (this, CL_PLATFORM_VERSION, 0, 0, &size));
264     char *value = tmpbuf (size);
265     NEED_SUCCESS (GetPlatformInfo, (this, CL_PLATFORM_VERSION, size, value, 0));
266     n = 1;
267     EXTEND (SP, n);
268     for (i = 0; i < n; ++i)
269     PUSHs (sv_2mortal (newSVpvn (value, size)));
270    
271     void
272     name (OpenCL::Platform this)
273     PPCODE:
274     size_t size;
275     int i, n;
276     NEED_SUCCESS (GetPlatformInfo, (this, CL_PLATFORM_NAME, 0, 0, &size));
277     char *value = tmpbuf (size);
278     NEED_SUCCESS (GetPlatformInfo, (this, CL_PLATFORM_NAME, size, value, 0));
279     n = 1;
280     EXTEND (SP, n);
281     for (i = 0; i < n; ++i)
282     PUSHs (sv_2mortal (newSVpvn (value, size)));
283    
284     void
285     vendor (OpenCL::Platform this)
286     PPCODE:
287     size_t size;
288     int i, n;
289     NEED_SUCCESS (GetPlatformInfo, (this, CL_PLATFORM_VENDOR, 0, 0, &size));
290     char *value = tmpbuf (size);
291     NEED_SUCCESS (GetPlatformInfo, (this, CL_PLATFORM_VENDOR, size, value, 0));
292     n = 1;
293     EXTEND (SP, n);
294     for (i = 0; i < n; ++i)
295     PUSHs (sv_2mortal (newSVpvn (value, size)));
296    
297     void
298     extensions (OpenCL::Platform this)
299     PPCODE:
300     size_t size;
301     int i, n;
302     NEED_SUCCESS (GetPlatformInfo, (this, CL_PLATFORM_EXTENSIONS, 0, 0, &size));
303     char *value = tmpbuf (size);
304     NEED_SUCCESS (GetPlatformInfo, (this, CL_PLATFORM_EXTENSIONS, size, value, 0));
305     n = 1;
306     EXTEND (SP, n);
307     for (i = 0; i < n; ++i)
308     PUSHs (sv_2mortal (newSVpvn (value, size)));
309    
310     #END:platform
311    
312 root 1.1 void
313     devices (OpenCL::Platform this, cl_device_type type = CL_DEVICE_TYPE_ALL)
314     PPCODE:
315     cl_device_id *list;
316     cl_uint count;
317     int i;
318    
319 root 1.2 NEED_SUCCESS (GetDeviceIDs, (this, type, 0, 0, &count));
320 root 1.4 list = tmpbuf (sizeof (*list) * count);
321 root 1.2 NEED_SUCCESS (GetDeviceIDs, (this, type, count, list, 0));
322 root 1.1
323     EXTEND (SP, count);
324     for (i = 0; i < count; ++i)
325     PUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Device", list [i]));
326    
327     void
328 root 1.8 context (OpenCL::Platform this, FUTURE properties, SV *devices, FUTURE notify = 0)
329     PPCODE:
330     if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV)
331     croak ("OpenCL::Platform argument 'device' must be an arrayref with device objects, in call");
332    
333 root 1.11 AV *av = (AV *)SvRV (devices);
334 root 1.8 cl_uint num_devices = av_len (av) + 1;
335     cl_device_id *device_list = tmpbuf (sizeof (cl_device_id) * num_devices);
336     int i;
337    
338     for (i = num_devices; i--; )
339     device_list [i] = SvPTROBJ ("clCreateContext", "devices", *av_fetch (av, i, 0), "OpenCL::Device");
340    
341     NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (0, num_devices, device_list, 0, 0, &res));
342     XPUSH_NEW_OBJ ("OpenCL::Context", ctx);
343    
344     void
345 root 1.7 context_from_type (OpenCL::Platform this, FUTURE properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0)
346 root 1.1 PPCODE:
347     cl_context_properties props[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)this, 0 };
348 root 1.8 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (props, type, 0, 0, &res));
349 root 1.2 XPUSH_NEW_OBJ ("OpenCL::Context", ctx);
350 root 1.1
351     MODULE = OpenCL PACKAGE = OpenCL::Device
352    
353     void
354     info (OpenCL::Device this, cl_device_info name)
355     PPCODE:
356 root 1.2 INFO (Device)
357 root 1.1
358     MODULE = OpenCL PACKAGE = OpenCL::Context
359    
360     void
361     DESTROY (OpenCL::Context context)
362     CODE:
363     clReleaseContext (context);
364    
365     void
366     info (OpenCL::Context this, cl_context_info name)
367     PPCODE:
368 root 1.2 INFO (Context)
369    
370     void
371 root 1.7 queue (OpenCL::Context this, OpenCL::Device device, cl_command_queue_properties properties = 0)
372 root 1.2 PPCODE:
373 root 1.8 NEED_SUCCESS_ARG (cl_command_queue queue, CreateCommandQueue, (this, device, properties, &res));
374 root 1.2 XPUSH_NEW_OBJ ("OpenCL::Queue", queue);
375    
376     void
377 root 1.5 user_event (OpenCL::Context this)
378     PPCODE:
379 root 1.8 NEED_SUCCESS_ARG (cl_event ev, CreateUserEvent, (this, &res));
380 root 1.5 XPUSH_NEW_OBJ ("OpenCL::UserEvent", ev);
381    
382     void
383 root 1.2 buffer (OpenCL::Context this, cl_mem_flags flags, size_t len)
384     PPCODE:
385 root 1.3 if (flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))
386     croak ("clCreateBuffer: cannot use/copy host ptr when no data is given, use $context->buffer_sv instead?");
387    
388 root 1.8 NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (this, flags, len, 0, &res));
389 root 1.3 XPUSH_NEW_OBJ ("OpenCL::Buffer", mem);
390 root 1.2
391     void
392     buffer_sv (OpenCL::Context this, cl_mem_flags flags, SV *data)
393     PPCODE:
394     STRLEN len;
395     char *ptr = SvPVbyte (data, len);
396 root 1.3
397     if (!(flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR)))
398     croak ("clCreateBuffer: have to specify use or copy host ptr when buffer data is given, use $context->buffer instead?");
399    
400 root 1.8 NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (this, flags, len, ptr, &res));
401 root 1.3 XPUSH_NEW_OBJ ("OpenCL::Buffer", mem);
402    
403     void
404 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)
405 root 1.3 PPCODE:
406     STRLEN len;
407     char *ptr = SvPVbyte (data, len);
408     const cl_image_format format = { channel_order, channel_type };
409 root 1.10 NEED_SUCCESS_ARG (cl_mem mem, CreateImage2D, (this, flags, &format, width, height, row_pitch, ptr, &res));
410 root 1.3 XPUSH_NEW_OBJ ("OpenCL::Image2D", mem);
411    
412     void
413 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)
414 root 1.3 PPCODE:
415     STRLEN len;
416     char *ptr = SvPVbyte (data, len);
417     const cl_image_format format = { channel_order, channel_type };
418 root 1.10 NEED_SUCCESS_ARG (cl_mem mem, CreateImage3D, (this, flags, &format, width, height, depth, row_pitch, slice_pitch, ptr, &res));
419 root 1.3 XPUSH_NEW_OBJ ("OpenCL::Image3D", mem);
420    
421     void
422     supported_image_formats (OpenCL::Context this, cl_mem_flags flags, cl_mem_object_type image_type)
423     PPCODE:
424     {
425     cl_uint count;
426     cl_image_format *list;
427     int i;
428    
429     NEED_SUCCESS (GetSupportedImageFormats, (this, flags, image_type, 0, 0, &count));
430     Newx (list, count, cl_image_format);
431     NEED_SUCCESS (GetSupportedImageFormats, (this, flags, image_type, count, list, 0));
432    
433     EXTEND (SP, count);
434     for (i = 0; i < count; ++i)
435     {
436     AV *av = newAV ();
437     av_store (av, 1, newSVuv (list [i].image_channel_data_type));
438     av_store (av, 0, newSVuv (list [i].image_channel_order));
439     PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
440     }
441 root 1.2 }
442    
443     void
444     sampler (OpenCL::Context this, cl_bool normalized_coords, cl_addressing_mode addressing_mode, cl_filter_mode filter_mode)
445     PPCODE:
446 root 1.8 NEED_SUCCESS_ARG (cl_sampler sampler, CreateSampler, (this, normalized_coords, addressing_mode, filter_mode, &res));
447 root 1.2 XPUSH_NEW_OBJ ("OpenCL::Sampler", sampler);
448 root 1.1
449     void
450 root 1.2 program_with_source (OpenCL::Context this, SV *program)
451 root 1.1 PPCODE:
452 root 1.2 STRLEN len;
453     size_t len2;
454     const char *ptr = SvPVbyte (program, len);
455    
456     len2 = len;
457 root 1.8 NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithSource, (this, 1, &ptr, &len2, &res));
458 root 1.2 XPUSH_NEW_OBJ ("OpenCL::Program", prog);
459 root 1.1
460 root 1.13 #BEGIN:context
461    
462     #END:context
463    
464 root 1.1 MODULE = OpenCL PACKAGE = OpenCL::Queue
465    
466     void
467     DESTROY (OpenCL::Queue this)
468     CODE:
469     clReleaseCommandQueue (this);
470    
471     void
472     info (OpenCL::Queue this, cl_command_queue_info name)
473     PPCODE:
474 root 1.2 INFO (CommandQueue)
475    
476     void
477 root 1.3 enqueue_read_buffer (OpenCL::Queue this, OpenCL::Buffer mem, cl_bool blocking, size_t offset, size_t len, SV *data, ...)
478 root 1.2 PPCODE:
479     cl_event ev = 0;
480     EVENT_LIST (6, items - 6);
481    
482     SvUPGRADE (data, SVt_PV);
483     SvGROW (data, len);
484     SvPOK_only (data);
485     SvCUR_set (data, len);
486     NEED_SUCCESS (EnqueueReadBuffer, (this, mem, blocking, offset, len, SvPVX (data), event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
487    
488     if (ev)
489     XPUSH_NEW_OBJ ("OpenCL::Event", ev);
490    
491     void
492 root 1.3 enqueue_write_buffer (OpenCL::Queue this, OpenCL::Buffer mem, cl_bool blocking, size_t offset, SV *data, ...)
493 root 1.2 PPCODE:
494     cl_event ev = 0;
495     STRLEN len;
496     char *ptr = SvPVbyte (data, len);
497     EVENT_LIST (5, items - 5);
498    
499     NEED_SUCCESS (EnqueueReadBuffer, (this, mem, blocking, offset, len, ptr, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
500    
501     if (ev)
502     XPUSH_NEW_OBJ ("OpenCL::Event", ev);
503    
504     void
505 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, ...)
506 root 1.2 PPCODE:
507     cl_event ev = 0;
508     EVENT_LIST (6, items - 6);
509    
510     NEED_SUCCESS (EnqueueCopyBuffer, (this, src, dst, src_offset, dst_offset, len, 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 root 1.3 void
516     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, ...)
517     PPCODE:
518     cl_event ev = 0;
519     const size_t src_origin[3] = { src_x, src_y, src_z };
520     const size_t region[3] = { width, height, depth };
521 root 1.10 EVENT_LIST (12, items - 12);
522    
523 root 1.11 if (!row_pitch)
524     row_pitch = img_row_pitch (src);
525    
526     if (depth > 1 && !slice_pitch)
527     slice_pitch = row_pitch * height;
528    
529     size_t len = slice_pitch ? slice_pitch * depth : row_pitch * height;
530 root 1.3
531     SvUPGRADE (data, SVt_PV);
532     SvGROW (data, len);
533     SvPOK_only (data);
534     SvCUR_set (data, len);
535     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));
536    
537     if (ev)
538     XPUSH_NEW_OBJ ("OpenCL::Event", ev);
539    
540     void
541 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, ...)
542 root 1.3 PPCODE:
543     cl_event ev = 0;
544     const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
545     const size_t region[3] = { width, height, depth };
546     STRLEN len;
547     char *ptr = SvPVbyte (data, len);
548 root 1.10 EVENT_LIST (12, items - 12);
549 root 1.3
550 root 1.11 if (!row_pitch)
551     row_pitch = img_row_pitch (dst);
552    
553     if (depth > 1 && !slice_pitch)
554     slice_pitch = row_pitch * height;
555    
556     size_t min_len = slice_pitch ? slice_pitch * depth : row_pitch * height;
557    
558     if (len < min_len)
559     croak ("clEnqueueWriteImage: data string is shorter than what would be transferred");
560    
561 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));
562    
563     if (ev)
564     XPUSH_NEW_OBJ ("OpenCL::Event", ev);
565    
566     void
567     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, ...)
568     PPCODE:
569     cl_event ev = 0;
570     const size_t src_origin[3] = { src_x, src_y, src_z };
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 (16, items - 16);
574    
575     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));
576    
577     if (ev)
578     XPUSH_NEW_OBJ ("OpenCL::Event", ev);
579    
580     void
581     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, ...)
582     PPCODE:
583     cl_event ev = 0;
584     const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
585     const size_t region[3] = { width, height, depth };
586     EVENT_LIST (10, items - 10);
587    
588     NEED_SUCCESS (EnqueueCopyBufferToImage, (this, src, dst, src_offset, dst_origin, region, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
589    
590     if (ev)
591     XPUSH_NEW_OBJ ("OpenCL::Event", ev);
592    
593     void
594 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, ...)
595 root 1.3 PPCODE:
596     cl_event ev = 0;
597     const size_t src_origin[3] = { src_x, src_y, src_z };
598     const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
599     const size_t region[3] = { width, height, depth };
600     EVENT_LIST (12, items - 12);
601    
602     NEED_SUCCESS (EnqueueCopyImage, (this, src, dst, src_origin, dst_origin, region, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
603    
604     if (ev)
605     XPUSH_NEW_OBJ ("OpenCL::Event", ev);
606    
607     void
608     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, ...)
609     PPCODE:
610     cl_event ev = 0;
611     const size_t src_origin[3] = { src_x, src_y, src_z };
612     const size_t region[3] = { width, height, depth };
613     EVENT_LIST (10, items - 10);
614    
615     NEED_SUCCESS (EnqueueCopyImageToBuffer, (this, src, dst, src_origin, region, dst_offset, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
616    
617     if (ev)
618     XPUSH_NEW_OBJ ("OpenCL::Event", ev);
619    
620     void
621     enqueue_task (OpenCL::Queue this, OpenCL::Kernel kernel, ...)
622     PPCODE:
623     cl_event ev = 0;
624     EVENT_LIST (2, items - 2);
625    
626     NEED_SUCCESS (EnqueueTask, (this, kernel, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
627    
628     if (ev)
629     XPUSH_NEW_OBJ ("OpenCL::Event", ev);
630    
631 root 1.4 void
632     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, ...)
633     PPCODE:
634     cl_event ev = 0;
635     size_t *gwo = 0, *gws, *lws = 0;
636     int gws_len;
637     size_t *lists;
638     int i;
639 root 1.5 EVENT_LIST (5, items - 5);
640 root 1.4
641     if (!SvROK (global_work_size) || SvTYPE (SvRV (global_work_size)) != SVt_PVAV)
642     croak ("clEnqueueNDRangeKernel: global_work_size must be an array reference");
643    
644     gws_len = AvFILLp (SvRV (global_work_size)) + 1;
645    
646     lists = tmpbuf (sizeof (size_t) * 3 * gws_len);
647    
648     gws = lists + gws_len * 0;
649     for (i = 0; i < gws_len; ++i)
650     gws [i] = SvIV (AvARRAY (SvRV (global_work_size))[i]);
651    
652     if (SvOK (global_work_offset))
653     {
654     if (!SvROK (global_work_offset) || SvTYPE (SvRV (global_work_offset)) != SVt_PVAV)
655     croak ("clEnqueueNDRangeKernel: global_work_offset must be undef or an array reference");
656    
657     if (AvFILLp (SvRV (global_work_size)) + 1 != gws_len)
658     croak ("clEnqueueNDRangeKernel: global_work_offset must be undef or an array of same size as global_work_size");
659    
660     gwo = lists + gws_len * 1;
661     for (i = 0; i < gws_len; ++i)
662     gwo [i] = SvIV (AvARRAY (SvRV (global_work_offset))[i]);
663     }
664    
665     if (SvOK (local_work_size))
666     {
667     if (SvOK (local_work_size) && !SvROK (local_work_size) || SvTYPE (SvRV (local_work_size)) != SVt_PVAV)
668     croak ("clEnqueueNDRangeKernel: global_work_size must be undef or an array reference");
669    
670     if (AvFILLp (SvRV (local_work_size)) + 1 != gws_len)
671     croak ("clEnqueueNDRangeKernel: local_work_local must be undef or an array of same size as global_work_size");
672    
673     lws = lists + gws_len * 2;
674     for (i = 0; i < gws_len; ++i)
675     lws [i] = SvIV (AvARRAY (SvRV (local_work_size))[i]);
676     }
677    
678     NEED_SUCCESS (EnqueueNDRangeKernel, (this, kernel, gws_len, gwo, gws, lws, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
679    
680     if (ev)
681     XPUSH_NEW_OBJ ("OpenCL::Event", ev);
682 root 1.3
683 root 1.2 void
684     enqueue_marker (OpenCL::Queue this)
685     PPCODE:
686     cl_event ev;
687     NEED_SUCCESS (EnqueueMarker, (this, &ev));
688     XPUSH_NEW_OBJ ("OpenCL::Event", ev);
689    
690     void
691     enqueue_wait_for_events (OpenCL::Queue this, ...)
692     CODE:
693     EVENT_LIST (1, items - 1);
694     NEED_SUCCESS (EnqueueWaitForEvents, (this, event_list_count, event_list_ptr));
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 root 1.13 MODULE = OpenCL PACKAGE = OpenCL::Image
724    
725     void
726     image_info (OpenCL::Image this, cl_image_info name)
727     PPCODE:
728     INFO (Image)
729    
730 root 1.2 MODULE = OpenCL PACKAGE = OpenCL::Sampler
731    
732     void
733     DESTROY (OpenCL::Sampler this)
734     CODE:
735     clReleaseSampler (this);
736    
737     void
738     info (OpenCL::Sampler this, cl_sampler_info name)
739     PPCODE:
740     INFO (Sampler)
741    
742     MODULE = OpenCL PACKAGE = OpenCL::Program
743    
744     void
745     DESTROY (OpenCL::Program this)
746     CODE:
747     clReleaseProgram (this);
748    
749     void
750     info (OpenCL::Program this, cl_program_info name)
751     PPCODE:
752     INFO (Program)
753    
754     void
755     build (OpenCL::Program this, OpenCL::Device device, SV *options = &PL_sv_undef)
756     CODE:
757     NEED_SUCCESS (BuildProgram, (this, 1, &device, SvPVbyte_nolen (options), 0, 0));
758    
759     void
760     build_info (OpenCL::Program this, OpenCL::Device device, cl_program_build_info name)
761     PPCODE:
762 root 1.1 size_t size;
763 root 1.2 NEED_SUCCESS (GetProgramBuildInfo, (this, device, name, 0, 0, &size));
764 root 1.10 SV *sv = sv_2mortal (newSV (size));
765 root 1.1 SvUPGRADE (sv, SVt_PV);
766     SvPOK_only (sv);
767     SvCUR_set (sv, size);
768 root 1.2 NEED_SUCCESS (GetProgramBuildInfo, (this, device, name, size, SvPVX (sv), 0));
769 root 1.1 XPUSHs (sv);
770    
771 root 1.2 void
772     kernel (OpenCL::Program program, SV *function)
773     PPCODE:
774 root 1.8 NEED_SUCCESS_ARG (cl_kernel kernel, CreateKernel, (program, SvPVbyte_nolen (function), &res));
775 root 1.2 XPUSH_NEW_OBJ ("OpenCL::Kernel", kernel);
776    
777     MODULE = OpenCL PACKAGE = OpenCL::Kernel
778    
779     void
780     DESTROY (OpenCL::Kernel this)
781     CODE:
782     clReleaseKernel (this);
783    
784     void
785     info (OpenCL::Kernel this, cl_kernel_info name)
786     PPCODE:
787     INFO (Kernel)
788    
789     void
790 root 1.13 work_group_info (OpenCL::Kernel this, OpenCL::Device device, cl_kernel_work_group_info name)
791     PPCODE:
792     size_t size;
793     NEED_SUCCESS (GetKernelWorkGroupInfo, (this, device, name, 0, 0, &size));
794     SV *sv = sv_2mortal (newSV (size));
795     SvUPGRADE (sv, SVt_PV);
796     SvPOK_only (sv);
797     SvCUR_set (sv, size);
798     NEED_SUCCESS (GetKernelWorkGroupInfo, (this, device, name, size, SvPVX (sv), 0));
799     XPUSHs (sv);
800    
801     void
802 root 1.3 set_char (OpenCL::Kernel this, cl_uint idx, cl_char value)
803     CODE:
804     clSetKernelArg (this, idx, sizeof (value), &value);
805    
806     void
807     set_uchar (OpenCL::Kernel this, cl_uint idx, cl_uchar value)
808     CODE:
809     clSetKernelArg (this, idx, sizeof (value), &value);
810    
811     void
812     set_short (OpenCL::Kernel this, cl_uint idx, cl_short value)
813     CODE:
814     clSetKernelArg (this, idx, sizeof (value), &value);
815    
816     void
817     set_ushort (OpenCL::Kernel this, cl_uint idx, cl_ushort value)
818     CODE:
819     clSetKernelArg (this, idx, sizeof (value), &value);
820    
821     void
822     set_int (OpenCL::Kernel this, cl_uint idx, cl_int value)
823     CODE:
824     clSetKernelArg (this, idx, sizeof (value), &value);
825    
826     void
827     set_uint (OpenCL::Kernel this, cl_uint idx, cl_uint value)
828     CODE:
829     clSetKernelArg (this, idx, sizeof (value), &value);
830    
831     void
832     set_long (OpenCL::Kernel this, cl_uint idx, cl_long value)
833     CODE:
834     clSetKernelArg (this, idx, sizeof (value), &value);
835    
836     void
837     set_ulong (OpenCL::Kernel this, cl_uint idx, cl_ulong value)
838     CODE:
839     clSetKernelArg (this, idx, sizeof (value), &value);
840    
841     void
842     set_half (OpenCL::Kernel this, cl_uint idx, cl_half value)
843     CODE:
844     clSetKernelArg (this, idx, sizeof (value), &value);
845    
846     void
847     set_float (OpenCL::Kernel this, cl_uint idx, cl_float value)
848     CODE:
849     clSetKernelArg (this, idx, sizeof (value), &value);
850    
851     void
852 root 1.5 set_double (OpenCL::Kernel this, cl_uint idx, cl_double value)
853     CODE:
854     clSetKernelArg (this, idx, sizeof (value), &value);
855    
856     void
857 root 1.3 set_memory (OpenCL::Kernel this, cl_uint idx, OpenCL::Memory_ornull value)
858     CODE:
859     clSetKernelArg (this, idx, sizeof (value), &value);
860    
861     void
862     set_buffer (OpenCL::Kernel this, cl_uint idx, OpenCL::Buffer_ornull value)
863     CODE:
864     clSetKernelArg (this, idx, sizeof (value), &value);
865    
866     void
867     set_image2d (OpenCL::Kernel this, cl_uint idx, OpenCL::Image2D_ornull value)
868     CODE:
869     clSetKernelArg (this, idx, sizeof (value), &value);
870    
871     void
872     set_image3d (OpenCL::Kernel this, cl_uint idx, OpenCL::Image3D_ornull value)
873     CODE:
874     clSetKernelArg (this, idx, sizeof (value), &value);
875    
876     void
877     set_sampler (OpenCL::Kernel this, cl_uint idx, OpenCL::Sampler value)
878     CODE:
879     clSetKernelArg (this, idx, sizeof (value), &value);
880    
881     void
882     set_event (OpenCL::Kernel this, cl_uint idx, OpenCL::Event value)
883 root 1.2 CODE:
884 root 1.3 clSetKernelArg (this, idx, sizeof (value), &value);
885 root 1.2
886     MODULE = OpenCL PACKAGE = OpenCL::Event
887    
888     void
889     DESTROY (OpenCL::Event this)
890     CODE:
891     clReleaseEvent (this);
892    
893     void
894     info (OpenCL::Event this, cl_event_info name)
895     PPCODE:
896     INFO (Event)
897    
898     void
899 root 1.13 profiling_info (OpenCL::Event this, cl_profiling_info name)
900     PPCODE:
901     INFO (EventProfiling)
902    
903     void
904 root 1.2 wait (OpenCL::Event this)
905     CODE:
906     clWaitForEvents (1, &this);
907    
908 root 1.5 MODULE = OpenCL PACKAGE = OpenCL::UserEvent
909    
910     void
911     set_status (OpenCL::UserEvent this, cl_int execution_status)
912     CODE:
913     clSetUserEventStatus (this, execution_status);
914