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.24 by root, Thu Apr 19 12:12:04 2012 UTC

4 4
5#ifdef __APPLE__ 5#ifdef __APPLE__
6 #include <OpenCL/opencl.h> 6 #include <OpenCL/opencl.h>
7#else 7#else
8 #include <CL/opencl.h> 8 #include <CL/opencl.h>
9#endif
10#if HAVE_OPENGL
11 #ifdef __APPLE__
12 #include <OpenGL/gl.h>
13 #include <OpenCL/cl_gl.h>
14 #else
15 #include <GL/gl.h>
16 #include <CL/cl_gl.h>
17 #endif
9#endif 18#endif
10 19
11typedef cl_platform_id OpenCL__Platform; 20typedef cl_platform_id OpenCL__Platform;
12typedef cl_device_id OpenCL__Device; 21typedef cl_device_id OpenCL__Device;
13typedef cl_context OpenCL__Context; 22typedef cl_context OpenCL__Context;
35 44
36/* up to two temporary buffers */ 45/* up to two temporary buffers */
37static void * 46static void *
38tmpbuf (size_t size) 47tmpbuf (size_t size)
39{ 48{
49 enum { buffers = 3 };
40 static int idx; 50 static int idx;
41 static void *buf [2]; 51 static void *buf [buffers];
42 static size_t len [2]; 52 static size_t len [buffers];
43 53
44 idx ^= 1; 54 idx = ++idx % buffers;
45 55
46 if (len [idx] < size) 56 if (len [idx] < size)
47 { 57 {
48 free (buf [idx]); 58 free (buf [idx]);
49 len [idx] = ((size + 31) & ~4095) + 4096 - 32; 59 len [idx] = ((size + 31) & ~4095) + 4096 - 32;
127{ 137{
128 if (SvROK (sv) && sv_derived_from (sv, pkg)) 138 if (SvROK (sv) && sv_derived_from (sv, pkg))
129 return (void *)SvIV (SvRV (sv)); 139 return (void *)SvIV (SvRV (sv));
130 140
131 croak ("%s: %s is not of type %s", func, svname, pkg); 141 croak ("%s: %s is not of type %s", func, svname, pkg);
142}
143
144/*****************************************************************************/
145
146static cl_context_properties *
147SvCONTEXTPROPERTIES (const char *func, const char *svname, SV *sv, cl_context_properties *extra, int extracount)
148{
149 if (!sv || !SvOK (sv))
150 if (extra)
151 sv = sv_2mortal (newRV_noinc ((SV *)newAV ())); // slow, but rarely used hopefully
152 else
153 return 0;
154
155 if (SvROK (sv) && SvTYPE (SvRV (sv)) == SVt_PVAV)
156 {
157 AV *av = (AV *)SvRV (sv);
158 int i, len = av_len (av);
159 cl_context_properties *p = tmpbuf (sizeof (cl_context_properties) * (len + extracount + 1));
160 cl_context_properties *l = p;
161
162 if (len & 1)
163 croak ("%s: %s is not a property list (must be even number of elements)", func, svname);
164
165 while (extracount--)
166 *l++ = *extra++;
167
168 for (i = 0; i < len; i += 2)
169 {
170 cl_context_properties t = SvIV (*av_fetch (av, i, 0));
171 cl_context_properties v;
172
173 switch (t)
174 {
175 default:
176 /* unknown property, treat as int */
177 v = SvIV (*av_fetch (av, i, 0));
178 break;
179 }
180
181 *l++ = t;
182 *l++ = v;
183 }
184
185 *l = 0;
186
187 return p;
188 }
189
190 croak ("%s: %s is not a property list (either undef or [type => value, ...])", func, svname);
132} 191}
133 192
134/*****************************************************************************/ 193/*****************************************************************************/
135 194
136static size_t 195static size_t
175 234
176PROTOTYPES: ENABLE 235PROTOTYPES: ENABLE
177 236
178BOOT: 237BOOT:
179{ 238{
180 HV *stash = gv_stashpv ("OpenCL", 1); 239 HV *stash = gv_stashpv ("OpenCL", 1);
181 static const ivstr *civ, const_iv[] = { 240 static const ivstr *civ, const_iv[] = {
182 { sizeof (cl_char ), "SIZEOF_CHAR" }, 241 { sizeof (cl_char ), "SIZEOF_CHAR" },
183 { sizeof (cl_uchar ), "SIZEOF_UCHAR" }, 242 { sizeof (cl_uchar ), "SIZEOF_UCHAR" },
184 { sizeof (cl_short ), "SIZEOF_SHORT" }, 243 { sizeof (cl_short ), "SIZEOF_SHORT" },
185 { sizeof (cl_ushort), "SIZEOF_USHORT" }, 244 { sizeof (cl_ushort), "SIZEOF_USHORT" },
186 { sizeof (cl_int ), "SIZEOF_INT" }, 245 { sizeof (cl_int ), "SIZEOF_INT" },
187 { sizeof (cl_uint ), "SIZEOF_UINT" }, 246 { sizeof (cl_uint ), "SIZEOF_UINT" },
188 { sizeof (cl_long ), "SIZEOF_LONG" }, 247 { sizeof (cl_long ), "SIZEOF_LONG" },
189 { sizeof (cl_ulong ), "SIZEOF_ULONG" }, 248 { sizeof (cl_ulong ), "SIZEOF_ULONG" },
190 { sizeof (cl_half ), "SIZEOF_HALF" }, 249 { sizeof (cl_half ), "SIZEOF_HALF" },
191 { sizeof (cl_float ), "SIZEOF_FLOAT" }, 250 { sizeof (cl_float ), "SIZEOF_FLOAT" },
192 { sizeof (cl_double), "SIZEOF_DOUBLE" }, 251 { sizeof (cl_double), "SIZEOF_DOUBLE" },
252 { HAVE_OPENGL , "HAVE_OPENGL" },
193#include "constiv.h" 253#include "constiv.h"
194 }; 254 };
195 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 255 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)); 256 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
197} 257}
198 258
199cl_int 259cl_int
200errno () 260errno ()
201 CODE: 261 CODE:
221 EXTEND (SP, count); 281 EXTEND (SP, count);
222 for (i = 0; i < count; ++i) 282 for (i = 0; i < count; ++i)
223 PUSHs (NEW_MORTAL_OBJ ("OpenCL::Platform", list [i])); 283 PUSHs (NEW_MORTAL_OBJ ("OpenCL::Platform", list [i]));
224 284
225void 285void
226context_from_type (FUTURE properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0) 286context_from_type (cl_context_properties *properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0)
227 PPCODE: 287 PPCODE:
228 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (0, type, 0, 0, &res)); 288 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (properties, type, 0, 0, &res));
229 XPUSH_NEW_OBJ ("OpenCL::Context", ctx); 289 XPUSH_NEW_OBJ ("OpenCL::Context", ctx);
230 290
231void 291void
232context (FUTURE properties, FUTURE devices, FUTURE notify = 0) 292context (cl_context_properties *properties = 0, FUTURE devices, FUTURE notify = 0)
233 PPCODE: 293 PPCODE:
234 /* der Gipfel der Kunst */ 294 /* der Gipfel der Kunst */
235 295
236void 296void
237wait_for_events (...) 297wait_for_events (...)
283 EXTEND (SP, count); 343 EXTEND (SP, count);
284 for (i = 0; i < count; ++i) 344 for (i = 0; i < count; ++i)
285 PUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Device", list [i])); 345 PUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Device", list [i]));
286 346
287void 347void
288context (OpenCL::Platform self, FUTURE properties, SV *devices, FUTURE notify = 0) 348context (OpenCL::Platform self, cl_context_properties *properties = 0, SV *devices, FUTURE notify = 0)
289 PPCODE: 349 PPCODE:
290 if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV) 350 if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV)
291 croak ("OpenCL::Platform argument 'device' must be an arrayref with device objects, in call"); 351 croak ("OpenCL::Platform argument 'device' must be an arrayref with device objects, in call");
292 352
293 AV *av = (AV *)SvRV (devices); 353 AV *av = (AV *)SvRV (devices);
296 int i; 356 int i;
297 357
298 for (i = num_devices; i--; ) 358 for (i = num_devices; i--; )
299 device_list [i] = SvPTROBJ ("clCreateContext", "devices", *av_fetch (av, i, 0), "OpenCL::Device"); 359 device_list [i] = SvPTROBJ ("clCreateContext", "devices", *av_fetch (av, i, 0), "OpenCL::Device");
300 360
301 NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (0, num_devices, device_list, 0, 0, &res)); 361 NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (properties, num_devices, device_list, 0, 0, &res));
302 XPUSH_NEW_OBJ ("OpenCL::Context", ctx); 362 XPUSH_NEW_OBJ ("OpenCL::Context", ctx);
303 363
304void 364void
305context_from_type (OpenCL::Platform self, FUTURE properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0) 365context_from_type (OpenCL::Platform self, SV *properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0)
306 PPCODE: 366 PPCODE:
307 cl_context_properties props[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)self, 0 }; 367 cl_context_properties extra[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)self };
368 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)); 369 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (props, type, 0, 0, &res));
309 XPUSH_NEW_OBJ ("OpenCL::Context", ctx); 370 XPUSH_NEW_OBJ ("OpenCL::Context", ctx);
310 371
311MODULE = OpenCL PACKAGE = OpenCL::Device 372MODULE = OpenCL PACKAGE = OpenCL::Device
312 373

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines