ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/OpenCL/OpenCL.xs
Revision: 1.8
Committed: Thu Nov 17 02:54:14 2011 UTC (12 years, 6 months ago) by root
Branch: MAIN
CVS Tags: rel-0_14
Changes since 1.7: +46 -113 lines
Log Message:
*** empty log message ***

File Contents

# Content
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 typedef cl_mem OpenCL__Memory;
12 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 typedef cl_sampler OpenCL__Sampler;
22 typedef cl_program OpenCL__Program;
23 typedef cl_kernel OpenCL__Kernel;
24 typedef cl_event OpenCL__Event;
25 typedef cl_event OpenCL__UserEvent;
26
27 typedef SV *FUTURE;
28
29 /*****************************************************************************/
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
53 typedef struct
54 {
55 IV iv;
56 const char *name;
57 #define const_iv(name) { (IV)CL_ ## name, # name },
58 } ivstr;
59
60 static const char *
61 iv2str (IV value, const ivstr *base, int count, const char *fallback)
62 {
63 int i;
64 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
82 return iv2str (value, enumstr, sizeof (enumstr) / sizeof (enumstr [0]), "ENUM(0x%04x)");
83 }
84
85 static const char *
86 err2str (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
97 static cl_int res;
98
99 #define FAIL(name) \
100 croak ("cl" # name ": %s", err2str (res));
101
102 #define NEED_SUCCESS(name,args) \
103 do { \
104 res = cl ## name args; \
105 \
106 if (res) \
107 FAIL (name); \
108 } while (0)
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
120 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 static cl_event *
132 event_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
160 MODULE = OpenCL PACKAGE = OpenCL
161
162 PROTOTYPES: ENABLE
163
164 BOOT:
165 {
166 HV *stash = gv_stashpv ("OpenCL", 1);
167 static const ivstr *civ, const_iv[] = {
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" },
179 #include "constiv.h"
180 };
181 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
182 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
183 }
184
185 cl_int
186 errno ()
187 CODE:
188 errno = res;
189
190 const char *
191 err2str (cl_int err)
192
193 const char *
194 enum2str (cl_uint value)
195
196 void
197 platforms ()
198 PPCODE:
199 {
200 cl_platform_id *list;
201 cl_uint count;
202 int i;
203
204 NEED_SUCCESS (GetPlatformIDs, (0, 0, &count));
205 list = tmpbuf (sizeof (*list) * count);
206 NEED_SUCCESS (GetPlatformIDs, (count, list, 0));
207
208 EXTEND (SP, count);
209 for (i = 0; i < count; ++i)
210 PUSHs (NEW_MORTAL_OBJ ("OpenCL::Platform", list [i]));
211 }
212
213 void
214 context_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
219 void
220 context (FUTURE properties, FUTURE devices, FUTURE notify = 0)
221 PPCODE:
222 /* der Gipfel der Kunst */
223
224 void
225 wait_for_events (...)
226 CODE:
227 {
228 EVENT_LIST (0, items);
229 NEED_SUCCESS (WaitForEvents, (event_list_count, event_list_ptr));
230 }
231
232 PROTOTYPES: DISABLE
233
234 MODULE = OpenCL PACKAGE = OpenCL::Platform
235
236 void
237 info (OpenCL::Platform this, cl_platform_info name)
238 PPCODE:
239 INFO (Platform)
240
241 void
242 devices (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
258 void
259 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 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
275 void
276 context_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
282 MODULE = OpenCL PACKAGE = OpenCL::Device
283
284 void
285 info (OpenCL::Device this, cl_device_info name)
286 PPCODE:
287 INFO (Device)
288
289 MODULE = OpenCL PACKAGE = OpenCL::Context
290
291 void
292 DESTROY (OpenCL::Context context)
293 CODE:
294 clReleaseContext (context);
295
296 void
297 info (OpenCL::Context this, cl_context_info name)
298 PPCODE:
299 INFO (Context)
300
301 void
302 queue (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
307 void
308 user_event (OpenCL::Context this)
309 PPCODE:
310 NEED_SUCCESS_ARG (cl_event ev, CreateUserEvent, (this, &res));
311 XPUSH_NEW_OBJ ("OpenCL::UserEvent", ev);
312
313 void
314 buffer (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
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
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
334 void
335 image2d (OpenCL::Context this, cl_mem_flags flags, cl_channel_order channel_order, cl_channel_type channel_type, size_t width, size_t height, SV *data)
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
343 void
344 image3d (OpenCL::Context this, cl_mem_flags flags, cl_channel_order channel_order, cl_channel_type channel_type, size_t width, size_t height, size_t depth, size_t slice_pitch, SV *data)
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
353 void
354 supported_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
375 void
376 sampler (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
381 void
382 program_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
392 MODULE = OpenCL PACKAGE = OpenCL::Queue
393
394 void
395 DESTROY (OpenCL::Queue this)
396 CODE:
397 clReleaseCommandQueue (this);
398
399 void
400 info (OpenCL::Queue this, cl_command_queue_info name)
401 PPCODE:
402 INFO (CommandQueue)
403
404 void
405 enqueue_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
421 void
422 enqueue_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
436 void
437 enqueue_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
452 void
453 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, ...)
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
472 void
473 enqueue_write_image (OpenCL::Queue this, OpenCL::Image dst, cl_bool blocking, size_t dst_x, size_t dst_y, size_t dst_z, size_t width, size_t height, size_t depth, size_t row_pitch, SV *data, ...)
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
490 void
491 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, ...)
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
506 void
507 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, ...)
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
521 void
522 enqueue_copy_image (OpenCL::Queue this, OpenCL::Image src, OpenCL::Buffer dst, size_t src_x, size_t src_y, size_t src_z, size_t dst_x, size_t dst_y, size_t dst_z, size_t width, size_t height, size_t depth, ...)
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
537 void
538 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, ...)
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
552 void
553 enqueue_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
565 void
566 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, ...)
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
619 void
620 enqueue_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
628 void
629 enqueue_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
636 void
637 enqueue_barrier (OpenCL::Queue this)
638 CODE:
639 NEED_SUCCESS (EnqueueBarrier, (this));
640
641 void
642 flush (OpenCL::Queue this)
643 CODE:
644 NEED_SUCCESS (Flush, (this));
645
646 void
647 finish (OpenCL::Queue this)
648 CODE:
649 NEED_SUCCESS (Finish, (this));
650
651 MODULE = OpenCL PACKAGE = OpenCL::Memory
652
653 void
654 DESTROY (OpenCL::Memory this)
655 CODE:
656 clReleaseMemObject (this);
657
658 void
659 info (OpenCL::Memory this, cl_mem_info name)
660 PPCODE:
661 INFO (MemObject)
662
663 MODULE = OpenCL PACKAGE = OpenCL::Sampler
664
665 void
666 DESTROY (OpenCL::Sampler this)
667 CODE:
668 clReleaseSampler (this);
669
670 void
671 info (OpenCL::Sampler this, cl_sampler_info name)
672 PPCODE:
673 INFO (Sampler)
674
675 MODULE = OpenCL PACKAGE = OpenCL::Program
676
677 void
678 DESTROY (OpenCL::Program this)
679 CODE:
680 clReleaseProgram (this);
681
682 void
683 info (OpenCL::Program this, cl_program_info name)
684 PPCODE:
685 INFO (Program)
686
687 void
688 build (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
692 void
693 build_info (OpenCL::Program this, OpenCL::Device device, cl_program_build_info name)
694 PPCODE:
695 {
696 size_t size;
697 SV *sv;
698
699 NEED_SUCCESS (GetProgramBuildInfo, (this, device, name, 0, 0, &size));
700 sv = sv_2mortal (newSV (size));
701 SvUPGRADE (sv, SVt_PV);
702 SvPOK_only (sv);
703 SvCUR_set (sv, size);
704 NEED_SUCCESS (GetProgramBuildInfo, (this, device, name, size, SvPVX (sv), 0));
705 XPUSHs (sv);
706 }
707
708 void
709 kernel (OpenCL::Program program, SV *function)
710 PPCODE:
711 NEED_SUCCESS_ARG (cl_kernel kernel, CreateKernel, (program, SvPVbyte_nolen (function), &res));
712 XPUSH_NEW_OBJ ("OpenCL::Kernel", kernel);
713
714 MODULE = OpenCL PACKAGE = OpenCL::Kernel
715
716 void
717 DESTROY (OpenCL::Kernel this)
718 CODE:
719 clReleaseKernel (this);
720
721 void
722 info (OpenCL::Kernel this, cl_kernel_info name)
723 PPCODE:
724 INFO (Kernel)
725
726 void
727 set_char (OpenCL::Kernel this, cl_uint idx, cl_char value)
728 CODE:
729 clSetKernelArg (this, idx, sizeof (value), &value);
730
731 void
732 set_uchar (OpenCL::Kernel this, cl_uint idx, cl_uchar value)
733 CODE:
734 clSetKernelArg (this, idx, sizeof (value), &value);
735
736 void
737 set_short (OpenCL::Kernel this, cl_uint idx, cl_short value)
738 CODE:
739 clSetKernelArg (this, idx, sizeof (value), &value);
740
741 void
742 set_ushort (OpenCL::Kernel this, cl_uint idx, cl_ushort value)
743 CODE:
744 clSetKernelArg (this, idx, sizeof (value), &value);
745
746 void
747 set_int (OpenCL::Kernel this, cl_uint idx, cl_int value)
748 CODE:
749 clSetKernelArg (this, idx, sizeof (value), &value);
750
751 void
752 set_uint (OpenCL::Kernel this, cl_uint idx, cl_uint value)
753 CODE:
754 clSetKernelArg (this, idx, sizeof (value), &value);
755
756 void
757 set_long (OpenCL::Kernel this, cl_uint idx, cl_long value)
758 CODE:
759 clSetKernelArg (this, idx, sizeof (value), &value);
760
761 void
762 set_ulong (OpenCL::Kernel this, cl_uint idx, cl_ulong value)
763 CODE:
764 clSetKernelArg (this, idx, sizeof (value), &value);
765
766 void
767 set_half (OpenCL::Kernel this, cl_uint idx, cl_half value)
768 CODE:
769 clSetKernelArg (this, idx, sizeof (value), &value);
770
771 void
772 set_float (OpenCL::Kernel this, cl_uint idx, cl_float value)
773 CODE:
774 clSetKernelArg (this, idx, sizeof (value), &value);
775
776 void
777 set_double (OpenCL::Kernel this, cl_uint idx, cl_double value)
778 CODE:
779 clSetKernelArg (this, idx, sizeof (value), &value);
780
781 void
782 set_memory (OpenCL::Kernel this, cl_uint idx, OpenCL::Memory_ornull value)
783 CODE:
784 clSetKernelArg (this, idx, sizeof (value), &value);
785
786 void
787 set_buffer (OpenCL::Kernel this, cl_uint idx, OpenCL::Buffer_ornull value)
788 CODE:
789 clSetKernelArg (this, idx, sizeof (value), &value);
790
791 void
792 set_image2d (OpenCL::Kernel this, cl_uint idx, OpenCL::Image2D_ornull value)
793 CODE:
794 clSetKernelArg (this, idx, sizeof (value), &value);
795
796 void
797 set_image3d (OpenCL::Kernel this, cl_uint idx, OpenCL::Image3D_ornull value)
798 CODE:
799 clSetKernelArg (this, idx, sizeof (value), &value);
800
801 void
802 set_sampler (OpenCL::Kernel this, cl_uint idx, OpenCL::Sampler value)
803 CODE:
804 clSetKernelArg (this, idx, sizeof (value), &value);
805
806 void
807 set_event (OpenCL::Kernel this, cl_uint idx, OpenCL::Event value)
808 CODE:
809 clSetKernelArg (this, idx, sizeof (value), &value);
810
811 MODULE = OpenCL PACKAGE = OpenCL::Event
812
813 void
814 DESTROY (OpenCL::Event this)
815 CODE:
816 clReleaseEvent (this);
817
818 void
819 info (OpenCL::Event this, cl_event_info name)
820 PPCODE:
821 INFO (Event)
822
823 void
824 wait (OpenCL::Event this)
825 CODE:
826 clWaitForEvents (1, &this);
827
828 MODULE = OpenCL PACKAGE = OpenCL::UserEvent
829
830 void
831 set_status (OpenCL::UserEvent this, cl_int execution_status)
832 CODE:
833 clSetUserEventStatus (this, execution_status);
834