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

Comparing OpenCL/OpenCL.xs (file contents):
Revision 1.2 by root, Tue Nov 15 09:24:40 2011 UTC vs.
Revision 1.9 by root, Thu Nov 17 03:05:56 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines