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

Comparing OpenCL/OpenCL.xs (file contents):
Revision 1.5 by root, Wed Nov 16 00:35:30 2011 UTC vs.
Revision 1.8 by root, Thu Nov 17 02:54:14 2011 UTC

22typedef cl_program OpenCL__Program; 22typedef cl_program OpenCL__Program;
23typedef cl_kernel OpenCL__Kernel; 23typedef cl_kernel OpenCL__Kernel;
24typedef cl_event OpenCL__Event; 24typedef cl_event OpenCL__Event;
25typedef cl_event OpenCL__UserEvent; 25typedef cl_event OpenCL__UserEvent;
26 26
27typedef SV *FUTURE;
28
27/*****************************************************************************/ 29/*****************************************************************************/
28 30
29/* up to two temporary buffers */ 31/* up to two temporary buffers */
30static void * 32static void *
31tmpbuf (size_t size) 33tmpbuf (size_t size)
90 return iv2str (err, errstr, sizeof (errstr) / sizeof (errstr [0]), "ERROR(%d)"); 92 return iv2str (err, errstr, sizeof (errstr) / sizeof (errstr [0]), "ERROR(%d)");
91} 93}
92 94
93/*****************************************************************************/ 95/*****************************************************************************/
94 96
95static cl_int last_error; 97static cl_int res;
96 98
97#define FAIL(name,err) \ 99#define FAIL(name) \
98 croak ("cl" # name ": %s", err2str (last_error = err)); 100 croak ("cl" # name ": %s", err2str (res));
99 101
100#define NEED_SUCCESS(name,args) \ 102#define NEED_SUCCESS(name,args) \
101 do { \ 103 do { \
102 cl_int res = cl ## name args; \ 104 res = cl ## name args; \
103 \ 105 \
104 if (res) \ 106 if (res) \
105 FAIL (name, res); \ 107 FAIL (name); \
106 } while (0) 108 } while (0)
109
110#define NEED_SUCCESS_ARG(retdecl, name, args) \
111 retdecl = cl ## name args; \
112 if (res) \
113 FAIL (name);
107 114
108/*****************************************************************************/ 115/*****************************************************************************/
109 116
110#define NEW_MORTAL_OBJ(class,ptr) sv_setref_pv (sv_newmortal (), class, ptr) 117#define NEW_MORTAL_OBJ(class,ptr) sv_setref_pv (sv_newmortal (), class, ptr)
111#define XPUSH_NEW_OBJ(class,ptr) XPUSHs (NEW_MORTAL_OBJ (class, ptr)) 118#define XPUSH_NEW_OBJ(class,ptr) XPUSHs (NEW_MORTAL_OBJ (class, ptr))
138 145
139#define INFO(class) \ 146#define INFO(class) \
140{ \ 147{ \
141 size_t size; \ 148 size_t size; \
142 SV *sv; \ 149 SV *sv; \
143 \ 150 \
144 NEED_SUCCESS (Get ## class ## Info, (this, name, 0, 0, &size)); \ 151 NEED_SUCCESS (Get ## class ## Info, (this, name, 0, 0, &size)); \
145 sv = sv_2mortal (newSV (size)); \ 152 sv = sv_2mortal (newSV (size)); \
146 SvUPGRADE (sv, SVt_PV); \ 153 SvUPGRADE (sv, SVt_PV); \
147 SvPOK_only (sv); \ 154 SvPOK_only (sv); \
148 SvCUR_set (sv, size); \ 155 SvCUR_set (sv, size); \
176} 183}
177 184
178cl_int 185cl_int
179errno () 186errno ()
180 CODE: 187 CODE:
181 errno = last_error; 188 errno = res;
182 189
183const char * 190const char *
184err2str (cl_int err) 191err2str (cl_int err)
185 192
186const char * 193const char *
202 for (i = 0; i < count; ++i) 209 for (i = 0; i < count; ++i)
203 PUSHs (NEW_MORTAL_OBJ ("OpenCL::Platform", list [i])); 210 PUSHs (NEW_MORTAL_OBJ ("OpenCL::Platform", list [i]));
204} 211}
205 212
206void 213void
207context_from_type_simple (cl_device_type type = CL_DEVICE_TYPE_DEFAULT) 214context_from_type (FUTURE properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0)
208 PPCODE: 215 PPCODE:
209{
210 cl_int res;
211 cl_context ctx = clCreateContextFromType (0, type, 0, 0, &res); 216 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (0, type, 0, 0, &res));
212
213 if (res)
214 FAIL (CreateContextFromType, res);
215
216 XPUSH_NEW_OBJ ("OpenCL::Context", ctx); 217 XPUSH_NEW_OBJ ("OpenCL::Context", ctx);
217} 218
219void
220context (FUTURE properties, FUTURE devices, FUTURE notify = 0)
221 PPCODE:
222 /* der Gipfel der Kunst */
218 223
219void 224void
220wait_for_events (...) 225wait_for_events (...)
221 CODE: 226 CODE:
222{ 227{
249 for (i = 0; i < count; ++i) 254 for (i = 0; i < count; ++i)
250 PUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Device", list [i])); 255 PUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Device", list [i]));
251} 256}
252 257
253void 258void
259context (OpenCL::Platform this, FUTURE properties, SV *devices, FUTURE notify = 0)
260 PPCODE:
261 if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV)
262 croak ("OpenCL::Platform argument 'device' must be an arrayref with device objects, in call");
263
264 AV *av = (SV *)SvRV (devices);
265 cl_uint num_devices = av_len (av) + 1;
266 cl_device_id *device_list = tmpbuf (sizeof (cl_device_id) * num_devices);
267 int i;
268
269 for (i = num_devices; i--; )
270 device_list [i] = SvPTROBJ ("clCreateContext", "devices", *av_fetch (av, i, 0), "OpenCL::Device");
271
272 NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (0, num_devices, device_list, 0, 0, &res));
273 XPUSH_NEW_OBJ ("OpenCL::Context", ctx);
274
275void
254context_from_type_simple (OpenCL::Platform this, cl_device_type type = CL_DEVICE_TYPE_DEFAULT) 276context_from_type (OpenCL::Platform this, FUTURE properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0)
255 PPCODE: 277 PPCODE:
256{
257 cl_int res;
258 cl_context_properties props[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)this, 0 }; 278 cl_context_properties props[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)this, 0 };
259 cl_context ctx = clCreateContextFromType (props, type, 0, 0, &res); 279 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (props, type, 0, 0, &res));
260
261 if (res)
262 FAIL (CreateContextFromType, res);
263
264 XPUSH_NEW_OBJ ("OpenCL::Context", ctx); 280 XPUSH_NEW_OBJ ("OpenCL::Context", ctx);
265}
266 281
267MODULE = OpenCL PACKAGE = OpenCL::Device 282MODULE = OpenCL PACKAGE = OpenCL::Device
268 283
269void 284void
270info (OpenCL::Device this, cl_device_info name) 285info (OpenCL::Device this, cl_device_info name)
271 PPCODE: 286 PPCODE:
272 INFO (Device) 287 INFO (Device)
273 288
274void
275context_simple (OpenCL::Device this)
276 PPCODE:
277{
278 cl_int res;
279 cl_context ctx = clCreateContext (0, 1, &this, 0, 0, &res);
280
281 if (res)
282 FAIL (CreateContext, res);
283
284 XPUSH_NEW_OBJ ("OpenCL::Context", ctx);
285}
286
287MODULE = OpenCL PACKAGE = OpenCL::Context 289MODULE = OpenCL PACKAGE = OpenCL::Context
288 290
289void 291void
290DESTROY (OpenCL::Context context) 292DESTROY (OpenCL::Context context)
291 CODE: 293 CODE:
295info (OpenCL::Context this, cl_context_info name) 297info (OpenCL::Context this, cl_context_info name)
296 PPCODE: 298 PPCODE:
297 INFO (Context) 299 INFO (Context)
298 300
299void 301void
300command_queue_simple (OpenCL::Context this, OpenCL::Device device) 302queue (OpenCL::Context this, OpenCL::Device device, cl_command_queue_properties properties = 0)
301 PPCODE: 303 PPCODE:
302{
303 cl_int res;
304 cl_command_queue queue = clCreateCommandQueue (this, device, 0, &res); 304 NEED_SUCCESS_ARG (cl_command_queue queue, CreateCommandQueue, (this, device, properties, &res));
305
306 if (res)
307 FAIL (CreateCommandQueue, res);
308
309 XPUSH_NEW_OBJ ("OpenCL::Queue", queue); 305 XPUSH_NEW_OBJ ("OpenCL::Queue", queue);
310}
311 306
312void 307void
313user_event (OpenCL::Context this) 308user_event (OpenCL::Context this)
314 PPCODE: 309 PPCODE:
315{
316 cl_int res;
317 cl_event ev = clCreateUserEvent (this, &res); 310 NEED_SUCCESS_ARG (cl_event ev, CreateUserEvent, (this, &res));
318
319 if (res)
320 FAIL (CreateUserevent, res);
321
322 XPUSH_NEW_OBJ ("OpenCL::UserEvent", ev); 311 XPUSH_NEW_OBJ ("OpenCL::UserEvent", ev);
323}
324 312
325void 313void
326buffer (OpenCL::Context this, cl_mem_flags flags, size_t len) 314buffer (OpenCL::Context this, cl_mem_flags flags, size_t len)
327 PPCODE: 315 PPCODE:
328{
329 cl_int res;
330 cl_mem mem;
331
332 if (flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR)) 316 if (flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))
333 croak ("clCreateBuffer: cannot use/copy host ptr when no data is given, use $context->buffer_sv instead?"); 317 croak ("clCreateBuffer: cannot use/copy host ptr when no data is given, use $context->buffer_sv instead?");
334 318
335 mem = clCreateBuffer (this, flags, len, 0, &res); 319 NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (this, flags, len, 0, &res));
336
337 if (res)
338 FAIL (CreateBuffer, res);
339
340 XPUSH_NEW_OBJ ("OpenCL::Buffer", mem); 320 XPUSH_NEW_OBJ ("OpenCL::Buffer", mem);
341}
342 321
343void 322void
344buffer_sv (OpenCL::Context this, cl_mem_flags flags, SV *data) 323buffer_sv (OpenCL::Context this, cl_mem_flags flags, SV *data)
345 PPCODE: 324 PPCODE:
346{
347 STRLEN len; 325 STRLEN len;
348 char *ptr = SvPVbyte (data, len); 326 char *ptr = SvPVbyte (data, len);
349 cl_int res;
350 cl_mem mem;
351 327
352 if (!(flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))) 328 if (!(flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR)))
353 croak ("clCreateBuffer: have to specify use or copy host ptr when buffer data is given, use $context->buffer instead?"); 329 croak ("clCreateBuffer: have to specify use or copy host ptr when buffer data is given, use $context->buffer instead?");
354 330
355 mem = clCreateBuffer (this, flags, len, ptr, &res); 331 NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (this, flags, len, ptr, &res));
356
357 if (res)
358 FAIL (CreateBuffer, res);
359
360 XPUSH_NEW_OBJ ("OpenCL::Buffer", mem); 332 XPUSH_NEW_OBJ ("OpenCL::Buffer", mem);
361}
362 333
363void 334void
364image2d (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) 335image2d (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)
365 PPCODE: 336 PPCODE:
366{
367 STRLEN len; 337 STRLEN len;
368 char *ptr = SvPVbyte (data, len); 338 char *ptr = SvPVbyte (data, len);
369 const cl_image_format format = { channel_order, channel_type }; 339 const cl_image_format format = { channel_order, channel_type };
370 cl_int res;
371 cl_mem mem = clCreateImage2D (this, flags, &format, width, height, len / height, ptr, &res); 340 NEED_SUCCESS_ARG (cl_mem mem, CreateImage2D, (this, flags, &format, width, height, len / height, ptr, &res));
372
373 if (res)
374 FAIL (CreateImage2D, res);
375
376 XPUSH_NEW_OBJ ("OpenCL::Image2D", mem); 341 XPUSH_NEW_OBJ ("OpenCL::Image2D", mem);
377}
378 342
379void 343void
380image3d (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) 344image3d (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)
381 PPCODE: 345 PPCODE:
382{
383 STRLEN len; 346 STRLEN len;
384 char *ptr = SvPVbyte (data, len); 347 char *ptr = SvPVbyte (data, len);
385 const cl_image_format format = { channel_order, channel_type }; 348 const cl_image_format format = { channel_order, channel_type };
386 cl_int res;
387 cl_mem mem = clCreateImage3D (this, flags, &format, width, height, 349 NEED_SUCCESS_ARG (cl_mem mem, CreateImage3D, (this, flags, &format, width, height,
388 depth, len / (height * slice_pitch), slice_pitch, ptr, &res); 350 depth, len / (height * slice_pitch), slice_pitch, ptr, &res));
389
390 if (res)
391 FAIL (CreateImage3D, res);
392
393 XPUSH_NEW_OBJ ("OpenCL::Image3D", mem); 351 XPUSH_NEW_OBJ ("OpenCL::Image3D", mem);
394}
395 352
396void 353void
397supported_image_formats (OpenCL::Context this, cl_mem_flags flags, cl_mem_object_type image_type) 354supported_image_formats (OpenCL::Context this, cl_mem_flags flags, cl_mem_object_type image_type)
398 PPCODE: 355 PPCODE:
399{ 356{
416} 373}
417 374
418void 375void
419sampler (OpenCL::Context this, cl_bool normalized_coords, cl_addressing_mode addressing_mode, cl_filter_mode filter_mode) 376sampler (OpenCL::Context this, cl_bool normalized_coords, cl_addressing_mode addressing_mode, cl_filter_mode filter_mode)
420 PPCODE: 377 PPCODE:
421{
422 cl_int res;
423 cl_sampler sampler = clCreateSampler (this, normalized_coords, addressing_mode, filter_mode, &res); 378 NEED_SUCCESS_ARG (cl_sampler sampler, CreateSampler, (this, normalized_coords, addressing_mode, filter_mode, &res));
424
425 if (res)
426 FAIL (CreateSampler, res);
427
428 XPUSH_NEW_OBJ ("OpenCL::Sampler", sampler); 379 XPUSH_NEW_OBJ ("OpenCL::Sampler", sampler);
429}
430 380
431void 381void
432program_with_source (OpenCL::Context this, SV *program) 382program_with_source (OpenCL::Context this, SV *program)
433 PPCODE: 383 PPCODE:
434{
435 STRLEN len; 384 STRLEN len;
436 size_t len2; 385 size_t len2;
437 const char *ptr = SvPVbyte (program, len); 386 const char *ptr = SvPVbyte (program, len);
438 cl_int res;
439 cl_program prog;
440 387
441 len2 = len; 388 len2 = len;
442 prog = clCreateProgramWithSource (this, 1, &ptr, &len2, &res); 389 NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithSource, (this, 1, &ptr, &len2, &res));
443
444 if (res)
445 FAIL (CreateProgramWithSource, res);
446
447 XPUSH_NEW_OBJ ("OpenCL::Program", prog); 390 XPUSH_NEW_OBJ ("OpenCL::Program", prog);
448}
449 391
450MODULE = OpenCL PACKAGE = OpenCL::Queue 392MODULE = OpenCL PACKAGE = OpenCL::Queue
451 393
452void 394void
453DESTROY (OpenCL::Queue this) 395DESTROY (OpenCL::Queue this)
764} 706}
765 707
766void 708void
767kernel (OpenCL::Program program, SV *function) 709kernel (OpenCL::Program program, SV *function)
768 PPCODE: 710 PPCODE:
769{
770 cl_int res;
771 cl_kernel kernel = clCreateKernel (program, SvPVbyte_nolen (function), &res); 711 NEED_SUCCESS_ARG (cl_kernel kernel, CreateKernel, (program, SvPVbyte_nolen (function), &res));
772
773 if (res)
774 FAIL (CreateKernel, res);
775
776 XPUSH_NEW_OBJ ("OpenCL::Kernel", kernel); 712 XPUSH_NEW_OBJ ("OpenCL::Kernel", kernel);
777}
778 713
779MODULE = OpenCL PACKAGE = OpenCL::Kernel 714MODULE = OpenCL PACKAGE = OpenCL::Kernel
780 715
781void 716void
782DESTROY (OpenCL::Kernel this) 717DESTROY (OpenCL::Kernel this)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines