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

Comparing OpenCL/OpenCL.pm (file contents):
Revision 1.53 by root, Tue Apr 24 14:30:57 2012 UTC vs.
Revision 1.57 by root, Tue Apr 24 23:58:34 2012 UTC

43 43
44OpenCL::Event objects are used to signal when something is complete. 44OpenCL::Event objects are used to signal when something is complete.
45 45
46=head2 HELPFUL RESOURCES 46=head2 HELPFUL RESOURCES
47 47
48The OpenCL spec used to develop this module (1.2 spec was available, but 48The OpenCL specs used to develop this module:
49no implementation was available to me :).
50 49
51 http://www.khronos.org/registry/cl/specs/opencl-1.1.pdf 50 http://www.khronos.org/registry/cl/specs/opencl-1.1.pdf
51 http://www.khronos.org/registry/cl/specs/opencl-1.2.pdf
52 http://www.khronos.org/registry/cl/specs/opencl-1.2-extensions.pdf
52 53
53OpenCL manpages: 54OpenCL manpages:
54 55
55 http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/ 56 http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/
57 http://www.khronos.org/registry/cl/sdk/1.2/docs/man/xhtml/
56 58
57If you are into UML class diagrams, the following diagram might help - if 59If you are into UML class diagrams, the following diagram might help - if
58not, it will be mildly cobfusing: 60not, it will be mildly confusing (also, the class hierarchy of this module
61is much more fine-grained):
59 62
60 http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/classDiagram.html 63 http://www.khronos.org/registry/cl/sdk/1.2/docs/man/xhtml/classDiagram.html
61 64
62Here's a tutorial from AMD (very AMD-centric, too), not sure how useful it 65Here's a tutorial from AMD (very AMD-centric, too), not sure how useful it
63is, but at least it's free of charge: 66is, but at least it's free of charge:
64 67
65 http://developer.amd.com/zones/OpenCLZone/courses/Documents/Introduction_to_OpenCL_Programming%20Training_Guide%20%28201005%29.pdf 68 http://developer.amd.com/zones/OpenCLZone/courses/Documents/Introduction_to_OpenCL_Programming%20Training_Guide%20%28201005%29.pdf
376 379
377For this to work, the OpenGL library must be loaded, a GLX context must 380For this to work, the OpenGL library must be loaded, a GLX context must
378have been created and be made current, and C<dlsym> must be available and 381have been created and be made current, and C<dlsym> must be available and
379capable of finding the function via C<RTLD_DEFAULT>. 382capable of finding the function via C<RTLD_DEFAULT>.
380 383
384=head2 EVENT SYSTEM
385
386OpenCL can generate a number of (potentially) asynchronous events, for
387example, after compiling a program, to signal a context-related error or,
388perhaps most important, to signal completion of queued jobs (by setting
389callbacks on OpenCL::Event objects).
390
391To facilitate this, this module maintains an event queue - each
392time an asynchronous event happens, it is queued, and perl will be
393interrupted. This is implemented via the L<Async::Interrupt> module. In
394addition, this module has L<AnyEvent> support, so it can seamlessly
395integrate itself into many event loops.
396
397Since this module is a bit hard to understand, here are some case examples:
398
399=head3 Don't use callbacks.
400
401When your program never uses any callbacks, then there will never be any
402notifications you need to take care of, and therefore no need to worry
403about all this.
404
405You can achieve a great deal by explicitly waiting for events, or using
406barriers and flush calls. In many programs, there is no need at all to
407tinker with asynchronous events.
408
409=head3 Use AnyEvent
410
411This module automatically registers a watcher that invokes all outstanding
412event callbacks when AnyEvent is initialised (and block asynchronous
413interruptions). Using this mode of operations is the safest and most
414recommended one.
415
416To use this, simply use AnyEvent and this module normally, make sure you
417have an event loop running:
418
419 use Gtk2 -init;
420 use AnyEvent;
421
422 # initialise AnyEvent, by creating a watcher, or:
423 AnyEvent::detect;
424
425 my $e = $queue->enqueue_marker;
426 $e->cb (sub {
427 warn "opencl is finished\n";
428 })
429
430 main Gtk2;
431
432Note that this module will not initialise AnyEvent for you. Before
433AnyEvent is initialised, the module will asynchronously interrupt perl
434instead. To avoid any surprises, it's best to explicitly initialise
435AnyEvent.
436
437You can temporarily enable asynchronous interruptions (see next paragraph)
438by calling C<$OpenCL::INTERRUPT->unblock> and disable them again by
439calling C<$OpenCL::INTERRUPT->block>.
440
441=head3 Let yourself be interrupted at any time
442
443This mode is the default unless AnyEvent is loaded and initialised. In
444this mode, OpenCL asynchronously interrupts a running perl program. The
445emphasis is on both I<asynchronously> and I<running> here.
446
447Asynchronously means that perl might execute your callbacks at any
448time. For example, in the following code (I<THAT YOU SHOULD NOT COPY>),
449the C<until> loop following the marker call will be interrupted by the
450callback:
451
452 my $e = $queue->enqueue_marker;
453 my $flag;
454 $e->cb (sub { $flag = 1 });
455 1 until $flag;
456 # $flag is now 1
457
458The reason why you shouldn't blindly copy the above code is that
459busy waiting is a really really bad thing, and really really bad for
460performance.
461
462While at first this asynchronous business might look exciting, it can be
463really hard, because you need to be prepared for the callback code to be
464executed at any time, which limits the amount of things the callback code
465can do safely.
466
467This can be mitigated somewhat by using C<<
468$OpenCL::INTERRUPT->scope_block >> (see the L<Async::Interrupt>
469documentation for details).
470
471The other problem is that your program must be actively I<running> to be
472interrupted. When you calculate stuff, your program is running. When you
473hang in some C functions or other block execution (by calling C<sleep>,
474C<select>, running an event loop and so on), your program is waiting, not
475running.
476
477One way around that would be to attach a read watcher to your event loop,
478listening for events on C<< $OpenCL::INTERRUPT->pipe_fileno >>, using a
479dummy callback (C<sub { }>) to temporarily execute some perl code.
480
481That is then awfully close to using the built-in AnyEvent support above,
482though, so consider that one instead.
483
484=head3 Be creative
485
486OpenCL exports the L<Async::Interrupt> object it uses in the global
487variable C<$OpenCL::INTERRUPT>. You can configure it in any way you like.
488
489So if you want to feel like a real pro, err, wait, if you feel no risk
490menas no fun, you can experiment by implementing your own mode of
491operations.
492
381=cut 493=cut
382 494
383package OpenCL; 495package OpenCL;
384 496
385use common::sense; 497use common::sense;
498use Async::Interrupt ();
499
500our $POLL_FUNC; # set by XS
386 501
387BEGIN { 502BEGIN {
388 our $VERSION = '0.96'; 503 our $VERSION = '0.97';
389 504
390 require XSLoader; 505 require XSLoader;
391 XSLoader::load (__PACKAGE__, $VERSION); 506 XSLoader::load (__PACKAGE__, $VERSION);
392 507
393 @OpenCL::Platform::ISA = 508 @OpenCL::Platform::ISA =
440 555
441Returns all available OpenCL::Platform objects. 556Returns all available OpenCL::Platform objects.
442 557
443L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetPlatformIDs.html> 558L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetPlatformIDs.html>
444 559
445=item $ctx = OpenCL::context_from_type $properties, $type = OpenCL::DEVICE_TYPE_DEFAULT, $notify = undef 560=item $ctx = OpenCL::context_from_type $properties, $type = OpenCL::DEVICE_TYPE_DEFAULT, $callback->($err, $pvt) = $print_stderr
446 561
447Tries to create a context from a default device and platform - never worked for me. 562Tries to create a context from a default device and platform type - never worked for me.
448 563
449L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateContextFromType.html> 564L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateContextFromType.html>
450 565
566=item $ctx = OpenCL::context $properties, \@devices, $callback->($err, $pvt) = $print_stderr)
567
568Create a new OpenCL::Context object using the given device object(s). This
569function isn't implemented yet, use C<< $platform->context >> instead.
570
571L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateContext.html>
572
451=item OpenCL::wait_for_events $wait_events... 573=item OpenCL::wait_for_events $wait_events...
452 574
453Waits for all events to complete. 575Waits for all events to complete.
454 576
455L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clWaitForEvents.html> 577L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clWaitForEvents.html>
578
579=item OpenCL::poll
580
581Checks if there are any outstanding events (see L<EVENT SYSTEM>) and
582invokes their callbacks.
583
584=item $OpenCL::INTERRUPT
585
586The L<Async::Interrupt> object used to signal asynchronous events (see
587L<EVENT SYSTEM>).
588
589=cut
590
591our $INTERRUPT = new Async::Interrupt c_cb => [$POLL_FUNC, 0];
592
593&_eq_initialise ($INTERRUPT->signal_func);
594
595=item $OpenCL::WATCHER
596
597The L<AnyEvent> watcher object used to watch for asynchronous events (see
598L<EVENT SYSTEM>). This variable is C<undef> until L<AnyEvent> has been
599loaded I<and> initialised (e.g. by calling C<AnyEvent::detect>).
600
601=cut
602
603our $WATCHER;
604
605sub _init_anyevent {
606 $INTERRUPT->block;
607 $WATCHER = AE::io ($INTERRUPT->pipe_fileno, 0, sub { $INTERRUPT->handle });
608}
609
610if (defined $AnyEvent::MODEL) {
611 _init_anyevent;
612} else {
613 push @AnyEvent::post_detect, \&_init_anyevent;
614}
456 615
457=back 616=back
458 617
459=head2 THE OpenCL::Object CLASS 618=head2 THE OpenCL::Object CLASS
460 619
472C<IV> type in your code and cast that to the correct type. 631C<IV> type in your code and cast that to the correct type.
473 632
474=cut 633=cut
475 634
476sub OpenCL::Object::id { 635sub OpenCL::Object::id {
636 ref $_[0] eq "SCALAR"
477 ${$_[0]} 637 ? ${ $_[0] }
638 : $_[0][0]
478} 639}
479 640
480=back 641=back
481 642
482=head2 THE OpenCL::Platform CLASS 643=head2 THE OpenCL::Platform CLASS
485 646
486=item @devices = $platform->devices ($type = OpenCL::DEVICE_TYPE_ALL) 647=item @devices = $platform->devices ($type = OpenCL::DEVICE_TYPE_ALL)
487 648
488Returns a list of matching OpenCL::Device objects. 649Returns a list of matching OpenCL::Device objects.
489 650
490=item $ctx = $platform->context_from_type ($properties, $type = OpenCL::DEVICE_TYPE_DEFAULT, $notify = undef) 651=item $ctx = $platform->context_from_type ($properties, $type = OpenCL::DEVICE_TYPE_DEFAULT, $callback->($err, $pvt) = $print_stderr)
491 652
492Tries to create a context. Never worked for me, and you need devices explicitly anyway. 653Tries to create a context. Never worked for me, and you need devices explicitly anyway.
493 654
494L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateContextFromType.html> 655L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateContextFromType.html>
495 656
496=item $ctx = $platform->context ($properties = undef, @$devices, $notify = undef) 657=item $ctx = $platform->context ($properties, \@devices, $callback->($err, $pvt) = $print_stderr)
497 658
498Create a new OpenCL::Context object using the given device object(s)- a 659Create a new OpenCL::Context object using the given device object(s)- a
499CL_CONTEXT_PLATFORM property is supplied automatically. 660CL_CONTEXT_PLATFORM property is supplied automatically.
500 661
501L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateContext.html> 662L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clCreateContext.html>
841=cut 1002=cut
842 1003
843sub OpenCL::Context::build_program { 1004sub OpenCL::Context::build_program {
844 my ($self, $prog, $options) = @_; 1005 my ($self, $prog, $options) = @_;
845 1006
1007 require Carp;
1008
846 $prog = $self->program_with_source ($prog) 1009 $prog = $self->program_with_source ($prog)
847 unless ref $prog; 1010 unless ref $prog;
848 1011
1012 # we build separately per device so we instantly know which one failed
849 for my $dev ($self->devices) { 1013 for my $dev ($self->devices) {
850 eval { $prog->build ($dev, $options); 1 } 1014 eval { $prog->build ([$dev], $options); 1 }
851 or Carp::croak "Building OpenCL program for device '" . $dev->name . "' failed:\n" 1015 or Carp::croak ("Building OpenCL program for device '" . $dev->name . "' failed:\n"
852 . $prog->build_log ($dev); 1016 . $prog->build_log ($dev));
853 } 1017 }
854 1018
855 $prog 1019 $prog
856} 1020}
857 1021
1080 1244
1081=item $ev = $queue->enqueue_task ($kernel, $wait_events...) 1245=item $ev = $queue->enqueue_task ($kernel, $wait_events...)
1082 1246
1083L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueTask.html> 1247L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueTask.html>
1084 1248
1085=item $ev = $queue->enqueue_nd_range_kernel ($kernel, @$global_work_offset, @$global_work_size, @$local_work_size, $wait_events...) 1249=item $ev = $queue->enqueue_nd_range_kernel ($kernel, \@global_work_offset, \@global_work_size, \@local_work_size, $wait_events...)
1086 1250
1087Enqueues a kernel execution. 1251Enqueues a kernel execution.
1088 1252
1089@$global_work_size must be specified as a reference to an array of 1253\@global_work_size must be specified as a reference to an array of
1090integers specifying the work sizes (element counts). 1254integers specifying the work sizes (element counts).
1091 1255
1092@$global_work_offset must be either C<undef> (in which case all offsets 1256\@global_work_offset must be either C<undef> (in which case all offsets
1093are C<0>), or a reference to an array of work offsets, with the same number 1257are C<0>), or a reference to an array of work offsets, with the same number
1094of elements as @$global_work_size. 1258of elements as \@global_work_size.
1095 1259
1096@$local_work_size must be either C<undef> (in which case the 1260\@local_work_size must be either C<undef> (in which case the
1097implementation is supposed to choose good local work sizes), or a 1261implementation is supposed to choose good local work sizes), or a
1098reference to an array of local work sizes, with the same number of 1262reference to an array of local work sizes, with the same number of
1099elements as @$global_work_size. 1263elements as \@global_work_size.
1100 1264
1101L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueNDRangeKernel.html> 1265L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueNDRangeKernel.html>
1102 1266
1103=item $ev = $queue->enqueue_acquire_gl_objects ([object, ...], $wait_events...) 1267=item $ev = $queue->enqueue_acquire_gl_objects ([object, ...], $wait_events...)
1104 1268
1349 1513
1350=head2 THE OpenCL::Program CLASS 1514=head2 THE OpenCL::Program CLASS
1351 1515
1352=over 4 1516=over 4
1353 1517
1354=item $program->build ($device, $options = "") 1518=item $program->build (\@devices = undef, $options = "", $cb->($program) = undef)
1355 1519
1356Tries to build the program with the given options. See also the 1520Tries to build the program with the given options. See also the
1357C<$ctx->build> convenience function. 1521C<$ctx->build> convenience function.
1358 1522
1523If a callback is specified, then it will be called when compilation is
1524finished. Note that many OpenCL implementations block your program while
1525compiling whether you use a callback or not. See C<build_async> if you
1526want to make sure the build is done in the background.
1527
1528Note that some OpenCL implementations atc up badly, and don't call the
1529callback in some error cases (but call it in others). This implementation
1530assumes the callback will always be called, and leaks memory if this is
1531not so. So best make sure you don't pass in invalid values.
1532
1359L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clBuildProgram.html> 1533L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clBuildProgram.html>
1534
1535=item $program->build_async (\@devices = undef, $options = "", $cb->($program) = undef)
1536
1537Similar to C<< ->build >>, except it starts a thread, and never fails (you
1538need to check the compilation status form the callback, or by polling).
1360 1539
1361=item $packed_value = $program->build_info ($device, $name) 1540=item $packed_value = $program->build_info ($device, $name)
1362 1541
1363Similar to C<< $platform->info >>, but returns build info for a previous 1542Similar to C<< $platform->info >>, but returns build info for a previous
1364build attempt for the given device. 1543build attempt for the given device.
1541 1720
1542Waits for the event to complete. 1721Waits for the event to complete.
1543 1722
1544L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clWaitForEvents.html> 1723L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clWaitForEvents.html>
1545 1724
1725=item $ev->cb ($exec_callback_type, $callback->($event, $event_command_exec_status))
1726
1727Adds a callback to the callback stack for the given event type. There is
1728no way to remove a callback again.
1729
1730L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clSetEventCallback.html>
1731
1546=item $packed_value = $ev->info ($name) 1732=item $packed_value = $ev->info ($name)
1547 1733
1548See C<< $platform->info >> for details. 1734See C<< $platform->info >> for details.
1549 1735
1550L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetEventInfo.html> 1736L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetEventInfo.html>
1610 1796
1611=over 4 1797=over 4
1612 1798
1613=item $ev->set_status ($execution_status) 1799=item $ev->set_status ($execution_status)
1614 1800
1801Sets the execution status of the user event. Can only be called once,
1802either with OpenCL::COMPLETE or a negative number as status.
1803
1615L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clSetUserEventStatus.html> 1804L<http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clSetUserEventStatus.html>
1616 1805
1617=back 1806=back
1618 1807
1619=cut 1808=cut

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines