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