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.11 by root, Thu Nov 17 04:17:43 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 size_t
132img_row_pitch (cl_mem img)
133{
134 size_t res;
135 clGetImageInfo (img, CL_IMAGE_ROW_PITCH, sizeof (res), &res, 0);
136 return res;
137}
138
139static cl_event *
140event_list (SV **items, int count)
141{
142 cl_event *list = tmpbuf (sizeof (cl_event) * count);
143
144 while (count--)
145 list [count] = SvPTROBJ ("clEnqueue", "wait_events", items [count], "OpenCL::Event");
146
147 return list;
148}
149
150#define EVENT_LIST(items,count) \
151 cl_uint event_list_count = (count); \
152 cl_event *event_list_ptr = event_list (&ST (items), event_list_count)
56 153
57#define INFO(class) \ 154#define INFO(class) \
58{ \ 155{ \
59 size_t size; \ 156 size_t size; \
60 SV *sv; \
61 \
62 NEED_SUCCESS (Get ## class ## Info, (this, name, 0, 0, &size)); \ 157 NEED_SUCCESS (Get ## class ## Info, (this, name, 0, 0, &size)); \
63 sv = sv_2mortal (newSV (size)); \ 158 SV *sv = sv_2mortal (newSV (size)); \
64 SvUPGRADE (sv, SVt_PV); \ 159 SvUPGRADE (sv, SVt_PV); \
65 SvPOK_only (sv); \ 160 SvPOK_only (sv); \
66 SvCUR_set (sv, size); \ 161 SvCUR_set (sv, size); \
67 NEED_SUCCESS (Get ## class ## Info, (this, name, size, SvPVX (sv), 0)); \ 162 NEED_SUCCESS (Get ## class ## Info, (this, name, size, SvPVX (sv), 0)); \
68 XPUSHs (sv); \ 163 XPUSHs (sv); \
73PROTOTYPES: ENABLE 168PROTOTYPES: ENABLE
74 169
75BOOT: 170BOOT:
76{ 171{
77 HV *stash = gv_stashpv ("OpenCL", 1); 172 HV *stash = gv_stashpv ("OpenCL", 1);
78 static const struct {
79 const char *name;
80 IV iv;
81 } *civ, const_iv[] = { 173 static const ivstr *civ, const_iv[] = {
82#define const_iv(name) { # name, (IV)CL_ ## name }, 174 { sizeof (cl_char ), "SIZEOF_CHAR" },
175 { sizeof (cl_uchar ), "SIZEOF_UCHAR" },
176 { sizeof (cl_short ), "SIZEOF_SHORT" },
177 { sizeof (cl_ushort), "SIZEOF_USHORT" },
178 { sizeof (cl_int ), "SIZEOF_INT" },
179 { sizeof (cl_uint ), "SIZEOF_UINT" },
180 { sizeof (cl_long ), "SIZEOF_LONG" },
181 { sizeof (cl_ulong ), "SIZEOF_ULONG" },
182 { sizeof (cl_half ), "SIZEOF_HALF" },
183 { sizeof (cl_float ), "SIZEOF_FLOAT" },
184 { sizeof (cl_double), "SIZEOF_DOUBLE" },
83#include "constiv.h" 185#include "constiv.h"
84 }; 186 };
85 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 187 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)); 188 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
87} 189}
88 190
191cl_int
192errno ()
193 CODE:
194 errno = res;
195
196const char *
197err2str (cl_int err)
198
199const char *
200enum2str (cl_uint value)
201
89void 202void
90platforms () 203platforms ()
91 PPCODE: 204 PPCODE:
92{
93 cl_platform_id *list; 205 cl_platform_id *list;
94 cl_uint count; 206 cl_uint count;
95 int i; 207 int i;
96 208
97 NEED_SUCCESS (GetPlatformIDs, (0, 0, &count)); 209 NEED_SUCCESS (GetPlatformIDs, (0, 0, &count));
98 Newx (list, count, cl_platform_id); 210 list = tmpbuf (sizeof (*list) * count);
99 NEED_SUCCESS (GetPlatformIDs, (count, list, 0)); 211 NEED_SUCCESS (GetPlatformIDs, (count, list, 0));
100 212
101 EXTEND (SP, count); 213 EXTEND (SP, count);
102 for (i = 0; i < count; ++i) 214 for (i = 0; i < count; ++i)
103 PUSHs (NEW_MORTAL_OBJ ("OpenCL::Platform", list [i])); 215 PUSHs (NEW_MORTAL_OBJ ("OpenCL::Platform", list [i]));
104 216
105 Safefree (list);
106}
107
108void 217void
109context_from_type_simple (cl_device_type type = CL_DEVICE_TYPE_DEFAULT) 218context_from_type (FUTURE properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0)
110 PPCODE: 219 PPCODE:
111{
112 cl_int res;
113 cl_context ctx = clCreateContextFromType (0, type, 0, 0, &res); 220 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); 221 XPUSH_NEW_OBJ ("OpenCL::Context", ctx);
119} 222
223void
224context (FUTURE properties, FUTURE devices, FUTURE notify = 0)
225 PPCODE:
226 /* der Gipfel der Kunst */
120 227
121void 228void
122wait_for_events (...) 229wait_for_events (...)
123 CODE: 230 CODE:
124{
125 EVENT_LIST (0, items); 231 EVENT_LIST (0, items);
126 NEED_SUCCESS (WaitForEvents, (event_list_count, event_list_ptr)); 232 NEED_SUCCESS (WaitForEvents, (event_list_count, event_list_ptr));
127}
128 233
129PROTOTYPES: DISABLE 234PROTOTYPES: DISABLE
130 235
131MODULE = OpenCL PACKAGE = OpenCL::Platform 236MODULE = OpenCL PACKAGE = OpenCL::Platform
132 237
136 INFO (Platform) 241 INFO (Platform)
137 242
138void 243void
139devices (OpenCL::Platform this, cl_device_type type = CL_DEVICE_TYPE_ALL) 244devices (OpenCL::Platform this, cl_device_type type = CL_DEVICE_TYPE_ALL)
140 PPCODE: 245 PPCODE:
141{
142 cl_device_id *list; 246 cl_device_id *list;
143 cl_uint count; 247 cl_uint count;
144 int i; 248 int i;
145 249
146 NEED_SUCCESS (GetDeviceIDs, (this, type, 0, 0, &count)); 250 NEED_SUCCESS (GetDeviceIDs, (this, type, 0, 0, &count));
147 Newx (list, count, cl_device_id); 251 list = tmpbuf (sizeof (*list) * count);
148 NEED_SUCCESS (GetDeviceIDs, (this, type, count, list, 0)); 252 NEED_SUCCESS (GetDeviceIDs, (this, type, count, list, 0));
149 253
150 EXTEND (SP, count); 254 EXTEND (SP, count);
151 for (i = 0; i < count; ++i) 255 for (i = 0; i < count; ++i)
152 PUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Device", list [i])); 256 PUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Device", list [i]));
153 257
154 Safefree (list);
155}
156
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 = (AV *)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, size_t row_pitch, 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, row_pitch, 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 row_pitch, 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, depth, row_pitch, slice_pitch, ptr, &res));
350 XPUSH_NEW_OBJ ("OpenCL::Image3D", mem);
351
352void
353supported_image_formats (OpenCL::Context this, cl_mem_flags flags, cl_mem_object_type image_type)
354 PPCODE:
355{
356 cl_uint count;
357 cl_image_format *list;
358 int i;
359
360 NEED_SUCCESS (GetSupportedImageFormats, (this, flags, image_type, 0, 0, &count));
361 Newx (list, count, cl_image_format);
362 NEED_SUCCESS (GetSupportedImageFormats, (this, flags, image_type, count, list, 0));
363
364 EXTEND (SP, count);
365 for (i = 0; i < count; ++i)
366 {
367 AV *av = newAV ();
368 av_store (av, 1, newSVuv (list [i].image_channel_data_type));
369 av_store (av, 0, newSVuv (list [i].image_channel_order));
370 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
371 }
247} 372}
248 373
249void 374void
250sampler (OpenCL::Context this, cl_bool normalized_coords, cl_addressing_mode addressing_mode, cl_filter_mode filter_mode) 375sampler (OpenCL::Context this, cl_bool normalized_coords, cl_addressing_mode addressing_mode, cl_filter_mode filter_mode)
251 PPCODE: 376 PPCODE:
252{
253 cl_int res;
254 cl_sampler sampler = clCreateSampler (this, normalized_coords, addressing_mode, filter_mode, &res); 377 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); 378 XPUSH_NEW_OBJ ("OpenCL::Sampler", sampler);
260}
261 379
262void 380void
263program_with_source (OpenCL::Context this, SV *program) 381program_with_source (OpenCL::Context this, SV *program)
264 PPCODE: 382 PPCODE:
265{
266 STRLEN len; 383 STRLEN len;
267 size_t len2; 384 size_t len2;
268 const char *ptr = SvPVbyte (program, len); 385 const char *ptr = SvPVbyte (program, len);
269 cl_int res;
270 cl_program prog;
271 386
272 len2 = len; 387 len2 = len;
273 prog = clCreateProgramWithSource (this, 1, &ptr, &len2, &res); 388 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); 389 XPUSH_NEW_OBJ ("OpenCL::Program", prog);
279}
280 390
281MODULE = OpenCL PACKAGE = OpenCL::Queue 391MODULE = OpenCL PACKAGE = OpenCL::Queue
282 392
283void 393void
284DESTROY (OpenCL::Queue this) 394DESTROY (OpenCL::Queue this)
289info (OpenCL::Queue this, cl_command_queue_info name) 399info (OpenCL::Queue this, cl_command_queue_info name)
290 PPCODE: 400 PPCODE:
291 INFO (CommandQueue) 401 INFO (CommandQueue)
292 402
293void 403void
294enqueue_read_buffer (OpenCL::Queue this, OpenCL::Memory mem, cl_bool blocking, size_t offset, size_t len, SV *data, ...) 404enqueue_read_buffer (OpenCL::Queue this, OpenCL::Buffer mem, cl_bool blocking, size_t offset, size_t len, SV *data, ...)
295 PPCODE: 405 PPCODE:
296{
297 cl_event ev = 0; 406 cl_event ev = 0;
298 EVENT_LIST (6, items - 6); 407 EVENT_LIST (6, items - 6);
299 408
300 SvUPGRADE (data, SVt_PV); 409 SvUPGRADE (data, SVt_PV);
301 SvGROW (data, len); 410 SvGROW (data, len);
303 SvCUR_set (data, len); 412 SvCUR_set (data, len);
304 NEED_SUCCESS (EnqueueReadBuffer, (this, mem, blocking, offset, len, SvPVX (data), event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); 413 NEED_SUCCESS (EnqueueReadBuffer, (this, mem, blocking, offset, len, SvPVX (data), event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
305 414
306 if (ev) 415 if (ev)
307 XPUSH_NEW_OBJ ("OpenCL::Event", ev); 416 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
308}
309 417
310void 418void
311enqueue_write_buffer (OpenCL::Queue this, OpenCL::Memory mem, cl_bool blocking, size_t offset, SV *data, ...) 419enqueue_write_buffer (OpenCL::Queue this, OpenCL::Buffer mem, cl_bool blocking, size_t offset, SV *data, ...)
312 PPCODE: 420 PPCODE:
313{
314 cl_event ev = 0; 421 cl_event ev = 0;
315 STRLEN len; 422 STRLEN len;
316 char *ptr = SvPVbyte (data, len); 423 char *ptr = SvPVbyte (data, len);
317 EVENT_LIST (5, items - 5); 424 EVENT_LIST (5, items - 5);
318 425
319 NEED_SUCCESS (EnqueueReadBuffer, (this, mem, blocking, offset, len, ptr, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); 426 NEED_SUCCESS (EnqueueReadBuffer, (this, mem, blocking, offset, len, ptr, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
320 427
321 if (ev) 428 if (ev)
322 XPUSH_NEW_OBJ ("OpenCL::Event", ev); 429 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
323}
324 430
325void 431void
326enqueue_copy_buffer (OpenCL::Queue this, OpenCL::Memory src, OpenCL::Memory dst, size_t src_offset, size_t dst_offset, size_t len, ...) 432enqueue_copy_buffer (OpenCL::Queue this, OpenCL::Buffer src, OpenCL::Buffer dst, size_t src_offset, size_t dst_offset, size_t len, ...)
327 PPCODE: 433 PPCODE:
328{
329 cl_event ev = 0; 434 cl_event ev = 0;
330 EVENT_LIST (6, items - 6); 435 EVENT_LIST (6, items - 6);
331 436
332 NEED_SUCCESS (EnqueueCopyBuffer, (this, src, dst, src_offset, dst_offset, len, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); 437 NEED_SUCCESS (EnqueueCopyBuffer, (this, src, dst, src_offset, dst_offset, len, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
333 438
334 if (ev) 439 if (ev)
335 XPUSH_NEW_OBJ ("OpenCL::Event", ev); 440 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
336} 441
442void
443enqueue_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, ...)
444 PPCODE:
445 cl_event ev = 0;
446 const size_t src_origin[3] = { src_x, src_y, src_z };
447 const size_t region[3] = { width, height, depth };
448 EVENT_LIST (12, items - 12);
449
450 if (!row_pitch)
451 row_pitch = img_row_pitch (src);
452
453 if (depth > 1 && !slice_pitch)
454 slice_pitch = row_pitch * height;
455
456 size_t len = slice_pitch ? slice_pitch * depth : row_pitch * height;
457
458 SvUPGRADE (data, SVt_PV);
459 SvGROW (data, len);
460 SvPOK_only (data);
461 SvCUR_set (data, len);
462 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));
463
464 if (ev)
465 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
466
467void
468enqueue_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, size_t slice_pitch, SV *data, ...)
469 PPCODE:
470 cl_event ev = 0;
471 const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
472 const size_t region[3] = { width, height, depth };
473 STRLEN len;
474 char *ptr = SvPVbyte (data, len);
475 EVENT_LIST (12, items - 12);
476
477 if (!row_pitch)
478 row_pitch = img_row_pitch (dst);
479
480 if (depth > 1 && !slice_pitch)
481 slice_pitch = row_pitch * height;
482
483 size_t min_len = slice_pitch ? slice_pitch * depth : row_pitch * height;
484
485 if (len < min_len)
486 croak ("clEnqueueWriteImage: data string is shorter than what would be transferred");
487
488 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));
489
490 if (ev)
491 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
492
493void
494enqueue_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, ...)
495 PPCODE:
496 cl_event ev = 0;
497 const size_t src_origin[3] = { src_x, src_y, src_z };
498 const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
499 const size_t region[3] = { width, height, depth };
500 EVENT_LIST (16, items - 16);
501
502 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));
503
504 if (ev)
505 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
506
507void
508enqueue_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, ...)
509 PPCODE:
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
520void
521enqueue_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, ...)
522 PPCODE:
523 cl_event ev = 0;
524 const size_t src_origin[3] = { src_x, src_y, src_z };
525 const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
526 const size_t region[3] = { width, height, depth };
527 EVENT_LIST (12, items - 12);
528
529 NEED_SUCCESS (EnqueueCopyImage, (this, src, dst, src_origin, dst_origin, region, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
530
531 if (ev)
532 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
533
534void
535enqueue_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, ...)
536 PPCODE:
537 cl_event ev = 0;
538 const size_t src_origin[3] = { src_x, src_y, src_z };
539 const size_t region[3] = { width, height, depth };
540 EVENT_LIST (10, items - 10);
541
542 NEED_SUCCESS (EnqueueCopyImageToBuffer, (this, src, dst, src_origin, region, dst_offset, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
543
544 if (ev)
545 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
546
547void
548enqueue_task (OpenCL::Queue this, OpenCL::Kernel kernel, ...)
549 PPCODE:
550 cl_event ev = 0;
551 EVENT_LIST (2, items - 2);
552
553 NEED_SUCCESS (EnqueueTask, (this, kernel, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
554
555 if (ev)
556 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
557
558void
559enqueue_nd_range_kernel (OpenCL::Queue this, OpenCL::Kernel kernel, SV *global_work_offset, SV *global_work_size, SV *local_work_size = &PL_sv_undef, ...)
560 PPCODE:
561 cl_event ev = 0;
562 size_t *gwo = 0, *gws, *lws = 0;
563 int gws_len;
564 size_t *lists;
565 int i;
566 EVENT_LIST (5, items - 5);
567
568 if (!SvROK (global_work_size) || SvTYPE (SvRV (global_work_size)) != SVt_PVAV)
569 croak ("clEnqueueNDRangeKernel: global_work_size must be an array reference");
570
571 gws_len = AvFILLp (SvRV (global_work_size)) + 1;
572
573 lists = tmpbuf (sizeof (size_t) * 3 * gws_len);
574
575 gws = lists + gws_len * 0;
576 for (i = 0; i < gws_len; ++i)
577 gws [i] = SvIV (AvARRAY (SvRV (global_work_size))[i]);
578
579 if (SvOK (global_work_offset))
580 {
581 if (!SvROK (global_work_offset) || SvTYPE (SvRV (global_work_offset)) != SVt_PVAV)
582 croak ("clEnqueueNDRangeKernel: global_work_offset must be undef or an array reference");
583
584 if (AvFILLp (SvRV (global_work_size)) + 1 != gws_len)
585 croak ("clEnqueueNDRangeKernel: global_work_offset must be undef or an array of same size as global_work_size");
586
587 gwo = lists + gws_len * 1;
588 for (i = 0; i < gws_len; ++i)
589 gwo [i] = SvIV (AvARRAY (SvRV (global_work_offset))[i]);
590 }
591
592 if (SvOK (local_work_size))
593 {
594 if (SvOK (local_work_size) && !SvROK (local_work_size) || SvTYPE (SvRV (local_work_size)) != SVt_PVAV)
595 croak ("clEnqueueNDRangeKernel: global_work_size must be undef or an array reference");
596
597 if (AvFILLp (SvRV (local_work_size)) + 1 != gws_len)
598 croak ("clEnqueueNDRangeKernel: local_work_local must be undef or an array of same size as global_work_size");
599
600 lws = lists + gws_len * 2;
601 for (i = 0; i < gws_len; ++i)
602 lws [i] = SvIV (AvARRAY (SvRV (local_work_size))[i]);
603 }
604
605 NEED_SUCCESS (EnqueueNDRangeKernel, (this, kernel, gws_len, gwo, gws, lws, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
606
607 if (ev)
608 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
337 609
338void 610void
339enqueue_marker (OpenCL::Queue this) 611enqueue_marker (OpenCL::Queue this)
340 PPCODE: 612 PPCODE:
341{
342 cl_event ev; 613 cl_event ev;
343 NEED_SUCCESS (EnqueueMarker, (this, &ev)); 614 NEED_SUCCESS (EnqueueMarker, (this, &ev));
344 XPUSH_NEW_OBJ ("OpenCL::Event", ev); 615 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
345}
346 616
347void 617void
348enqueue_wait_for_events (OpenCL::Queue this, ...) 618enqueue_wait_for_events (OpenCL::Queue this, ...)
349 CODE: 619 CODE:
350{
351 EVENT_LIST (1, items - 1); 620 EVENT_LIST (1, items - 1);
352 NEED_SUCCESS (EnqueueWaitForEvents, (this, event_list_count, event_list_ptr)); 621 NEED_SUCCESS (EnqueueWaitForEvents, (this, event_list_count, event_list_ptr));
353}
354 622
355void 623void
356enqueue_barrier (OpenCL::Queue this) 624enqueue_barrier (OpenCL::Queue this)
357 CODE: 625 CODE:
358 NEED_SUCCESS (EnqueueBarrier, (this)); 626 NEED_SUCCESS (EnqueueBarrier, (this));
359 627
628void
629flush (OpenCL::Queue this)
630 CODE:
631 NEED_SUCCESS (Flush, (this));
632
633void
634finish (OpenCL::Queue this)
635 CODE:
636 NEED_SUCCESS (Finish, (this));
637
360MODULE = OpenCL PACKAGE = OpenCL::Memory 638MODULE = OpenCL PACKAGE = OpenCL::Memory
361 639
362void 640void
363DESTROY (OpenCL::Memory this) 641DESTROY (OpenCL::Memory this)
364 CODE: 642 CODE:
399 NEED_SUCCESS (BuildProgram, (this, 1, &device, SvPVbyte_nolen (options), 0, 0)); 677 NEED_SUCCESS (BuildProgram, (this, 1, &device, SvPVbyte_nolen (options), 0, 0));
400 678
401void 679void
402build_info (OpenCL::Program this, OpenCL::Device device, cl_program_build_info name) 680build_info (OpenCL::Program this, OpenCL::Device device, cl_program_build_info name)
403 PPCODE: 681 PPCODE:
404{
405 size_t size; 682 size_t size;
406 SV *sv;
407
408 NEED_SUCCESS (GetProgramBuildInfo, (this, device, name, 0, 0, &size)); 683 NEED_SUCCESS (GetProgramBuildInfo, (this, device, name, 0, 0, &size));
409 sv = sv_2mortal (newSV (size)); 684 SV *sv = sv_2mortal (newSV (size));
410 SvUPGRADE (sv, SVt_PV); 685 SvUPGRADE (sv, SVt_PV);
411 SvPOK_only (sv); 686 SvPOK_only (sv);
412 SvCUR_set (sv, size); 687 SvCUR_set (sv, size);
413 NEED_SUCCESS (GetProgramBuildInfo, (this, device, name, size, SvPVX (sv), 0)); 688 NEED_SUCCESS (GetProgramBuildInfo, (this, device, name, size, SvPVX (sv), 0));
414 XPUSHs (sv); 689 XPUSHs (sv);
415}
416 690
417void 691void
418kernel (OpenCL::Program program, SV *function) 692kernel (OpenCL::Program program, SV *function)
419 PPCODE: 693 PPCODE:
420{
421 cl_int res;
422 cl_kernel kernel = clCreateKernel (program, SvPVbyte_nolen (function), &res); 694 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); 695 XPUSH_NEW_OBJ ("OpenCL::Kernel", kernel);
428}
429 696
430MODULE = OpenCL PACKAGE = OpenCL::Kernel 697MODULE = OpenCL PACKAGE = OpenCL::Kernel
431 698
432void 699void
433DESTROY (OpenCL::Kernel this) 700DESTROY (OpenCL::Kernel this)
438info (OpenCL::Kernel this, cl_kernel_info name) 705info (OpenCL::Kernel this, cl_kernel_info name)
439 PPCODE: 706 PPCODE:
440 INFO (Kernel) 707 INFO (Kernel)
441 708
442void 709void
443set_bool (OpenCL::Kernel this, cl_uint idx, cl_bool value) 710set_char (OpenCL::Kernel this, cl_uint idx, cl_char value)
444 CODE: 711 CODE:
445 clKernelSetArg (this, idx, sizeof (value), &value); 712 clSetKernelArg (this, idx, sizeof (value), &value);
713
714void
715set_uchar (OpenCL::Kernel this, cl_uint idx, cl_uchar value)
716 CODE:
717 clSetKernelArg (this, idx, sizeof (value), &value);
718
719void
720set_short (OpenCL::Kernel this, cl_uint idx, cl_short value)
721 CODE:
722 clSetKernelArg (this, idx, sizeof (value), &value);
723
724void
725set_ushort (OpenCL::Kernel this, cl_uint idx, cl_ushort value)
726 CODE:
727 clSetKernelArg (this, idx, sizeof (value), &value);
728
729void
730set_int (OpenCL::Kernel this, cl_uint idx, cl_int value)
731 CODE:
732 clSetKernelArg (this, idx, sizeof (value), &value);
733
734void
735set_uint (OpenCL::Kernel this, cl_uint idx, cl_uint value)
736 CODE:
737 clSetKernelArg (this, idx, sizeof (value), &value);
738
739void
740set_long (OpenCL::Kernel this, cl_uint idx, cl_long value)
741 CODE:
742 clSetKernelArg (this, idx, sizeof (value), &value);
743
744void
745set_ulong (OpenCL::Kernel this, cl_uint idx, cl_ulong value)
746 CODE:
747 clSetKernelArg (this, idx, sizeof (value), &value);
748
749void
750set_half (OpenCL::Kernel this, cl_uint idx, cl_half value)
751 CODE:
752 clSetKernelArg (this, idx, sizeof (value), &value);
753
754void
755set_float (OpenCL::Kernel this, cl_uint idx, cl_float value)
756 CODE:
757 clSetKernelArg (this, idx, sizeof (value), &value);
758
759void
760set_double (OpenCL::Kernel this, cl_uint idx, cl_double value)
761 CODE:
762 clSetKernelArg (this, idx, sizeof (value), &value);
763
764void
765set_memory (OpenCL::Kernel this, cl_uint idx, OpenCL::Memory_ornull value)
766 CODE:
767 clSetKernelArg (this, idx, sizeof (value), &value);
768
769void
770set_buffer (OpenCL::Kernel this, cl_uint idx, OpenCL::Buffer_ornull value)
771 CODE:
772 clSetKernelArg (this, idx, sizeof (value), &value);
773
774void
775set_image2d (OpenCL::Kernel this, cl_uint idx, OpenCL::Image2D_ornull value)
776 CODE:
777 clSetKernelArg (this, idx, sizeof (value), &value);
778
779void
780set_image3d (OpenCL::Kernel this, cl_uint idx, OpenCL::Image3D_ornull value)
781 CODE:
782 clSetKernelArg (this, idx, sizeof (value), &value);
783
784void
785set_sampler (OpenCL::Kernel this, cl_uint idx, OpenCL::Sampler value)
786 CODE:
787 clSetKernelArg (this, idx, sizeof (value), &value);
788
789void
790set_event (OpenCL::Kernel this, cl_uint idx, OpenCL::Event value)
791 CODE:
792 clSetKernelArg (this, idx, sizeof (value), &value);
446 793
447MODULE = OpenCL PACKAGE = OpenCL::Event 794MODULE = OpenCL PACKAGE = OpenCL::Event
448 795
449void 796void
450DESTROY (OpenCL::Event this) 797DESTROY (OpenCL::Event this)
459void 806void
460wait (OpenCL::Event this) 807wait (OpenCL::Event this)
461 CODE: 808 CODE:
462 clWaitForEvents (1, &this); 809 clWaitForEvents (1, &this);
463 810
811MODULE = OpenCL PACKAGE = OpenCL::UserEvent
812
813void
814set_status (OpenCL::UserEvent this, cl_int execution_status)
815 CODE:
816 clSetUserEventStatus (this, execution_status);
817

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines