| 1 | package Peta::NN::Parallel; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: independent pieces of work on several cores | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # Independent pieces of work on several cores: each task runs in a process | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | # of its own (Parallel::ForkManager) and hands its result back as plain data. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # This is coarse on purpose. Training runs of different models, of different | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # widths of one model, or of one width on different seeds have nothing to do | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # with each other, and each takes seconds to minutes; a process per run costs | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | # nothing beside that. A child starts as a copy of its parent, so what the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # parent has loaded (word lists, training pairs) is there without being sent. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 17 | ▶ | use v5.36; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 17 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 17 | our $VERSION = '0.2610090'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 17 | our @EXPORT_OK = qw(in_parallel workers); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # How many processes to use when the caller leaves it open: PETA_NN_WORKERS, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # or 1, which is no parallelism at all. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 12 | ▶ | sub workers () { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
workers: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 12 | ▶ | my $asked = $ENV{PETA_NN_WORKERS} // 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$ENV{"PETA_NN_WORKERS"} // 1
MC/DC100%1/1
$ENV{"PETA_NN_WORKERS"} // 1
$ENV{"PETA_NN_WORKERS"}
Truth table100%2/2
A
A= $ENV{"PETA_NN_WORKERS"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 12 | ▶ | die "PETA_NN_WORKERS is a whole number of 1 or more, not '$asked'\n" if $asked !~ /\A[1-9][0-9]*\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 9 | return $asked; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # Run the tasks, at most $workers at a time, and return what they returned, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # in the order of the tasks. A task is a sub; what it returns must be plain | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # data (no objects, no code), since it crosses from one process to another. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # With one worker, or one task, everything runs here, in order. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # A task that dies takes the whole call with it, after the others are done. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 51 | ▶ | sub in_parallel ($workers, @tasks) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
in_parallel: called CC=9 SCAR=22.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 51 | ▶ | return map { scalar $_->() } @tasks if $workers <= 1 || @tasks <= 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$workers <= 1 or @tasks <= 1
MC/DC100%2/2
$workers <= 1 or @tasks <= 1
$workers <= 1
@tasks <= 1
Truth table100%3/3
A or B
A= $workers <= 1B= @tasks <= 1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 32 | require Parallel::ForkManager; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 32 | ▶ | my $manager = Parallel::ForkManager->new($workers < @tasks ? $workers : scalar @tasks); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 32 | my (@result, @failed); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 96 | ▶ | $manager->run_on_finish(sub ($pid, $code, $n, $signal, $core, $data) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=23.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 96 | ▶ | if ($signal || $code || !$data) { push @failed, "task $n ended with " . ($signal ? "signal $signal" : "status $code") } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition33%2/6
$signal or $code or not $data
$signal or $code
MC/DC0%0/3
$signal or $code or not $data
$signal
$code
not $data
Truth table25%1/4
A or B or not(C)
A= $signalB= $codeC= not $data
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 4 | elsif (defined $data->{error}) { push @failed, "task $n: $data->{error}" } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 92 | else { $result[$n] = $data->{value} } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 32 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 32 | for my $n (0 .. $#tasks) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 119 | ▶ | $manager->start($n) and next; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 14 | my $value = eval { scalar $tasks[$n]->() }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 14 | ▶ | $manager->finish(0, defined $value || !$@ ? { value => $value } : { error => $@ =~ s/\s+\z//r }); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $value or not $@
MC/DC100%2/2
defined $value or not $@
defined $value
not $@
Truth table100%3/3
A or not(B)
A= defined $valueB= not $@
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 18 | $manager->wait_all_children; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 18 | ▶ | die join("\n", @failed) . "\n" if @failed; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 15 | return @result; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 17 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||