lib/Peta/NN/Parallel.pm

filter: Statementstmt 96.5% 28 / 29 Branchbran 83.3% 15 / 18 Conditioncond 71.4% 10 / 14 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 85.5% 65 / 76 CC 16 SCAR 20.7
CC
16
Cov
87%
__ANON__
10.5
in_parallel
9.4
workers
3.0
CRAP 7.9
« lib/Peta/NN/Chain.pm ↑ Summary lib/Peta/NN/Backend/Plain.pm »
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
l!l
66
MC/DC100%1/1
$ENV{"PETA_NN_WORKERS"} // 1
$ENV{"PETA_NN_WORKERS"}
Truth table100%2/2
A
A= $ENV{"PETA_NN_WORKERS"}
Aresult
11
00
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
BranchTrueFalseTotal
unless $asked =~ /\A[1-9][0-9]*\z/u 3 9 12
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
BranchTrueFalseTotal
if $workers <= 1 or @tasks <= 1 19 32 51
Condition100%3/3
$workers <= 1 or @tasks <= 1
l!l&&r!l&&!r
11832
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
ABresult
1X1
011
000
35  
36 32 require Parallel::ForkManager;
37 32 ▶ my $manager = Parallel::ForkManager->new($workers < @tasks ? $workers : scalar @tasks);
Branch: 1 branch
BranchTrueFalseTotal
$workers < @tasks ? : 21 11 32
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
BranchTrueFalseTotal
if ($signal or $code or not $data) { } 0 96 96
elsif (defined $data->{'error'}) { } 4 92 96
$signal ? : 0 0 0
Condition33%2/6
$signal or $code or not $data
l!l&&r!l&&!r
0096
$signal or $code
l!l&&r!l&&!r
0096
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
ABCresult
1XX1
01X1
0011
0000
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
BranchTrueFalseTotal
$manager->start($n) and next 105 14 119
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
BranchTrueFalseTotal
defined $value || !$@ ? : 13 1 14
Condition100%3/3
defined $value or not $@
l!l&&r!l&&!r
1211
MC/DC100%2/2
defined $value or not $@
defined $value not $@
Truth table100%3/3
A or not(B)
A= defined $valueB= not $@
ABresult
1X1
011
000
48 }
49 18 $manager->wait_all_children;
50 18 ▶ die join("\n", @failed) . "\n" if @failed;
Branch: 1 branch
BranchTrueFalseTotal
if @failed 3 15 18
51 15 return @result;
52 }
53  
54 17 1;
55  
56 __END__
« lib/Peta/NN/Chain.pm ↑ Summary lib/Peta/NN/Backend/Plain.pm »