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.5 by root, Wed Nov 16 00:35:30 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 { 27/*****************************************************************************/
28
29/* up to two temporary buffers */
30static void *
31tmpbuf (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
51typedef struct
52{
13 IV iv; 53 IV iv;
14 const char *name; 54 const char *name;
15} cl_error[] = {
16#define def_error(name) { (IV)CL_ ## name, # name }, 55 #define const_iv(name) { (IV)CL_ ## name, # name },
17#include "invalid.h" 56} ivstr;
18};
19 57
20static const char * 58static const char *
21clstrerror (cl_int res) 59iv2str (IV value, const ivstr *base, int count, const char *fallback)
22{ 60{
23 int i; 61 int i;
24 static char numbuf [32]; 62 static char strbuf [32];
25 63
26 for (i = sizeof (cl_error) / sizeof (cl_error [0]); i--; ) 64 for (i = count; i--; )
27 if (cl_error [i].iv == res) 65 if (base [i].iv == value)
28 return cl_error [i].name; 66 return base [i].name;
29 67
30 snprintf (numbuf, sizeof (numbuf), "ERROR(%d)", res); 68 snprintf (strbuf, sizeof (strbuf), fallback, (int)value);
31 69
32 return numbuf; 70 return strbuf;
33} 71}
34 72
73static const char *
74enum2str (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
83static const char *
84err2str (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
95static cl_int last_error;
96
35#define FAIL(name,res) \ 97#define FAIL(name,err) \
36 croak (# name ": %s", clstrerror (res)); 98 croak ("cl" # name ": %s", err2str (last_error = err));
37 99
38#define NEED_SUCCESS(name,args) \ 100#define NEED_SUCCESS(name,args) \
39 do { \ 101 do { \
40 cl_int res = name args; \ 102 cl_int res = cl ## name args; \
41 \ 103 \
42 if (res) \ 104 if (res) \
43 FAIL (name, res); \ 105 FAIL (name, res); \
44 } while (0) 106 } while (0)
45 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
113static void *
114SvPTROBJ (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
124static cl_event *
125event_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
46MODULE = OpenCL PACKAGE = OpenCL 153MODULE = OpenCL PACKAGE = OpenCL
47 154
155PROTOTYPES: ENABLE
156
48BOOT: 157BOOT:
49{ 158{
50 HV *stash = gv_stashpv ("OpenCL", 1); 159 HV *stash = gv_stashpv ("OpenCL", 1);
51 static const struct {
52 const char *name;
53 IV iv;
54 } *civ, const_iv[] = { 160 static const ivstr *civ, const_iv[] = {
55#define const_iv(name) { # name, (IV)CL_ ## name }, 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" },
56#include "constiv.h" 172#include "constiv.h"
57 }; 173 };
58 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 174 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)); 175 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
60} 176}
61 177
178cl_int
179errno ()
180 CODE:
181 errno = last_error;
182
183const char *
184err2str (cl_int err)
185
186const char *
187enum2str (cl_uint value)
188
62void 189void
63platforms () 190platforms ()
64 PPCODE: 191 PPCODE:
65{ 192{
66 cl_platform_id *list; 193 cl_platform_id *list;
67 cl_uint count; 194 cl_uint count;
68 int i; 195 int i;
69 196
70 NEED_SUCCESS (clGetPlatformIDs, (0, 0, &count)); 197 NEED_SUCCESS (GetPlatformIDs, (0, 0, &count));
71 Newx (list, count, cl_platform_id); 198 list = tmpbuf (sizeof (*list) * count);
72 NEED_SUCCESS (clGetPlatformIDs, (count, list, 0)); 199 NEED_SUCCESS (GetPlatformIDs, (count, list, 0));
73 200
74 EXTEND (SP, count); 201 EXTEND (SP, count);
75 for (i = 0; i < count; ++i) 202 for (i = 0; i < count; ++i)
76 PUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Platform", list [i])); 203 PUSHs (NEW_MORTAL_OBJ ("OpenCL::Platform", list [i]));
77
78 Safefree (list);
79} 204}
80 205
81void 206void
82context_from_type_simple (cl_device_type type = CL_DEVICE_TYPE_DEFAULT) 207context_from_type_simple (cl_device_type type = CL_DEVICE_TYPE_DEFAULT)
83 PPCODE: 208 PPCODE:
84{ 209{
85 cl_int res; 210 cl_int res;
86 cl_context ctx = clCreateContextFromType (0, type, 0, 0, &res); 211 cl_context ctx = clCreateContextFromType (0, type, 0, 0, &res);
87 212
88 if (res) 213 if (res)
89 FAIL (clCreateContextFromType, res); 214 FAIL (CreateContextFromType, res);
90 215
91 XPUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Context", ctx)); 216 XPUSH_NEW_OBJ ("OpenCL::Context", ctx);
92} 217}
218
219void
220wait_for_events (...)
221 CODE:
222{
223 EVENT_LIST (0, items);
224 NEED_SUCCESS (WaitForEvents, (event_list_count, event_list_ptr));
225}
226
227PROTOTYPES: DISABLE
93 228
94MODULE = OpenCL PACKAGE = OpenCL::Platform 229MODULE = OpenCL PACKAGE = OpenCL::Platform
95 230
96void 231void
97info (OpenCL::Platform this, cl_platform_info name) 232info (OpenCL::Platform this, cl_platform_info name)
233 PPCODE:
234 INFO (Platform)
235
236void
237devices (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
253void
254context_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
267MODULE = OpenCL PACKAGE = OpenCL::Device
268
269void
270info (OpenCL::Device this, cl_device_info name)
271 PPCODE:
272 INFO (Device)
273
274void
275context_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
287MODULE = OpenCL PACKAGE = OpenCL::Context
288
289void
290DESTROY (OpenCL::Context context)
291 CODE:
292 clReleaseContext (context);
293
294void
295info (OpenCL::Context this, cl_context_info name)
296 PPCODE:
297 INFO (Context)
298
299void
300command_queue_simple (OpenCL::Context this, OpenCL::Device device)
301 PPCODE:
302{
303 cl_int res;
304 cl_command_queue queue = clCreateCommandQueue (this, device, 0, &res);
305
306 if (res)
307 FAIL (CreateCommandQueue, res);
308
309 XPUSH_NEW_OBJ ("OpenCL::Queue", queue);
310}
311
312void
313user_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
325void
326buffer (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
343void
344buffer_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
363void
364image2d (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
379void
380image3d (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
396void
397supported_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
418void
419sampler (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
431void
432program_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
450MODULE = OpenCL PACKAGE = OpenCL::Queue
451
452void
453DESTROY (OpenCL::Queue this)
454 CODE:
455 clReleaseCommandQueue (this);
456
457void
458info (OpenCL::Queue this, cl_command_queue_info name)
459 PPCODE:
460 INFO (CommandQueue)
461
462void
463enqueue_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
479void
480enqueue_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
494void
495enqueue_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
510void
511enqueue_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
530void
531enqueue_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
548void
549enqueue_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
564void
565enqueue_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
579void
580enqueue_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
595void
596enqueue_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
610void
611enqueue_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
623void
624enqueue_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
677void
678enqueue_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
686void
687enqueue_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
694void
695enqueue_barrier (OpenCL::Queue this)
696 CODE:
697 NEED_SUCCESS (EnqueueBarrier, (this));
698
699void
700flush (OpenCL::Queue this)
701 CODE:
702 NEED_SUCCESS (Flush, (this));
703
704void
705finish (OpenCL::Queue this)
706 CODE:
707 NEED_SUCCESS (Finish, (this));
708
709MODULE = OpenCL PACKAGE = OpenCL::Memory
710
711void
712DESTROY (OpenCL::Memory this)
713 CODE:
714 clReleaseMemObject (this);
715
716void
717info (OpenCL::Memory this, cl_mem_info name)
718 PPCODE:
719 INFO (MemObject)
720
721MODULE = OpenCL PACKAGE = OpenCL::Sampler
722
723void
724DESTROY (OpenCL::Sampler this)
725 CODE:
726 clReleaseSampler (this);
727
728void
729info (OpenCL::Sampler this, cl_sampler_info name)
730 PPCODE:
731 INFO (Sampler)
732
733MODULE = OpenCL PACKAGE = OpenCL::Program
734
735void
736DESTROY (OpenCL::Program this)
737 CODE:
738 clReleaseProgram (this);
739
740void
741info (OpenCL::Program this, cl_program_info name)
742 PPCODE:
743 INFO (Program)
744
745void
746build (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
750void
751build_info (OpenCL::Program this, OpenCL::Device device, cl_program_build_info name)
98 PPCODE: 752 PPCODE:
99{ 753{
100 size_t size; 754 size_t size;
101 SV *sv; 755 SV *sv;
102 756
103 NEED_SUCCESS (clGetPlatformInfo, (this, name, 0, 0, &size)); 757 NEED_SUCCESS (GetProgramBuildInfo, (this, device, name, 0, 0, &size));
104 sv = sv_2mortal (newSV (size)); 758 sv = sv_2mortal (newSV (size));
105 SvUPGRADE (sv, SVt_PV); 759 SvUPGRADE (sv, SVt_PV);
106 SvPOK_only (sv); 760 SvPOK_only (sv);
107 SvCUR_set (sv, size); 761 SvCUR_set (sv, size);
108 NEED_SUCCESS (clGetPlatformInfo, (this, name, size, SvPVX (sv), 0)); 762 NEED_SUCCESS (GetProgramBuildInfo, (this, device, name, size, SvPVX (sv), 0));
109 XPUSHs (sv); 763 XPUSHs (sv);
110} 764}
111 765
112void 766void
113devices (OpenCL::Platform this, cl_device_type type = CL_DEVICE_TYPE_ALL) 767kernel (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: 768 PPCODE:
181 clReleaseContext (context);
182
183void
184info (OpenCL::Context this, cl_context_info name)
185 PPCODE:
186{ 769{
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; 770 cl_int res;
204 cl_command_queue queue = clCreateCommandQueue (this, device, 0, &res); 771 cl_kernel kernel = clCreateKernel (program, SvPVbyte_nolen (function), &res);
205 772
206 if (res) 773 if (res)
207 FAIL (clCreateCommandQueue, res); 774 FAIL (CreateKernel, res);
208 775
209 XPUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Queue", queue)); 776 XPUSH_NEW_OBJ ("OpenCL::Kernel", kernel);
210} 777}
211 778
212MODULE = OpenCL PACKAGE = OpenCL::Queue 779MODULE = OpenCL PACKAGE = OpenCL::Kernel
213 780
214void 781void
215DESTROY (OpenCL::Queue this) 782DESTROY (OpenCL::Kernel this)
216 CODE:
217 clReleaseCommandQueue (this);
218
219void
220info (OpenCL::Queue this, cl_command_queue_info name)
221 PPCODE: 783 CODE:
222{ 784 clReleaseKernel (this);
223 size_t size;
224 SV *sv;
225 785
226 NEED_SUCCESS (clGetCommandQueueInfo, (this, name, 0, 0, &size)); 786void
227 sv = sv_2mortal (newSV (size)); 787info (OpenCL::Kernel this, cl_kernel_info name)
228 SvUPGRADE (sv, SVt_PV); 788 PPCODE:
229 SvPOK_only (sv); 789 INFO (Kernel)
230 SvCUR_set (sv, size);
231 NEED_SUCCESS (clGetCommandQueueInfo, (this, name, size, SvPVX (sv), 0));
232 XPUSHs (sv);
233}
234 790
791void
792set_char (OpenCL::Kernel this, cl_uint idx, cl_char value)
793 CODE:
794 clSetKernelArg (this, idx, sizeof (value), &value);
795
796void
797set_uchar (OpenCL::Kernel this, cl_uint idx, cl_uchar value)
798 CODE:
799 clSetKernelArg (this, idx, sizeof (value), &value);
800
801void
802set_short (OpenCL::Kernel this, cl_uint idx, cl_short value)
803 CODE:
804 clSetKernelArg (this, idx, sizeof (value), &value);
805
806void
807set_ushort (OpenCL::Kernel this, cl_uint idx, cl_ushort value)
808 CODE:
809 clSetKernelArg (this, idx, sizeof (value), &value);
810
811void
812set_int (OpenCL::Kernel this, cl_uint idx, cl_int value)
813 CODE:
814 clSetKernelArg (this, idx, sizeof (value), &value);
815
816void
817set_uint (OpenCL::Kernel this, cl_uint idx, cl_uint value)
818 CODE:
819 clSetKernelArg (this, idx, sizeof (value), &value);
820
821void
822set_long (OpenCL::Kernel this, cl_uint idx, cl_long value)
823 CODE:
824 clSetKernelArg (this, idx, sizeof (value), &value);
825
826void
827set_ulong (OpenCL::Kernel this, cl_uint idx, cl_ulong value)
828 CODE:
829 clSetKernelArg (this, idx, sizeof (value), &value);
830
831void
832set_half (OpenCL::Kernel this, cl_uint idx, cl_half value)
833 CODE:
834 clSetKernelArg (this, idx, sizeof (value), &value);
835
836void
837set_float (OpenCL::Kernel this, cl_uint idx, cl_float value)
838 CODE:
839 clSetKernelArg (this, idx, sizeof (value), &value);
840
841void
842set_double (OpenCL::Kernel this, cl_uint idx, cl_double value)
843 CODE:
844 clSetKernelArg (this, idx, sizeof (value), &value);
845
846void
847set_memory (OpenCL::Kernel this, cl_uint idx, OpenCL::Memory_ornull value)
848 CODE:
849 clSetKernelArg (this, idx, sizeof (value), &value);
850
851void
852set_buffer (OpenCL::Kernel this, cl_uint idx, OpenCL::Buffer_ornull value)
853 CODE:
854 clSetKernelArg (this, idx, sizeof (value), &value);
855
856void
857set_image2d (OpenCL::Kernel this, cl_uint idx, OpenCL::Image2D_ornull value)
858 CODE:
859 clSetKernelArg (this, idx, sizeof (value), &value);
860
861void
862set_image3d (OpenCL::Kernel this, cl_uint idx, OpenCL::Image3D_ornull value)
863 CODE:
864 clSetKernelArg (this, idx, sizeof (value), &value);
865
866void
867set_sampler (OpenCL::Kernel this, cl_uint idx, OpenCL::Sampler value)
868 CODE:
869 clSetKernelArg (this, idx, sizeof (value), &value);
870
871void
872set_event (OpenCL::Kernel this, cl_uint idx, OpenCL::Event value)
873 CODE:
874 clSetKernelArg (this, idx, sizeof (value), &value);
875
876MODULE = OpenCL PACKAGE = OpenCL::Event
877
878void
879DESTROY (OpenCL::Event this)
880 CODE:
881 clReleaseEvent (this);
882
883void
884info (OpenCL::Event this, cl_event_info name)
885 PPCODE:
886 INFO (Event)
887
888void
889wait (OpenCL::Event this)
890 CODE:
891 clWaitForEvents (1, &this);
892
893MODULE = OpenCL PACKAGE = OpenCL::UserEvent
894
895void
896set_status (OpenCL::UserEvent this, cl_int execution_status)
897 CODE:
898 clSetUserEventStatus (this, execution_status);
899

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines