MCE/Flow.pm

filter: Statementstmt 84.7% 205 / 242 Branchbran 60.2% 112 / 186 Conditioncond 59.4% 44 / 74 MC/DCmcdc 33.3% 15 / 45 Subroutinesub 87.5% 14 / 16 Podpod 100% 5 / 5 totaltotal 69.5% 395 / 568 SCAR 62.4
CC
131
Cov
72%
run
101.1
run_seq
51.7
import
51.4
CRAP 511.3
« MCE/Map.pm ↑ Summary MCE/Loop.pm »
1 ###############################################################################
2 ## ----------------------------------------------------------------------------
3 ## Parallel flow model for building creative applications.
4 ##
5 ###############################################################################
6  
7 package MCE::Flow;
8  
9 37 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 37 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 37 ▶no warnings qw( threads recursion uninitialized );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 37 our $VERSION = '1.889';
15  
16 ## no critic (BuiltinFunctions::ProhibitStringyEval)
17 ## no critic (Subroutines::ProhibitSubroutinePrototypes)
18 ## no critic (TestingAndDebugging::ProhibitNoStrict)
19  
20 37 ▶use Scalar::Util qw( looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 37 ▶use MCE;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 37 our @CARP_NOT = qw( MCE );
24  
25 37 ▶my $_tid = $INC{'threads.pm'} ? threads->tid() : 0;
Branch: 1 branch
BranchTrueFalseTotal
$INC{'threads.pm'} ? : 0 37 37
26  
27 sub CLONE {
28 0 ▶ $_tid = threads->tid() if $INC{'threads.pm'};
Subroutine CLONE: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"threads.pm"} 0 0 0
29 }
30  
31 ###############################################################################
32 ## ----------------------------------------------------------------------------
33 ## Import routine.
34 ##
35 ###############################################################################
36  
37 37 my ($_MCE, $_def, $_params, $_tag) = ({}, {}, {}, 'MCE::Flow');
38 37 my ($_prev_c, $_prev_n, $_prev_t, $_prev_w) = ({}, {}, {}, {});
39 37 my ($_user_tasks) = ({});
40  
41 sub import {
42 37 ▶ my ($_class, $_pkg) = (shift, caller);
Subroutine import: called CC=13 SCAR=39.4
43  
44 37 my $_p = $_def->{$_pkg} = {
45 MAX_WORKERS => 'auto',
46 CHUNK_SIZE => 'auto',
47 };
48  
49 ## Import functions.
50 37 ▶ if ($_pkg !~ /^MCE::/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_pkg =~ /^MCE::/) 37 0 37
51 37 ▶ no strict 'refs'; no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
52 37 *{ $_pkg.'::mce_flow_f' } = \&run_file;
53 37 *{ $_pkg.'::mce_flow_s' } = \&run_seq;
54 37 *{ $_pkg.'::mce_flow' } = \&run;
55 }
56  
57 ## Process module arguments.
58 37 ▶ while ( my $_argument = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $_argument = shift()) 0 37 37
59 0 my $_arg = lc $_argument;
60  
61 0 ▶ $_p->{MAX_WORKERS} = shift, next if ( $_arg eq 'max_workers' );
Branch: 1 branch
BranchTrueFalseTotal
if $_arg eq "max_workers" 0 0 0
62 0 ▶ $_p->{CHUNK_SIZE} = shift, next if ( $_arg eq 'chunk_size' );
Branch: 1 branch
BranchTrueFalseTotal
if $_arg eq "chunk_size" 0 0 0
63 0 ▶ $_p->{TMP_DIR} = shift, next if ( $_arg eq 'tmp_dir' );
Branch: 1 branch
BranchTrueFalseTotal
if $_arg eq "tmp_dir" 0 0 0
64 0 ▶ $_p->{FREEZE} = shift, next if ( $_arg eq 'freeze' );
Branch: 1 branch
BranchTrueFalseTotal
if $_arg eq "freeze" 0 0 0
65 0 ▶ $_p->{THAW} = shift, next if ( $_arg eq 'thaw' );
Branch: 1 branch
BranchTrueFalseTotal
if $_arg eq "thaw" 0 0 0
66 0 ▶ $_p->{INIT_RELAY} = shift, next if ( $_arg eq 'init_relay' );
Branch: 1 branch
BranchTrueFalseTotal
if $_arg eq "init_relay" 0 0 0
67 0 ▶ $_p->{USE_THREADS} = shift, next if ( $_arg eq 'use_threads' );
Branch: 1 branch
BranchTrueFalseTotal
if $_arg eq "use_threads" 0 0 0
68  
69 ## Sereal 3.015+, if available, is used automatically by MCE 1.8+.
70 0 ▶ if ( $_arg eq 'sereal' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_arg eq "sereal") 0 0 0
71 0 ▶ if ( shift eq '0' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (shift() eq 0) 0 0 0
72 0 require Storable;
73 0 $_p->{FREEZE} = \&Storable::freeze;
74 0 $_p->{THAW} = \&Storable::thaw;
75 }
76 0 next;
77 }
78  
79 0 _croak("Error: ($_argument) invalid module option");
80 }
81  
82 37 $_p->{MAX_WORKERS} = MCE::_parse_max_workers($_p->{MAX_WORKERS});
83  
84 37 MCE::_validate_number($_p->{MAX_WORKERS}, 'MAX_WORKERS', $_tag);
85 MCE::_validate_number($_p->{CHUNK_SIZE}, 'CHUNK_SIZE', $_tag)
86 37 ▶ unless ($_p->{CHUNK_SIZE} eq 'auto');
Branch: 1 branch
BranchTrueFalseTotal
unless $_p->{"CHUNK_SIZE"} eq "auto" 0 37 37
87  
88 37 return;
89 }
90  
91 ###############################################################################
92 ## ----------------------------------------------------------------------------
93 ## Init and finish routines.
94 ##
95 ###############################################################################
96  
97 sub init (@) {
98  
99 29 ▶ shift if (defined $_[0] && $_[0] eq 'MCE::Flow');
Subroutine init: called CC=4 SCAR=14.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and $_[0] eq "MCE::Flow" 23 6 29
Condition66%2/3
defined $_[0] and $_[0] eq "MCE::Flow"
!ll&&!rl&&r
0623
MC/DC50%1/2
defined $_[0] and $_[0] eq "MCE::Flow"
defined $_[0] $_[0] eq "MCE::Flow"
Truth table66%2/3
A and B
A= defined $_[0]B= $_[0] eq "MCE::Flow"
ABresult
0X0
100
111
100 29 my $_pkg = "$$.$_tid.".caller();
101  
102 29 ▶ $_params->{$_pkg} = (ref $_[0] eq 'HASH') ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 29 29
103  
104 29 @_ = ();
105  
106 29 return;
107 }
108  
109 sub finish (@) {
110  
111 101 ▶ shift if (defined $_[0] && $_[0] eq 'MCE::Flow');
Subroutine finish: called CC=9 SCAR=22.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and $_[0] eq "MCE::Flow" 101 0 101
Condition33%1/3
defined $_[0] and $_[0] eq "MCE::Flow"
!ll&&!rl&&r
00101
MC/DC0%0/2
defined $_[0] and $_[0] eq "MCE::Flow"
defined $_[0] $_[0] eq "MCE::Flow"
Truth table33%1/3
A and B
A= defined $_[0]B= $_[0] eq "MCE::Flow"
ABresult
0X0
100
111
112 101 ▶ my $_pkg = (defined $_[0]) ? shift : "$$.$_tid.".caller();
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 66 35 101
113  
114 101 ▶ if ( $_pkg eq 'MCE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_pkg eq 'MCE') { } 37 64 101
elsif ($_MCE->{$_pkg} and $_MCE->{$_pkg}{'_init_pid'} eq "${$}.$_tid") { } 35 29 64
115 37 for my $_k ( keys %{ $_MCE } ) { MCE::Flow->finish($_k, 1); }
116 }
117 ▶ elsif ( $_MCE->{$_pkg} && $_MCE->{$_pkg}{_init_pid} eq "$$.$_tid" ) {
Condition66%2/3
$_MCE->{$_pkg} and $_MCE->{$_pkg}{"_init_pid"} eq "${$}.$_tid"
!ll&&!rl&&r
02935
MC/DC50%1/2
$_MCE->{$_pkg} and $_MCE->{$_pkg}{"_init_pid"} eq "${$}.$_tid"
$_MCE->{$_pkg} $_MCE->{$_pkg}{"_init_pid"} eq "${$}.$_tid"
Truth table66%2/3
A and B
A= $_MCE->{$_pkg}B= $_MCE->{$_pkg}{"_init_pid"} eq "${$}.$_tid"
ABresult
0X0
100
111
118 35 ▶ $_MCE->{$_pkg}->shutdown(@_) if $_MCE->{$_pkg}{_spawned};
Branch: 1 branch
BranchTrueFalseTotal
if $_MCE->{$_pkg}{"_spawned"} 35 0 35
119  
120 35 delete $_user_tasks->{$_pkg};
121 35 delete $_prev_c->{$_pkg};
122 35 delete $_prev_n->{$_pkg};
123 35 delete $_prev_t->{$_pkg};
124 35 delete $_prev_w->{$_pkg};
125 35 delete $_MCE->{$_pkg};
126 }
127  
128 101 @_ = ();
129  
130 101 return;
131 }
132  
133 ###############################################################################
134 ## ----------------------------------------------------------------------------
135 ## Parallel flow with MCE -- file.
136 ##
137 ###############################################################################
138  
139 sub run_file (@) {
140  
141 4 ▶ shift if (defined $_[0] && $_[0] eq 'MCE::Flow');
Subroutine run_file: called CC=21 SCAR=34.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and $_[0] eq "MCE::Flow" 0 4 4
Condition33%1/3
defined $_[0] and $_[0] eq "MCE::Flow"
!ll&&!rl&&r
040
MC/DC0%0/2
defined $_[0] and $_[0] eq "MCE::Flow"
defined $_[0] $_[0] eq "MCE::Flow"
Truth table33%1/3
A and B
A= defined $_[0]B= $_[0] eq "MCE::Flow"
ABresult
0X0
100
111
142  
143 4 ▶ my ($_file, $_pos); my $_start_pos = (ref $_[0] eq 'HASH') ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 4 0 4
144 4 my $_pid = "$$.$_tid.".caller();
145  
146 4 ▶ if (defined (my $_p = $_params->{$_pid})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $_p = $_params->{$_pid})) { } 4 0 4
147 4 ▶ delete $_p->{input_data} if (exists $_p->{input_data});
Branch: 1 branch
BranchTrueFalseTotal
if exists $_p->{"input_data"} 0 4 4
148 4 ▶ delete $_p->{sequence} if (exists $_p->{sequence});
Branch: 1 branch
BranchTrueFalseTotal
if exists $_p->{"sequence"} 0 4 4
149 }
150 else {
151 0 $_params->{$_pid} = {};
152 }
153  
154 4 for my $_i ($_start_pos .. @_ - 1) {
155 8 my $_r = ref $_[$_i];
156 8 ▶ if ($_r eq '' || $_r eq 'SCALAR' || $_r =~ /^(?:GLOB|FileHandle|IO::)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_r eq "" or $_r eq "SCALAR" or $_r =~ /^(?:GLOB|FileHandle|IO::)/) 2 4 6
Condition83%5/6
$_r eq "" or $_r eq "SCALAR" or $_r =~ /^(?:GLOB|FileHandle|IO::)/
l!l&&r!l&&!r
224
$_r eq "" or $_r eq "SCALAR"
l!l&&r!l&&!r
206
MC/DC66%2/3
$_r eq "" or $_r eq "SCALAR" or $_r =~ /^(?:GLOB|FileHandle|IO::)/
$_r eq "" $_r eq "SCALAR" $_r =~ /^(?:GLOB|FileHandle|IO::)/
Truth table75%3/4
A or B or C
A= $_r eq ""B= $_r eq "SCALAR"C= $_r =~ /^(?:GLOB|FileHandle|IO::)/
ABCresult
1XX1
01X1
0011
0000
157 4 $_file = $_[$_i]; $_pos = $_i;
158 4 last;
159 }
160 }
161  
162 4 ▶ if (defined $_file && ref $_file eq '' && $_file ne '') {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $_file and ref $_file eq '' and $_file ne '') { } 2 2 4
elsif (ref $_file eq 'SCALAR' or ref($_file) =~ /^(?:GLOB|FileHandle|IO::)/) { } 2 0 2
Condition66%4/6
defined $_file and ref $_file eq "" and $_file ne ""
!ll&&!rl&&r
202
defined $_file and ref $_file eq ""
!ll&&!rl&&r
022
MC/DC33%1/3
defined $_file and ref $_file eq "" and $_file ne ""
defined $_file ref $_file eq "" $_file ne ""
Truth table50%2/4
A and B and C
A= defined $_fileB= ref $_file eq ""C= $_file ne ""
ABCresult
0XX0
10X0
1100
1111
163 2 ▶ _croak("$_tag: ($_file) does not exist") unless (-e $_file);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $_file 0 2 2
164 2 ▶ _croak("$_tag: ($_file) is not readable") unless (-r $_file);
Branch: 1 branch
BranchTrueFalseTotal
unless -r $_file 0 2 2
165 2 ▶ _croak("$_tag: ($_file) is not a plain file") unless (-f $_file);
Branch: 1 branch
BranchTrueFalseTotal
unless -f $_file 0 2 2
166 2 $_params->{$_pid}{_file} = $_file;
167 }
168 ▶ elsif (ref $_file eq 'SCALAR' || ref($_file) =~ /^(?:GLOB|FileHandle|IO::)/) {
Condition33%1/3
ref $_file eq "SCALAR" or ref($_file) =~ /^(?:GLOB|FileHandle|IO::)/
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $_file eq "SCALAR" or ref($_file) =~ /^(?:GLOB|FileHandle|IO::)/
ref $_file eq "SCALAR" ref($_file) =~ /^(?:GLOB|FileHandle|IO::)/
Truth table33%1/3
A or B
A= ref $_file eq "SCALAR"B= ref($_file) =~ /^(?:GLOB|FileHandle|IO::)/
ABresult
1X1
011
000
169 2 $_params->{$_pid}{_file} = $_file;
170 }
171 else {
172 0 _croak("$_tag: (file) is not specified or valid");
173 }
174  
175 4 ▶ if (defined $_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_pos) 4 0 4
176 4 pop @_ for ($_pos .. @_ - 1);
177 }
178  
179 4 return run(@_);
180 }
181  
182 ###############################################################################
183 ## ----------------------------------------------------------------------------
184 ## Parallel flow with MCE -- sequence.
185 ##
186 ###############################################################################
187  
188 sub run_seq (@) {
189  
190 2 ▶ shift if (defined $_[0] && $_[0] eq 'MCE::Flow');
Subroutine run_seq: called CC=22 SCAR=39.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and $_[0] eq "MCE::Flow" 0 2 2
Condition33%1/3
defined $_[0] and $_[0] eq "MCE::Flow"
!ll&&!rl&&r
020
MC/DC0%0/2
defined $_[0] and $_[0] eq "MCE::Flow"
defined $_[0] $_[0] eq "MCE::Flow"
Truth table33%1/3
A and B
A= defined $_[0]B= $_[0] eq "MCE::Flow"
ABresult
0X0
100
111
191  
192 2 ▶ my ($_begin, $_end, $_pos); my $_start_pos = (ref $_[0] eq 'HASH') ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 2 0 2
193 2 my $_pid = "$$.$_tid.".caller();
194  
195 2 ▶ if (defined (my $_p = $_params->{$_pid})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $_p = $_params->{$_pid})) { } 2 0 2
196 2 ▶ delete $_p->{sequence} if (exists $_p->{sequence});
Branch: 1 branch
BranchTrueFalseTotal
if exists $_p->{"sequence"} 0 2 2
197 2 ▶ delete $_p->{input_data} if (exists $_p->{input_data});
Branch: 1 branch
BranchTrueFalseTotal
if exists $_p->{"input_data"} 0 2 2
198 2 ▶ delete $_p->{_file} if (exists $_p->{_file});
Branch: 1 branch
BranchTrueFalseTotal
if exists $_p->{"_file"} 0 2 2
199 }
200 else {
201 0 $_params->{$_pid} = {};
202 }
203  
204 2 for my $_i ($_start_pos .. @_ - 1) {
205 4 my $_r = ref $_[$_i];
206  
207 4 ▶ if ($_r eq '' || $_r =~ /^Math::/ || $_r eq 'HASH' || $_r eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_r eq "" or $_r =~ /^Math::/ or $_r eq "HASH" or $_r eq "ARRAY") 0 2 2
Condition66%6/9
$_r eq "" or $_r =~ /^Math::/ or $_r eq "HASH" or $_r eq "ARRAY"
l!l&&r!l&&!r
202
$_r eq "" or $_r =~ /^Math::/ or $_r eq "HASH"
l!l&&r!l&&!r
202
$_r eq "" or $_r =~ /^Math::/
l!l&&r!l&&!r
202
MC/DC25%1/4
$_r eq "" or $_r =~ /^Math::/ or $_r eq "HASH" or $_r eq "ARRAY"
$_r eq "" $_r =~ /^Math::/ $_r eq "HASH" $_r eq "ARRAY"
Truth table40%2/5
A or B or C or D
A= $_r eq ""B= $_r =~ /^Math::/C= $_r eq "HASH"D= $_r eq "ARRAY"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
208 2 $_pos = $_i;
209  
210 2 ▶ if ($_r eq '' || $_r =~ /^Math::/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($_r eq '' or $_r =~ /^Math::/) { } 2 0 2
elsif ($_r eq 'HASH') { } 0 0 0
elsif ($_r eq 'ARRAY') { } 0 0 0
Condition33%1/3
$_r eq "" or $_r =~ /^Math::/
l!l&&r!l&&!r
200
MC/DC0%0/2
$_r eq "" or $_r =~ /^Math::/
$_r eq "" $_r =~ /^Math::/
Truth table33%1/3
A or B
A= $_r eq ""B= $_r =~ /^Math::/
ABresult
1X1
011
000
211 2 $_begin = $_[$_pos], $_end = $_[$_pos + 1];
212 $_params->{$_pid}{sequence} = [
213 2 $_[$_pos], $_[$_pos + 1], $_[$_pos + 2], $_[$_pos + 3]
214 ];
215 }
216 elsif ($_r eq 'HASH') {
217 0 $_begin = $_[$_pos]->{begin}, $_end = $_[$_pos]->{end};
218 0 $_params->{$_pid}{sequence} = $_[$_pos];
219 }
220 elsif ($_r eq 'ARRAY') {
221 0 $_begin = $_[$_pos]->[0], $_end = $_[$_pos]->[1];
222 0 $_params->{$_pid}{sequence} = $_[$_pos];
223 }
224  
225 2 last;
226 }
227 }
228  
229 _croak("$_tag: (sequence) is not specified or valid")
230 2 ▶ unless (exists $_params->{$_pid}{sequence});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_params->{$_pid}{"sequence"} 0 2 2
231 2 ▶ _croak("$_tag: (begin) is not specified for sequence")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_begin 0 2 2
232 unless (defined $_begin);
233 2 ▶ _croak("$_tag: (end) is not specified for sequence")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_end 0 2 2
234 unless (defined $_end);
235  
236 2 $_params->{$_pid}{sequence_run} = undef;
237  
238 2 ▶ if (defined $_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_pos) 2 0 2
239 2 pop @_ for ($_pos .. @_ - 1);
240 }
241  
242 2 return run(@_);
243 }
244  
245 ###############################################################################
246 ## ----------------------------------------------------------------------------
247 ## Parallel flow with MCE.
248 ##
249 ###############################################################################
250  
251 sub run (@) {
252  
253 74 ▶ shift if (defined $_[0] && $_[0] eq 'MCE::Flow');
Subroutine run: called CC=65 SCAR=46.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and $_[0] eq "MCE::Flow" 0 74 74
Condition33%1/3
defined $_[0] and $_[0] eq "MCE::Flow"
!ll&&!rl&&r
0740
MC/DC0%0/2
defined $_[0] and $_[0] eq "MCE::Flow"
defined $_[0] $_[0] eq "MCE::Flow"
Truth table33%1/3
A and B
A= defined $_[0]B= $_[0] eq "MCE::Flow"
ABresult
0X0
100
111
254  
255 74 ▶ my $_pkg = caller() eq 'MCE::Flow' ? caller(1) : caller();
Branch: 1 branch
BranchTrueFalseTotal
caller() eq 'MCE::Flow' ? : 6 68 74
256 74 my $_pid = "$$.$_tid.$_pkg";
257  
258 74 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "HASH") 48 26 74
259 48 ▶ $_params->{$_pid} = {} unless defined $_params->{$_pid};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_params->{$_pid} 9 39 48
260 48 for my $_p (keys %{ $_[0] }) {
261 72 $_params->{$_pid}{$_p} = $_[0]->{$_p};
262 }
263  
264 48 shift;
265 }
266  
267 ## -------------------------------------------------------------------------
268  
269 74 my (@_code, @_name, @_thrs, @_wrks); my $_init_mce = 0; my $_pos = 0;
270  
271 74 ▶ while (ref $_[0] eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "CODE") 94 74 168
272 94 push @_code, $_[0];
273  
274 94 ▶ if (defined (my $_p = $_params->{$_pid})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $_p = $_params->{$_pid})) 94 0 94
275 push @_name, (ref $_p->{task_name} eq 'ARRAY')
276 94 ▶ ? $_p->{task_name}->[$_pos] : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_p->{'task_name'} eq 'ARRAY' ? : 28 66 94
277 push @_thrs, (ref $_p->{use_threads} eq 'ARRAY')
278 94 ▶ ? $_p->{use_threads}->[$_pos] : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_p->{'use_threads'} eq 'ARRAY' ? : 0 94 94
279 push @_wrks, (ref $_p->{max_workers} eq 'ARRAY')
280 94 ▶ ? $_p->{max_workers}->[$_pos] : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_p->{'max_workers'} eq 'ARRAY' ? : 40 54 94
281 }
282  
283 $_init_mce = 1 if (
284 !defined $_prev_c->{$_pid}[$_pos] ||
285 94 ▶ $_prev_c->{$_pid}[$_pos] != $_code[$_pos]
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_prev_c->{$_pid}[$_pos] or $_prev_c->{$_pid}[$_pos] != $_code[$_pos] 76 18 94
Condition66%2/3
not defined $_prev_c->{$_pid}[$_pos] or $_prev_c->{$_pid}[$_pos] != $_code[$_pos]
l!l&&r!l&&!r
76018
MC/DC50%1/2
not defined $_prev_c->{$_pid}[$_pos] or $_prev_c->{$_pid}[$_pos] != $_code[$_pos]
not defined $_prev_c->{$_pid}[$_pos] $_prev_c->{$_pid}[$_pos] != $_code[$_pos]
Truth table66%2/3
not(A) or B
A= not defined $_prev_c->{$_pid}[$_pos]B= $_prev_c->{$_pid}[$_pos] != $_code[$_pos]
ABresult
1X1
011
000
286 );
287  
288 94 ▶ $_init_mce = 1 if ($_prev_n->{$_pid}[$_pos] ne $_name[$_pos]);
Branch: 1 branch
BranchTrueFalseTotal
if $_prev_n->{$_pid}[$_pos] ne $_name[$_pos] 12 82 94
289 94 ▶ $_init_mce = 1 if ($_prev_t->{$_pid}[$_pos] ne $_thrs[$_pos]);
Branch: 1 branch
BranchTrueFalseTotal
if $_prev_t->{$_pid}[$_pos] ne $_thrs[$_pos] 0 94 94
290 94 ▶ $_init_mce = 1 if ($_prev_w->{$_pid}[$_pos] ne $_wrks[$_pos]);
Branch: 1 branch
BranchTrueFalseTotal
if $_prev_w->{$_pid}[$_pos] ne $_wrks[$_pos] 24 70 94
291  
292 94 $_prev_c->{$_pid}[$_pos] = $_code[$_pos];
293 94 $_prev_n->{$_pid}[$_pos] = $_name[$_pos];
294 94 $_prev_t->{$_pid}[$_pos] = $_thrs[$_pos];
295 94 $_prev_w->{$_pid}[$_pos] = $_wrks[$_pos];
296  
297 94 shift; $_pos++;
298 }
299  
300 74 ▶ if (defined $_prev_c->{$_pid}[$_pos]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_prev_c->{$_pid}[$_pos]) 0 74 74
301 0 pop @{ $_prev_c->{$_pid} } for ($_pos .. $#{ $_prev_c->{$_pid } });
302 0 pop @{ $_prev_n->{$_pid} } for ($_pos .. $#{ $_prev_n->{$_pid } });
303 0 pop @{ $_prev_t->{$_pid} } for ($_pos .. $#{ $_prev_t->{$_pid } });
304 0 pop @{ $_prev_w->{$_pid} } for ($_pos .. $#{ $_prev_w->{$_pid } });
305  
306 0 $_init_mce = 1;
307 }
308  
309 74 ▶ return unless (scalar @_code);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_code 0 74 74
310  
311 ## -------------------------------------------------------------------------
312  
313 74 my $_input_data; my $_max_workers = $_def->{$_pkg}{MAX_WORKERS};
314 74 my $_r = ref $_[0];
315  
316 74 ▶ if (@_ == 1 && $_r =~ /^(?:ARRAY|HASH|SCALAR|GLOB|FileHandle|IO::)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and $_r =~ /^(?:ARRAY|HASH|SCALAR|GLOB|FileHandle|IO::)/) 4 70 74
Condition66%2/3
@_ == 1 and $_r =~ /^(?:ARRAY|HASH|SCALAR|GLOB|FileHandle|IO::)/
!ll&&!rl&&r
7004
MC/DC50%1/2
@_ == 1 and $_r =~ /^(?:ARRAY|HASH|SCALAR|GLOB|FileHandle|IO::)/
@_ == 1 $_r =~ /^(?:ARRAY|HASH|SCALAR|GLOB|FileHandle|IO::)/
Truth table66%2/3
A and B
A= @_ == 1B= $_r =~ /^(?:ARRAY|HASH|SCALAR|GLOB|FileHandle|IO::)/
ABresult
0X0
100
111
317 4 $_input_data = shift;
318 }
319  
320 74 ▶ if (defined (my $_p = $_params->{$_pid})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $_p = $_params->{$_pid})) 74 0 74
321 $_max_workers = MCE::_parse_max_workers($_p->{max_workers})
322 74 ▶ if (exists $_p->{max_workers} && ref $_p->{max_workers} ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if exists $_p->{"max_workers"} and ref $_p->{"max_workers"} ne "ARRAY" 54 20 74
Condition66%2/3
exists $_p->{"max_workers"} and ref $_p->{"max_workers"} ne "ARRAY"
!ll&&!rl&&r
02054
MC/DC50%1/2
exists $_p->{"max_workers"} and ref $_p->{"max_workers"} ne "ARRAY"
exists $_p->{"max_workers"} ref $_p->{"max_workers"} ne "ARRAY"
Truth table66%2/3
A and B
A= exists $_p->{"max_workers"}B= ref $_p->{"max_workers"} ne "ARRAY"
ABresult
0X0
100
111
323  
324 74 ▶ delete $_p->{sequence} if (defined $_input_data || scalar @_);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_input_data or scalar @_ 10 64 74
Condition100%2/2
defined $_input_data or scalar @_
l!l
470
MC/DC100%1/1
defined $_input_data or scalar @_
defined $_input_data
Truth table100%2/2
A
A= defined $_input_data
Aresult
11
00
325 74 ▶ delete $_p->{user_func} if (exists $_p->{user_func});
Branch: 1 branch
BranchTrueFalseTotal
if exists $_p->{"user_func"} 0 74 74
326 74 ▶ delete $_p->{user_tasks} if (exists $_p->{user_tasks});
Branch: 1 branch
BranchTrueFalseTotal
if exists $_p->{"user_tasks"} 0 74 74
327 }
328  
329 74 ▶ if (@_code > 1 && $_max_workers > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_code > 1 and $_max_workers > 1) 20 54 74
Condition66%2/3
@_code > 1 and $_max_workers > 1
!ll&&!rl&&r
54020
MC/DC50%1/2
@_code > 1 and $_max_workers > 1
@_code > 1 $_max_workers > 1
Truth table66%2/3
A and B
A= @_code > 1B= $_max_workers > 1
ABresult
0X0
100
111
330 20 $_max_workers = int($_max_workers / @_code + 0.5) + 1;
331 }
332  
333 my $_chunk_size = MCE::_parse_chunk_size(
334 74 $_def->{$_pkg}{CHUNK_SIZE}, $_max_workers, $_params->{$_pid},
335 $_input_data, scalar @_
336 );
337  
338 74 ▶ if (defined (my $_p = $_params->{$_pid})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $_p = $_params->{$_pid})) 74 0 74
339 74 ▶ if (exists $_p->{_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_p->{'_file'}) { } 4 70 74
340 4 $_input_data = delete $_p->{_file};
341 } else {
342 70 ▶ $_input_data = $_p->{input_data} if exists $_p->{input_data};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_p->{"input_data"} 0 70 70
343 }
344 }
345  
346 ## -------------------------------------------------------------------------
347  
348 74 MCE::_save_state($_MCE->{$_pid});
349  
350 74 ▶ if ($_init_mce) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_init_mce) { } 64 10 74
351 64 ▶ $_MCE->{$_pid}->shutdown() if (defined $_MCE->{$_pid});
Branch: 1 branch
BranchTrueFalseTotal
if defined $_MCE->{$_pid} 0 64 64
352  
353 ## must clear arrays for nested session to work with Perl < v5.14
354 64 _gen_user_tasks($_pid, [@_code], [@_name], [@_thrs], [@_wrks]);
355  
356 64 @_code = @_name = @_thrs = @_wrks = ();
357  
358 my %_opts = (
359 max_workers => $_max_workers, task_name => $_tag,
360 64 user_tasks => $_user_tasks->{$_pid},
361 );
362  
363 64 ▶ if (defined (my $_p = $_params->{$_pid})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $_p = $_params->{$_pid})) 64 0 64
364 64 local $_;
365  
366 64 for (keys %{ $_p }) {
367 114 ▶ next if ($_ eq 'max_workers' && ref $_p->{max_workers} eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "max_workers" and ref $_p->{"max_workers"} eq "ARRAY" 12 102 114
Condition100%3/3
$_ eq "max_workers" and ref $_p->{"max_workers"} eq "ARRAY"
!ll&&!rl&&r
505212
MC/DC100%2/2
$_ eq "max_workers" and ref $_p->{"max_workers"} eq "ARRAY"
$_ eq "max_workers" ref $_p->{"max_workers"} eq "ARRAY"
Truth table100%3/3
A and B
A= $_ eq "max_workers"B= ref $_p->{"max_workers"} eq "ARRAY"
ABresult
0X0
100
111
368 102 ▶ next if ($_ eq 'task_name' && ref $_p->{task_name} eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "task_name" and ref $_p->{"task_name"} eq "ARRAY" 6 96 102
Condition66%2/3
$_ eq "task_name" and ref $_p->{"task_name"} eq "ARRAY"
!ll&&!rl&&r
9606
MC/DC50%1/2
$_ eq "task_name" and ref $_p->{"task_name"} eq "ARRAY"
$_ eq "task_name" ref $_p->{"task_name"} eq "ARRAY"
Truth table66%2/3
A and B
A= $_ eq "task_name"B= ref $_p->{"task_name"} eq "ARRAY"
ABresult
0X0
100
111
369 96 ▶ next if ($_ eq 'use_threads' && ref $_p->{use_threads} eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "use_threads" and ref $_p->{"use_threads"} eq "ARRAY" 0 96 96
Condition33%1/3
$_ eq "use_threads" and ref $_p->{"use_threads"} eq "ARRAY"
!ll&&!rl&&r
9600
MC/DC0%0/2
$_ eq "use_threads" and ref $_p->{"use_threads"} eq "ARRAY"
$_ eq "use_threads" ref $_p->{"use_threads"} eq "ARRAY"
Truth table33%1/3
A and B
A= $_ eq "use_threads"B= ref $_p->{"use_threads"} eq "ARRAY"
ABresult
0X0
100
111
370  
371 96 ▶ next if ($_ eq 'chunk_size');
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "chunk_size" 0 96 96
372 96 ▶ next if ($_ eq 'input_data');
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "input_data" 0 96 96
373 96 ▶ next if ($_ eq 'sequence_run');
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "sequence_run" 0 96 96
374  
375 _croak("$_tag: ($_) is not a valid constructor argument")
376 96 ▶ unless (exists $MCE::_valid_fields_new{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $MCE::_valid_fields_new{$_} 0 96 96
377  
378 96 $_opts{$_} = $_p->{$_};
379 }
380 }
381  
382 64 for my $_k (qw/ tmp_dir freeze thaw init_relay use_threads /) {
383 $_opts{$_k} = $_def->{$_pkg}{uc($_k)}
384 320 ▶ if (exists $_def->{$_pkg}{uc($_k)} && !exists $_opts{$_k});
Branch: 1 branch
BranchTrueFalseTotal
if exists $_def->{$_pkg}{uc $_k} and not exists $_opts{$_k} 0 320 320
Condition33%1/3
exists $_def->{$_pkg}{uc $_k} and not exists $_opts{$_k}
!ll&&!rl&&r
32000
MC/DC0%0/2
exists $_def->{$_pkg}{uc $_k} and not exists $_opts{$_k}
exists $_def->{$_pkg}{uc $_k} not exists $_opts{$_k}
Truth table33%1/3
A and not(B)
A= exists $_def->{$_pkg}{uc $_k}B= not exists $_opts{$_k}
ABresult
0X0
100
111
385 }
386  
387 64 $_MCE->{$_pid} = MCE->new(pkg => $_pkg, %_opts);
388 }
389 else {
390 ## Workers may persist after running. Thus, updating the MCE instance.
391 ## These options do not require respawning.
392 10 ▶ if (defined (my $_p = $_params->{$_pid})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $_p = $_params->{$_pid})) 10 0 10
393 10 for my $_k (qw(
394 RS interval stderr_file stdout_file user_error user_output
395 job_delay submit_delay on_post_exit on_post_run user_args
396 flush_file flush_stderr flush_stdout gather max_retries
397 )) {
398 160 ▶ $_MCE->{$_pid}{$_k} = $_p->{$_k} if (exists $_p->{$_k});
Branch: 1 branch
BranchTrueFalseTotal
if exists $_p->{$_k} 10 150 160
399 }
400 }
401 }
402  
403 ## -------------------------------------------------------------------------
404  
405 74 ▶ my @_a; my $_wa = wantarray; $_MCE->{$_pid}{gather} = \@_a if (defined $_wa);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_wa 31 43 74
406  
407 74 ▶ if (defined $_input_data) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $_input_data) { } 8 66 74
elsif (scalar @_) { } 6 60 66
408 8 @_ = ();
409 8 $_MCE->{$_pid}->process({ chunk_size => $_chunk_size }, $_input_data);
410 7 delete $_MCE->{$_pid}{input_data};
411 }
412 elsif (scalar @_) {
413 6 $_MCE->{$_pid}->process({ chunk_size => $_chunk_size }, \@_);
414 2 delete $_MCE->{$_pid}{input_data};
415 }
416 else {
417 60 ▶ if (defined $_params->{$_pid} && exists $_params->{$_pid}{sequence}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_params->{$_pid} and exists $_params->{$_pid}{'sequence'}) { } 2 58 60
Condition66%2/3
defined $_params->{$_pid} and exists $_params->{$_pid}{"sequence"}
!ll&&!rl&&r
0582
MC/DC50%1/2
defined $_params->{$_pid} and exists $_params->{$_pid}{"sequence"}
defined $_params->{$_pid} exists $_params->{$_pid}{"sequence"}
Truth table66%2/3
A and B
A= defined $_params->{$_pid}B= exists $_params->{$_pid}{"sequence"}
ABresult
0X0
100
111
418 $_MCE->{$_pid}->run({
419 chunk_size => $_chunk_size,
420 sequence => $_params->{$_pid}{sequence}
421 2 }, 0);
422 2 ▶ if (exists $_params->{$_pid}{sequence_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_params->{$_pid}{"sequence_run"}) 2 0 2
423 2 delete $_params->{$_pid}{sequence_run};
424 2 delete $_params->{$_pid}{sequence};
425 }
426 2 delete $_MCE->{$_pid}{sequence};
427 }
428 else {
429 58 $_MCE->{$_pid}->run({ chunk_size => $_chunk_size }, 0);
430 }
431 }
432  
433 45 MCE::_restore_state();
434  
435 45 ▶ delete $_MCE->{$_pid}{gather} if (defined $_wa);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_wa 18 27 45
436  
437 45 ▶ return ((defined $_wa) ? @_a : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $_wa ? : 18 27 45
438 }
439  
440 ###############################################################################
441 ## ----------------------------------------------------------------------------
442 ## Private methods.
443 ##
444 ###############################################################################
445  
446 sub _croak {
447  
448 0 ▶ goto &MCE::_croak;
Subroutine _croak: not called CC=1 SCAR=6.9
449 }
450  
451 sub _gen_user_tasks {
452  
453 64 ▶ my ($_pid, $_code_ref, $_name_ref, $_thrs_ref, $_wrks_ref) = @_;
Subroutine _gen_user_tasks: called CC=2 SCAR=6.9
454  
455 64 @{ $_user_tasks->{$_pid} } = ();
456  
457 64 ▶ for (my $_i = 0; $_i < @{ $_code_ref }; $_i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_i < @{$_code_ref;}) 76 64 140
458 76 push @{ $_user_tasks->{$_pid} }, {
459 task_name => $_name_ref->[$_i],
460 use_threads => $_thrs_ref->[$_i],
461 max_workers => $_wrks_ref->[$_i],
462 user_func => $_code_ref->[$_i]
463 }
464 }
465  
466 64 return;
467 }
468  
469 37 1;
470  
471 __END__
« MCE/Map.pm ↑ Summary MCE/Loop.pm »