iaw4 has asked for the wisdom of the Perl Monks concerning the following question:

I am (still) wondering whether there is something like apple's grand central dispatch for perl. perl seems well suited to it, too. Syntax could be something like:

sub runme { my $arg=shift; # do something very long that has no side effects return @stuff; } sub a { runme(1); } sub b { runme(2); } sub c { runme(3); } my @results = grandcentral_like_runall( \a, \b, \c );
grandcentral_like_runall then runs these subroutines on as many cores as the processor has, and continues on only after all three are complete.

Does some package with this sort of functionality already exist?

/iaw

Replies are listed 'Best First'.
Re: grand central dispatch like mechanism?
by BrowserUk (Patriarch) on Feb 17, 2010 at 16:36 UTC

    No. But it is trivial to write your own:

    #! perl -slw use strict; use threads; sub grandCentral { my @t = map threads->create( $_ ), @_; return map $_->join, @t; } sub runme { my $arg = shift; sleep $arg * 2; return $arg; } sub a{ runme( 1 ) } sub b{ runme( 2 ) } sub c{ runme( 3 ) } sub d{ runme( 4 ) } my $start = time; my @results = grandCentral( \&a, \&b, \&c, \&d ); print for @results; printf "Took %d seconds\n", time - $start; __END__ C:\test>centralDispatch.pl 1 2 3 4 Took 8 seconds

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      thanks, superdoc. this is 95% of the functionality. the other 5% is intelligent queuing---with 2 processors, first run 2 processes, then run the next 2 processes...
        this is 95% of the functionality. the other 5% is intelligent queuing-

        No. That code will be intelligently dispatched by the OS kernel scheduler. As coded, it will still only take 8 seconds to run even if you only have a single processor.

        It is really hard to explain how it does that if you do not understand Timeslicing & Preemptive Multitasking, but trust me, it works.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: grand central dispatch like mechanism?
by Corion (Patriarch) on Feb 17, 2010 at 15:25 UTC

    I use Dominus' runN to run many instances of a program in parallel.