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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines