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.13 by root, Sun Nov 20 01:09:48 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines