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

Comparing OpenCL/OpenCL.xs (file contents):
Revision 1.3 by root, Tue Nov 15 20:38:07 2011 UTC vs.
Revision 1.10 by root, Thu Nov 17 03:56:07 2011 UTC

20typedef cl_mem OpenCL__Image3D_ornull; 20typedef cl_mem OpenCL__Image3D_ornull;
21typedef cl_sampler OpenCL__Sampler; 21typedef cl_sampler OpenCL__Sampler;
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;
26
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/*****************************************************************************/
25 52
26typedef struct 53typedef struct
27{ 54{
28 IV iv; 55 IV iv;
29 const char *name; 56 const char *name;
63 }; 90 };
64 91
65 return iv2str (err, errstr, sizeof (errstr) / sizeof (errstr [0]), "ERROR(%d)"); 92 return iv2str (err, errstr, sizeof (errstr) / sizeof (errstr [0]), "ERROR(%d)");
66} 93}
67 94
95/*****************************************************************************/
96
97static cl_int res;
98
68#define FAIL(name,res) \ 99#define FAIL(name) \
69 croak ("cl" # name ": %s", err2str (res)); 100 croak ("cl" # name ": %s", err2str (res));
70 101
71#define NEED_SUCCESS(name,args) \ 102#define NEED_SUCCESS(name,args) \
72 do { \ 103 do { \
73 cl_int res = cl ## name args; \ 104 res = cl ## name args; \
74 \ 105 \
75 if (res) \ 106 if (res) \
76 FAIL (name, res); \ 107 FAIL (name); \
77 } 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/*****************************************************************************/
78 116
79#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)
80#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))
81 119
82/*TODO*/ 120static void *
83#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)
84 145
85#define INFO(class) \ 146#define INFO(class) \
86{ \ 147{ \
87 size_t size; \ 148 size_t size; \
88 SV *sv; \
89 \
90 NEED_SUCCESS (Get ## class ## Info, (this, name, 0, 0, &size)); \ 149 NEED_SUCCESS (Get ## class ## Info, (this, name, 0, 0, &size)); \
91 sv = sv_2mortal (newSV (size)); \ 150 SV *sv = sv_2mortal (newSV (size)); \
92 SvUPGRADE (sv, SVt_PV); \ 151 SvUPGRADE (sv, SVt_PV); \
93 SvPOK_only (sv); \ 152 SvPOK_only (sv); \
94 SvCUR_set (sv, size); \ 153 SvCUR_set (sv, size); \
95 NEED_SUCCESS (Get ## class ## Info, (this, name, size, SvPVX (sv), 0)); \ 154 NEED_SUCCESS (Get ## class ## Info, (this, name, size, SvPVX (sv), 0)); \
96 XPUSHs (sv); \ 155 XPUSHs (sv); \
97} 156}
98 157
99static void *
100SvPTROBJ (const char *func, const char *svname, SV *sv, const char *pkg)
101{
102 if (SvROK (sv) && sv_derived_from (sv, pkg))
103 return (void *)SvIV (SvRV (sv));
104
105 croak ("%s: %s is not of type %s", func, svname, pkg);
106}
107
108MODULE = OpenCL PACKAGE = OpenCL 158MODULE = OpenCL PACKAGE = OpenCL
109 159
110PROTOTYPES: ENABLE 160PROTOTYPES: ENABLE
111 161
112BOOT: 162BOOT:
113{ 163{
114 HV *stash = gv_stashpv ("OpenCL", 1); 164 HV *stash = gv_stashpv ("OpenCL", 1);
115 static const ivstr *civ, const_iv[] = { 165 static const ivstr *civ, const_iv[] = {
116 { sizeof (cl_char ), "SIZEOF_CHAR" }, 166 { sizeof (cl_char ), "SIZEOF_CHAR" },
117 { sizeof (cl_uchar ), "SIZEOF_UCHAR" }, 167 { sizeof (cl_uchar ), "SIZEOF_UCHAR" },
118 { sizeof (cl_short ), "SIZEOF_SHORT" }, 168 { sizeof (cl_short ), "SIZEOF_SHORT" },
119 { sizeof (cl_ushort), "SIZEOF_USHORT"}, 169 { sizeof (cl_ushort), "SIZEOF_USHORT" },
120 { sizeof (cl_int ), "SIZEOF_INT" }, 170 { sizeof (cl_int ), "SIZEOF_INT" },
121 { sizeof (cl_uint ), "SIZEOF_UINT" }, 171 { sizeof (cl_uint ), "SIZEOF_UINT" },
122 { sizeof (cl_long ), "SIZEOF_LONG" }, 172 { sizeof (cl_long ), "SIZEOF_LONG" },
123 { sizeof (cl_ulong ), "SIZEOF_ULONG" }, 173 { sizeof (cl_ulong ), "SIZEOF_ULONG" },
124 { sizeof (cl_half ), "SIZEOF_HALF" }, 174 { sizeof (cl_half ), "SIZEOF_HALF" },
125 { sizeof (cl_float ), "SIZEOF_FLOAT" }, 175 { sizeof (cl_float ), "SIZEOF_FLOAT" },
176 { sizeof (cl_double), "SIZEOF_DOUBLE" },
126#include "constiv.h" 177#include "constiv.h"
127 }; 178 };
128 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 179 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
129 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); 180 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
130} 181}
131 182
183cl_int
184errno ()
185 CODE:
186 errno = res;
187
132const char * 188const char *
133err2str (cl_int err) 189err2str (cl_int err)
134 190
135const char * 191const char *
136enum2str (cl_uint value) 192enum2str (cl_uint value)
137 193
138void 194void
139platforms () 195platforms ()
140 PPCODE: 196 PPCODE:
141{
142 cl_platform_id *list; 197 cl_platform_id *list;
143 cl_uint count; 198 cl_uint count;
144 int i; 199 int i;
145 200
146 NEED_SUCCESS (GetPlatformIDs, (0, 0, &count)); 201 NEED_SUCCESS (GetPlatformIDs, (0, 0, &count));
147 Newx (list, count, cl_platform_id); 202 list = tmpbuf (sizeof (*list) * count);
148 NEED_SUCCESS (GetPlatformIDs, (count, list, 0)); 203 NEED_SUCCESS (GetPlatformIDs, (count, list, 0));
149 204
150 EXTEND (SP, count); 205 EXTEND (SP, count);
151 for (i = 0; i < count; ++i) 206 for (i = 0; i < count; ++i)
152 PUSHs (NEW_MORTAL_OBJ ("OpenCL::Platform", list [i])); 207 PUSHs (NEW_MORTAL_OBJ ("OpenCL::Platform", list [i]));
153 208
154 Safefree (list);
155}
156
157void 209void
158context_from_type_simple (cl_device_type type = CL_DEVICE_TYPE_DEFAULT) 210context_from_type (FUTURE properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0)
159 PPCODE: 211 PPCODE:
160{
161 cl_int res;
162 cl_context ctx = clCreateContextFromType (0, type, 0, 0, &res); 212 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (0, type, 0, 0, &res));
163
164 if (res)
165 FAIL (CreateContextFromType, res);
166
167 XPUSH_NEW_OBJ ("OpenCL::Context", ctx); 213 XPUSH_NEW_OBJ ("OpenCL::Context", ctx);
168} 214
215void
216context (FUTURE properties, FUTURE devices, FUTURE notify = 0)
217 PPCODE:
218 /* der Gipfel der Kunst */
169 219
170void 220void
171wait_for_events (...) 221wait_for_events (...)
172 CODE: 222 CODE:
173{
174 EVENT_LIST (0, items); 223 EVENT_LIST (0, items);
175 NEED_SUCCESS (WaitForEvents, (event_list_count, event_list_ptr)); 224 NEED_SUCCESS (WaitForEvents, (event_list_count, event_list_ptr));
176}
177 225
178PROTOTYPES: DISABLE 226PROTOTYPES: DISABLE
179 227
180MODULE = OpenCL PACKAGE = OpenCL::Platform 228MODULE = OpenCL PACKAGE = OpenCL::Platform
181 229
185 INFO (Platform) 233 INFO (Platform)
186 234
187void 235void
188devices (OpenCL::Platform this, cl_device_type type = CL_DEVICE_TYPE_ALL) 236devices (OpenCL::Platform this, cl_device_type type = CL_DEVICE_TYPE_ALL)
189 PPCODE: 237 PPCODE:
190{
191 cl_device_id *list; 238 cl_device_id *list;
192 cl_uint count; 239 cl_uint count;
193 int i; 240 int i;
194 241
195 NEED_SUCCESS (GetDeviceIDs, (this, type, 0, 0, &count)); 242 NEED_SUCCESS (GetDeviceIDs, (this, type, 0, 0, &count));
196 Newx (list, count, cl_device_id); 243 list = tmpbuf (sizeof (*list) * count);
197 NEED_SUCCESS (GetDeviceIDs, (this, type, count, list, 0)); 244 NEED_SUCCESS (GetDeviceIDs, (this, type, count, list, 0));
198 245
199 EXTEND (SP, count); 246 EXTEND (SP, count);
200 for (i = 0; i < count; ++i) 247 for (i = 0; i < count; ++i)
201 PUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Device", list [i])); 248 PUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Device", list [i]));
202 249
203 Safefree (list);
204}
205
206void 250void
251context (OpenCL::Platform this, FUTURE properties, SV *devices, FUTURE notify = 0)
252 PPCODE:
253 if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV)
254 croak ("OpenCL::Platform argument 'device' must be an arrayref with device objects, in call");
255
256 AV *av = (SV *)SvRV (devices);
257 cl_uint num_devices = av_len (av) + 1;
258 cl_device_id *device_list = tmpbuf (sizeof (cl_device_id) * num_devices);
259 int i;
260
261 for (i = num_devices; i--; )
262 device_list [i] = SvPTROBJ ("clCreateContext", "devices", *av_fetch (av, i, 0), "OpenCL::Device");
263
264 NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (0, num_devices, device_list, 0, 0, &res));
265 XPUSH_NEW_OBJ ("OpenCL::Context", ctx);
266
267void
207context_from_type_simple (OpenCL::Platform this, cl_device_type type = CL_DEVICE_TYPE_DEFAULT) 268context_from_type (OpenCL::Platform this, FUTURE properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0)
208 PPCODE: 269 PPCODE:
209{
210 cl_int res;
211 cl_context_properties props[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)this, 0 }; 270 cl_context_properties props[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)this, 0 };
212 cl_context ctx = clCreateContextFromType (props, type, 0, 0, &res); 271 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (props, type, 0, 0, &res));
213
214 if (res)
215 FAIL (CreateContextFromType, res);
216
217 XPUSH_NEW_OBJ ("OpenCL::Context", ctx); 272 XPUSH_NEW_OBJ ("OpenCL::Context", ctx);
218}
219 273
220MODULE = OpenCL PACKAGE = OpenCL::Device 274MODULE = OpenCL PACKAGE = OpenCL::Device
221 275
222void 276void
223info (OpenCL::Device this, cl_device_info name) 277info (OpenCL::Device this, cl_device_info name)
224 PPCODE: 278 PPCODE:
225 INFO (Device) 279 INFO (Device)
226 280
227void
228context_simple (OpenCL::Device this)
229 PPCODE:
230{
231 cl_int res;
232 cl_context ctx = clCreateContext (0, 1, &this, 0, 0, &res);
233
234 if (res)
235 FAIL (CreateContext, res);
236
237 XPUSH_NEW_OBJ ("OpenCL::Context", ctx);
238}
239
240MODULE = OpenCL PACKAGE = OpenCL::Context 281MODULE = OpenCL PACKAGE = OpenCL::Context
241 282
242void 283void
243DESTROY (OpenCL::Context context) 284DESTROY (OpenCL::Context context)
244 CODE: 285 CODE:
248info (OpenCL::Context this, cl_context_info name) 289info (OpenCL::Context this, cl_context_info name)
249 PPCODE: 290 PPCODE:
250 INFO (Context) 291 INFO (Context)
251 292
252void 293void
253command_queue_simple (OpenCL::Context this, OpenCL::Device device) 294queue (OpenCL::Context this, OpenCL::Device device, cl_command_queue_properties properties = 0)
254 PPCODE: 295 PPCODE:
255{
256 cl_int res;
257 cl_command_queue queue = clCreateCommandQueue (this, device, 0, &res); 296 NEED_SUCCESS_ARG (cl_command_queue queue, CreateCommandQueue, (this, device, properties, &res));
258
259 if (res)
260 FAIL (CreateCommandQueue, res);
261
262 XPUSH_NEW_OBJ ("OpenCL::Queue", queue); 297 XPUSH_NEW_OBJ ("OpenCL::Queue", queue);
263} 298
299void
300user_event (OpenCL::Context this)
301 PPCODE:
302 NEED_SUCCESS_ARG (cl_event ev, CreateUserEvent, (this, &res));
303 XPUSH_NEW_OBJ ("OpenCL::UserEvent", ev);
264 304
265void 305void
266buffer (OpenCL::Context this, cl_mem_flags flags, size_t len) 306buffer (OpenCL::Context this, cl_mem_flags flags, size_t len)
267 PPCODE: 307 PPCODE:
268{
269 cl_int res;
270 cl_mem mem;
271
272 if (flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR)) 308 if (flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))
273 croak ("clCreateBuffer: cannot use/copy host ptr when no data is given, use $context->buffer_sv instead?"); 309 croak ("clCreateBuffer: cannot use/copy host ptr when no data is given, use $context->buffer_sv instead?");
274 310
275 mem = clCreateBuffer (this, flags, len, 0, &res); 311 NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (this, flags, len, 0, &res));
276
277 if (res)
278 FAIL (CreateBuffer, res);
279
280 XPUSH_NEW_OBJ ("OpenCL::Buffer", mem); 312 XPUSH_NEW_OBJ ("OpenCL::Buffer", mem);
281}
282 313
283void 314void
284buffer_sv (OpenCL::Context this, cl_mem_flags flags, SV *data) 315buffer_sv (OpenCL::Context this, cl_mem_flags flags, SV *data)
285 PPCODE: 316 PPCODE:
286{
287 STRLEN len; 317 STRLEN len;
288 char *ptr = SvPVbyte (data, len); 318 char *ptr = SvPVbyte (data, len);
289 cl_int res;
290 cl_mem mem;
291 319
292 if (!(flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))) 320 if (!(flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR)))
293 croak ("clCreateBuffer: have to specify use or copy host ptr when buffer data is given, use $context->buffer instead?"); 321 croak ("clCreateBuffer: have to specify use or copy host ptr when buffer data is given, use $context->buffer instead?");
294 322
295 mem = clCreateBuffer (this, flags, len, ptr, &res); 323 NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (this, flags, len, ptr, &res));
296
297 if (res)
298 FAIL (CreateBuffer, res);
299
300 XPUSH_NEW_OBJ ("OpenCL::Buffer", mem); 324 XPUSH_NEW_OBJ ("OpenCL::Buffer", mem);
301}
302 325
303void 326void
304image2d (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) 327image2d (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)
305 PPCODE: 328 PPCODE:
306{
307 STRLEN len; 329 STRLEN len;
308 char *ptr = SvPVbyte (data, len); 330 char *ptr = SvPVbyte (data, len);
309 const cl_image_format format = { channel_order, channel_type }; 331 const cl_image_format format = { channel_order, channel_type };
310 cl_int res;
311 cl_mem mem = clCreateImage2D (this, flags, &format, width, height, len / height, ptr, &res); 332 NEED_SUCCESS_ARG (cl_mem mem, CreateImage2D, (this, flags, &format, width, height, row_pitch, ptr, &res));
312
313 if (res)
314 FAIL (CreateImage2D, res);
315
316 XPUSH_NEW_OBJ ("OpenCL::Image2D", mem); 333 XPUSH_NEW_OBJ ("OpenCL::Image2D", mem);
317}
318 334
319void 335void
320image3d (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) 336image3d (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)
321 PPCODE: 337 PPCODE:
322{
323 STRLEN len; 338 STRLEN len;
324 char *ptr = SvPVbyte (data, len); 339 char *ptr = SvPVbyte (data, len);
325 const cl_image_format format = { channel_order, channel_type }; 340 const cl_image_format format = { channel_order, channel_type };
326 cl_int res; 341 NEED_SUCCESS_ARG (cl_mem mem, CreateImage3D, (this, flags, &format, width, height, depth, row_pitch, slice_pitch, ptr, &res));
327 cl_mem mem = clCreateImage3D (this, flags, &format, width, height,
328 depth, len / (height * slice_pitch), slice_pitch, ptr, &res);
329
330 if (res)
331 FAIL (CreateImage3D, res);
332
333 XPUSH_NEW_OBJ ("OpenCL::Image3D", mem); 342 XPUSH_NEW_OBJ ("OpenCL::Image3D", mem);
334}
335 343
336void 344void
337supported_image_formats (OpenCL::Context this, cl_mem_flags flags, cl_mem_object_type image_type) 345supported_image_formats (OpenCL::Context this, cl_mem_flags flags, cl_mem_object_type image_type)
338 PPCODE: 346 PPCODE:
339{ 347{
356} 364}
357 365
358void 366void
359sampler (OpenCL::Context this, cl_bool normalized_coords, cl_addressing_mode addressing_mode, cl_filter_mode filter_mode) 367sampler (OpenCL::Context this, cl_bool normalized_coords, cl_addressing_mode addressing_mode, cl_filter_mode filter_mode)
360 PPCODE: 368 PPCODE:
361{
362 cl_int res;
363 cl_sampler sampler = clCreateSampler (this, normalized_coords, addressing_mode, filter_mode, &res); 369 NEED_SUCCESS_ARG (cl_sampler sampler, CreateSampler, (this, normalized_coords, addressing_mode, filter_mode, &res));
364
365 if (res)
366 FAIL (CreateSampler, res);
367
368 XPUSH_NEW_OBJ ("OpenCL::Sampler", sampler); 370 XPUSH_NEW_OBJ ("OpenCL::Sampler", sampler);
369}
370 371
371void 372void
372program_with_source (OpenCL::Context this, SV *program) 373program_with_source (OpenCL::Context this, SV *program)
373 PPCODE: 374 PPCODE:
374{
375 STRLEN len; 375 STRLEN len;
376 size_t len2; 376 size_t len2;
377 const char *ptr = SvPVbyte (program, len); 377 const char *ptr = SvPVbyte (program, len);
378 cl_int res;
379 cl_program prog;
380 378
381 len2 = len; 379 len2 = len;
382 prog = clCreateProgramWithSource (this, 1, &ptr, &len2, &res); 380 NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithSource, (this, 1, &ptr, &len2, &res));
383
384 if (res)
385 FAIL (CreateProgramWithSource, res);
386
387 XPUSH_NEW_OBJ ("OpenCL::Program", prog); 381 XPUSH_NEW_OBJ ("OpenCL::Program", prog);
388}
389 382
390MODULE = OpenCL PACKAGE = OpenCL::Queue 383MODULE = OpenCL PACKAGE = OpenCL::Queue
391 384
392void 385void
393DESTROY (OpenCL::Queue this) 386DESTROY (OpenCL::Queue this)
400 INFO (CommandQueue) 393 INFO (CommandQueue)
401 394
402void 395void
403enqueue_read_buffer (OpenCL::Queue this, OpenCL::Buffer mem, cl_bool blocking, size_t offset, size_t len, SV *data, ...) 396enqueue_read_buffer (OpenCL::Queue this, OpenCL::Buffer mem, cl_bool blocking, size_t offset, size_t len, SV *data, ...)
404 PPCODE: 397 PPCODE:
405{
406 cl_event ev = 0; 398 cl_event ev = 0;
407 EVENT_LIST (6, items - 6); 399 EVENT_LIST (6, items - 6);
408 400
409 SvUPGRADE (data, SVt_PV); 401 SvUPGRADE (data, SVt_PV);
410 SvGROW (data, len); 402 SvGROW (data, len);
412 SvCUR_set (data, len); 404 SvCUR_set (data, len);
413 NEED_SUCCESS (EnqueueReadBuffer, (this, mem, blocking, offset, len, SvPVX (data), event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); 405 NEED_SUCCESS (EnqueueReadBuffer, (this, mem, blocking, offset, len, SvPVX (data), event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
414 406
415 if (ev) 407 if (ev)
416 XPUSH_NEW_OBJ ("OpenCL::Event", ev); 408 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
417}
418 409
419void 410void
420enqueue_write_buffer (OpenCL::Queue this, OpenCL::Buffer mem, cl_bool blocking, size_t offset, SV *data, ...) 411enqueue_write_buffer (OpenCL::Queue this, OpenCL::Buffer mem, cl_bool blocking, size_t offset, SV *data, ...)
421 PPCODE: 412 PPCODE:
422{
423 cl_event ev = 0; 413 cl_event ev = 0;
424 STRLEN len; 414 STRLEN len;
425 char *ptr = SvPVbyte (data, len); 415 char *ptr = SvPVbyte (data, len);
426 EVENT_LIST (5, items - 5); 416 EVENT_LIST (5, items - 5);
427 417
428 NEED_SUCCESS (EnqueueReadBuffer, (this, mem, blocking, offset, len, ptr, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); 418 NEED_SUCCESS (EnqueueReadBuffer, (this, mem, blocking, offset, len, ptr, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
429 419
430 if (ev) 420 if (ev)
431 XPUSH_NEW_OBJ ("OpenCL::Event", ev); 421 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
432}
433 422
434void 423void
435enqueue_copy_buffer (OpenCL::Queue this, OpenCL::Buffer src, OpenCL::Buffer dst, size_t src_offset, size_t dst_offset, size_t len, ...) 424enqueue_copy_buffer (OpenCL::Queue this, OpenCL::Buffer src, OpenCL::Buffer dst, size_t src_offset, size_t dst_offset, size_t len, ...)
436 PPCODE: 425 PPCODE:
437{
438 cl_event ev = 0; 426 cl_event ev = 0;
439 EVENT_LIST (6, items - 6); 427 EVENT_LIST (6, items - 6);
440 428
441 NEED_SUCCESS (EnqueueCopyBuffer, (this, src, dst, src_offset, dst_offset, len, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); 429 NEED_SUCCESS (EnqueueCopyBuffer, (this, src, dst, src_offset, dst_offset, len, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
442 430
443 if (ev) 431 if (ev)
444 XPUSH_NEW_OBJ ("OpenCL::Event", ev); 432 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
445}
446 433
447 /*TODO http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueReadBufferRect.html */ 434 /*TODO http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueReadBufferRect.html */
448 /*TODO http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueWriteBufferRect.html */ 435 /*TODO http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueWriteBufferRect.html */
449 436
450void 437void
451enqueue_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, ...) 438enqueue_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, ...)
452 PPCODE: 439 PPCODE:
453{
454 cl_event ev = 0; 440 cl_event ev = 0;
455 const size_t src_origin[3] = { src_x, src_y, src_z }; 441 const size_t src_origin[3] = { src_x, src_y, src_z };
456 const size_t region[3] = { width, height, depth }; 442 const size_t region[3] = { width, height, depth };
457 size_t len = row_pitch * slice_pitch * depth; 443 size_t len = row_pitch * (slice_pitch ? slice_pitch : 1) * depth;
458 EVENT_LIST (11, items - 11); 444 EVENT_LIST (12, items - 12);
445
446 if (!len)
447 croak ("enqueue_read_image: currently, row_pitch must be specified to be non-zero");
459 448
460 SvUPGRADE (data, SVt_PV); 449 SvUPGRADE (data, SVt_PV);
461 SvGROW (data, len); 450 SvGROW (data, len);
462 SvPOK_only (data); 451 SvPOK_only (data);
463 SvCUR_set (data, len); 452 SvCUR_set (data, len);
464 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)); 453 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));
465 454
466 if (ev) 455 if (ev)
467 XPUSH_NEW_OBJ ("OpenCL::Event", ev); 456 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
468}
469 457
470void 458void
471enqueue_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, ...) 459enqueue_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, ...)
472 PPCODE: 460 PPCODE:
473{
474 cl_event ev = 0; 461 cl_event ev = 0;
475 const size_t dst_origin[3] = { dst_x, dst_y, dst_z }; 462 const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
476 const size_t region[3] = { width, height, depth }; 463 const size_t region[3] = { width, height, depth };
477 STRLEN len; 464 STRLEN len;
478 char *ptr = SvPVbyte (data, len); 465 char *ptr = SvPVbyte (data, len);
479 size_t slice_pitch = len / (row_pitch * height);
480 EVENT_LIST (11, items - 11); 466 EVENT_LIST (12, items - 12);
481 467
482 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)); 468 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));
483 469
484 if (ev) 470 if (ev)
485 XPUSH_NEW_OBJ ("OpenCL::Event", ev); 471 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
486}
487 472
488void 473void
489enqueue_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, ...) 474enqueue_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, ...)
490 PPCODE: 475 PPCODE:
491{
492 cl_event ev = 0; 476 cl_event ev = 0;
493 const size_t src_origin[3] = { src_x, src_y, src_z }; 477 const size_t src_origin[3] = { src_x, src_y, src_z };
494 const size_t dst_origin[3] = { dst_x, dst_y, dst_z }; 478 const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
495 const size_t region[3] = { width, height, depth }; 479 const size_t region[3] = { width, height, depth };
496 EVENT_LIST (16, items - 16); 480 EVENT_LIST (16, items - 16);
497 481
498 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)); 482 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));
499 483
500 if (ev) 484 if (ev)
501 XPUSH_NEW_OBJ ("OpenCL::Event", ev); 485 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
502}
503 486
504void 487void
505enqueue_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, ...) 488enqueue_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, ...)
506 PPCODE: 489 PPCODE:
507{
508 cl_event ev = 0; 490 cl_event ev = 0;
509 const size_t dst_origin[3] = { dst_x, dst_y, dst_z }; 491 const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
510 const size_t region[3] = { width, height, depth }; 492 const size_t region[3] = { width, height, depth };
511 EVENT_LIST (10, items - 10); 493 EVENT_LIST (10, items - 10);
512 494
513 NEED_SUCCESS (EnqueueCopyBufferToImage, (this, src, dst, src_offset, dst_origin, region, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); 495 NEED_SUCCESS (EnqueueCopyBufferToImage, (this, src, dst, src_offset, dst_origin, region, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
514 496
515 if (ev) 497 if (ev)
516 XPUSH_NEW_OBJ ("OpenCL::Event", ev); 498 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
517}
518 499
519void 500void
520enqueue_copy_image (OpenCL::Queue this, OpenCL::Image 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, ...) 501enqueue_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, ...)
521 PPCODE: 502 PPCODE:
522{
523 cl_event ev = 0; 503 cl_event ev = 0;
524 const size_t src_origin[3] = { src_x, src_y, src_z }; 504 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 }; 505 const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
526 const size_t region[3] = { width, height, depth }; 506 const size_t region[3] = { width, height, depth };
527 EVENT_LIST (12, items - 12); 507 EVENT_LIST (12, items - 12);
528 508
529 NEED_SUCCESS (EnqueueCopyImage, (this, src, dst, src_origin, dst_origin, region, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); 509 NEED_SUCCESS (EnqueueCopyImage, (this, src, dst, src_origin, dst_origin, region, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
530 510
531 if (ev) 511 if (ev)
532 XPUSH_NEW_OBJ ("OpenCL::Event", ev); 512 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
533}
534 513
535void 514void
536enqueue_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, ...) 515enqueue_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, ...)
537 PPCODE: 516 PPCODE:
538{
539 cl_event ev = 0; 517 cl_event ev = 0;
540 const size_t src_origin[3] = { src_x, src_y, src_z }; 518 const size_t src_origin[3] = { src_x, src_y, src_z };
541 const size_t region[3] = { width, height, depth }; 519 const size_t region[3] = { width, height, depth };
542 EVENT_LIST (10, items - 10); 520 EVENT_LIST (10, items - 10);
543 521
544 NEED_SUCCESS (EnqueueCopyImageToBuffer, (this, src, dst, src_origin, region, dst_offset, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); 522 NEED_SUCCESS (EnqueueCopyImageToBuffer, (this, src, dst, src_origin, region, dst_offset, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
545 523
546 if (ev) 524 if (ev)
547 XPUSH_NEW_OBJ ("OpenCL::Event", ev); 525 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
548}
549 526
550void 527void
551enqueue_task (OpenCL::Queue this, OpenCL::Kernel kernel, ...) 528enqueue_task (OpenCL::Queue this, OpenCL::Kernel kernel, ...)
552 PPCODE: 529 PPCODE:
553{
554 cl_event ev = 0; 530 cl_event ev = 0;
555 EVENT_LIST (2, items - 2); 531 EVENT_LIST (2, items - 2);
556 532
557 NEED_SUCCESS (EnqueueTask, (this, kernel, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); 533 NEED_SUCCESS (EnqueueTask, (this, kernel, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
558 534
559 if (ev) 535 if (ev)
560 XPUSH_NEW_OBJ ("OpenCL::Event", ev); 536 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
561}
562 537
563 /*TODO http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueNDRangeKernel.html */ 538void
539enqueue_nd_range_kernel (OpenCL::Queue this, OpenCL::Kernel kernel, SV *global_work_offset, SV *global_work_size, SV *local_work_size = &PL_sv_undef, ...)
540 PPCODE:
541 cl_event ev = 0;
542 size_t *gwo = 0, *gws, *lws = 0;
543 int gws_len;
544 size_t *lists;
545 int i;
546 EVENT_LIST (5, items - 5);
547
548 if (!SvROK (global_work_size) || SvTYPE (SvRV (global_work_size)) != SVt_PVAV)
549 croak ("clEnqueueNDRangeKernel: global_work_size must be an array reference");
550
551 gws_len = AvFILLp (SvRV (global_work_size)) + 1;
552
553 lists = tmpbuf (sizeof (size_t) * 3 * gws_len);
554
555 gws = lists + gws_len * 0;
556 for (i = 0; i < gws_len; ++i)
557 gws [i] = SvIV (AvARRAY (SvRV (global_work_size))[i]);
558
559 if (SvOK (global_work_offset))
560 {
561 if (!SvROK (global_work_offset) || SvTYPE (SvRV (global_work_offset)) != SVt_PVAV)
562 croak ("clEnqueueNDRangeKernel: global_work_offset must be undef or an array reference");
563
564 if (AvFILLp (SvRV (global_work_size)) + 1 != gws_len)
565 croak ("clEnqueueNDRangeKernel: global_work_offset must be undef or an array of same size as global_work_size");
566
567 gwo = lists + gws_len * 1;
568 for (i = 0; i < gws_len; ++i)
569 gwo [i] = SvIV (AvARRAY (SvRV (global_work_offset))[i]);
570 }
571
572 if (SvOK (local_work_size))
573 {
574 if (SvOK (local_work_size) && !SvROK (local_work_size) || SvTYPE (SvRV (local_work_size)) != SVt_PVAV)
575 croak ("clEnqueueNDRangeKernel: global_work_size must be undef or an array reference");
576
577 if (AvFILLp (SvRV (local_work_size)) + 1 != gws_len)
578 croak ("clEnqueueNDRangeKernel: local_work_local must be undef or an array of same size as global_work_size");
579
580 lws = lists + gws_len * 2;
581 for (i = 0; i < gws_len; ++i)
582 lws [i] = SvIV (AvARRAY (SvRV (local_work_size))[i]);
583 }
584
585 NEED_SUCCESS (EnqueueNDRangeKernel, (this, kernel, gws_len, gwo, gws, lws, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
586
587 if (ev)
588 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
564 589
565void 590void
566enqueue_marker (OpenCL::Queue this) 591enqueue_marker (OpenCL::Queue this)
567 PPCODE: 592 PPCODE:
568{
569 cl_event ev; 593 cl_event ev;
570 NEED_SUCCESS (EnqueueMarker, (this, &ev)); 594 NEED_SUCCESS (EnqueueMarker, (this, &ev));
571 XPUSH_NEW_OBJ ("OpenCL::Event", ev); 595 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
572}
573 596
574void 597void
575enqueue_wait_for_events (OpenCL::Queue this, ...) 598enqueue_wait_for_events (OpenCL::Queue this, ...)
576 CODE: 599 CODE:
577{
578 EVENT_LIST (1, items - 1); 600 EVENT_LIST (1, items - 1);
579 NEED_SUCCESS (EnqueueWaitForEvents, (this, event_list_count, event_list_ptr)); 601 NEED_SUCCESS (EnqueueWaitForEvents, (this, event_list_count, event_list_ptr));
580}
581 602
582void 603void
583enqueue_barrier (OpenCL::Queue this) 604enqueue_barrier (OpenCL::Queue this)
584 CODE: 605 CODE:
585 NEED_SUCCESS (EnqueueBarrier, (this)); 606 NEED_SUCCESS (EnqueueBarrier, (this));
636 NEED_SUCCESS (BuildProgram, (this, 1, &device, SvPVbyte_nolen (options), 0, 0)); 657 NEED_SUCCESS (BuildProgram, (this, 1, &device, SvPVbyte_nolen (options), 0, 0));
637 658
638void 659void
639build_info (OpenCL::Program this, OpenCL::Device device, cl_program_build_info name) 660build_info (OpenCL::Program this, OpenCL::Device device, cl_program_build_info name)
640 PPCODE: 661 PPCODE:
641{
642 size_t size; 662 size_t size;
643 SV *sv;
644
645 NEED_SUCCESS (GetProgramBuildInfo, (this, device, name, 0, 0, &size)); 663 NEED_SUCCESS (GetProgramBuildInfo, (this, device, name, 0, 0, &size));
646 sv = sv_2mortal (newSV (size)); 664 SV *sv = sv_2mortal (newSV (size));
647 SvUPGRADE (sv, SVt_PV); 665 SvUPGRADE (sv, SVt_PV);
648 SvPOK_only (sv); 666 SvPOK_only (sv);
649 SvCUR_set (sv, size); 667 SvCUR_set (sv, size);
650 NEED_SUCCESS (GetProgramBuildInfo, (this, device, name, size, SvPVX (sv), 0)); 668 NEED_SUCCESS (GetProgramBuildInfo, (this, device, name, size, SvPVX (sv), 0));
651 XPUSHs (sv); 669 XPUSHs (sv);
652}
653 670
654void 671void
655kernel (OpenCL::Program program, SV *function) 672kernel (OpenCL::Program program, SV *function)
656 PPCODE: 673 PPCODE:
657{
658 cl_int res;
659 cl_kernel kernel = clCreateKernel (program, SvPVbyte_nolen (function), &res); 674 NEED_SUCCESS_ARG (cl_kernel kernel, CreateKernel, (program, SvPVbyte_nolen (function), &res));
660
661 if (res)
662 FAIL (CreateKernel, res);
663
664 XPUSH_NEW_OBJ ("OpenCL::Kernel", kernel); 675 XPUSH_NEW_OBJ ("OpenCL::Kernel", kernel);
665}
666 676
667MODULE = OpenCL PACKAGE = OpenCL::Kernel 677MODULE = OpenCL PACKAGE = OpenCL::Kernel
668 678
669void 679void
670DESTROY (OpenCL::Kernel this) 680DESTROY (OpenCL::Kernel this)
725set_float (OpenCL::Kernel this, cl_uint idx, cl_float value) 735set_float (OpenCL::Kernel this, cl_uint idx, cl_float value)
726 CODE: 736 CODE:
727 clSetKernelArg (this, idx, sizeof (value), &value); 737 clSetKernelArg (this, idx, sizeof (value), &value);
728 738
729void 739void
740set_double (OpenCL::Kernel this, cl_uint idx, cl_double value)
741 CODE:
742 clSetKernelArg (this, idx, sizeof (value), &value);
743
744void
730set_memory (OpenCL::Kernel this, cl_uint idx, OpenCL::Memory_ornull value) 745set_memory (OpenCL::Kernel this, cl_uint idx, OpenCL::Memory_ornull value)
731 CODE: 746 CODE:
732 clSetKernelArg (this, idx, sizeof (value), &value); 747 clSetKernelArg (this, idx, sizeof (value), &value);
733 748
734void 749void
771void 786void
772wait (OpenCL::Event this) 787wait (OpenCL::Event this)
773 CODE: 788 CODE:
774 clWaitForEvents (1, &this); 789 clWaitForEvents (1, &this);
775 790
791MODULE = OpenCL PACKAGE = OpenCL::UserEvent
792
793void
794set_status (OpenCL::UserEvent this, cl_int execution_status)
795 CODE:
796 clSetUserEventStatus (this, execution_status);
797

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines