ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/OpenCL/OpenCL.xs
(Generate patch)

Comparing OpenCL/OpenCL.xs (file contents):
Revision 1.23 by root, Mon Apr 16 09:43:30 2012 UTC vs.
Revision 1.31 by root, Thu Apr 19 14:36:46 2012 UTC

1#include "EXTERN.h" 1#include "EXTERN.h"
2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4
5#ifdef I_DLFCN
6 #include <dlfcn.h>
7#endif
4 8
5#ifdef __APPLE__ 9#ifdef __APPLE__
6 #include <OpenCL/opencl.h> 10 #include <OpenCL/opencl.h>
7#else 11#else
8 #include <CL/opencl.h> 12 #include <CL/opencl.h>
31 35
32typedef SV *FUTURE; 36typedef SV *FUTURE;
33 37
34/*****************************************************************************/ 38/*****************************************************************************/
35 39
40// name must include a leading underscore
41static void *
42getsym (const char *name)
43{
44 #if defined I_DLFCN && defined RTLD_DEFAULT
45 #if !DLSYM_NEEDS_UNDERSCORE
46 ++name; // skip _
47 #endif
48 return dlsym (RTLD_DEFAULT, name);
49 #else
50 return 0;
51 #endif
52}
53
54/*****************************************************************************/
55
36/* up to two temporary buffers */ 56/* up to two temporary buffers */
37static void * 57static void *
38tmpbuf (size_t size) 58tmpbuf (size_t size)
39{ 59{
60 enum { buffers = 3 };
40 static int idx; 61 static int idx;
41 static void *buf [2]; 62 static void *buf [buffers];
42 static size_t len [2]; 63 static size_t len [buffers];
43 64
44 idx ^= 1; 65 idx = ++idx % buffers;
45 66
46 if (len [idx] < size) 67 if (len [idx] < size)
47 { 68 {
48 free (buf [idx]); 69 free (buf [idx]);
49 len [idx] = ((size + 31) & ~4095) + 4096 - 32; 70 len [idx] = ((size + 31) & ~4095) + 4096 - 32;
127{ 148{
128 if (SvROK (sv) && sv_derived_from (sv, pkg)) 149 if (SvROK (sv) && sv_derived_from (sv, pkg))
129 return (void *)SvIV (SvRV (sv)); 150 return (void *)SvIV (SvRV (sv));
130 151
131 croak ("%s: %s is not of type %s", func, svname, pkg); 152 croak ("%s: %s is not of type %s", func, svname, pkg);
153}
154
155/*****************************************************************************/
156
157static cl_context_properties *
158SvCONTEXTPROPERTIES (const char *func, const char *svname, SV *sv, cl_context_properties *extra, int extracount)
159{
160 if (!sv || !SvOK (sv))
161 if (extra)
162 sv = sv_2mortal (newRV_noinc ((SV *)newAV ())); // slow, but rarely used hopefully
163 else
164 return 0;
165
166 if (SvROK (sv) && SvTYPE (SvRV (sv)) == SVt_PVAV)
167 {
168 AV *av = (AV *)SvRV (sv);
169 int i, len = av_len (av) + 1;
170 cl_context_properties *p = tmpbuf (sizeof (cl_context_properties) * (len + extracount + 1));
171 cl_context_properties *l = p;
172
173 if (len & 1)
174 croak ("%s: %s is not a property list (must be even number of elements)", func, svname);
175
176 while (extracount--)
177 *l++ = *extra++;
178
179 for (i = 0; i < len; i += 2)
180 {
181 cl_context_properties t = SvIV (*av_fetch (av, i , 0));
182 SV *p_sv = *av_fetch (av, i + 1, 0);
183 cl_context_properties v;
184
185 switch (t)
186 {
187 default:
188 /* unknown property, treat as int */
189 v = SvIV (p_sv);
190 break;
191 }
192
193 *l++ = t;
194 *l++ = v;
195 }
196
197 *l = 0;
198
199 return p;
200 }
201
202 croak ("%s: %s is not a property list (either undef or [type => value, ...])", func, svname);
132} 203}
133 204
134/*****************************************************************************/ 205/*****************************************************************************/
135 206
136static size_t 207static size_t
175 246
176PROTOTYPES: ENABLE 247PROTOTYPES: ENABLE
177 248
178BOOT: 249BOOT:
179{ 250{
180 HV *stash = gv_stashpv ("OpenCL", 1); 251 HV *stash = gv_stashpv ("OpenCL", 1);
181 static const ivstr *civ, const_iv[] = { 252 static const ivstr *civ, const_iv[] = {
182 { sizeof (cl_char ), "SIZEOF_CHAR" }, 253 { sizeof (cl_char ), "SIZEOF_CHAR" },
183 { sizeof (cl_uchar ), "SIZEOF_UCHAR" }, 254 { sizeof (cl_uchar ), "SIZEOF_UCHAR" },
184 { sizeof (cl_short ), "SIZEOF_SHORT" }, 255 { sizeof (cl_short ), "SIZEOF_SHORT" },
185 { sizeof (cl_ushort), "SIZEOF_USHORT" }, 256 { sizeof (cl_ushort), "SIZEOF_USHORT" },
186 { sizeof (cl_int ), "SIZEOF_INT" }, 257 { sizeof (cl_int ), "SIZEOF_INT" },
187 { sizeof (cl_uint ), "SIZEOF_UINT" }, 258 { sizeof (cl_uint ), "SIZEOF_UINT" },
188 { sizeof (cl_long ), "SIZEOF_LONG" }, 259 { sizeof (cl_long ), "SIZEOF_LONG" },
189 { sizeof (cl_ulong ), "SIZEOF_ULONG" }, 260 { sizeof (cl_ulong ), "SIZEOF_ULONG" },
190 { sizeof (cl_half ), "SIZEOF_HALF" }, 261 { sizeof (cl_half ), "SIZEOF_HALF" },
191 { sizeof (cl_float ), "SIZEOF_FLOAT" }, 262 { sizeof (cl_float ), "SIZEOF_FLOAT" },
192 { sizeof (cl_double), "SIZEOF_DOUBLE" }, 263 { sizeof (cl_double), "SIZEOF_DOUBLE" },
193#include "constiv.h" 264#include "constiv.h"
194 }; 265 };
195 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 266 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
196 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); 267 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
197} 268}
198 269
199cl_int 270cl_int
200errno () 271errno ()
201 CODE: 272 CODE:
221 EXTEND (SP, count); 292 EXTEND (SP, count);
222 for (i = 0; i < count; ++i) 293 for (i = 0; i < count; ++i)
223 PUSHs (NEW_MORTAL_OBJ ("OpenCL::Platform", list [i])); 294 PUSHs (NEW_MORTAL_OBJ ("OpenCL::Platform", list [i]));
224 295
225void 296void
226context_from_type (FUTURE properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0) 297context_from_type (cl_context_properties *properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0)
227 PPCODE: 298 PPCODE:
228 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (0, type, 0, 0, &res)); 299 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (properties, type, 0, 0, &res));
229 XPUSH_NEW_OBJ ("OpenCL::Context", ctx); 300 XPUSH_NEW_OBJ ("OpenCL::Context", ctx);
230 301
231void 302void
232context (FUTURE properties, FUTURE devices, FUTURE notify = 0) 303context (cl_context_properties *properties = 0, FUTURE devices, FUTURE notify = 0)
233 PPCODE: 304 PPCODE:
234 /* der Gipfel der Kunst */ 305 /* der Gipfel der Kunst */
235 306
236void 307void
237wait_for_events (...) 308wait_for_events (...)
283 EXTEND (SP, count); 354 EXTEND (SP, count);
284 for (i = 0; i < count; ++i) 355 for (i = 0; i < count; ++i)
285 PUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Device", list [i])); 356 PUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Device", list [i]));
286 357
287void 358void
288context (OpenCL::Platform self, FUTURE properties, SV *devices, FUTURE notify = 0) 359context (OpenCL::Platform self, cl_context_properties *properties = 0, SV *devices, FUTURE notify = 0)
289 PPCODE: 360 PPCODE:
290 if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV) 361 if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV)
291 croak ("OpenCL::Platform argument 'device' must be an arrayref with device objects, in call"); 362 croak ("OpenCL::Platform::context argument 'device' must be an arrayref with device objects, in call");
292 363
293 AV *av = (AV *)SvRV (devices); 364 AV *av = (AV *)SvRV (devices);
294 cl_uint num_devices = av_len (av) + 1; 365 cl_uint num_devices = av_len (av) + 1;
295 cl_device_id *device_list = tmpbuf (sizeof (cl_device_id) * num_devices); 366 cl_device_id *device_list = tmpbuf (sizeof (cl_device_id) * num_devices);
296 int i; 367 int i;
297 368
298 for (i = num_devices; i--; ) 369 for (i = num_devices; i--; )
299 device_list [i] = SvPTROBJ ("clCreateContext", "devices", *av_fetch (av, i, 0), "OpenCL::Device"); 370 device_list [i] = SvPTROBJ ("clCreateContext", "devices", *av_fetch (av, i, 0), "OpenCL::Device");
300 371
301 NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (0, num_devices, device_list, 0, 0, &res)); 372 NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (properties, num_devices, device_list, 0, 0, &res));
302 XPUSH_NEW_OBJ ("OpenCL::Context", ctx); 373 XPUSH_NEW_OBJ ("OpenCL::Context", ctx);
303 374
304void 375void
305context_from_type (OpenCL::Platform self, FUTURE properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0) 376context_from_type (OpenCL::Platform self, SV *properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0)
306 PPCODE: 377 PPCODE:
307 cl_context_properties props[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)self, 0 }; 378 cl_context_properties extra[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)self };
379 cl_context_properties *props = SvCONTEXTPROPERTIES ("OpenCL::Platform::context_from_type", "properties", properties, extra, 2);
308 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (props, type, 0, 0, &res)); 380 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (props, type, 0, 0, &res));
309 XPUSH_NEW_OBJ ("OpenCL::Context", ctx); 381 XPUSH_NEW_OBJ ("OpenCL::Context", ctx);
310 382
311MODULE = OpenCL PACKAGE = OpenCL::Device 383MODULE = OpenCL PACKAGE = OpenCL::Device
312 384
565 637
566void 638void
567buffer (OpenCL::Context self, cl_mem_flags flags, size_t len) 639buffer (OpenCL::Context self, cl_mem_flags flags, size_t len)
568 PPCODE: 640 PPCODE:
569 if (flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR)) 641 if (flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))
570 croak ("clCreateBuffer: cannot use/copy host ptr when no data is given, use $context->buffer_sv instead?"); 642 croak ("OpenCL::Context::buffer: cannot use/copy host ptr when no data is given, use $context->buffer_sv instead?");
571 643
572 NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (self, flags, len, 0, &res)); 644 NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (self, flags, len, 0, &res));
573 XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem); 645 XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem);
574 646
575void 647void
576buffer_sv (OpenCL::Context self, cl_mem_flags flags, SV *data) 648buffer_sv (OpenCL::Context self, cl_mem_flags flags, SV *data)
577 PPCODE: 649 PPCODE:
578 STRLEN len; 650 STRLEN len;
579 char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0; 651 char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0;
580 if (!(flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))) 652 if (!(flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR)))
581 croak ("clCreateBuffer: have to specify use or copy host ptr when buffer data is given, use $context->buffer instead?"); 653 croak ("OpenCL::Context::buffer_sv: you have to specify use or copy host ptr when buffer data is given, use $context->buffer instead?");
582 NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (self, flags, len, ptr, &res)); 654 NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (self, flags, len, ptr, &res));
583 XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem); 655 XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem);
584 656
585void 657void
586image2d (OpenCL::Context self, 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) 658image2d (OpenCL::Context self, 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)
597 STRLEN len; 669 STRLEN len;
598 char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0; 670 char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0;
599 const cl_image_format format = { channel_order, channel_type }; 671 const cl_image_format format = { channel_order, channel_type };
600 NEED_SUCCESS_ARG (cl_mem mem, CreateImage3D, (self, flags, &format, width, height, depth, row_pitch, slice_pitch, ptr, &res)); 672 NEED_SUCCESS_ARG (cl_mem mem, CreateImage3D, (self, flags, &format, width, height, depth, row_pitch, slice_pitch, ptr, &res));
601 XPUSH_NEW_OBJ ("OpenCL::Image3D", mem); 673 XPUSH_NEW_OBJ ("OpenCL::Image3D", mem);
674
675#if cl_apple_gl_sharing || cl_khr_gl_sharing
676
677void
678gl_buffer (OpenCL::Context self, cl_mem_flags flags, cl_GLuint bufobj)
679 PPCODE:
680 NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLBuffer, (self, flags, bufobj, &res));
681 XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem);
682
683void
684gl_texture2d (OpenCL::Context self, cl_mem_flags flags, cl_GLenum target, cl_GLint miplevel, cl_GLuint texture)
685 PPCODE:
686 NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture2D, (self, flags, target, miplevel, texture, &res));
687 XPUSH_NEW_OBJ ("OpenCL::Image2D", mem);
688
689void
690gl_texture3d (OpenCL::Context self, cl_mem_flags flags, cl_GLenum target, cl_GLint miplevel, cl_GLuint texture)
691 PPCODE:
692 NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture3D, (self, flags, target, miplevel, texture, &res));
693 XPUSH_NEW_OBJ ("OpenCL::Image3D", mem);
694
695void
696gl_renderbuffer (OpenCL::Context self, cl_mem_flags flags, cl_GLuint renderbuffer)
697 PPCODE:
698 NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLRenderbuffer, (self, flags, renderbuffer, &res));
699 XPUSH_NEW_OBJ ("OpenCL::Image2D", mem);
700
701#endif
602 702
603void 703void
604supported_image_formats (OpenCL::Context self, cl_mem_flags flags, cl_mem_object_type image_type) 704supported_image_formats (OpenCL::Context self, cl_mem_flags flags, cl_mem_object_type image_type)
605 PPCODE: 705 PPCODE:
606{ 706{
958 NEED_SUCCESS (EnqueueNDRangeKernel, (self, kernel, gws_len, gwo, gws, lws, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); 1058 NEED_SUCCESS (EnqueueNDRangeKernel, (self, kernel, gws_len, gwo, gws, lws, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
959 1059
960 if (ev) 1060 if (ev)
961 XPUSH_NEW_OBJ ("OpenCL::Event", ev); 1061 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
962 1062
1063#if cl_apple_gl_sharing || cl_khr_gl_sharing
1064
1065void
1066enqueue_acquire_gl_objects (OpenCL::Queue self, SV *objects, ...)
1067 ALIAS:
1068 enqueue_release_gl_objects = 1
1069 CODE:
1070 if (!SvROK (objects) || SvTYPE (SvRV (objects)) != SVt_PVAV)
1071 croak ("OpenCL::Queue::enqueue_acquire/release_gl_objects argument 'objects' must be an arrayref with memory objects, in call");
1072
1073 cl_event ev = 0;
1074 EVENT_LIST (2, items - 2);
1075 AV *av = (AV *)SvRV (objects);
1076 cl_uint num_objects = av_len (av) + 1;
1077 cl_mem *object_list = tmpbuf (sizeof (cl_mem) * num_objects);
1078 int i;
1079
1080 for (i = num_objects; i--; )
1081 object_list [i] = SvPTROBJ ("OpenCL::Queue::enqueue_acquire/release_gl_objects", "objects", *av_fetch (av, i, 0), "OpenCL::Memory");
1082
1083 if (ix)
1084 NEED_SUCCESS (EnqueueReleaseGLObjects, (self, num_objects, object_list, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1085 else
1086 NEED_SUCCESS (EnqueueAcquireGLObjects, (self, num_objects, object_list, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1087
1088 if (ev)
1089 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
1090
1091#endif
1092
963void 1093void
964enqueue_marker (OpenCL::Queue self) 1094enqueue_marker (OpenCL::Queue self)
965 PPCODE: 1095 PPCODE:
966 cl_event ev; 1096 cl_event ev;
967 NEED_SUCCESS (EnqueueMarker, (self, &ev)); 1097 NEED_SUCCESS (EnqueueMarker, (self, &ev));
1126 NEED_SUCCESS (RetainMemObject, (value [i])); 1256 NEED_SUCCESS (RetainMemObject, (value [i]));
1127 PUSHs (NEW_MORTAL_OBJ ("OpenCL::Memory", value [i])); 1257 PUSHs (NEW_MORTAL_OBJ ("OpenCL::Memory", value [i]));
1128 } 1258 }
1129 1259
1130#END:mem 1260#END:mem
1261
1262#if cl_apple_gl_sharing || cl_khr_gl_sharing
1263
1264void
1265gl_object_info (OpenCL::Memory self)
1266 PPCODE:
1267 cl_gl_object_type type;
1268 cl_GLuint name;
1269 NEED_SUCCESS (GetGLObjectInfo, (self, &type, &name));
1270 EXTEND (SP, 2);
1271 PUSHs (sv_2mortal (newSVuv (type)));
1272 PUSHs (sv_2mortal (newSVuv (name)));
1273
1274#endif
1131 1275
1132MODULE = OpenCL PACKAGE = OpenCL::BufferObj 1276MODULE = OpenCL PACKAGE = OpenCL::BufferObj
1133 1277
1134void 1278void
1135sub_buffer_region (OpenCL::BufferObj self, cl_mem_flags flags, size_t origin, size_t size) 1279sub_buffer_region (OpenCL::BufferObj self, cl_mem_flags flags, size_t origin, size_t size)
1166 EXTEND (SP, 1); 1310 EXTEND (SP, 1);
1167 const int i = 0; 1311 const int i = 0;
1168 PUSHs (sv_2mortal (newSVuv (value [i]))); 1312 PUSHs (sv_2mortal (newSVuv (value [i])));
1169 1313
1170#END:image 1314#END:image
1315
1316#if cl_apple_gl_sharing || cl_khr_gl_sharing
1317
1318#BEGIN:gl_texture
1319
1320void
1321target (OpenCL::Image self)
1322 PPCODE:
1323 cl_GLenum value [1];
1324 NEED_SUCCESS (GetGLTextureInfo, (self, CL_GL_TEXTURE_TARGET, sizeof (value), value, 0));
1325 EXTEND (SP, 1);
1326 const int i = 0;
1327 PUSHs (sv_2mortal (newSVuv (value [i])));
1328
1329void
1330gl_mipmap_level (OpenCL::Image self)
1331 PPCODE:
1332 cl_GLint value [1];
1333 NEED_SUCCESS (GetGLTextureInfo, (self, CL_GL_MIPMAP_LEVEL, sizeof (value), value, 0));
1334 EXTEND (SP, 1);
1335 const int i = 0;
1336 PUSHs (sv_2mortal (newSViv (value [i])));
1337
1338#END:gl_texture
1339
1340#endif
1171 1341
1172MODULE = OpenCL PACKAGE = OpenCL::Sampler 1342MODULE = OpenCL PACKAGE = OpenCL::Sampler
1173 1343
1174void 1344void
1175DESTROY (OpenCL::Sampler self) 1345DESTROY (OpenCL::Sampler self)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines