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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines