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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines