… | |
… | |
18 | vendors) - usually there is only one. |
18 | vendors) - usually there is only one. |
19 | |
19 | |
20 | Each platform gives you access to a number of OpenCL::Device objects, e.g. |
20 | Each platform gives you access to a number of OpenCL::Device objects, e.g. |
21 | your graphics card. |
21 | your graphics card. |
22 | |
22 | |
23 | From a platform and some devices, you create an OpenCL::Context, which is |
23 | From a platform and some device(s), you create an OpenCL::Context, which is |
24 | a very central object in OpenCL: Once you have a context you can create |
24 | a very central object in OpenCL: Once you have a context you can create |
25 | most other objects: |
25 | most other objects: |
26 | |
26 | |
27 | OpenCL::Program objects, which store source code and, after building |
27 | OpenCL::Program objects, which store source code and, after building for a |
28 | ("compiling and linking"), also binary programs. For each kernel function |
28 | specific device ("compiling and linking"), also binary programs. For each |
29 | in a program you can then create an OpenCL::Kernel object which represents |
29 | kernel function in a program you can then create an OpenCL::Kernel object |
30 | basically a function call with argument values. |
30 | which represents basically a function call with argument values. |
31 | |
31 | |
32 | OpenCL::Memory objects of various flavours: OpenCL::Buffers objects (flat |
32 | OpenCL::Memory objects of various flavours: OpenCL::Buffers objects (flat |
33 | memory areas, think array) and OpenCL::Image objects (think 2d or 3d |
33 | memory areas, think arrays or structs) and OpenCL::Image objects (think 2d |
34 | array) for bulk data and input and output for kernels. |
34 | or 3d array) for bulk data and input and output for kernels. |
35 | |
35 | |
36 | OpenCL::Sampler objects, which are kind of like texture filter modes in |
36 | OpenCL::Sampler objects, which are kind of like texture filter modes in |
37 | OpenGL. |
37 | OpenGL. |
38 | |
38 | |
39 | OpenCL::Queue objects - command queues, which allow you to submit memory |
39 | OpenCL::Queue objects - command queues, which allow you to submit memory |
… | |
… | |
52 | |
52 | |
53 | OpenCL manpages: |
53 | OpenCL manpages: |
54 | |
54 | |
55 | http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/ |
55 | http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/ |
56 | |
56 | |
|
|
57 | If you are into UML class diagrams, the following diagram might help - if |
|
|
58 | not, it will be mildly cobfusing: |
|
|
59 | |
|
|
60 | http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/classDiagram.html |
|
|
61 | |
|
|
62 | Here's a tutorial from AMD (very AMD-centric, too), not sure how useful it |
|
|
63 | is, but at least it's free of charge: |
|
|
64 | |
|
|
65 | http://developer.amd.com/zones/OpenCLZone/courses/Documents/Introduction_to_OpenCL_Programming%20Training_Guide%20%28201005%29.pdf |
|
|
66 | |
|
|
67 | And here's NVIDIA's OpenCL Best Practises Guide: |
|
|
68 | |
|
|
69 | http://developer.download.nvidia.com/compute/cuda/3_2/toolkit/docs/OpenCL_Best_Practices_Guide.pdf |
|
|
70 | |
57 | =head1 BASIC WORKFLOW |
71 | =head1 BASIC WORKFLOW |
58 | |
72 | |
59 | To get something done, you basically have to do this once: |
73 | To get something done, you basically have to do this once (refer to the |
|
|
74 | examples below for actual code, this is just a high-level description): |
60 | |
75 | |
61 | Find some platform (e.g. the first one) and some device (e.g. the first |
76 | Find some platform (e.g. the first one) and some device(s) (e.g. the first |
62 | device you can find), and create a context from those. |
77 | device of the platform), and create a context from those. |
63 | |
78 | |
64 | Create a command queue from your context, and program objects from your |
79 | Create program objects from your OpenCL source code, then build (compile) |
65 | OpenCL source code, build the programs. |
80 | the programs for each device you want to run them on. |
66 | |
81 | |
67 | Create kernel objects for all kernels you want to use. |
82 | Create kernel objects for all kernels you want to use (surprisingly, these |
|
|
83 | are not device-specific). |
68 | |
84 | |
69 | Then, to execute stuff, you repeat this: |
85 | Then, to execute stuff, you repeat these steps, possibly resuing or |
|
|
86 | sharing some buffers: |
70 | |
87 | |
71 | Create some input and output buffers from your context. Initialise the |
88 | Create some input and output buffers from your context. Set these as |
72 | input buffers with data. Set these as arguments to your kernel. |
89 | arguments to your kernel. |
|
|
90 | |
|
|
91 | Enqueue buffer writes to initialise your input buffers (when not |
|
|
92 | initialised at creation time). |
73 | |
93 | |
74 | Enqueue the kernel execution. |
94 | Enqueue the kernel execution. |
75 | |
95 | |
76 | Enqueue buffer reads for your output buffer to read results. |
96 | Enqueue buffer reads for your output buffer to read results. |
77 | |
97 | |
78 | The next section shows how this can be done. |
|
|
79 | |
|
|
80 | =head1 EXAMPLES |
98 | =head1 EXAMPLES |
81 | |
99 | |
82 | =head2 Enumerate all devices and get contexts for them. |
100 | =head2 Enumerate all devices and get contexts for them. |
|
|
101 | |
|
|
102 | Best run this once to get a feel for the platforms and devices in your |
|
|
103 | system. |
83 | |
104 | |
84 | for my $platform (OpenCL::platforms) { |
105 | for my $platform (OpenCL::platforms) { |
85 | printf "platform: %s\n", $platform->info (OpenCL::PLATFORM_NAME); |
106 | printf "platform: %s\n", $platform->info (OpenCL::PLATFORM_NAME); |
86 | printf "extensions: %s\n", $platform->info (OpenCL::PLATFORM_EXTENSIONS); |
107 | printf "extensions: %s\n", $platform->info (OpenCL::PLATFORM_EXTENSIONS); |
87 | for my $device ($platform->devices) { |
108 | for my $device ($platform->devices) { |
… | |
… | |
91 | } |
112 | } |
92 | } |
113 | } |
93 | |
114 | |
94 | =head2 Get a useful context and a command queue. |
115 | =head2 Get a useful context and a command queue. |
95 | |
116 | |
96 | my $dev = ((OpenCL::platforms)[0]->devices)[0]; |
117 | This is a useful boilerplate for any OpenCL program that only wants to use |
97 | my $ctx = $dev->context; |
118 | one device, |
98 | my $queue = $ctx->queue ($dev); |
119 | |
|
|
120 | my ($platform) = OpenCL::platforms; # find first platform |
|
|
121 | my ($dev) = $platform->devices; # find first device of platform |
|
|
122 | my $ctx = $platform->context (undef, [$dev]); # create context out of those |
|
|
123 | my $queue = $ctx->queue ($dev); # create a command queue for the device |
99 | |
124 | |
100 | =head2 Print all supported image formats of a context. |
125 | =head2 Print all supported image formats of a context. |
|
|
126 | |
|
|
127 | Best run this once for your context, to see whats available and how to |
|
|
128 | gather information. |
101 | |
129 | |
102 | for my $type (OpenCL::MEM_OBJECT_IMAGE2D, OpenCL::MEM_OBJECT_IMAGE3D) { |
130 | for my $type (OpenCL::MEM_OBJECT_IMAGE2D, OpenCL::MEM_OBJECT_IMAGE3D) { |
103 | print "supported image formats for ", OpenCL::enum2str $type, "\n"; |
131 | print "supported image formats for ", OpenCL::enum2str $type, "\n"; |
104 | |
132 | |
105 | for my $f ($ctx->supported_image_formats (0, $type)) { |
133 | for my $f ($ctx->supported_image_formats (0, $type)) { |
… | |
… | |
124 | |
152 | |
125 | my $src = ' |
153 | my $src = ' |
126 | __kernel void |
154 | __kernel void |
127 | squareit (__global float *input, __global float *output) |
155 | squareit (__global float *input, __global float *output) |
128 | { |
156 | { |
129 | size_t id = get_global_id (0); |
157 | $id = get_global_id (0); |
130 | output [id] = input [id] * input [id]; |
158 | output [id] = input [id] * input [id]; |
131 | } |
159 | } |
132 | '; |
160 | '; |
133 | |
161 | |
134 | my $prog = $ctx->program_with_source ($src); |
162 | my $prog = $ctx->program_with_source ($src); |
135 | |
163 | |
|
|
164 | # build croaks on compile errors, so catch it and print the compile errors |
136 | eval { $prog->build ($dev); 1 } |
165 | eval { $prog->build ($dev); 1 } |
137 | or die $prog->build_info ($dev, OpenCL::PROGRAM_BUILD_LOG); |
166 | or die $prog->build_info ($dev, OpenCL::PROGRAM_BUILD_LOG); |
138 | |
167 | |
139 | my $kernel = $prog->kernel ("squareit"); |
168 | my $kernel = $prog->kernel ("squareit"); |
140 | |
169 | |
141 | =head2 Create some input and output float buffers, then call squareit on them. |
170 | =head2 Create some input and output float buffers, then call the |
|
|
171 | 'squareit' kernel on them. |
142 | |
172 | |
143 | my $input = $ctx->buffer_sv (OpenCL::MEM_COPY_HOST_PTR, pack "f*", 1, 2, 3, 4.5); |
173 | my $input = $ctx->buffer_sv (OpenCL::MEM_COPY_HOST_PTR, pack "f*", 1, 2, 3, 4.5); |
144 | my $output = $ctx->buffer (0, OpenCL::SIZEOF_FLOAT * 5); |
174 | my $output = $ctx->buffer (0, OpenCL::SIZEOF_FLOAT * 5); |
145 | |
175 | |
146 | # set buffer |
176 | # set buffer |
… | |
… | |
185 | |
215 | |
186 | =head1 DOCUMENTATION |
216 | =head1 DOCUMENTATION |
187 | |
217 | |
188 | =head2 BASIC CONVENTIONS |
218 | =head2 BASIC CONVENTIONS |
189 | |
219 | |
190 | This is not a 1:1 C-style translation of OpenCL to Perl - instead I |
220 | This is not a one-to-one C-style translation of OpenCL to Perl - instead |
191 | attempted to make the interface as type-safe as possible and introducing |
221 | I attempted to make the interface as type-safe as possible by introducing |
192 | object syntax where it makes sense. There are a number of important |
222 | object syntax where it makes sense. There are a number of important |
193 | differences between the OpenCL C API and this module: |
223 | differences between the OpenCL C API and this module: |
194 | |
224 | |
195 | =over 4 |
225 | =over 4 |
196 | |
226 | |
… | |
… | |
201 | =item * OpenCL uses CamelCase for function names (C<clGetPlatformInfo>), |
231 | =item * OpenCL uses CamelCase for function names (C<clGetPlatformInfo>), |
202 | while this module uses underscores as word separator and often leaves out |
232 | while this module uses underscores as word separator and often leaves out |
203 | prefixes (C<< $platform->info >>). |
233 | prefixes (C<< $platform->info >>). |
204 | |
234 | |
205 | =item * OpenCL often specifies fixed vector function arguments as short |
235 | =item * OpenCL often specifies fixed vector function arguments as short |
206 | arrays (C<size_t origin[3]>), while this module explicitly expects the |
236 | arrays (C<$origin[3]>), while this module explicitly expects the |
207 | components as separate arguments- |
237 | components as separate arguments- |
|
|
238 | |
|
|
239 | =item * Structures are often specified with their components, and returned |
|
|
240 | as arrayrefs. |
208 | |
241 | |
209 | =item * Where possible, one of the pitch values is calculated from the |
242 | =item * Where possible, one of the pitch values is calculated from the |
210 | perl scalar length and need not be specified. |
243 | perl scalar length and need not be specified. |
211 | |
244 | |
212 | =item * When enqueuing commands, the wait list is specified by adding |
245 | =item * When enqueuing commands, the wait list is specified by adding |
… | |
… | |
245 | |
278 | |
246 | =over 4 |
279 | =over 4 |
247 | |
280 | |
248 | =item $int = OpenCL::errno |
281 | =item $int = OpenCL::errno |
249 | |
282 | |
250 | The last error returned by a function - it's only changed on errors. |
283 | The last error returned by a function - it's only valid after an error occured |
|
|
284 | and before calling another OpenCL function. |
251 | |
285 | |
252 | =item $str = OpenCL::err2str $errval |
286 | =item $str = OpenCL::err2str $errval |
253 | |
287 | |
254 | Comverts an error value into a human readable string. |
288 | Comverts an error value into a human readable string. |
255 | |
289 | |
… | |
… | |
297 | |
331 | |
298 | Returns a list of matching OpenCL::Device objects. |
332 | Returns a list of matching OpenCL::Device objects. |
299 | |
333 | |
300 | =item $ctx = $platform->context_from_type ($properties, $type = OpenCL::DEVICE_TYPE_DEFAULT, $notify = undef) |
334 | =item $ctx = $platform->context_from_type ($properties, $type = OpenCL::DEVICE_TYPE_DEFAULT, $notify = undef) |
301 | |
335 | |
302 | Tries to create a context. Never worked for me. |
336 | Tries to create a context. Never worked for me, and you need devices explitly anyway. |
303 | |
337 | |
304 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateContextFromType.html> |
338 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateContextFromType.html> |
305 | |
339 | |
|
|
340 | =item $ctx = $device->context ($properties = undef, @$devices, $notify = undef) |
|
|
341 | |
|
|
342 | Create a new OpenCL::Context object using the given device object(s)- a |
|
|
343 | CL_CONTEXT_PLATFORM property is supplied automatically. |
|
|
344 | |
|
|
345 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateContext.html> |
|
|
346 | |
306 | =back |
347 | =back |
307 | |
348 | |
308 | =head2 THE OpenCL::Device CLASS |
349 | =head2 THE OpenCL::Device CLASS |
309 | |
350 | |
310 | =over 4 |
351 | =over 4 |
… | |
… | |
313 | |
354 | |
314 | See C<< $platform->info >> for details. |
355 | See C<< $platform->info >> for details. |
315 | |
356 | |
316 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetDeviceInfo.html> |
357 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetDeviceInfo.html> |
317 | |
358 | |
318 | =item $ctx = $device->context ($properties = undef, $notify = undef) |
|
|
319 | |
|
|
320 | Create a new OpenCL::Context object. |
|
|
321 | |
|
|
322 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateContext.html> |
|
|
323 | |
|
|
324 | =back |
359 | =back |
325 | |
360 | |
326 | =head2 THE OpenCL::Context CLASS |
361 | =head2 THE OpenCL::Context CLASS |
327 | |
362 | |
328 | =over 4 |
363 | =over 4 |
… | |
… | |
353 | |
388 | |
354 | =item $buf = $ctx->buffer_sv ($flags, $data) |
389 | =item $buf = $ctx->buffer_sv ($flags, $data) |
355 | |
390 | |
356 | Creates a new OpenCL::Buffer object and initialise it with the given data values. |
391 | Creates a new OpenCL::Buffer object and initialise it with the given data values. |
357 | |
392 | |
358 | =item $img = $ctx->image2d ($flags, $channel_order, $channel_type, $width, $height, $data) |
393 | =item $img = $ctx->image2d ($flags, $channel_order, $channel_type, $width, $height, $row_pitch = 0, $data = undef) |
359 | |
394 | |
360 | Creates a new OpenCL::Image2D object and optionally initialises it with the given data values. |
395 | Creates a new OpenCL::Image2D object and optionally initialises it with the given data values. |
361 | |
396 | |
362 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateImage2D.html> |
397 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateImage2D.html> |
363 | |
398 | |
364 | =item $img = $ctx->image3d ($flags, $channel_order, $channel_type, $width, $height, $depth, $slice_pitch, $data) |
399 | =item $img = $ctx->image3d ($flags, $channel_order, $channel_type, $width, $height, $depth, $row_pitch = 0, $slice_pitch = 0, $data = undef) |
365 | |
400 | |
366 | Creates a new OpenCL::Image3D object and optionally initialises it with the given data values. |
401 | Creates a new OpenCL::Image3D object and optionally initialises it with the given data values. |
367 | |
402 | |
368 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateImage3D.html> |
403 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateImage3D.html> |
369 | |
404 | |
… | |
… | |
432 | |
467 | |
433 | =item $ev = $queue->enqueue_read_image ($src, $blocking, $x, $y, $z, $width, $height, $depth, $row_pitch, $slice_pitch, $data, $wait_events...) |
468 | =item $ev = $queue->enqueue_read_image ($src, $blocking, $x, $y, $z, $width, $height, $depth, $row_pitch, $slice_pitch, $data, $wait_events...) |
434 | |
469 | |
435 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueReadImage.html> |
470 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueReadImage.html> |
436 | |
471 | |
437 | =item $ev = $queue->enqueue_write_image ($src, $blocking, $x, $y, $z, $width, $height, $depth, $row_pitch, $data, $wait_events...) |
472 | =item $ev = $queue->enqueue_write_image ($src, $blocking, $x, $y, $z, $width, $height, $depth, $row_pitch, $slice_pitch, $data, $wait_events...) |
438 | |
473 | |
439 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueWriteImage.html> |
474 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueWriteImage.html> |
440 | |
475 | |
441 | =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...) |
476 | =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, $dst_row_pitch, $dst_slice_pitch, $wait_event...) |
442 | |
477 | |
443 | Yeah. |
478 | Yeah. |
444 | |
479 | |
445 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueCopyBufferRect.html> |
480 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueCopyBufferRect.html> |
446 | |
481 | |
447 | =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, ...) |
482 | =item $ev = $queue->enqueue_copy_buffer_to_image ($src_buffer, $dst_image, $src_offset, $dst_x, $dst_y, $dst_z, $width, $height, $depth, $wait_events...) |
448 | |
483 | |
449 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueCopyBufferToImage.html>. |
484 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueCopyBufferToImage.html>. |
450 | |
485 | |
451 | =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, ...) |
486 | =item $ev = $queue->enqueue_copy_image ($src_image, $dst_image, $src_x, $src_y, $src_z, $dst_x, $dst_y, $dst_z, $width, $height, $depth, $wait_events...) |
452 | |
487 | |
453 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueCopyImage.html> |
488 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueCopyImage.html> |
454 | |
489 | |
455 | =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, ...) |
490 | =item $ev = $queue->enqueue_copy_image_to_buffer ($src_image, $dst_image, $src_x, $src_y, $src_z, $width, $height, $depth, $dst_offset, $wait_events...) |
456 | |
491 | |
457 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueCopyImageToBuffer.html> |
492 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueCopyImageToBuffer.html> |
458 | |
493 | |
459 | =item $ev = $queue->enqueue_task ($kernel, $wait_events...) |
494 | =item $ev = $queue->enqueue_task ($kernel, $wait_events...) |
460 | |
495 | |
… | |
… | |
625 | package OpenCL; |
660 | package OpenCL; |
626 | |
661 | |
627 | use common::sense; |
662 | use common::sense; |
628 | |
663 | |
629 | BEGIN { |
664 | BEGIN { |
630 | our $VERSION = '0.03'; |
665 | our $VERSION = '0.15'; |
631 | |
666 | |
632 | require XSLoader; |
667 | require XSLoader; |
633 | XSLoader::load (__PACKAGE__, $VERSION); |
668 | XSLoader::load (__PACKAGE__, $VERSION); |
634 | |
669 | |
635 | @OpenCL::Buffer::ISA = |
670 | @OpenCL::Buffer::ISA = |