--- OpenCL/OpenCL.pm 2011/11/17 01:36:52 1.9 +++ OpenCL/OpenCL.pm 2011/11/17 03:42:03 1.16 @@ -20,18 +20,18 @@ Each platform gives you access to a number of OpenCL::Device objects, e.g. your graphics card. -From a platform and some devices, you create an OpenCL::Context, which is +From a platform and some device(s), you create an OpenCL::Context, which is a very central object in OpenCL: Once you have a context you can create most other objects: -OpenCL::Program objects, which store source code and, after building -("compiling and linking"), also binary programs. For each kernel function -in a program you can then create an OpenCL::Kernel object which represents -basically a function call with argument values. +OpenCL::Program objects, which store source code and, after building for a +specific device ("compiling and linking"), also binary programs. For each +kernel function in a program you can then create an OpenCL::Kernel object +which represents basically a function call with argument values. OpenCL::Memory objects of various flavours: OpenCL::Buffers objects (flat -memory areas, think array) and OpenCL::Image objects (think 2d or 3d -array) for bulk data and input and output for kernels. +memory areas, think arrays or structs) and OpenCL::Image objects (think 2d +or 3d array) for bulk data and input and output for kernels. OpenCL::Sampler objects, which are kind of like texture filter modes in OpenGL. @@ -54,53 +54,77 @@ http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/ +Here's a tutorial from AMD (very AMD-centric, too), not sure how useful it +is, but at least it's free of charge: + + http://developer.amd.com/zones/OpenCLZone/courses/Documents/Introduction_to_OpenCL_Programming%20Training_Guide%20%28201005%29.pdf + +If you are into UML class diagrams, the following diagram might help - if +not, it will be mildly cofusing: + + http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/classDiagram.html + =head1 BASIC WORKFLOW -To get something done, you basically have to do this once: +To get something done, you basically have to do this once (refer to the +examples below for actual code, this is just a high-level description): + +Find some platform (e.g. the first one) and some device(s) (e.g. the first +device of the platform), and create a context from those. -Find some platform (e.g. the first one) and some device (e.g. the first -device you can find), and create a context from those. +Create program objects from your OpenCL source code, then build (compile) +the programs for each device you want to run them on. -Create a command queue from your context, and program objects from your -OpenCL source code, build the programs. +Create kernel objects for all kernels you want to use (surprisingly, these +are not device-specific). -Create kernel objects for all kernels you want to use. +Then, to execute stuff, you repeat these steps, possibly resuing or +sharing some buffers: -Then, to execute stuff, you repeat this: +Create some input and output buffers from your context. Set these as +arguments to your kernel. -Create some input and output buffers from your context. Initialise the -input buffers with data. Set these as arguments to your kernel. +Enqueue buffer writes to initialise your input buffers (when not +initialised at creation time). Enqueue the kernel execution. Enqueue buffer reads for your output buffer to read results. -The next section shows how this can be done. - =head1 EXAMPLES =head2 Enumerate all devices and get contexts for them. +Best run this once to get a feel for the platforms and devices in your +system. + for my $platform (OpenCL::platforms) { - warn $platform->info (OpenCL::PLATFORM_NAME); - warn $platform->info (OpenCL::PLATFORM_EXTENSIONS); + printf "platform: %s\n", $platform->info (OpenCL::PLATFORM_NAME); + printf "extensions: %s\n", $platform->info (OpenCL::PLATFORM_EXTENSIONS); for my $device ($platform->devices) { - warn $device->info (OpenCL::DEVICE_NAME); - my $ctx = $device->context_simple; + printf "+ device: %s\n", $device->info (OpenCL::DEVICE_NAME); + my $ctx = $device->context; # do stuff } } =head2 Get a useful context and a command queue. - my $dev = ((OpenCL::platforms)[0]->devices)[0]; - my $ctx = $dev->context_simple; - my $queue = $ctx->queue ($dev); +This is a useful boilerplate for any OpenCL program that only wants to use +one device, + + my ($platform) = OpenCL::platforms; # find first platform + my ($dev) = $platform->devices; # find first device of platform + my $ctx = $platform->context (undef, [$dev]); # create context out of those + my $queue = $ctx->queue ($dev); # create a command queue for the device =head2 Print all supported image formats of a context. +Best run this once for your context, to see whats available and how to +gather information. + for my $type (OpenCL::MEM_OBJECT_IMAGE2D, OpenCL::MEM_OBJECT_IMAGE3D) { - say "supported image formats for ", OpenCL::enum2str $type; + print "supported image formats for ", OpenCL::enum2str $type, "\n"; for my $f ($ctx->supported_image_formats (0, $type)) { printf " %-10s %-20s\n", OpenCL::enum2str $f->[0], OpenCL::enum2str $f->[1]; @@ -113,11 +137,11 @@ my $buf = $ctx->buffer_sv (OpenCL::MEM_COPY_HOST_PTR, "helmut"); $queue->enqueue_read_buffer ($buf, 1, 1, 3, my $data); - warn $data; + print "$data\n"; my $ev = $queue->enqueue_read_buffer ($buf, 0, 1, 3, my $data); $ev->wait; - warn $data; + print "$data\n"; # prints "elm" =head2 Create and build a program, then create a kernel out of one of its functions. @@ -126,19 +150,21 @@ __kernel void squareit (__global float *input, __global float *output) { - size_t id = get_global_id (0); + $id = get_global_id (0); output [id] = input [id] * input [id]; } '; my $prog = $ctx->program_with_source ($src); + # build croaks on compile errors, so catch it and print the compile errors eval { $prog->build ($dev); 1 } or die $prog->build_info ($dev, OpenCL::PROGRAM_BUILD_LOG); my $kernel = $prog->kernel ("squareit"); -=head2 Create some input and output float buffers, then call squareit on them. +=head2 Create some input and output float buffers, then call the +'squareit' kernel on them. my $input = $ctx->buffer_sv (OpenCL::MEM_COPY_HOST_PTR, pack "f*", 1, 2, 3, 4.5); my $output = $ctx->buffer (0, OpenCL::SIZEOF_FLOAT * 5); @@ -154,7 +180,7 @@ $queue->enqueue_read_buffer ($output, 1, 0, OpenCL::SIZEOF_FLOAT * 4, my $data); # print the results: - say join ", ", unpack "f*", $data; + printf "%s\n", join ", ", unpack "f*", $data; =head2 The same enqueue operations as before, but assuming an out-of-order queue, showing off barriers. @@ -187,8 +213,8 @@ =head2 BASIC CONVENTIONS -This is not a 1:1 C-style translation of OpenCL to Perl - instead I -attempted to make the interface as type-safe as possible and introducing +This is not a one-to-one C-style translation of OpenCL to Perl - instead +I attempted to make the interface as type-safe as possible by introducing object syntax where it makes sense. There are a number of important differences between the OpenCL C API and this module: @@ -203,9 +229,12 @@ prefixes (C<< $platform->info >>). =item * OpenCL often specifies fixed vector function arguments as short -arrays (C), while this module explicitly expects the +arrays (C<$origin[3]>), while this module explicitly expects the components as separate arguments- +=item * Structures are often specified with their components, and returned +as arrayrefs. + =item * Where possible, one of the pitch values is calculated from the perl scalar length and need not be specified. @@ -247,7 +276,8 @@ =item $int = OpenCL::errno -The last error returned by a function - it's only changed on errors. +The last error returned by a function - it's only valid after an error occured +and before calling another OpenCL function. =item $str = OpenCL::err2str $errval @@ -267,7 +297,7 @@ L -=item $ctx = OpenCL::context_from_type_simple $type = OpenCL::DEVICE_TYPE_DEFAULT +=item $ctx = OpenCL::context_from_type $properties, $type = OpenCL::DEVICE_TYPE_DEFAULT, $notify = undef Tries to create a context from a default device and platform - never worked for me. @@ -297,12 +327,19 @@ Returns a list of matching OpenCL::Device objects. -=item $ctx = $platform->context_from_type_simple ($type = OpenCL::DEVICE_TYPE_DEFAULT) +=item $ctx = $platform->context_from_type ($properties, $type = OpenCL::DEVICE_TYPE_DEFAULT, $notify = undef) -Tries to create a context. Never worked for me. +Tries to create a context. Never worked for me, and you need devices explitly anyway. L +=item $ctx = $device->context ($properties = undef, @$devices, $notify = undef) + +Create a new OpenCL::Context object using the given device object(s)- a +CL_CONTEXT_PLATFORM property is supplied automatically. + +L + =back =head2 THE OpenCL::Device CLASS @@ -315,12 +352,6 @@ L -=item $ctx = $device->context_simple - -Convenience function to create a new OpenCL::Context object. - -L - =back =head2 THE OpenCL::Context CLASS @@ -438,21 +469,21 @@ L -=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...) +=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, $wait_event...) Yeah. L -=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, ...) +=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...) L. -=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, ...) +=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...) L -=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, ...) +=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...) L @@ -627,7 +658,7 @@ use common::sense; BEGIN { - our $VERSION = '0.03'; + our $VERSION = '0.14'; require XSLoader; XSLoader::load (__PACKAGE__, $VERSION);