--- OpenCL/OpenCL.pm 2011/11/17 02:10:39 1.10 +++ OpenCL/OpenCL.pm 2011/11/17 02:54:14 1.11 @@ -20,14 +20,14 @@ 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 @@ -56,31 +56,38 @@ =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 (e.g. the first -device you can find), and create a context from those. +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. -Create a command queue from your context, and program objects from your -OpenCL source code, build the programs. +Create program objects from your OpenCL source code, then build (compile) +the programs for each device you want to run them on. -Create kernel objects for all kernels you want to use. +Create kernel objects for all kernels you want to use (surprisingly, these +are not device-specific). -Then, to execute stuff, you repeat this: +Then, to execute stuff, you repeat these steps, possibly resuing or +sharing some buffers: -Create some input and output buffers from your context. Initialise the -input buffers with data. Set these as arguments to your kernel. +Create some input and output buffers from your context. 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 +100,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"; @@ -133,12 +147,14 @@ 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); @@ -247,7 +263,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 @@ -299,10 +316,17 @@ =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 +339,6 @@ L -=item $ctx = $device->context ($properties = undef, $notify = undef) - -Create a new OpenCL::Context object. - -L - =back =head2 THE OpenCL::Context CLASS