ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/OpenCL/OpenCL.xs
(Generate patch)

Comparing OpenCL/OpenCL.xs (file contents):
Revision 1.1 by root, Tue Nov 15 06:50:30 2011 UTC vs.
Revision 1.8 by root, Thu Nov 17 02:54:14 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines