--- OpenCL/OpenCL.pm 2012/04/19 19:36:24 1.38 +++ OpenCL/OpenCL.pm 2012/04/24 14:30:57 1.53 @@ -159,12 +159,7 @@ } '; - 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_log; - + my $prog = $ctx->build_program ($src); my $kernel = $prog->kernel ("squareit"); =head2 Create some input and output float buffers, then call the @@ -262,17 +257,15 @@ float2 z = m; float2 c = (float2)(sin (time * 0.05005), cos (time * 0.06001)); - for (int i = 0; i < 100 && dot (z, z) < 4.f; ++i) + for (int i = 0; i < 25 && dot (z, z) < 4.f; ++i) z = (float2)(z.x * z.x - z.y * z.y, 2.f * z.x * z.y) + c; float3 colour = (float3)(z.x, z.y, z.x * z.y); write_imagef (img, (int2)(get_global_id (0), get_global_id (1)), (float4)(colour * p.x * p.x, 1.)); } EOF - my $prog = $ctx->program_with_source ($src); - eval { $prog->build ($dev); 1 } - or die $prog->build_log ($dev); + my $prog = $ctx->build_program ($src); my $kernel = $prog->kernel ("juliatunnel"); # program compiled, kernel ready, now draw and loop @@ -290,7 +283,7 @@ $queue->enqueue_release_gl_objects ([$tex]); # wait - $queue->flush; + $queue->finish; # now draw the texture, the defaults should be all right glTexParameterf GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; @@ -338,7 +331,9 @@ =item * When enqueuing commands, the wait list is specified by adding extra arguments to the function - anywhere a C<$wait_events...> argument -is documented this can be any number of event objects. +is documented this can be any number of event objects. As an extsnion +implemented by this module, C values will be ignored in the event +list. =item * When enqueuing commands, if the enqueue method is called in void context, no event is created. In all other contexts an event is returned @@ -383,6 +378,43 @@ have been created and be made current, and C must be available and capable of finding the function via C. +=cut + +package OpenCL; + +use common::sense; + +BEGIN { + our $VERSION = '0.96'; + + require XSLoader; + XSLoader::load (__PACKAGE__, $VERSION); + + @OpenCL::Platform::ISA = + @OpenCL::Device::ISA = + @OpenCL::Context::ISA = + @OpenCL::Queue::ISA = + @OpenCL::Memory::ISA = + @OpenCL::Sampler::ISA = + @OpenCL::Program::ISA = + @OpenCL::Kernel::ISA = + @OpenCL::Event::ISA = OpenCL::Object::; + + @OpenCL::Buffer::ISA = + @OpenCL::Image::ISA = OpenCL::Memory::; + + @OpenCL::BufferObj::ISA = OpenCL::Buffer::; + + @OpenCL::Image2D::ISA = + @OpenCL::Image3D::ISA = + @OpenCL::Image2DArray::ISA = + @OpenCL::Image1D::ISA = + @OpenCL::Image1DArray::ISA = + @OpenCL::Image1DBuffer::ISA = OpenCL::Image::; + + @OpenCL::UserEvent::ISA = OpenCL::Event::; +} + =head2 THE OpenCL PACKAGE =over 4 @@ -424,6 +456,29 @@ =back +=head2 THE OpenCL::Object CLASS + +This is the base class for all objects in the OpenCL module. The only +method it implements is the C method, which is only useful if you want +to interface to OpenCL on the C level. + +=over 4 + +=item $iv = $obj->id + +OpenCL objects are represented by pointers or integers on the C level. If +you want to interface to an OpenCL object directly on the C level, then +you need this value, which is returned by this method. You should use an +C type in your code and cast that to the correct type. + +=cut + +sub OpenCL::Object::id { + ${$_[0]} +} + +=back + =head2 THE OpenCL::Platform CLASS =over 4 @@ -456,6 +511,13 @@ L +=item $platform->unload_compiler + +Attempts to unload the compiler for this platform, for endless +profit. Does nothing on OpenCL 1.1. + +L + =for gengetinfo begin platform =item $string = $platform->profile @@ -750,9 +812,9 @@ Calls C with C and returns the result. -=item $uint = $device->reference_count_ext +=item $uint = $device->reference_count_ext -Calls C with C and returns the result. +Calls C with C and returns the result. =item @device_partition_property_exts = $device->partition_style_ext @@ -766,12 +828,43 @@ =over 4 +=item $prog = $ctx->build_program ($program, $options = "") + +This convenience function tries to build the program on all devices in +the context. If the build fails, then the function will C with the +build log. Otherwise ti returns the program object. + +The C<$program> can either be a C object or a string +containing the program. In the latter case, a program objetc will be +created automatically. + +=cut + +sub OpenCL::Context::build_program { + my ($self, $prog, $options) = @_; + + $prog = $self->program_with_source ($prog) + unless ref $prog; + + for my $dev ($self->devices) { + eval { $prog->build ($dev, $options); 1 } + or Carp::croak "Building OpenCL program for device '" . $dev->name . "' failed:\n" + . $prog->build_log ($dev); + } + + $prog +} + =item $queue = $ctx->queue ($device, $properties) Create a new OpenCL::Queue object from the context and the given device. L +Example: create an out-of-order queue. + + $queue = $ctx->queue ($device, OpenCL::QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE); + =item $ev = $ctx->user_event Creates a new OpenCL::UserEvent object. @@ -790,6 +883,13 @@ Creates a new OpenCL::Buffer (actually OpenCL::BufferObj) object and initialise it with the given data values. +=item $img = $ctx->image ($self, $flags, $channel_order, $channel_type, $type, $width, $height, $depth, $array_size = 0, $row_pitch = 0, $slice_pitch = 0, $num_mip_level = 0, $num_samples = 0, $*data = &PL_sv_undef) + +Creates a new OpenCL::Image object and optionally initialises it with +the given data values. + +L + =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 @@ -811,14 +911,21 @@ http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateFromGLBuffer.html -=item $ctx->gl_texture2d ($flags, $target, $miplevel, $texture) +=item $img = $ctx->gl_texture ($flags, $target, $miplevel, $texture) + +Creates a new OpenCL::Image object that refers to the given OpenGL +texture object or buffer. + +http://www.khronos.org/registry/cl/sdk/1.2/docs/man/xhtml/clCreateFromGLTexture.html + +=item $img = $ctx->gl_texture2d ($flags, $target, $miplevel, $texture) Creates a new OpenCL::Image2D object that refers to the given OpenGL 2D texture object. http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateFromGLTexture2D.html -=item $ctx->gl_texture3d ($flags, $target, $miplevel, $texture) +=item $img = $ctx->gl_texture3d ($flags, $target, $miplevel, $texture) Creates a new OpenCL::Image3D object that refers to the given OpenGL 3D texture object. @@ -891,11 +998,17 @@ They also allow you to specify any number of other event objects that this request has to wait for before it starts executing, by simply passing the -event objects as extra parameters to the enqueue methods. +event objects as extra parameters to the enqueue methods. To simplify +program design, this module ignores any C values in the list of +events. This makes it possible to code operations such as this, without +having to put a valid event object into C<$event> first: + + $event = $queue->enqueue_xxx (..., $event); Queues execute in-order by default, without any parallelism, so in most cases (i.e. you use only one queue) it's not necessary to wait for or -create event objects. +create event objects, althoguh an our of order queue is often a bit +faster. =over 4 @@ -949,6 +1062,22 @@ L. +=item $ev = $queue->enqueue_fill_buffer ($mem, $pattern, $offset, $size, ...) + +Fills the given buffer object with repeated applications of C<$pattern>, +starting at C<$offset> for C<$size> octets. + +L + +=item $ev = $queue->enqueue_fill_image ($img, $r, $g, $b, $a, $x, $y, $z, $width, $height, $depth, ...) + +Fills the given image area with the given rgba colour components. The +components are normally floating point values between C<0> and C<1>, +except when the image channel data type is a signe dor unsigned +unnormalised format, in which case the range is determined by the format. + +L + =item $ev = $queue->enqueue_task ($kernel, $wait_events...) L @@ -971,10 +1100,6 @@ L -=item $ev = $queue->enqueue_marker ($wait_events...) - -L - =item $ev = $queue->enqueue_acquire_gl_objects ([object, ...], $wait_events...) Enqueues a list (an array-ref of OpenCL::Memory objects) to be acquired @@ -993,9 +1118,13 @@ L -=item $queue->enqueue_barrier +=item $ev = $queue->enqueue_marker ($wait_events...) + +L -L +=item $ev = $queue->enqueue_barrier ($wait_events...) + +L =item $queue->flush @@ -1120,11 +1249,13 @@ =head2 THE OpenCL::Image CLASS -This is the superclass of all image objects - OpenCL::Image2D and OpenCL::Image3D. +This is the superclass of all image objects - OpenCL::Image1D, +OpenCL::Image1DArray, OpenCL::Image1DBuffer, OpenCL::Image2D, +OpenCL::Image2DArray and OpenCL::Image3D. =over 4 -=item $packed_value = $ev->image_info ($name) +=item $packed_value = $image->image_info ($name) See C<< $platform->info >> for details. @@ -1133,6 +1264,11 @@ L +=item ($channel_order, $channel_data_type) = $image->format + +Returns the channel order and type used to create the image by calling +C with C. + =for gengetinfo begin image =item $int = $image->element_size @@ -1217,7 +1353,8 @@ =item $program->build ($device, $options = "") -Tries to build the program with the givne options. +Tries to build the program with the given options. See also the +C<$ctx->build> convenience function. L @@ -1235,6 +1372,12 @@ L +=item @kernels = $program->kernels_in_program + +Returns all kernels successfully compiled for all devices in program. + +http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateKernelsInProgram.html + =for gengetinfo begin program_build =item $build_status = $program->build_status ($device) @@ -1371,12 +1514,17 @@ TYPE is one of C, C, C, C, C, C, C, C, C, C, C, C, C, -C, C, C or C. +C, C, C, C or C. Chars and integers (including the half type) are specified as integers, float and double as floating point values, memory/buffer/image2d/image3d -must be an object of that type or C, and sampler and event must be -objects of that type. +must be an object of that type or C, local-memory arguments are +set by specifying the size, and sampler and event must be objects of that +type. + +Setting an argument for a kernel does NOT keep a reference to the object - +for example, if you set an argument to some image object, free the image, +and call the kernel, you will run into undefined behaviour. L @@ -1470,27 +1618,6 @@ =cut -package OpenCL; - -use common::sense; - -BEGIN { - our $VERSION = '0.95'; - - require XSLoader; - XSLoader::load (__PACKAGE__, $VERSION); - - @OpenCL::Buffer::ISA = - @OpenCL::Image::ISA = OpenCL::Memory::; - - @OpenCL::BufferObj::ISA = OpenCL::Buffer::; - - @OpenCL::Image2D::ISA = - @OpenCL::Image3D::ISA = OpenCL::Image::; - - @OpenCL::UserEvent::ISA = OpenCL::Event::; -} - 1; =head1 AUTHOR