--- OpenCL/OpenCL.pm 2011/11/17 02:10:39 1.10 +++ OpenCL/OpenCL.pm 2011/11/20 01:09:48 1.20 @@ -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::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. +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::Buffer objects (flat +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,33 +54,54 @@ http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/ +If you are into UML class diagrams, the following diagram might help - if +not, it will be mildly cobfusing: + + http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/classDiagram.html + +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 + +And here's NVIDIA's OpenCL Best Practises Guide: + + http://developer.download.nvidia.com/compute/cuda/3_2/toolkit/docs/OpenCL_Best_Practices_Guide.pdf + =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) { printf "platform: %s\n", $platform->info (OpenCL::PLATFORM_NAME); printf "extensions: %s\n", $platform->info (OpenCL::PLATFORM_EXTENSIONS); @@ -93,12 +114,19 @@ =head2 Get a useful context and a command queue. - my $dev = ((OpenCL::platforms)[0]->devices)[0]; - my $ctx = $dev->context; - 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) { print "supported image formats for ", OpenCL::enum2str $type, "\n"; @@ -126,19 +154,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); @@ -187,8 +217,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: @@ -198,16 +228,18 @@ to free objects explicitly (C), the release function is called automatically once all Perl references to it go away. -=item * OpenCL uses CamelCase for function names (C), -while this module uses underscores as word separator and often leaves out -prefixes (C<< $platform->info >>). +=item * OpenCL uses CamelCase for function names +(e.g. C, C), while this module +uses underscores as word separator and often leaves out prefixes +(C, C<< $platform->info >>). =item * OpenCL often specifies fixed vector function arguments as short arrays (C), while this module explicitly expects the -components as separate arguments- +components as separate arguments (C<$orig_x, $orig_y, $orig_z>) in +function calls. -=item * Where possible, one of the pitch values is calculated from the -perl scalar length and need not be specified. +=item * Structures are often specified by flattening out their components +as with short vectors, and returned as arrayrefs. =item * When enqueuing commands, the wait list is specified by adding extra arguments to the function - anywhere a C<$wait_events...> argument @@ -247,7 +279,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 @@ -285,23 +318,57 @@ =over 4 +=item @devices = $platform->devices ($type = OpenCL::DEVICE_TYPE_ALL) + +Returns a list of matching OpenCL::Device objects. + +=item $ctx = $platform->context_from_type ($properties, $type = OpenCL::DEVICE_TYPE_DEFAULT, $notify = undef) + +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 + =item $packed_value = $platform->info ($name) Calls C and returns the packed, raw value - for strings, this will be the string, for other values you probably need to -use the correct C. This might get improved in the future. Hopefully. +use the correct C. + +It's best to avoid this method and use one of the predefined C +methods. L -=item @devices = $platform->devices ($type = OpenCL::DEVICE_TYPE_ALL) +=for gengetinfo begin platform -Returns a list of matching OpenCL::Device objects. -=item $ctx = $platform->context_from_type ($properties, $type = OpenCL::DEVICE_TYPE_DEFAULT, $notify = undef) +=item $string = $platform->profile -Tries to create a context. Never worked for me. +Calls C with C and returns the result(s). -L +=item $string = $platform->version + +Calls C with C and returns the result(s). + +=item $string = $platform->name + +Calls C with C and returns the result(s). + +=item $string = $platform->vendor + +Calls C with C and returns the result(s). + +=item $string = $platform->extensions + +Calls C with C and returns the result(s). +=for gengetinfo end platform =back @@ -315,24 +382,12 @@ L -=item $ctx = $device->context ($properties = undef, $notify = undef) - -Create a new OpenCL::Context object. - -L - =back =head2 THE OpenCL::Context CLASS =over 4 -=item $packed_value = $ctx->info ($name) - -See C<< $platform->info >> for details. - -L - =item $queue = $ctx->queue ($device, $properties) Create a new OpenCL::Queue object from the context and the given device. @@ -355,13 +410,13 @@ Creates a new OpenCL::Buffer object and initialise it with the given data values. -=item $img = $ctx->image2d ($flags, $channel_order, $channel_type, $width, $height, $data) +=item $img = $ctx->image2d ($flags, $channel_order, $channel_type, $width, $height, $row_pitch = 0, $data = undef) Creates a new OpenCL::Image2D object and optionally initialises it with the given data values. L -=item $img = $ctx->image3d ($flags, $channel_order, $channel_type, $width, $height, $depth, $slice_pitch, $data) +=item $img = $ctx->image3d ($flags, $channel_order, $channel_type, $width, $height, $depth, $row_pitch = 0, $slice_pitch = 0, $data = undef) Creates a new OpenCL::Image3D object and optionally initialises it with the given data values. @@ -386,6 +441,16 @@ L +=item $packed_value = $ctx->info ($name) + +See C<< $platform->info >> for details. + +L + +=for gengetinfo begin context + +=for gengetinfo end context + =back =head2 THE OpenCL::Queue CLASS @@ -434,25 +499,25 @@ L -=item $ev = $queue->enqueue_write_image ($src, $blocking, $x, $y, $z, $width, $height, $depth, $row_pitch, $data, $wait_events...) +=item $ev = $queue->enqueue_write_image ($src, $blocking, $x, $y, $z, $width, $height, $depth, $row_pitch, $slice_pitch, $data, $wait_events...) 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, $dst_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 @@ -516,6 +581,23 @@ =back +=head2 THE OpenCL::Image CLASS + +This is the superclass of all image objects - OpenCL::Image2D and OpenCL::Image3D. + +=over 4 + +=item $packed_value = $ev->image_info ($name) + +See C<< $platform->info >> for details. + +The reason this method is not called C is that there already is an +C<< ->info >> method inherited from C. + +L + +=back + =head2 THE OpenCL::Sampler CLASS =over 4 @@ -570,6 +652,15 @@ L +=item $packed_value = $kernel->work_group_info ($device, $name) + +See C<< $platform->info >> for details. + +The reason this method is not called C is that there already is an +C<< ->info >> method. + +L + =item $kernel->set_TYPE ($index, $value) This is a family of methods to set the kernel argument with the number C<$index> to the give C<$value>. @@ -600,6 +691,15 @@ L +=item $packed_value = $ev->profiling_info ($name) + +See C<< $platform->info >> for details. + +The reason this method is not called C is that there already is an +C<< ->info >> method. + +L + =item $ev->wait Waits for the event to complete. @@ -627,7 +727,7 @@ use common::sense; BEGIN { - our $VERSION = '0.03'; + our $VERSION = '0.15'; require XSLoader; XSLoader::load (__PACKAGE__, $VERSION);