ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/OpenCL/OpenCL.pm
Revision: 1.5
Committed: Wed Nov 16 00:35:30 2011 UTC (12 years, 6 months ago) by root
Branch: MAIN
Changes since 1.4: +471 -32 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 OpenCL - Open Computing Language Bindings
4
5 =head1 SYNOPSIS
6
7 use OpenCL;
8
9 =head1 DESCRIPTION
10
11 This is an early release which might be useful, but hasn't seen any testing.
12
13 =head1 HELPFUL RESOURCES
14
15 The OpenCL spec used to develop this module (1.2 spec was available, but
16 no implementation was available to me :).
17
18 http://www.khronos.org/registry/cl/specs/opencl-1.1.pdf
19
20 OpenCL manpages:
21
22 http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/
23
24 =head1 EXAMPLES
25
26 =head2 Enumerate all devices and get contexts for them.
27
28 for my $platform (OpenCL::platforms) {
29 warn $platform->info (OpenCL::PLATFORM_NAME);
30 warn $platform->info (OpenCL::PLATFORM_EXTENSIONS);
31 for my $device ($platform->devices) {
32 warn $device->info (OpenCL::DEVICE_NAME);
33 my $ctx = $device->context_simple;
34 # do stuff
35 }
36 }
37
38 =head2 Get a useful context and a command queue.
39
40 my $dev = ((OpenCL::platforms)[0]->devices)[0];
41 my $ctx = $dev->context_simple;
42 my $queue = $ctx->command_queue_simple ($dev);
43
44 =head2 Print all supported image formats of a context.
45
46 for my $type (OpenCL::MEM_OBJECT_IMAGE2D, OpenCL::MEM_OBJECT_IMAGE3D) {
47 say "supported image formats for ", OpenCL::enum2str $type;
48
49 for my $f ($ctx->supported_image_formats (0, $type)) {
50 printf " %-10s %-20s\n", OpenCL::enum2str $f->[0], OpenCL::enum2str $f->[1];
51 }
52 }
53
54 =head2 Create a buffer with some predefined data, read it back synchronously,
55 then asynchronously.
56
57 my $buf = $ctx->buffer_sv (OpenCL::MEM_COPY_HOST_PTR, "helmut");
58
59 $queue->enqueue_read_buffer ($buf, 1, 1, 3, my $data);
60 warn $data;
61
62 my $ev = $queue->enqueue_read_buffer ($buf, 0, 1, 3, my $data);
63 $ev->wait;
64 warn $data;
65
66 =head2 Create and build a program, then create a kernel out of one of its
67 functions.
68
69 my $src = '
70 __kernel void
71 squareit (__global float *input, __global float *output)
72 {
73 size_t id = get_global_id (0);
74 output [id] = input [id] * input [id];
75 }
76 ';
77
78 my $prog = $ctx->program_with_source ($src);
79
80 eval { $prog->build ($dev); 1 }
81 or die $prog->build_info ($dev, OpenCL::PROGRAM_BUILD_LOG);
82
83 my $kernel = $prog->kernel ("squareit");
84
85 =head2 Create some input and output float buffers, then call squareit on them.
86
87 my $input = $ctx->buffer_sv (OpenCL::MEM_COPY_HOST_PTR, pack "f*", 1, 2, 3, 4.5);
88 my $output = $ctx->buffer (0, OpenCL::SIZEOF_FLOAT * 5);
89
90 # set buffer
91 $kernel->set_buffer (0, $input);
92 $kernel->set_buffer (1, $output);
93
94 # execute it for all 4 numbers
95 $queue->enqueue_nd_range_kernel ($kernel, undef, [4], undef);
96
97 # enqueue a synchronous read
98 $queue->enqueue_read_buffer ($output, 1, 0, OpenCL::SIZEOF_FLOAT * 4, my $data);
99
100 # print the results:
101 say join ", ", unpack "f*", $data;
102
103 =head2 The same enqueue operations as before, but assuming an out-of-order queue,
104 showing off barriers.
105
106 # execute it for all 4 numbers
107 $queue->enqueue_nd_range_kernel ($kernel, undef, [4], undef);
108
109 # enqueue a barrier to ensure in-order execution
110 $queue->enqueue_barrier;
111
112 # enqueue an async read
113 $queue->enqueue_read_buffer ($output, 0, 0, OpenCL::SIZEOF_FLOAT * 4, my $data);
114
115 # wait for all requests to finish
116 $queue->finish;
117
118 =head2 The same enqueue operations as before, but assuming an out-of-order queue,
119 showing off event objects and wait lists.
120
121 # execute it for all 4 numbers
122 my $ev = $queue->enqueue_nd_range_kernel ($kernel, undef, [4], undef);
123
124 # enqueue an async read
125 $ev = $queue->enqueue_read_buffer ($output, 0, 0, OpenCL::SIZEOF_FLOAT * 4, my $data, $ev);
126
127 # wait for the last event to complete
128 $ev->wait;
129
130 =head1 DOCUMENTATION
131
132 =head2 BASIC CONVENTIONS
133
134 This is not a 1:1 C-style translation of OpenCL to Perl - instead I
135 attempted to make the interface as type-safe as possible and introducing
136 object syntax where it makes sense. There are a number of important
137 differences between the OpenCL C API and this module:
138
139 =over 4
140
141 =item * Object lifetime managament is automatic - there is no need
142 to free objects explicitly (C<clReleaseXXX>), the release function
143 is called automatically once all Perl references to it go away.
144
145 =item * OpenCL uses CamelCase for function names (C<clGetPlatformInfo>),
146 while this module uses underscores as word separator and often leaves out
147 prefixes (C<< $platform->info >>).
148
149 =item * OpenCL often specifies fixed vector function arguments as short
150 arrays (C<size_t origin[3]>), while this module explicitly expects the
151 components as separate arguments-
152
153 =item * Where possible, the row_pitch value is calculated from the perl
154 scalar length and need not be specified.
155
156 =item * When enqueuing commands, the wait list is specified by adding
157 extra arguments to the function - everywhere a C<$wait_events...> argument
158 is documented this can be any number of event objects.
159
160 =item * When enqueuing commands, if the enqueue method is called in void
161 context, no event is created. In all other contexts an event is returned
162 by the method.
163
164 =item * This module expects all functions to return C<CL_SUCCESS>. If any
165 other status is returned the function will throw an exception, so you
166 don't normally have to to any error checking.
167
168 =back
169
170 =head2 THE OpenCL PACKAGE
171
172 =over 4
173
174 =item $int = OpenCL::errno
175
176 The last error returned by a function - it's only changed on errors.
177
178 =item $str = OpenCL::err2str $errval
179
180 Comverts an error value into a human readable string.
181
182 =item $str = OpenCL::err2str $enum
183
184 Converts most enum values (inof parameter names, image format constants,
185 object types, addressing and filter modes, command types etc.) into a
186 human readbale string. When confronted with some random integer it can be
187 very helpful to pass it through this function to maybe get some readable
188 string out of it.
189
190 =item @platforms = OpenCL::platforms
191
192 Returns all available OpenCL::Platform objects.
193
194 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetPlatformIDs.html>
195
196 =item $ctx = OpenCL::context_from_type_simple $type = OpenCL::DEVICE_TYPE_DEFAULT
197
198 Tries to create a context from a default device and platform - never worked for me.
199
200 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateContextFromType.html>
201
202 =item OpenCL::wait_for_events $wait_events...
203
204 Waits for all events to complete.
205
206 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clWaitForEvents.html>
207
208 =back
209
210 =head2 THE OpenCL::Platform CLASS
211
212 =over 4
213
214 =item $packed_value = $platform->info ($name)
215
216 Calls C<clGetPlatformInfo> and returns the packed, raw value - for
217 strings, this will be the string, for other values you probably need to
218 use the correct C<unpack>. This might get improved in the future. Hopefully.
219
220 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetPlatformInfo.html>
221
222 =item @devices = $platform->devices ($type = OpenCL::DEVICE_TYPE_ALL)
223
224 Returns a list of matching OpenCL::Device objects.
225
226 =item $ctx = $platform->context_from_type_simple ($type = OpenCL::DEVICE_TYPE_DEFAULT)
227
228 Tries to create a context. Never worked for me.
229
230 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateContextFromType.html>
231
232 =back
233
234 =head2 THE OpenCL::Device CLASS
235
236 =over 4
237
238 =item $packed_value = $device->info ($name)
239
240 See C<< $platform->info >> for details.
241
242 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetDeviceInfo.html>
243
244 =item $ctx = $device->context_simple
245
246 Convenience function to create a new OpenCL::Context object.
247
248 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateContext.html>
249
250 =back
251
252 =head2 THE OpenCL::Context CLASS
253
254 =over 4
255
256 =item $packed_value = $ctx->info ($name)
257
258 See C<< $platform->info >> for details.
259
260 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetContextInfo.html>
261
262 =item $queue = $ctx->command_queue_simple ($device)
263
264 Convenience function to create a new OpenCL::Queue object from the context and the given device.
265
266 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateCommandQueue.html>
267
268 =item $ev = $ctx->user_event
269
270 Creates a new OpenCL::UserEvent object.
271
272 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateUserEvent.html>
273
274 =item $buf = $ctx->buffer ($flags, $len)
275
276 Creates a new OpenCL::Buffer object with the given flags and octet-size.
277
278 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateBuffer.html>
279
280 =item $buf = $ctx->buffer_sv ($flags, $data)
281
282 Creates a new OpenCL::Buffer object and initialise it with the given data values.
283
284 =item $img = $ctx->image2d ($flags, $channel_order, $channel_type, $width, $height, $data)
285
286 Creates a new OpenCL::Image2D object and optionally initialises it with the given data values.
287
288 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateImage2D.html>
289
290 =item $img = $ctx->image3d ($flags, $channel_order, $channel_type, $width, $height, $depth, $slice_pitch, $data)
291
292 Creates a new OpenCL::Image3D object and optionally initialises it with the given data values.
293
294 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateImage3D.html>
295
296 =item @formats = $ctx->supported_image_formats ($flags, $image_type)
297
298 Returns a list of matching image formats - each format is an arrayref with
299 two values, $channel_order and $channel_type, in it.
300
301 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetSupportedImageFormats.html>
302
303 =item $sampler = $ctx->sampler ($normalized_coords, $addressing_mode, $filter_mode)
304
305 Creates a new OpenCL::Sampler object.
306
307 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateSampler.html>
308
309 =item $program = $ctx->program_with_source ($string)
310
311 Creates a new OpenCL::Program object from the given source code.
312
313 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateProgramWithSource.html>
314
315 =back
316
317 =head2 THE OpenCL::Queue CLASS
318
319 An OpenCL::Queue represents an execution queue for OpenCL. You execute
320 requests by calling their respective C<enqueue_xxx> method and waitinf for
321 it to complete in some way.
322
323 All the enqueue methods return an event object that can be used to wait
324 for completion, unless the method is called in void context, in which case
325 no event object is created.
326
327 They also allow you to specify any number of other event objects that this
328 request has to wait for before it starts executing, by simply passing the
329 event objects as extra parameters to the enqueue methods.
330
331 Queues execute in-order by default, without any parallelism, so in most
332 cases it's not necessary to wait for or create event objects.
333
334 =over 4
335
336 =item $packed_value = $ctx->info ($name)
337
338 See C<< $platform->info >> for details.
339
340 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetCommandQueueInfo.html>
341
342 =item $ev = $queue->enqueue_read_buffer ($buffer, $blocking, $offset, $len, $data, $wait_events...)
343
344 Reads data from buffer into the given string.
345
346 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueReadBuffer.html>
347
348 =item $ev = $queue->enqueue_write_buffer ($buffer, $blocking, $offset, $data, $wait_events...)
349
350 Writes data to buffer from the given string.
351
352 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueWriteBuffer.html>
353
354 =item $ev = $queue->enqueue_copy_buffer ($src, $dst, $src_offset, $dst_offset, $len, $wait_events...)
355
356 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueCopyBuffer.html>
357
358 =item $ev = $queue->enqueue_read_image ($src, $blocking, $x, $y, $z, $width, $height, $depth, $row_pitch, $slice_pitch, $data, $wait_events...)
359
360 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueReadImage.html>
361
362 =item $ev = $queue->enqueue_write_image ($src, $blocking, $x, $y, $z, $width, $height, $depth, $row_pitch, $data, $wait_events...)
363
364 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueWriteImage.html>
365
366 =item $ev = $queue->enqueue_copy_buffer_rect ($src, $dst, $src_x, $src_y, $src_z, $dst_x, $dst_y, $dst_z, $width, $height, $depth, $src_row_pitch, $src_slice_pitch, 4dst_row_pitch, $dst_slice_pitch, $ait_event...)
367
368 Yeah.
369
370 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueCopyBufferRect.html>
371
372 =item $ev = $queue->enqueue_copy_buffer_to_image (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, ...)
373
374 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueCopyBufferToImage.html>.
375
376 =item $ev = $queue->enqueue_copy_image (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, ...)
377
378 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueCopyImage.html>
379
380 =item $ev = $queue->enqueue_copy_image_to_buffer (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, ...)
381
382 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueCopyImageToBuffer.html>
383
384 =item $ev = $queue->enqueue_task ($kernel, $wait_events...)
385
386 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueTask.html>
387
388 =item $ev = $queue->enqueue_nd_range_kernel ($kernel, @$global_work_offset, @$global_work_size, @$local_work_size, $wait_events...)
389
390 Enqueues a kernel execution.
391
392 @$global_work_size must be specified as a reference to an array of
393 integers specifying the work sizes (element counts).
394
395 @$global_work_offset must be either C<undef> (in which case all offsets
396 are C<0>), or a reference to an array of work offsets, with the same number
397 of elements as @$global_work_size.
398
399 @$local_work_size must be either C<undef> (in which case the
400 implementation is supposed to choose good local work sizes), or a
401 reference to an array of local work sizes, with the same number of
402 elements as @$global_work_size.
403
404 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueNDRangeKernel.html>
405
406 =item $ev = $queue->enqueue_marker
407
408 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueMarker.html>
409
410 =item $ev = $queue->enqueue_wait_for_events ($wait_events...)
411
412 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueWaitForEvents.html>
413
414 =item $queue->enqueue_barrier
415
416 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueBarrier.html>
417
418 =item $queue->flush
419
420 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clFlush.html>
421
422 =item $queue->finish
423
424 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clFinish.html>
425
426 =back
427
428 =head2 THE OpenCL::Memory CLASS
429
430 This the superclass of all memory objects - OpenCL::Buffer, OpenCL::Image,
431 OpenCL::Image2D and OpenCL::Image3D. The subclasses of this class
432 currently only exist to allow type-checking.
433
434 =over 4
435
436 =item $packed_value = $memory->info ($name)
437
438 See C<< $platform->info >> for details.
439
440 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetMemObjectInfo.html>
441
442 =back
443
444 =head2 THE OpenCL::Sampler CLASS
445
446 =over 4
447
448 =item $packed_value = $sampler->info ($name)
449
450 See C<< $platform->info >> for details.
451
452 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetSamplerInfo.html>
453
454 =back
455
456 =head2 THE OpenCL::Program CLASS
457
458 =over 4
459
460 =item $packed_value = $program->info ($name)
461
462 See C<< $platform->info >> for details.
463
464 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetProgramInfo.html>
465
466 =item $program->build ($device, $options = "")
467
468 Tries to build the program with the givne options.
469
470 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clBuildProgram.html>
471
472 =item $packed_value = $program->build_info ($device, $name)
473
474 Similar to C<< $platform->info >>, but returns build info for a previous
475 build attempt for the given device.
476
477 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetBuildInfo.html>
478
479 =item $kernel = $program->kernel ($function_name)
480
481 Creates an OpenCL::Kernel object out of the named C<__kernel> function in
482 the program.
483
484 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateKernel.html>
485
486 =back
487
488 =head2 THE OpenCL::Kernel CLASS
489
490 =over 4
491
492 =item $packed_value = $kernel->info ($name)
493
494 See C<< $platform->info >> for details.
495
496 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetKernelInfo.html>
497
498 =item $kernel->set_TYPE ($index, $value)
499
500 This is a family of methods to set the kernel argument with the number C<$index> to the give C<$value>.
501
502 TYPE is one of C<char>, C<uchar>, C<short>, C<ushort>, C<int>, C<uint>,
503 C<long>, C<ulong>, C<half>, C<float>, C<double>, C<memory>, C<buffer>,
504 C<image2d>, C<image3d>, C<sampler> or C<event>.
505
506 Chars and integers (including the half type) are specified as integers,
507 float and double as floating point values, memory/buffer/image2d/image3d
508 must be an object of that type or C<undef>, and sampler and event must be
509 objects of that type.
510
511 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clSetKernelArg.html>
512
513 =back
514
515 =head2 THE OpenCL::Event CLASS
516
517 This is the superclass for all event objects (including OpenCL::UserEvent
518 objects).
519
520 =over 4
521
522 =item $packed_value = $ev->info ($name)
523
524 See C<< $platform->info >> for details.
525
526 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetEventInfo.html>
527
528 =item $ev->wait
529
530 Waits for the event to complete.
531
532 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clWaitForEvents.html>
533
534 =back
535
536 =head2 THE OpenCL::UserEvent CLASS
537
538 This is a subclass of OpenCL::Event.
539
540 =over 4
541
542 =item $ev->set_status ($execution_status)
543
544 L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clSetUserEventStatus.html>
545
546 =back
547
548 =cut
549
550 package OpenCL;
551
552 use common::sense;
553
554 BEGIN {
555 our $VERSION = '0.03';
556
557 require XSLoader;
558 XSLoader::load (__PACKAGE__, $VERSION);
559
560 @OpenCL::Buffer::ISA =
561 @OpenCL::Image::ISA = OpenCL::Memory::;
562
563 @OpenCL::Image2D::ISA =
564 @OpenCL::Image3D::ISA = OpenCL::Image::;
565
566 @OpenCL::UserEvent::ISA = OpenCL::Event::;
567 }
568
569 1;
570
571 =head1 AUTHOR
572
573 Marc Lehmann <schmorp@schmorp.de>
574 http://home.schmorp.de/
575
576 =cut
577