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

Comparing OpenCL/OpenCL.pm (file contents):
Revision 1.79 by root, Sat May 5 13:30:07 2012 UTC vs.
Revision 1.88 by root, Tue Oct 22 17:25:38 2013 UTC

28specific device ("compiling and linking"), also binary programs. For each 28specific device ("compiling and linking"), also binary programs. For each
29kernel function in a program you can then create an OpenCL::Kernel object 29kernel function in a program you can then create an OpenCL::Kernel object
30which represents basically a function call with argument values. 30which represents basically a function call with argument values.
31 31
32OpenCL::Memory objects of various flavours: OpenCL::Buffer objects (flat 32OpenCL::Memory objects of various flavours: OpenCL::Buffer objects (flat
33memory areas, think arrays or structs) and OpenCL::Image objects (think 2d 33memory areas, think arrays or structs) and OpenCL::Image objects (think 2D
34or 3d array) for bulk data and input and output for kernels. 34or 3D array) for bulk data and input and output for kernels.
35 35
36OpenCL::Sampler objects, which are kind of like texture filter modes in 36OpenCL::Sampler objects, which are kind of like texture filter modes in
37OpenGL. 37OpenGL.
38 38
39OpenCL::Queue objects - command queues, which allow you to submit memory 39OpenCL::Queue objects - command queues, which allow you to submit memory
96initialised at creation time). 96initialised at creation time).
97 97
98Enqueue the kernel execution. 98Enqueue the kernel execution.
99 99
100Enqueue buffer reads for your output buffer to read results. 100Enqueue buffer reads for your output buffer to read results.
101
102=head1 OPENCL 1.1 VS. OPENCL 1.2
103
104This module supports both OpenCL version 1.1 and 1.2, although the OpenCL
1051.2 interface hasn't been tested much for lack of availability of an
106actual implementation.
107
108Every function or method in this manual page that interfaces to a
109particular OpenCL function has a link to the its C manual page.
110
111If the link contains a F<1.1>, then this function is an OpenCL 1.1
112function. Most but not all also exist in OpenCL 1.2, and this module
113tries to emulate the missing ones for you, when told to do so at
114compiletime. You cna check whether a function was removed in OpenCL 1.2 by
115replacing the F<1.1> component in the URL by F<1.2>.
116
117If the link contains a F<1.2>, then this is a OpenCL 1.2-only
118function. Even if the module was compiled with OpenCL 1.2 header files
119and has an 1.2 OpenCL library, calling such a function on a platform that
120doesn't implement 1.2 causes undefined behaviour, usually a crash (But
121this is not guaranteed).
122
123You can find out whether this module was compiled to prefer 1.1
124functionality by ooking at C<OpenCL::PREFER_1_1> - if it is true, then
1251.1 functions generally are implemented using 1.1 OpenCL functions. If it
126is false, then 1.1 functions missing from 1.2 are emulated by calling 1.2
127fucntions.
128
129This is a somewhat sorry state of affairs, but the Khronos group choose to
130make every release of OpenCL source and binary incompatible with previous
131releases.
132 101
133=head1 EXAMPLES 102=head1 EXAMPLES
134 103
135=head2 Enumerate all devices and get contexts for them. 104=head2 Enumerate all devices and get contexts for them.
136 105
244 $ev->wait; 213 $ev->wait;
245 214
246=head2 Use the OpenGL module to share a texture between OpenCL and OpenGL and draw some julia 215=head2 Use the OpenGL module to share a texture between OpenCL and OpenGL and draw some julia
247set flight effect. 216set flight effect.
248 217
249This is quite a long example to get you going - you can download it from 218This is quite a long example to get you going - you can also download it
250L<http://cvs.schmorp.de/OpenCL/examples/juliaflight>. 219from L<http://cvs.schmorp.de/OpenCL/examples/juliaflight>.
251 220
252 use OpenGL ":all"; 221 use OpenGL ":all";
253 use OpenCL; 222 use OpenCL;
254 223
255 my $S = $ARGV[0] || 256; # window/texture size, smaller is faster 224 my $S = $ARGV[0] || 256; # window/texture size, smaller is faster
263 # find and use the first opencl device that let's us get a shared opengl context 232 # find and use the first opencl device that let's us get a shared opengl context
264 my $platform; 233 my $platform;
265 my $dev; 234 my $dev;
266 my $ctx; 235 my $ctx;
267 236
237 sub get_context {
268 for (OpenCL::platforms) { 238 for (OpenCL::platforms) {
269 $platform = $_; 239 $platform = $_;
270 for ($platform->devices) { 240 for ($platform->devices) {
271 $dev = $_; 241 $dev = $_;
272 $ctx = $platform->context ([OpenCL::GLX_DISPLAY_KHR, undef, OpenCL::GL_CONTEXT_KHR, undef], [$dev]) 242 $ctx = eval { $platform->context ([OpenCL::GLX_DISPLAY_KHR, undef, OpenCL::GL_CONTEXT_KHR, undef], [$dev]) }
273 and last; 243 and return;
244 }
274 } 245 }
246
247 die "cannot find suitable OpenCL device\n";
275 } 248 }
276 249
277 $ctx 250 get_context;
278 or die "cannot find suitable OpenCL device\n";
279 251
280 my $queue = $ctx->queue ($dev); 252 my $queue = $ctx->queue ($dev);
281 253
282 # now attach an opencl image2d object to the opengl texture 254 # now attach an opencl image2d object to the opengl texture
283 my $tex = $ctx->gl_texture2d (OpenCL::MEM_WRITE_ONLY, GL_TEXTURE_2D, 0, $texid); 255 my $tex = $ctx->gl_texture2d (OpenCL::MEM_WRITE_ONLY, GL_TEXTURE_2D, 0, $texid);
424=item * This module expects all functions to return C<OpenCL::SUCCESS>. If any 396=item * This module expects all functions to return C<OpenCL::SUCCESS>. If any
425other status is returned the function will throw an exception, so you 397other status is returned the function will throw an exception, so you
426don't normally have to to any error checking. 398don't normally have to to any error checking.
427 399
428=back 400=back
401
402=head2 CONSTANTS
403
404All C<CL_xxx> constants that this module supports are always available
405in the C<OpenCL> namespace as C<OpenCL::xxx> (i.e. without the C<CL_>
406prefix). Constants which are not defined in the header files used during
407compilation, or otherwise are not available, will have the value C<0> (in
408some cases, this will make them indistinguishable from real constants,
409sorry).
410
411The latest version of this module knows and exports the constants
412listed in L<http://cvs.schmorp.de/OpenCL/constiv.h>.
413
414=head2 OPENCL 1.1 VS. OPENCL 1.2
415
416This module supports both OpenCL version 1.1 and 1.2, although the OpenCL
4171.2 interface hasn't been tested much for lack of availability of an
418actual implementation.
419
420Every function or method in this manual page that interfaces to a
421particular OpenCL function has a link to the its C manual page.
422
423If the link contains a F<1.1>, then this function is an OpenCL 1.1
424function. Most but not all also exist in OpenCL 1.2, and this module
425tries to emulate the missing ones for you, when told to do so at
426compiletime. You can check whether a function was removed in OpenCL 1.2 by
427replacing the F<1.1> component in the URL by F<1.2>.
428
429If the link contains a F<1.2>, then this is a OpenCL 1.2-only
430function. Even if the module was compiled with OpenCL 1.2 header files
431and has an 1.2 OpenCL library, calling such a function on a platform that
432doesn't implement 1.2 causes undefined behaviour, usually a crash (But
433this is not guaranteed).
434
435You can find out whether this module was compiled to prefer 1.1
436functionality by ooking at C<OpenCL::PREFER_1_1> - if it is true, then
4371.1 functions generally are implemented using 1.1 OpenCL functions. If it
438is false, then 1.1 functions missing from 1.2 are emulated by calling 1.2
439fucntions.
440
441This is a somewhat sorry state of affairs, but the Khronos group choose to
442make every release of OpenCL source and binary incompatible with previous
443releases.
429 444
430=head2 PERL AND OPENCL TYPES 445=head2 PERL AND OPENCL TYPES
431 446
432This handy(?) table lists OpenCL types and their perl, PDL and pack/unpack 447This handy(?) table lists OpenCL types and their perl, PDL and pack/unpack
433format equivalents: 448format equivalents:
585use Async::Interrupt (); 600use Async::Interrupt ();
586 601
587our $POLL_FUNC; # set by XS 602our $POLL_FUNC; # set by XS
588 603
589BEGIN { 604BEGIN {
590 our $VERSION = '0.99'; 605 our $VERSION = '1.01';
591 606
592 require XSLoader; 607 require XSLoader;
593 XSLoader::load (__PACKAGE__, $VERSION); 608 XSLoader::load (__PACKAGE__, $VERSION);
594 609
595 @OpenCL::Platform::ISA = 610 @OpenCL::Platform::ISA =
631The last error returned by a function - it's only valid after an error occured 646The last error returned by a function - it's only valid after an error occured
632and before calling another OpenCL function. 647and before calling another OpenCL function.
633 648
634=item $str = OpenCL::err2str [$errval] 649=item $str = OpenCL::err2str [$errval]
635 650
636Converts an error value into a human readable string. IF no error value is 651Converts an error value into a human readable string. If no error value is
637given, then the last error will be used (as returned by OpenCL::errno). 652given, then the last error will be used (as returned by OpenCL::errno).
653
654The latest version of this module knows the error constants
655listed in L<http://cvs.schmorp.de/OpenCL/errstr.h>.
638 656
639=item $str = OpenCL::enum2str $enum 657=item $str = OpenCL::enum2str $enum
640 658
641Converts most enum values (of parameter names, image format constants, 659Converts most enum values (of parameter names, image format constants,
642object types, addressing and filter modes, command types etc.) into a 660object types, addressing and filter modes, command types etc.) into a
643human readable string. When confronted with some random integer it can be 661human readable string. When confronted with some random integer it can be
644very helpful to pass it through this function to maybe get some readable 662very helpful to pass it through this function to maybe get some readable
645string out of it. 663string out of it.
664
665The latest version of this module knows the enumaration constants
666listed in L<http://cvs.schmorp.de/OpenCL/enumstr.h>.
646 667
647=item @platforms = OpenCL::platforms 668=item @platforms = OpenCL::platforms
648 669
649Returns all available OpenCL::Platform objects. 670Returns all available OpenCL::Platform objects.
650 671
1202OpenCL::MEM_USE_HOST_PTR, OpenCL::MEM_ALLOC_HOST_PTR, OpenCL::MEM_COPY_HOST_PTR, 1223OpenCL::MEM_USE_HOST_PTR, OpenCL::MEM_ALLOC_HOST_PTR, OpenCL::MEM_COPY_HOST_PTR,
1203OpenCL::MEM_HOST_WRITE_ONLY, OpenCL::MEM_HOST_READ_ONLY, OpenCL::MEM_HOST_NO_ACCESS. 1224OpenCL::MEM_HOST_WRITE_ONLY, OpenCL::MEM_HOST_READ_ONLY, OpenCL::MEM_HOST_NO_ACCESS.
1204 1225
1205L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateBuffer.html> 1226L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateBuffer.html>
1206 1227
1228A somewhat informative thread on the flags is:
1229
1230L<http://www.khronos.org/message_boards/viewtopic.php?f=28&t=2440>
1231
1207=item $buf = $ctx->buffer_sv ($flags, $data) 1232=item $buf = $ctx->buffer_sv ($flags, $data)
1208 1233
1209Creates a new OpenCL::Buffer (actually OpenCL::BufferObj) object and 1234Creates a new OpenCL::Buffer (actually OpenCL::BufferObj) object and
1210initialise it with the given data values. 1235initialise it with the given data values.
1211 1236
1406 1431
1407=item $ev = $queue->copy_buffer ($src, $dst, $src_offset, $dst_offset, $len, $wait_events...) 1432=item $ev = $queue->copy_buffer ($src, $dst, $src_offset, $dst_offset, $len, $wait_events...)
1408 1433
1409L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueCopyBuffer.html> 1434L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueCopyBuffer.html>
1410 1435
1411=item $ev = $queue->read_buffer_rect (OpenCL::Memory buf, cl_bool blocking, $buf_x, $buf_y, $buf_z, $host_x, $host_y, $host_z, $width, $height, $depth, $buf_row_pitch, $buf_slice_pitch, $host_row_pitch, $host_slice_pitch, $data, $wait_events...) 1436$eue->read_buffer_rect ($buf, cl_bool blocking, $buf_x, $buf_y, $buf_z, $host_x, $host_y, $host_z, $width, $height, $depth, $buf_row_pitch, $buf_slice_pitch, $host_row_pitch, $host_slice_pitch, $data, $wait_events...)
1412 1437
1413http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueReadBufferRect.html 1438http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueReadBufferRect.html
1414 1439
1415=item $ev = $queue->write_buffer_rect (OpenCL::Memory buf, cl_bool blocking, $buf_x, $buf_y, $buf_z, $host_x, $host_y, $host_z, $width, $height, $depth, $buf_row_pitch, $buf_slice_pitch, $host_row_pitch, $host_slice_pitch, $data, $wait_events...) 1440=item $ev = $queue->write_buffer_rect ($buf, $blocking, $buf_y, $host_x, $host_z, $height, $buf_row_pitch, $host_row_pitch, $data, $wait_events...)
1416 1441
1417http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueWriteBufferRect.html 1442http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueWriteBufferRect.html
1418 1443
1419=item $ev = $queue->copy_buffer_to_image ($src_buffer, $dst_image, $src_offset, $dst_x, $dst_y, $dst_z, $width, $height, $depth, $wait_events...) 1444=item $ev = $queue->copy_buffer_to_image ($src_buffer, $dst_image, $src_offset, $dst_x, $dst_y, $dst_z, $width, $height, $depth, $wait_events...)
1420 1445
1604it. You are not necessarily meant to do it this way, this example just 1629it. You are not necessarily meant to do it this way, this example just
1605shows you the accessors to use :) 1630shows you the accessors to use :)
1606 1631
1607 my $mapped = $queue->map_image ($image, 1, OpenCL::MAP_WRITE); 1632 my $mapped = $queue->map_image ($image, 1, OpenCL::MAP_WRITE);
1608 1633
1609 $mapped->set ($_ * $mapped->row_pitch, pack "C", 5) 1634 $mapped->write ($_ * $mapped->row_pitch, pack "C", 5)
1610 for 0..$image->height; 1635 for 0 .. $mapped->height - 1;
1611 1636
1612 $mapped->unmap;. 1637 $mapped->unmap;.
1613 $mapped->wait; # only needed for out of order queues normally 1638 $mapped->wait; # only needed for out of order queues normally
1614 1639
1615=item $ev = $queue->unmap ($mapped, $wait_events...) 1640=item $ev = $queue->unmap ($mapped, $wait_events...)
1634=item $packed_value = $memory->info ($name) 1659=item $packed_value = $memory->info ($name)
1635 1660
1636See C<< $platform->info >> for details. 1661See C<< $platform->info >> for details.
1637 1662
1638L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetMemObjectInfo.html> 1663L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetMemObjectInfo.html>
1664
1665=item $memory->destructor_callback ($cb->())
1666
1667Sets a callback that will be invoked after the memory object is destructed.
1668
1669L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clSetMemObjectDestructorCallback.html>
1639 1670
1640=for gengetinfo begin mem 1671=for gengetinfo begin mem
1641 1672
1642=item $mem_object_type = $mem->type 1673=item $mem_object_type = $mem->type
1643 1674
2321 2352
2322This is a subclass of OpenCL::Mapped, representing mapped images. 2353This is a subclass of OpenCL::Mapped, representing mapped images.
2323 2354
2324=over 4 2355=over 4
2325 2356
2357=item $pixels = $mapped->width
2358
2359=item $pixels = $mapped->height
2360
2361=item $pixels = $mapped->depth
2362
2363Return the width/height/depth of the mapped image region, in pixels.
2364
2326=item $bytes = $mapped->row_pitch 2365=item $bytes = $mapped->row_pitch
2327 2366
2328=item $bytes = $mapped->slice_pitch 2367=item $bytes = $mapped->slice_pitch
2329 2368
2330Return the row or slice pitch of the image that has been mapped. 2369Return the row or slice pitch of the image that has been mapped.
2331 2370
2371=item $bytes = $mapped->element_size
2372
2373Return the size of a single pixel.
2374
2375=item $data = $mapped->get_row ($count, $x=0, $y=0, $z=0)
2376
2377Return C<$count> pixels from the given coordinates. The pixel data must
2378be completely contained within a single row.
2379
2380If C<$count> is C<undef>, then all the remaining pixels in that row are
2381returned.
2382
2383=item $mapped->set_row ($data, $x=0, $y=0, $z=0)
2384
2385Write the given pixel data at the given coordinate. The pixel data must
2386be completely contained within a single row.
2387
2332=back 2388=back
2333
2334 2389
2335=cut 2390=cut
2336 2391
23371; 23921;
2338 2393

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines