Parallel::Loops

Execute loops using parallel forked subprocesses
Download

Parallel::Loops Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Publisher Name:
  • Peter Valdemar M?rch
  • Publisher web site:
  • http://search.cpan.org/~pmorch/

Parallel::Loops Tags


Parallel::Loops Description

Execute loops using parallel forked subprocesses Often a loop performs calculations where each iteration of the loop does not depend on the previous iteration, and the iterations really could be carried out in any order.Parallel::Loops is a Perl module that allows you to run such loops in parallel using all the CPUs at your disposal.Return values are automatically transfered from children to parents via %hashes or @arrays, that have explicitly been configured for that sort of sharing via $pl->share(). Hashes will transfer keys that are set in children (but not cleared or unset), and elements that are pushed to @arrays in children are pushed to the parent @array too (but note that the order is not guaranteed to be the same as it would have been if done all in one process, since there is no way of knowing which child would finish first!)If you can see past the slightly awkward syntax, you're basically getting foreach and while loops that can run in parallel without having to bother with fork, pipes, signals etc. This is all handled for you by this module.SYNOPSIS use Parallel::Loops; my $maxProcs = 5; my $pl = Parallel::Loops->new($maxProcs); my @parameters = ( 0 .. 9 ); # We want to perform some hefty calculation for each @input and # store each calculation's result in %output. For that reason, we # "tie" %output, so that changes to %output in any child process # (see below) are automatically transfered and updated in the # parent also. my %returnValues; $pl->share( \%returnValues ); $pl->foreach( \@parameters, sub { # This sub "magically" executed in parallel forked child # processes # Lets just create a simple example, but this could be a # massive calculation that will be parallelized, so that # $maxProcs different processes are calculating sqrt # simultaneously for different values of $_ on different CPUs $returnValues{$_} = sqrt($_); }); foreach (@parameters) { printf "i: %d sqrt(i): %f\n", $_, $returnValues{$_}; }You can also use @arrays instead of %hashes, and/or while loops instead of foreach: my @returnValues; $pl->share(\@returnValues); my $i = 0; $pl->while ( sub { $i++ < 10 }, sub { # This sub "magically" executed in parallel forked # child processes push @returnValues, ; });And you can have both foreach and while return values so that $pl->share() isn't required at all: my $maxProcs = 5; my $pl = Parallel::Loops->new($maxProcs); my %returnValues = $pl->foreach( , sub { # Again, this is executed in a forked child $_ => sqrt($_); }); Requirements: · Perl


Parallel::Loops Related Software