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

Comparing OpenCL/OpenCL.xs (file contents):
Revision 1.7 by root, Thu Nov 17 02:10:39 2011 UTC vs.
Revision 1.8 by root, Thu Nov 17 02:54:14 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines