… | |
… | |
6 | |
6 | |
7 | use OpenCL; |
7 | use OpenCL; |
8 | |
8 | |
9 | =head1 DESCRIPTION |
9 | =head1 DESCRIPTION |
10 | |
10 | |
11 | This is an early release which might be useful, but hasn't seen any testing. |
11 | This is an early release which might be useful, but hasn't seen much testing. |
12 | |
12 | |
|
|
13 | =head2 OpenCL FROM 10000 FEET HEIGHT |
|
|
14 | |
|
|
15 | Here is a high level overview of OpenCL: |
|
|
16 | |
|
|
17 | First you need to find one or more OpenCL::Platforms (kind of like |
|
|
18 | vendors) - usually there is only one. |
|
|
19 | |
|
|
20 | Each platform gives you access to a number of OpenCL::Device objects, e.g. |
|
|
21 | your graphics card. |
|
|
22 | |
|
|
23 | From a platform and some devices, you create an OpenCL::Context, which is |
|
|
24 | a very central object in OpenCL: Once you have a context you can create |
|
|
25 | most other objects: |
|
|
26 | |
|
|
27 | OpenCL::Program objects, which store source code and, after building |
|
|
28 | ("compiling and linking"), also binary programs. For each kernel function |
|
|
29 | in a program you can then create an OpenCL::Kernel object which represents |
|
|
30 | basically a function call with argument values. |
|
|
31 | |
|
|
32 | OpenCL::Memory objects of various flavours: OpenCL::Buffers objects (flat |
|
|
33 | memory areas, think array) and OpenCL::Image objects (think 2d or 3d |
|
|
34 | array) for bulk data and input and output for kernels. |
|
|
35 | |
|
|
36 | OpenCL::Sampler objects, which are kind of like texture filter modes in |
|
|
37 | OpenGL. |
|
|
38 | |
|
|
39 | OpenCL::Queue objects - command queues, which allow you to submit memory |
|
|
40 | reads, writes and copies, as well as kernel calls to your devices. They |
|
|
41 | also offer a variety of methods to synchronise request execution, for |
|
|
42 | example with barriers or OpenCL::Event objects. |
|
|
43 | |
|
|
44 | OpenCL::Event objects are used to signal when something is complete. |
|
|
45 | |
13 | =head1 HELPFUL RESOURCES |
46 | =head2 HELPFUL RESOURCES |
14 | |
47 | |
15 | The OpenCL spec used to develop this module (1.2 spec was available, but |
48 | The OpenCL spec used to develop this module (1.2 spec was available, but |
16 | no implementation was available to me :). |
49 | no implementation was available to me :). |
17 | |
50 | |
18 | http://www.khronos.org/registry/cl/specs/opencl-1.1.pdf |
51 | http://www.khronos.org/registry/cl/specs/opencl-1.1.pdf |
19 | |
52 | |
20 | OpenCL manpages: |
53 | OpenCL manpages: |
21 | |
54 | |
22 | 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/ |
23 | |
56 | |
|
|
57 | =head1 BASIC WORKFLOW |
|
|
58 | |
|
|
59 | To get something done, you basically have to do this once: |
|
|
60 | |
|
|
61 | Find some platform (e.g. the first one) and some device (e.g. the first |
|
|
62 | device you can find), and create a context from those. |
|
|
63 | |
|
|
64 | Create a command queue from your context, and program objects from your |
|
|
65 | OpenCL source code, build the programs. |
|
|
66 | |
|
|
67 | Create kernel objects for all kernels you want to use. |
|
|
68 | |
|
|
69 | Then, to execute stuff, you repeat this: |
|
|
70 | |
|
|
71 | Create some input and output buffers from your context. Initialise the |
|
|
72 | input buffers with data. Set these as arguments to your kernel. |
|
|
73 | |
|
|
74 | Enqueue the kernel execution. |
|
|
75 | |
|
|
76 | Enqueue buffer reads for your output buffer to read results. |
|
|
77 | |
|
|
78 | The next section shows how this can be done. |
|
|
79 | |
24 | =head1 EXAMPLES |
80 | =head1 EXAMPLES |
25 | |
81 | |
26 | =head2 Enumerate all devices and get contexts for them. |
82 | =head2 Enumerate all devices and get contexts for them. |
27 | |
83 | |
28 | for my $platform (OpenCL::platforms) { |
84 | for my $platform (OpenCL::platforms) { |
29 | warn $platform->info (OpenCL::PLATFORM_NAME); |
85 | printf "platform: %s\n", $platform->info (OpenCL::PLATFORM_NAME); |
30 | warn $platform->info (OpenCL::PLATFORM_EXTENSIONS); |
86 | printf "extensions: %s\n", $platform->info (OpenCL::PLATFORM_EXTENSIONS); |
31 | for my $device ($platform->devices) { |
87 | for my $device ($platform->devices) { |
32 | warn $device->info (OpenCL::DEVICE_NAME); |
88 | printf "+ device: %s\n", $device->info (OpenCL::DEVICE_NAME); |
33 | my $ctx = $device->context_simple; |
89 | my $ctx = $device->context; |
34 | # do stuff |
90 | # do stuff |
35 | } |
91 | } |
36 | } |
92 | } |
37 | |
93 | |
38 | =head2 Get a useful context and a command queue. |
94 | =head2 Get a useful context and a command queue. |
39 | |
95 | |
40 | my $dev = ((OpenCL::platforms)[0]->devices)[0]; |
96 | my $dev = ((OpenCL::platforms)[0]->devices)[0]; |
41 | my $ctx = $dev->context_simple; |
97 | my $ctx = $dev->context; |
42 | my $queue = $ctx->command_queue_simple ($dev); |
98 | my $queue = $ctx->queue ($dev); |
43 | |
99 | |
44 | =head2 Print all supported image formats of a context. |
100 | =head2 Print all supported image formats of a context. |
45 | |
101 | |
46 | for my $type (OpenCL::MEM_OBJECT_IMAGE2D, OpenCL::MEM_OBJECT_IMAGE3D) { |
102 | for my $type (OpenCL::MEM_OBJECT_IMAGE2D, OpenCL::MEM_OBJECT_IMAGE3D) { |
47 | say "supported image formats for ", OpenCL::enum2str $type; |
103 | print "supported image formats for ", OpenCL::enum2str $type, "\n"; |
48 | |
104 | |
49 | for my $f ($ctx->supported_image_formats (0, $type)) { |
105 | for my $f ($ctx->supported_image_formats (0, $type)) { |
50 | printf " %-10s %-20s\n", OpenCL::enum2str $f->[0], OpenCL::enum2str $f->[1]; |
106 | printf " %-10s %-20s\n", OpenCL::enum2str $f->[0], OpenCL::enum2str $f->[1]; |
51 | } |
107 | } |
52 | } |
108 | } |
… | |
… | |
55 | then asynchronously. |
111 | then asynchronously. |
56 | |
112 | |
57 | my $buf = $ctx->buffer_sv (OpenCL::MEM_COPY_HOST_PTR, "helmut"); |
113 | my $buf = $ctx->buffer_sv (OpenCL::MEM_COPY_HOST_PTR, "helmut"); |
58 | |
114 | |
59 | $queue->enqueue_read_buffer ($buf, 1, 1, 3, my $data); |
115 | $queue->enqueue_read_buffer ($buf, 1, 1, 3, my $data); |
60 | warn $data; |
116 | print "$data\n"; |
61 | |
117 | |
62 | my $ev = $queue->enqueue_read_buffer ($buf, 0, 1, 3, my $data); |
118 | my $ev = $queue->enqueue_read_buffer ($buf, 0, 1, 3, my $data); |
63 | $ev->wait; |
119 | $ev->wait; |
64 | warn $data; |
120 | print "$data\n"; # prints "elm" |
65 | |
121 | |
66 | =head2 Create and build a program, then create a kernel out of one of its |
122 | =head2 Create and build a program, then create a kernel out of one of its |
67 | functions. |
123 | functions. |
68 | |
124 | |
69 | my $src = ' |
125 | my $src = ' |
… | |
… | |
96 | |
152 | |
97 | # enqueue a synchronous read |
153 | # enqueue a synchronous read |
98 | $queue->enqueue_read_buffer ($output, 1, 0, OpenCL::SIZEOF_FLOAT * 4, my $data); |
154 | $queue->enqueue_read_buffer ($output, 1, 0, OpenCL::SIZEOF_FLOAT * 4, my $data); |
99 | |
155 | |
100 | # print the results: |
156 | # print the results: |
101 | say join ", ", unpack "f*", $data; |
157 | printf "%s\n", join ", ", unpack "f*", $data; |
102 | |
158 | |
103 | =head2 The same enqueue operations as before, but assuming an out-of-order queue, |
159 | =head2 The same enqueue operations as before, but assuming an out-of-order queue, |
104 | showing off barriers. |
160 | showing off barriers. |
105 | |
161 | |
106 | # execute it for all 4 numbers |
162 | # execute it for all 4 numbers |
… | |
… | |
148 | |
204 | |
149 | =item * OpenCL often specifies fixed vector function arguments as short |
205 | =item * OpenCL often specifies fixed vector function arguments as short |
150 | arrays (C<size_t origin[3]>), while this module explicitly expects the |
206 | arrays (C<size_t origin[3]>), while this module explicitly expects the |
151 | components as separate arguments- |
207 | components as separate arguments- |
152 | |
208 | |
153 | =item * Where possible, the row_pitch value is calculated from the perl |
209 | =item * Where possible, one of the pitch values is calculated from the |
154 | scalar length and need not be specified. |
210 | perl scalar length and need not be specified. |
155 | |
211 | |
156 | =item * When enqueuing commands, the wait list is specified by adding |
212 | =item * When enqueuing commands, the wait list is specified by adding |
157 | extra arguments to the function - everywhere a C<$wait_events...> argument |
213 | extra arguments to the function - anywhere a C<$wait_events...> argument |
158 | is documented this can be any number of event objects. |
214 | is documented this can be any number of event objects. |
159 | |
215 | |
160 | =item * When enqueuing commands, if the enqueue method is called in void |
216 | =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 |
217 | context, no event is created. In all other contexts an event is returned |
162 | by the method. |
218 | by the method. |
… | |
… | |
165 | other status is returned the function will throw an exception, so you |
221 | other status is returned the function will throw an exception, so you |
166 | don't normally have to to any error checking. |
222 | don't normally have to to any error checking. |
167 | |
223 | |
168 | =back |
224 | =back |
169 | |
225 | |
|
|
226 | =head2 PERL AND OPENCL TYPES |
|
|
227 | |
|
|
228 | This handy(?) table lists OpenCL types and their perl, PDL and pack/unpack |
|
|
229 | format equivalents: |
|
|
230 | |
|
|
231 | OpenCL perl PDL pack/unpack |
|
|
232 | char IV - c |
|
|
233 | uchar IV byte C |
|
|
234 | short IV short s |
|
|
235 | ushort IV ushort S |
|
|
236 | int IV long? l |
|
|
237 | uint IV - L |
|
|
238 | long IV longlong q |
|
|
239 | ulong IV - Q |
|
|
240 | float NV float f |
|
|
241 | half IV ushort S |
|
|
242 | double NV double d |
|
|
243 | |
170 | =head2 THE OpenCL PACKAGE |
244 | =head2 THE OpenCL PACKAGE |
171 | |
245 | |
172 | =over 4 |
246 | =over 4 |
173 | |
247 | |
174 | =item $int = OpenCL::errno |
248 | =item $int = OpenCL::errno |
… | |
… | |
177 | |
251 | |
178 | =item $str = OpenCL::err2str $errval |
252 | =item $str = OpenCL::err2str $errval |
179 | |
253 | |
180 | Comverts an error value into a human readable string. |
254 | Comverts an error value into a human readable string. |
181 | |
255 | |
182 | =item $str = OpenCL::err2str $enum |
256 | =item $str = OpenCL::enum2str $enum |
183 | |
257 | |
184 | Converts most enum values (inof parameter names, image format constants, |
258 | Converts most enum values (inof parameter names, image format constants, |
185 | object types, addressing and filter modes, command types etc.) into a |
259 | object types, addressing and filter modes, command types etc.) into a |
186 | human readbale string. When confronted with some random integer it can be |
260 | 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 |
261 | very helpful to pass it through this function to maybe get some readable |
… | |
… | |
191 | |
265 | |
192 | Returns all available OpenCL::Platform objects. |
266 | Returns all available OpenCL::Platform objects. |
193 | |
267 | |
194 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetPlatformIDs.html> |
268 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetPlatformIDs.html> |
195 | |
269 | |
196 | =item $ctx = OpenCL::context_from_type_simple $type = OpenCL::DEVICE_TYPE_DEFAULT |
270 | =item $ctx = OpenCL::context_from_type $properties, $type = OpenCL::DEVICE_TYPE_DEFAULT, $notify = undef |
197 | |
271 | |
198 | Tries to create a context from a default device and platform - never worked for me. |
272 | Tries to create a context from a default device and platform - never worked for me. |
199 | |
273 | |
200 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateContextFromType.html> |
274 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateContextFromType.html> |
201 | |
275 | |
… | |
… | |
221 | |
295 | |
222 | =item @devices = $platform->devices ($type = OpenCL::DEVICE_TYPE_ALL) |
296 | =item @devices = $platform->devices ($type = OpenCL::DEVICE_TYPE_ALL) |
223 | |
297 | |
224 | Returns a list of matching OpenCL::Device objects. |
298 | Returns a list of matching OpenCL::Device objects. |
225 | |
299 | |
226 | =item $ctx = $platform->context_from_type_simple ($type = OpenCL::DEVICE_TYPE_DEFAULT) |
300 | =item $ctx = $platform->context_from_type ($properties, $type = OpenCL::DEVICE_TYPE_DEFAULT, $notify = undef) |
227 | |
301 | |
228 | Tries to create a context. Never worked for me. |
302 | Tries to create a context. Never worked for me. |
229 | |
303 | |
230 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateContextFromType.html> |
304 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateContextFromType.html> |
231 | |
305 | |
… | |
… | |
239 | |
313 | |
240 | See C<< $platform->info >> for details. |
314 | See C<< $platform->info >> for details. |
241 | |
315 | |
242 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetDeviceInfo.html> |
316 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetDeviceInfo.html> |
243 | |
317 | |
244 | =item $ctx = $device->context_simple |
318 | =item $ctx = $device->context ($properties = undef, $notify = undef) |
245 | |
319 | |
246 | Convenience function to create a new OpenCL::Context object. |
320 | Create a new OpenCL::Context object. |
247 | |
321 | |
248 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateContext.html> |
322 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateContext.html> |
249 | |
323 | |
250 | =back |
324 | =back |
251 | |
325 | |
… | |
… | |
257 | |
331 | |
258 | See C<< $platform->info >> for details. |
332 | See C<< $platform->info >> for details. |
259 | |
333 | |
260 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetContextInfo.html> |
334 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetContextInfo.html> |
261 | |
335 | |
262 | =item $queue = $ctx->command_queue_simple ($device) |
336 | =item $queue = $ctx->queue ($device, $properties) |
263 | |
337 | |
264 | Convenience function to create a new OpenCL::Queue object from the context and the given device. |
338 | Create a new OpenCL::Queue object from the context and the given device. |
265 | |
339 | |
266 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateCommandQueue.html> |
340 | L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateCommandQueue.html> |
267 | |
341 | |
268 | =item $ev = $ctx->user_event |
342 | =item $ev = $ctx->user_event |
269 | |
343 | |
… | |
… | |
327 | They also allow you to specify any number of other event objects that this |
401 | 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 |
402 | request has to wait for before it starts executing, by simply passing the |
329 | event objects as extra parameters to the enqueue methods. |
403 | event objects as extra parameters to the enqueue methods. |
330 | |
404 | |
331 | Queues execute in-order by default, without any parallelism, so in most |
405 | 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. |
406 | cases (i.e. you use only one queue) it's not necessary to wait for or |
|
|
407 | create event objects. |
333 | |
408 | |
334 | =over 4 |
409 | =over 4 |
335 | |
410 | |
336 | =item $packed_value = $ctx->info ($name) |
411 | =item $packed_value = $ctx->info ($name) |
337 | |
412 | |