blib/lib/HPC/Runner/Command/execute_job/Utils/MCE.pm

filter: Statementstmt 20.9% 34 / 162 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 36.3% 8 / 22 Podpod 37.5% 3 / 8 totaltotal 16.4% 45 / 274 CC 35 SCAR 37.7
CC
35
Cov
15%
parse_cmd_file
132.0
parse_file_mce
56.0
process_lines
42.0
CRAP 43.6
« blib/lib/HPC/Runner/Command/submit_jobs/Utils/Scheduler/Submit.pm ↑ Summary blib/lib/HPC/Runner/Command/stats.pm »
1 package HPC::Runner::Command::execute_job::Utils::MCE;
2  
3 1 ▶use MooseX::App::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use MooseX::Types::Path::Tiny qw/Path Paths AbsPath AbsFile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 with 'HPC::Runner::Command::execute_job::Base';
9  
10 1 ▶use MCE;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use MCE::Queue;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use DateTime::Format::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Memoize;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 HPC::Runner::App::MCE
17  
18 Execute the job.
19  
20 =cut
21  
22 =head2 Command Line Options
23  
24 =cut
25  
26 1 option 'commands' => (
27 is => 'rw',
28 isa => 'Num',
29 required => 0,
30 default => 1,
31 );
32  
33 =head2 read_command
34  
35 Commands in the command file are 0 indexed
36 The first command is 0
37  
38 =cut
39  
40 has 'read_command' => (
41 is => 'rw',
42 isa => 'Num|Undef',
43 required => 0,
44 predicate => 'has_read_command',
45 lazy => 1,
46 default => sub {
47 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
48 0 ▶ if ( $self->can('task_id') && $self->can('batch_index_start') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->can('task_id') and $self->can('batch_index_start')) { } 0 0 0
elsif ($self->can('batch_index_start')) { } 0 0 0
Condition0%0/3
$self->can("task_id") and $self->can("batch_index_start")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->can("task_id") and $self->can("batch_index_start")
$self->can("task_id") $self->can("batch_index_start")
Truth table0%0/3
A and B
A= $self->can("task_id")B= $self->can("batch_index_start")
ABresult
0X0
100
111
49 0 return $self->task_id - $self->batch_index_start;
50 }
51 elsif ( $self->can('batch_index_start') ) {
52 0 return $self->batch_index_start - 1;
53 }
54 else {
55 0 $self->log->fatal(
56 'Not able to determine job execution type. Exiting.');
57 0 exit 1;
58 }
59 }
60 1 );
61  
62 =head3 single_node
63  
64 Enable this option if you want to run from a single node or workstation
65  
66 =cut
67  
68 1 option 'single_node' => (
69 traits => ['Bool'],
70 is => 'rw',
71 isa => 'Bool',
72 default => 0,
73 handles => {
74 'not_single_node' => 'not',
75 },
76 documentation => 'Run a job from a single node or workstation.',
77 );
78  
79 =head3 jobname
80  
81 Specify a job name, and jobs will be 001_jobname, 002_jobname, 003_jobname
82  
83 =cut
84  
85 option 'jobname' => (
86 is => 'rw',
87 isa => 'Str',
88 required => 0,
89 traits => ['String'],
90 default => q{job},
91 default => sub {
92 return
93 $ENV{SLURM_JOB_NAME}
94 || $ENV{SBATCH_JOB_NAME}
95 || $ENV{PBS_JOBNAME}
96 || $ENV{JOB_NAME}
97 0 ▶ || 'job';
Subroutine __ANON__: not called CC=5 SCAR=34.0
Condition0%0/11
$ENV{'SLURM_JOB_NAME'} || $ENV{'SBATCH_JOB_NAME'} || $ENV{'PBS_JOBNAME'} || $ENV{'JOB_NAME'} || 'job'
l!l
00
$ENV{"SLURM_JOB_NAME"} or $ENV{"SBATCH_JOB_NAME"} or $ENV{"PBS_JOBNAME"} or $ENV{"JOB_NAME"}
l!l&&r!l&&!r
000
$ENV{"SLURM_JOB_NAME"} or $ENV{"SBATCH_JOB_NAME"} or $ENV{"PBS_JOBNAME"}
l!l&&r!l&&!r
000
$ENV{"SLURM_JOB_NAME"} or $ENV{"SBATCH_JOB_NAME"}
l!l&&r!l&&!r
000
MC/DC0%0/4
$ENV{'SLURM_JOB_NAME'} || $ENV{'SBATCH_JOB_NAME'} || $ENV{'PBS_JOBNAME'} || $ENV{'JOB_NAME'} || 'job'
$ENV{"SLURM_JOB_NAME"} $ENV{"SBATCH_JOB_NAME"} $ENV{"PBS_JOBNAME"} $ENV{"JOB_NAME"}
Truth table0%0/5
A or B or C or D
A= $ENV{"SLURM_JOB_NAME"}B= $ENV{"SBATCH_JOB_NAME"}C= $ENV{"PBS_JOBNAME"}D= $ENV{"JOB_NAME"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
98 },
99 1 predicate => 'has_jobname',
100 handles => {
101 add_jobname => 'append',
102 clear_jobname => 'clear',
103 },
104 documentation =>
105 q{Specify a job name, each job will be appended with its batch order},
106 );
107  
108 =head2 Attributes
109  
110 =cut
111  
112 has 'queue' => (
113 is => 'rw',
114 lazy => 0, ## must be 0 to ensure the queue is created prior to spawning
115 default => sub {
116 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
117 0 return MCE::Queue->new();
118 }
119 1 );
120  
121 has 'mce' => (
122 is => 'rw',
123 lazy => 1,
124 clearer => '_clear_mce',
125 default => sub {
126 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
127 return MCE->new(
128 max_workers => $self->procs,
129 use_threads => 0,
130 user_func => sub {
131 0 ▶ my $mce = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
132 0 while (1) {
133 0 my ( $counter, $cmd ) = $self->queue->dequeue(2);
134 0 ▶ last unless defined $counter;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $counter 0 0 0
135 0 $self->counter($counter);
136 0 $self->cmd($cmd);
137 0 $self->run_command_mce();
138 }
139 }
140 0 );
141 }
142 1 );
143  
144 1 has 'using_mce' => (
145 is => 'rw',
146 isa => 'Bool',
147 default => 1,
148 required => 1,
149 );
150  
151 =head2 Subroutines
152  
153 =cut
154  
155 =head3 go
156  
157 Initialize MCE queues
158  
159 =cut
160  
161 sub run_mce {
162 0 ▶ my $self = shift;
Subroutine run_mce: not called CC=1 SCAR=6.9
Pod undocumented
163  
164 0 my $dt1 = DateTime->now( time_zone => 'local' );
165  
166 0 $self->prepend_logfile("MAIN_");
167 0 $self->append_logfile(".log");
168 0 $self->log( $self->init_log );
169  
170 0 $self->mce->spawn;
171  
172 #MCE specific
173 0 $self->parse_file_mce;
174  
175 #$DB::single = 2;
176  
177 # MCE workers dequeue 2 elements at a time. Thus the reason for * 2.
178 0 $self->queue->enqueue( (undef) x ( $self->procs * 2 ) );
179  
180 # MCE will automatically shutdown after running for 1 or no args.
181 0 $self->mce->run(1);
182  
183 #End MCE specific
184  
185 0 my $dt2 = DateTime->now( time_zone => 'local' );
186 0 my $duration = $dt2 - $dt1;
187 0 my $format =
188 DateTime::Format::Duration->new(
189 pattern => '%e days, %H hours, %M minutes, %S seconds' );
190  
191 0 $self->log_main_messages( 'info',
192 "Total execution time " . $format->format_duration($duration) );
193 0 return;
194 }
195  
196 =head3 parse_file_mce
197  
198 The default method of parsing the file.
199  
200 #starts a comment
201 wait - says wait until all other processes/threads exitcode
202  
203 #this is a one line command
204 echo "starting"
205  
206 #This is a multiline command
207 echo "starting line 1" \
208 echo "starting line 2" \
209 echo "finishing
210  
211 =cut
212  
213 sub parse_file_mce {
214 0 ▶ my $self = shift;
Subroutine parse_file_mce: not called CC=7 SCAR=40.3
Pod documented
215  
216 0 $self->process_table;
217  
218 0 ▶ my $fh = IO::File->new( $self->infile, q{<} )
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($self->infile, "<") or $self->log_main_messages("fatal", 'Error opening file ' . $self->infile . ' ' . $!) 0 0 0
219 or $self->log_main_messages( "fatal",
220 "Error opening file " . $self->infile . " " . $! );
221 0 ▶ die print "The infile does not exist!\n" unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 0 0
222  
223 0 ▶ if ( $self->single_node ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->single_node) { } 0 0 0
elsif (defined $self->read_command) { } 0 0 0
224 0 $self->log_main_messages( 'info', 'Running in single node mode' );
225 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
226 0 my $line = $_;
227 0 $self->process_lines($line);
228 }
229 }
230 elsif ( defined $self->read_command ) {
231 0 $self->log_main_messages( 'info',
232 'Executing Command # ' . $self->read_command );
233 0 my $cmds = $self->parse_cmd_file($fh);
234  
235 0 foreach my $cmd (@$cmds) {
236 0 map { $self->process_lines( $_ . "\n" ) } split( "\n", $cmd );
237 0 $self->wait(0);
238 }
239 }
240 else {
241 0 $self->log_main_messages( 'fatal', 'No running mode found. Exiting' );
242 0 exit 1;
243 }
244 }
245  
246 =head3 parse_cmd_file
247  
248 Parse the command file for the read_command
249 Commands are 0 indexed
250  
251 =cut
252  
253 sub parse_cmd_file {
254 0 ▶ my $self = shift;
Subroutine parse_cmd_file: not called CC=11 SCAR=48.8
Pod documented
255 0 my $fh = shift;
256  
257 0 my $x = 0;
258 0 my $add_cmds = 0;
259 0 my $cmd_count = 0;
260  
261 0 my @cmds = ();
262 0 my $cmd = '';
263  
264 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
265 0 my $line = $_;
266 0 ▶ next unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 0 0
267 0 ▶ next unless $line =~ m/\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/ 0 0 0
268  
269 0 $cmd .= $line;
270 0 ▶ next if $line =~ m/\\$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\\$/ 0 0 0
271 0 ▶ next if $line =~ m/^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 0 0 0
272 0 ▶ if ( $x == $self->read_command && $cmd_count < $self->commands ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x == $self->read_command and $cmd_count < $self->commands) 0 0 0
Condition0%0/3
$x == $self->read_command and $cmd_count < $self->commands
!ll&&!rl&&r
000
MC/DC0%0/2
$x == $self->read_command and $cmd_count < $self->commands
$x == $self->read_command $cmd_count < $self->commands
Truth table0%0/3
A and B
A= $x == $self->read_commandB= $cmd_count < $self->commands
ABresult
0X0
100
111
273 0 $add_cmds = 1;
274 }
275 0 ▶ if ($add_cmds) {
Branch: 1 branch
BranchTrueFalseTotal
if ($add_cmds) 0 0 0
276 0 push( @cmds, $cmd );
277 0 $cmd_count++;
278 }
279 0 $x++;
280  
281 0 ▶ if ( $x >= $self->read_command && $cmd_count >= $self->commands ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= $self->read_command and $cmd_count >= $self->commands) 0 0 0
Condition0%0/3
$x >= $self->read_command and $cmd_count >= $self->commands
!ll&&!rl&&r
000
MC/DC0%0/2
$x >= $self->read_command and $cmd_count >= $self->commands
$x >= $self->read_command $cmd_count >= $self->commands
Truth table0%0/3
A and B
A= $x >= $self->read_commandB= $cmd_count >= $self->commands
ABresult
0X0
100
111
282 0 last;
283 }
284 0 $cmd = '';
285 }
286  
287 0 close($fh);
288 0 return \@cmds;
289 }
290  
291 ##TODO separate out single node mode
292 sub process_lines {
293 0 ▶ my $self = shift;
Subroutine process_lines: not called CC=6 SCAR=37.4
Pod undocumented
294 0 my $line = shift;
295  
296 0 ▶ if ( $line =~ m/^#TASK/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^#TASK/) 0 0 0
297 0 $self->add_cmd($line);
298 }
299  
300 0 ▶ $self->check_single_node($line) if $self->single_node;
Branch: 1 branch
BranchTrueFalseTotal
if $self->single_node 0 0 0
301  
302 0 ▶ return if $line =~ m/^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 0 0 0
303 0 $self->add_cmd($line);
304  
305 ##Bash style we continue to the next lime if the current line ends in \
306 0 ▶ return if $line =~ m/\\$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\\$/ 0 0 0
307 0 ▶ if ( $self->match_cmd(qr/^wait$/) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->match_cmd(qr/^wait$/)) { } 0 0 0
308 0 $self->hold_pool;
309 }
310 else {
311 0 $self->add_pool;
312 }
313 }
314  
315 sub check_single_node {
316 0 ▶ my $self = shift;
Subroutine check_single_node: not called CC=3 SCAR=24.8
Pod undocumented
317 0 my $line = shift;
318  
319 0 ▶ if ( $line =~ m/^#HPC jobname=/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^#HPC jobname=/) 0 0 0
320 0 $self->hold_pool;
321 0 $self->_clear_mce;
322 0 my ( $t1, $t2 ) = parse_meta($line);
323 0 $self->jobname($t2);
324 ##Trigger outdir
325 0 $self->logname($t2);
326 0 $self->logfile( $self->set_logfile );
327 0 $self->logdir( $self->set_logdir );
328 }
329 0 ▶ if ( $line =~ m/^#HPC procs=/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^#HPC procs=/) 0 0 0
330 0 $self->hold_pool;
331 0 $self->_clear_mce;
332 0 my ( $t1, $t2 ) = parse_meta($line);
333 0 $self->procs($t2);
334 0 $self->hold_pool;
335 }
336 }
337  
338 sub add_pool {
339 0 ▶ my $self = shift;
Subroutine add_pool: not called CC=2 SCAR=17.9
Pod undocumented
340 0 $self->log_main_messages( 'debug', "Enqueuing command:\n\t" . $self->cmd );
341  
342 ##Task ID is the counter for the array
343 0 ▶ $self->task_id( $self->counter ) if $self->can('task_id');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("task_id") 0 0 0
344  
345 0 $self->queue->enqueue( $self->counter, $self->cmd );
346 0 $self->clear_cmd;
347 0 $self->inc_counter;
348 }
349  
350 sub hold_pool {
351 0 ▶ my $self = shift;
Subroutine hold_pool: not called CC=3 SCAR=24.8
Pod undocumented
352  
353 0 ▶ $self->log_main_messages( 'debug', "Beginning command:\n\t" . $self->cmd )
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_cmd 0 0 0
354 if $self->has_cmd;
355 0 ▶ $self->log_main_messages( 'debug',
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_cmd 0 0 0
356 'Waiting for all threads to complete...' )
357 if $self->has_cmd;
358  
359 0 $self->wait(1);
360 0 push( @{ $self->jobref }, [] );
361 0 $self->queue->enqueue( (undef) x ( $self->procs * 2 ) );
362 0 $self->mce->run(0); # 0 indicates do not shutdown after running
363  
364 0 $self->log_main_messages( 'debug',
365 'All children have completed processing!' );
366 0 $self->clear_cmd;
367 }
368  
369 1 memoize('parse_meta');
370  
371 sub parse_meta {
372 0 ▶ my $line = shift;
Subroutine parse_meta: not called CC=1 SCAR=6.9
373 0 my ( @match, $t1, $t2 );
374  
375 0 @match = $line =~ m/ (\w+)=(.+)$/;
376 0 ( $t1, $t2 ) = ( $match[0], $match[1] );
377  
378 0 return ( $t1, $2 );
379 }
380  
381 =head3 run_command_mce
382  
383 MCE knows which subcommand to use from Runner/MCE - object mce
384  
385 =cut
386  
387 sub run_command_mce {
388 0 ▶ my $self = shift;
Subroutine run_command_mce: not called CC=1 SCAR=6.9
Pod documented
389  
390 0 my $pid = $$;
391  
392 #$DB::single = 2;
393  
394 0 push( @{ $self->jobref->[-1] }, $pid );
395 0 $self->_log_commands($pid);
396  
397 0 return;
398 }
399  
400 =head1 AUTHOR
401  
402 Jillian Rowe E<lt>jillian.e.rowe@gmail.comE<gt>
403 Mario Roy E<lt>marioeroy@gmail.comE<gt>
404  
405 =cut
406  
407 1 1;
« blib/lib/HPC/Runner/Command/submit_jobs/Utils/Scheduler/Submit.pm ↑ Summary blib/lib/HPC/Runner/Command/stats.pm »