… | |
… | |
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/ |
|
|
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. |
23 | |
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 | |
… | |
… | |
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_simple; |
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 | say "supported image formats for ", OpenCL::enum2str $type; |
… | |
… | |
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 |
… | |
… | |
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 | |