blib/lib/HPC/Runner/Slurm.pm

filter: Statementstmt 24.8% 71 / 286 Branchbran 0.0% 0 / 114 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 48.1% 13 / 27 Podpod 100% 9 / 9 totaltotal 20.0% 93 / 465 CC 67 SCAR 54.1
CC
67
Cov
17%
parse_file_slurm
552.0
get_nodes
342.0
submit_slurm
110.0
CRAP 223.7
↑ Summary Build »
1 package HPC::Runner::Slurm;
2  
3 1 ▶use File::Path qw(make_path remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use File::Temp qw/ tempfile tempdir /;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use List::Util qw/shuffle/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 # use IPC::Cmd qw/can_run/;
16  
17 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 extends 'HPC::Runner';
19 1 with 'MooseX::SimpleConfig';
20  
21 # For pretty man pages!
22 1 $ENV{TERM}='xterm-256color';
23  
24 =head1 NAME
25  
26 HPC::Runner::Slurm - Job Submission to Slurm
27  
28 =head1 VERSION
29  
30 Version 0.01
31  
32 =cut
33  
34 1 our $VERSION = '2.40';
35  
36 =head1 SYNOPSIS
37  
38 Indepth documentation is at https://wcmc-q.atlassian.net/wiki/display/HPCSLURM/HPC-Runner-Slurm .
39  
40 package Main;
41 extends 'HPC::Runner::Slurm';
42  
43 Main->new_with_options(infile => '/path/to/commands');
44  
45 This module is a wrapper around sbatch and can be used to submit arbirtary bash commands to slurm.
46  
47 It has two levels of management. The first is the main sbatch command, and the second is the actual job, which runs commands in parallel, controlled by HPC::Runner::Threads or HPC::Runner::MCE.
48  
49 It supports job dependencies. Put in the command 'wait' to tell slurm that some job or jobs depend on some other jobs completion. Put in the command 'newnode' to tell HPC::Runner::Slurm to submit the job to a new node.
50  
51 The only necessary option is the --infile.
52  
53 =head2 Submit Script
54  
55 cmd1
56 cmd2 && cmd3
57 cmd4 \
58 --option cmd4 \
59 #Tell HPC::Runner::Slurm to put in some job dependencies.
60 wait
61 cmd5
62 #Tell HPC::Runner::Slurm to pass things off to a new node, but this job doesn't depend on the previous
63 newnode
64 cmd6
65  
66 =head1 User Options
67  
68 User options can be passed to the script with script --opt1 or in a configfile. It uses MooseX::SimpleConfig for the commands
69  
70 =head2 configfile
71  
72 Config file to pass to command line as --configfile /path/to/file. It should be a yaml or xml (untested)
73 This is optional. Paramaters can be passed straight to the command line
74  
75 =head3 example.yml
76  
77 ---
78 infile: "/path/to/commands/testcommand.in"
79 outdir: "path/to/testdir"
80 module:
81 - "R2"
82 - "shared"
83  
84 =cut
85  
86 1 has '+configfile' => (
87 required => 0,
88 documentation => q{If you get tired of putting all your options on the command line create a config file instead.
89 ---
90 infile: "/path/to/commands/testcommand.in"
91 outdir: "path/to/testdir"
92 module:
93 - "R2"
94 - "shared"
95 }
96 );
97  
98  
99 =head2 infile
100  
101 infile of commands separated by newline
102  
103 =head3 example.in
104  
105 cmd1
106 cmd2 --input --input \
107 --someotherinput
108 wait
109 #Wait tells slurm to make sure previous commands have exited with exit status 0.
110 cmd3 ##very heavy job
111 newnode
112 #cmd3 is a very heavy job so lets start the next job on a new node
113  
114 =cut
115  
116 #We already have infile in HPC::Runner, just wanted to include slurm specific documentation here
117  
118 =head2 module
119  
120 modules to load with slurm
121 Should use the same names used in 'module load'
122  
123 Example. R2 becomes 'module load R2'
124  
125 =cut
126  
127 1 has 'module' => (
128 is => 'rw',
129 isa => 'ArrayRef',
130 required => 0,
131 documentation => q{List of modules to load ex. R2, samtools, etc},
132 );
133  
134 =head2 jobname
135  
136 Specify a job name, and jobs will be jobname_1, jobname_2, jobname_x
137  
138 =cut
139  
140 1 has 'jobname' => (
141 is => 'rw',
142 isa => 'Str',
143 required => 0,
144 traits => ['String'],
145 default => q{job},
146 handles => {
147 add_jobname => 'append',
148 clear_jobname => 'clear',
149 replace_jobname => 'replace',
150 },
151 documentation => q{Specify a job name, each job will be appended with its batch order},
152 );
153  
154 =head2 afterok
155  
156 The afterok switch in slurm. --afterok 123 will tell slurm to start this job after job 123 has completed successfully.
157  
158 =cut
159  
160 has afterok => (
161 is => 'rw',
162 isa => 'ArrayRef',
163 required => 0,
164 default => sub {
165 0 ▶ return [];
Subroutine __ANON__: not called CC=1 SCAR=6.9
166 },
167 1 );
168  
169  
170 =head2 cpus_per_task
171  
172 slurm item --cpus_per_task defaults to 4, which is probably fine
173  
174 =cut
175  
176 1 has 'cpus_per_task' => (
177 is => 'rw',
178 isa => 'Str',
179 required => 0,
180 default => 4,
181 );
182  
183 =head2 commands_per_node
184  
185 --commands_per_node defaults to 8, which is probably fine
186  
187 =cut
188  
189 1 has 'commands_per_node' => (
190 is => 'rw',
191 isa => 'Str',
192 required => 0,
193 default => 8,
194 documentation => q{Commands to run on each node. This is not the same as concurrent_commands_per_node!},
195 );
196  
197 =head2 partition
198  
199 #Should probably have something at some point that you can specify multiple partitions....
200  
201 Specify the partition. Defaults to the partition that has the most nodes.
202  
203 =cut
204  
205 1 has 'partition' => (
206 is => 'rw',
207 isa => 'Str',
208 required => 0,
209 default => '',
210 documentation => q{Slurm partition to submit jobs to. Defaults to the partition with the most available nodes}
211 );
212  
213 =head2 nodelist
214  
215 Defaults to the nodes on the defq queue
216  
217 =cut
218  
219 1 has 'nodelist' => (
220 is => 'rw',
221 isa => 'ArrayRef',
222 required => 0,
223 documentation => q{List of nodes to submit jobs to. Defaults to the partition with the most nodes.},
224 );
225  
226 =head2 submit_slurm
227  
228 Bool value whether or not to submit to slurm. If you are looking to debug your files, or this script you will want to set this to zero.
229 Don't submit to slurm with --nosubmit_to_slurm from the command line or
230 $self->submit_to_slurm(0); within your code
231  
232 =cut
233  
234 1 has 'submit_to_slurm' => (
235 is => 'rw',
236 isa => 'Bool',
237 default => 1,
238 required => 1,
239 documentation => q{Bool value whether or not to submit to slurm. If you are looking to debug your files, or this script you will want to set this to zero.},
240 );
241  
242 =head2 template_file
243  
244 actual template file
245  
246 One is generated here for you, but you can always supply your own with --template_file /path/to/template
247  
248 =cut
249  
250 has 'template_file' => (
251 traits => ['NoGetopt'],
252 is => 'rw',
253 isa => 'Str',
254 default => sub {
255 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
256  
257 0 my($fh, $filename) = tempfile();
258  
259 0 my $tt =<<EOF;
260 #!/bin/bash
261 #
262 #SBATCH --share
263 #SBATCH --get-user-env
264 #SBATCH --job-name=[% JOBNAME %]
265 #SBATCH --output=[% OUT %]
266 [% IF PARTITION %]
267 #SBATCH --partition=[% PARTITION %]
268 [% END %]
269 [% IF NODE %]
270 #SBATCH --nodelist=[% NODE %]
271 [% END %]
272 #SBATCH --cpus-per-task=[% CPU %]
273 [% IF AFTEROK %]
274 #SBATCH --dependency=afterok:[% AFTEROK %]
275 [% END %]
276  
277 [% IF MODULE %]
278 [% FOR d = MODULE %]
279 module load [% d %]
280 [% END %]
281 [% END %]
282  
283 [% COMMAND %]
284 EOF
285  
286 0 print $fh $tt;
287 0 return $filename;
288 },
289 1 );
290  
291  
292 =head2 serial
293 Option to run all jobs serially, one after the other, no parallelism
294 The default is to use 4 procs
295  
296 =cut
297  
298 1 has serial => (
299 is => 'rw',
300 isa => 'Bool',
301 default => 0,
302 documentation => q{Use this if you wish to run each job run one after another, with each job starting only after the previous has completed successfully}
303 );
304  
305  
306  
307 =head2 user
308  
309 user running the script. Passed to slurm for mail information
310  
311 =cut
312  
313 has 'user' => (
314 is => 'rw',
315 isa => 'Str',
316 0 ▶ default => sub { return $ENV{LOGNAME} || $ENV{USER} || getpwuid($<); },
Subroutine __ANON__: not called CC=3 SCAR=24.8
Condition0%0/6
$ENV{'LOGNAME'} || $ENV{'USER'} || getpwuid $<
l!l&&r!l&&!r
000
$ENV{"LOGNAME"} or $ENV{"USER"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$ENV{'LOGNAME'} || $ENV{'USER'} || getpwuid $<
$ENV{"LOGNAME"} $ENV{"USER"} getpwuid $<
Truth table0%0/4
A or B || C
A= $ENV{"LOGNAME"}B= $ENV{"USER"}C= getpwuid $<
ABCresult
1XX1
01X1
0011
0000
317 1 required => 1,
318 documentation => q{This defaults to your current user ID. This can only be changed if running as an admin user}
319 );
320  
321 =head2 use_threads
322  
323 Bool value to indicate whether or not to use threads. Default is uses processes
324  
325 If using threads your perl must be compiled to use threads!
326  
327 =cut
328  
329 1 has 'use_threads' => (
330 is => 'rw',
331 isa => 'Bool',
332 default => 0,
333 required => 0,
334 documentation => q{Use threads to run jobs},
335 );
336  
337 =head2 use_processes
338  
339 Bool value to indicate whether or not to use processes. Default is uses processes
340  
341 =cut
342  
343 1 has 'use_processes' => (
344 is => 'rw',
345 isa => 'Bool',
346 default => 1,
347 required => 0,
348 documentation => q{Use processes to run jobs},
349 );
350  
351 =head2 use_gnuparallel
352  
353 Bool value to indicate whether or not to use processes. Default is uses processes
354  
355 =cut
356  
357 1 has 'use_gnuparallel' => (
358 is => 'rw',
359 isa => 'Bool',
360 default => 0,
361 required => 0,
362 documentation => q{Use gnu-parallel to run jobs and manage threads. This is the best option if you do not know how many threads your application uses!}
363 );
364  
365 =head2 use_custom
366  
367 Use a custom command
368  
369 =cut
370  
371 1 has 'use_custom' => (
372 is => 'rw',
373 isa => 'Bool',
374 default => 0,
375 required => 0,
376 documentation => q{Use custom command to run jobs},
377 );
378  
379 1 has 'custom_command' => (
380 is => 'rw',
381 isa => 'Str',
382 predicate => 'has_custom_command',
383 default => q{},
384 );
385  
386 =head1 Internal Variables
387  
388 You should not need to mess with any of these.
389  
390 =head2 template
391  
392 template object for writing slurm batch submission script
393  
394 =cut
395  
396 has 'template' => (
397 traits => ['NoGetopt'],
398 is => 'rw',
399 required => 0,
400 0 ▶ default => sub {return Template->new(ABSOLUTE => 1) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
401 # documentation => q{Path to Slurm template file if you do not wish to use the default}
402 1 );
403  
404  
405 =head2 cmd_counter
406  
407 keep track of the number of commands - when we get to more than commands_per_node restart so we get submit to a new node.
408  
409 =cut
410  
411 1 has 'cmd_counter' => (
412 traits => ['Counter', 'NoGetopt'],
413 is => 'ro',
414 isa => 'Num',
415 required => 1,
416 default => 0,
417 handles => {
418 inc_cmd_counter => 'inc',
419 dec_cmd_counter => 'dec',
420 reset_cmd_counter => 'reset',
421 },
422 );
423  
424 =head2 node_counter
425  
426 Keep track of which node we are on
427  
428 =cut
429  
430 1 has 'node_counter' => (
431 traits => ['Counter', 'NoGetopt'],
432 is => 'ro',
433 isa => 'Num',
434 required => 1,
435 default => 0,
436 handles => {
437 inc_node_counter => 'inc',
438 dec_node_counter => 'dec',
439 reset_node_counter => 'reset',
440 },
441 );
442  
443 =head2 batch_counter
444  
445 Keep track of how many batches we have submited to slurm
446  
447 =cut
448  
449 1 has 'batch_counter' => (
450 traits => ['Counter', 'NoGetopt'],
451 is => 'ro',
452 isa => 'Num',
453 required => 1,
454 default => 1,
455 handles => {
456 inc_batch_counter => 'inc',
457 dec_batch_counter => 'dec',
458 reset_batch_counter => 'reset',
459 },
460 );
461  
462 =head2 node
463  
464 Node we are running on
465  
466 =cut
467  
468 has 'node' => (
469 traits => ['NoGetopt'],
470 is => 'rw',
471 isa => 'Str|Undef',
472 lazy => 1,
473 default => sub {
474 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
475 0 ▶ return $self->nodelist()->[0] if $self->nodelist;
Branch: 1 branch
BranchTrueFalseTotal
if $self->nodelist 0 0 0
476 0 return "";
477 }
478 1 );
479  
480 =head2 cmd
481  
482 Current command specified by infile
483  
484 =cut
485  
486 1 has 'cmd' => (
487 traits => ['String', 'NoGetopt'],
488 is => 'rw',
489 isa => 'Str',
490 required => 0,
491 default => q{},
492 handles => {
493 add_cmd => 'append',
494 match_cmd => 'match',
495 },
496 clearer => 'clear_cmd',
497 predicate => 'has_cmd',
498 );
499  
500 =head2 batch
501  
502 List of commands to submit to slurm
503  
504 =cut
505  
506 1 has 'batch' => (
507 traits => ['String', 'NoGetopt',],
508 is => 'rw',
509 isa => 'Str',
510 default => q{},
511 required => 0,
512 handles => {
513 add_batch => 'append',
514 },
515 clearer => 'clear_batch',
516 predicate => 'has_batch',
517 );
518  
519 =head2 cmdfile
520  
521 File of commands for mcerunner/parallelrunner
522 Is cleared at the end of each slurm submission
523  
524 =cut
525  
526 1 has 'cmdfile' => (
527 traits => ['String', 'NoGetopt'],
528 default => q{},
529 is => 'rw',
530 isa => 'Str',
531 required => 0,
532 handles => {
533 clear_cmdfile => 'clear',
534 },
535 );
536  
537 =head2 slurmfile
538  
539 File generated from slurm template
540  
541 =cut
542  
543 1 has 'slurmfile' => (
544 traits => ['String', 'NoGetopt'],
545 default => q{},
546 is => 'rw',
547 isa => 'Str',
548 required => 0,
549 handles => {
550 clear_slurmfile => 'clear',
551 },
552 );
553  
554 #=head2 jobref
555  
556 #Array of arrays details slurmjob id. Index -1 is the most recent job submissisions, and there will be an index -2 if there are any job dependencies
557  
558 #=cut
559  
560 #has 'jobref' => (
561 #traits => ['NoGetopt'],
562 #is => 'rw',
563 #isa => 'ArrayRef',
564 #default => sub { [ [] ] },
565 #);
566  
567 ## Added this HPC::Runner base class
568 #=head2 wait
569  
570 #Boolean value indicates any job dependencies
571  
572 #=cut
573  
574 #has 'wait' => (
575 #traits => ['NoGetopt'],
576 #is => 'rw',
577 #isa => 'Bool',
578 #default => 0,
579 #);
580  
581 =head1 SUBROUTINES/METHODS
582  
583 =cut
584  
585 =head2 run()
586  
587 First sub called
588 Calling system module load * does not work within a screen session!
589  
590 =cut
591  
592 sub run {
593 0 ▶ my $self = shift;
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
594  
595 0 $DB::single=2;
596  
597 0 $self->logname('slurm_logs');
598 0 $self->log($self->init_log);
599  
600 0 ▶ if($self->serial){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->serial) 0 0 0
601 0 $self->procs(1);
602 }
603  
604 0 $self->check_files;
605 0 $self->parse_file_slurm;
606 }
607  
608  
609 =head2 check_files()
610  
611 Check to make sure the outdir exists.
612 If it doesn't exist the entire path will be created
613  
614 =cut
615  
616 sub check_files{
617 0 ▶ my($self) = @_;
Subroutine check_files: not called CC=2 SCAR=17.9
Pod documented
618 0 my($t);
619  
620 0 $DB::single=2;
621  
622 0 $t = $self->outdir;
623 0 $t =~ s/\/$//g;
624 0 $self->outdir($t);
625  
626 #make the outdir
627 0 ▶ make_path($self->outdir) if ! -d $self->outdir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $self->outdir 0 0 0
628  
629 0 $self->get_nodes;
630 0 $DB::single=2;
631 }
632  
633 =head2 get_nodes
634  
635 Get the nodes from sinfo if not supplied
636  
637 If the nodelist is supplied partition must be supplied
638  
639 =cut
640  
641 sub get_nodes{
642 0 ▶ my($self) = @_;
Subroutine get_nodes: not called CC=18 SCAR=58.3
Pod documented
643  
644 0 $DB::single=2;
645 # #Fix - had this backwards
646 0 ▶ if($self->nodelist && !$self->partition){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->nodelist and not $self->partition) 0 0 0
Condition0%0/3
$self->nodelist and not $self->partition
!ll&&!rl&&r
000
MC/DC0%0/2
$self->nodelist and not $self->partition
$self->nodelist not $self->partition
Truth table0%0/3
A and not(B)
A= $self->nodelistB= not $self->partition
ABresult
0X0
100
111
647 0 print "If you define a nodelist you must define a partition!\n";
648 0 die;
649 }
650  
651 0 my @s = `sinfo -r`;
652 0 my $href;
653  
654 0 foreach my $s (@s) {
655 0 my @nodes = ();
656 0 my $noderef = [];
657 0 ▶ next if $s =~ m/^PARTITION/i;
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ /^PARTITION/i 0 0 0
658 0 my @t = ($s =~ /(\S+)/g);
659 0 $t[0] =~ s/\*//g;
660 0 ▶ next unless $t[1] =~ m/up/;
Branch: 1 branch
BranchTrueFalseTotal
unless $t[1] =~ /up/ 0 0 0
661  
662 0 my $nodes = $t[5];
663  
664 #list of nodes
665 0 ▶ if($nodes =~ m/\[/){
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes =~ /\[/) { } 0 0 0
666 0 my($n) = ($nodes =~ m/\[(\S+)\]/g);
667 0 my @n = split(",", $n);
668  
669 0 foreach my $nt (@n) {
670 0 ▶ if($nt =~ m/-/){
Branch: 1 branch
BranchTrueFalseTotal
if ($nt =~ /-/) { } 0 0 0
671 0 my(@m) = ($nt =~ m/(\d+)-(\d+)/g);
672 0 push(@$noderef, ($m[0]..$m[1]));
673 }
674 else{
675 0 my($m) = ($nt =~ m/(\d+)/g);
676 0 push(@$noderef, $m);
677 }
678 }
679 }
680 else{ #only one node
681 0 my($m) = ($nodes =~ m/(\d+)/g);
682 0 push(@$noderef, $m);
683 }
684  
685 0 ▶ if(exists $href->{$t[0]}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$href{$t[0]}) { } 0 0 0
686 0 my $aref = $href->{$t[0]};
687 0 ▶ push(@$aref, @$noderef) if $noderef;
Branch: 1 branch
BranchTrueFalseTotal
if $noderef 0 0 0
688 0 $href->{$t[0]} = $aref;
689 }
690 else{
691 # $href->{$t[0]} = \@nodes;
692 0 $href->{$t[0]} = $noderef;
693 }
694 }
695  
696 #Got the nodes lets find out which partition has the most nodes
697 #Unless we already have a defined partition, then we don't care
698  
699  
700 0 my $holder = 0;
701 0 my $bpart;
702  
703 0 ▶ while(my($part, $nodes) = each %{$href}){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$href;}) 0 0 0
704 0 ▶ next unless $nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless $nodes 0 0 0
705 0 ▶ next unless ref($nodes) eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $nodes eq "ARRAY" 0 0 0
706  
707 0 @$nodes = map { $part.$_ } @$nodes;
708  
709 0 ▶ if(scalar @$nodes > $holder){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$nodes > $holder) 0 0 0
710 0 $holder = scalar @$nodes;
711 0 $bpart = $part;
712 }
713 }
714  
715 #Allow for user defined partition and/or partition/nodelist
716 #Also randomize nodelist so we quit hammering the first node
717 0 $DB::single=2;
718  
719 0 ▶ if($self->partition && $self->nodelist){
Branch: 2 branches
BranchTrueFalseTotal
if ($self->partition and $self->nodelist) { } 0 0 0
elsif ($self->partition) { } 0 0 0
Condition0%0/3
$self->partition and $self->nodelist
!ll&&!rl&&r
000
MC/DC0%0/2
$self->partition and $self->nodelist
$self->partition $self->nodelist
Truth table0%0/3
A and B
A= $self->partitionB= $self->nodelist
ABresult
0X0
100
111
720 0 $DB::single=2;
721 0 return;
722 }
723 elsif($self->partition){
724 0 $DB::single=2;
725 0 my @shuffle = shuffle @{$href->{$self->partition}};
726 # $self->nodelist($href->{$self->partition});
727 0 $self->nodelist(\@shuffle);
728 0 return;
729 }
730  
731 0 $self->partition($bpart);
732 # $self->nodelist($href->{$bpart});
733 0 my @shuffle = shuffle @{$href->{$bpart}};
734 0 $self->nodelist(\@shuffle);
735 }
736  
737 =head2 parse_file_slurm
738  
739 Parse the file looking for the following conditions
740  
741 lines ending in `\`
742 wait
743 nextnode
744  
745 Batch commands in groups of $self->cpus_per_task, or smaller as wait and nextnode indicate
746  
747 =cut
748  
749 sub parse_file_slurm{
750 0 ▶ my $self = shift;
Subroutine parse_file_slurm: not called CC=23 SCAR=63.1
Pod documented
751 0 ▶ my $fh = IO::File->new( $self->infile, q{<} ) or print "Error opening file ".$self->infile." ".$!; # even better!
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($self->infile, "<") or print 'Error opening file ' . $self->infile . ' ' . $! 0 0 0
752  
753 0 ▶ if($self->afterok){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->afterok) 0 0 0
754 0 $self->wait(1);
755 0 $self->jobref->[0] = $self->afterok;
756 0 push(@{$self->jobref}, []);
757 }
758 0 $DB::single=2;
759  
760 0 ▶ while(<$fh>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
761 0 my $line = $_;
762 0 ▶ next unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 0 0
763 0 ▶ next unless $line =~ m/\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/ 0 0 0
764 0 ▶ if($line =~ m/^#HPC/){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^#HPC/) 0 0 0
765 0 $self->check_meta($line);
766 }
767 0 ▶ next if $line =~ m/^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 0 0 0
768  
769 #$DB::single=2;
770  
771 #Do a sanity check for nohup
772 0 ▶ if($line =~ m/^nohup/){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^nohup/) 0 0 0
773 0 die print "You cannot submit jobs to the queue using nohup! Please remove nohup and try again.\n";
774 }
775  
776 0 ▶ if( 0 == $self->cmd_counter % ($self->commands_per_node + 1) && $self->batch ){
Branch: 1 branch
BranchTrueFalseTotal
if (0 == $self->cmd_counter % ($self->commands_per_node + 1) and $self->batch) 0 0 0
Condition0%0/3
0 == $self->cmd_counter % ($self->commands_per_node + 1) and $self->batch
!ll&&!rl&&r
000
MC/DC0%0/2
0 == $self->cmd_counter % ($self->commands_per_node + 1) and $self->batch
0 == $self->cmd_counter % ($self->commands_per_node + 1) $self->batch
Truth table0%0/3
A and B
A= 0 == $self->cmd_counter % ($self->commands_per_node + 1)B= $self->batch
ABresult
0X0
100
111
777 #Run this batch and start the next
778 0 $DB::single=2;
779 0 $self->work;
780 0 ▶ push(@{$self->jobref}, []) if $self->serial;
Branch: 1 branch
BranchTrueFalseTotal
if $self->serial 0 0 0
781 }
782  
783 0 ▶ if($self->has_cmd){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_cmd) { } 0 0 0
784 0 $self->add_cmd($line);
785 0 $self->add_batch($line);
786 0 ▶ if($line =~ m/\\$/){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\\$/) { } 0 0 0
787 0 next;
788 }
789 else{
790 0 $self->add_cmd("\n");
791 0 $self->add_batch("\n");
792 0 $self->clear_cmd;
793 0 $self->inc_cmd_counter;
794 }
795 }
796 else{
797 0 $self->add_cmd($line);
798  
799 0 ▶ if($line =~ m/\\$/){
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /\\$/) { } 0 0 0
elsif ($self->match_cmd(qr/^wait$/)) { } 0 0 0
elsif ($self->match_cmd(qr/^newnode$/)) { } 0 0 0
800 0 $self->add_batch($line);
801 0 next;
802 }
803 elsif( $self->match_cmd(qr/^wait$/) ){
804 #submit this batch and get the job id so the next can depend upon it
805 0 $self->clear_cmd;
806 0 $self->wait(1);
807 0 ▶ $self->work if $self->has_batch;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_batch 0 0 0
808 0 push(@{$self->jobref}, []);
809 }
810 elsif( $self->match_cmd(qr/^newnode$/) ){
811 0 $self->clear_cmd;
812 0 ▶ $self->work if $self->has_batch;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_batch 0 0 0
813 0 ▶ push(@{$self->jobref}, []) if $self->serial;
Branch: 1 branch
BranchTrueFalseTotal
if $self->serial 0 0 0
814 }
815 else{
816 #Don't want to increase command count for wait and newnode
817 0 $self->inc_cmd_counter;
818 }
819 0 ▶ $self->add_batch($line."\n") if $self->has_cmd;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_cmd 0 0 0
820 0 $DB::single=2;
821 0 $self->clear_cmd;
822 }
823 }
824 0 ▶ $self->work if $self->has_batch;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_batch 0 0 0
825 0 ▶ push(@{$self->jobref}, []) if $self->serial;
Branch: 1 branch
BranchTrueFalseTotal
if $self->serial 0 0 0
826 }
827  
828 =head2 check_meta
829  
830 allow for changing parameters mid through the script
831  
832 #Job1
833 echo "this is job one" && \
834 bin/dostuff bblahblahblah
835  
836 #HPC cpu_per_task=12
837  
838 echo "This is my new job with new HPC params!"
839  
840 =cut
841  
842  
843 sub check_meta{
844 0 ▶ my $self = shift;
Subroutine check_meta: not called CC=1 SCAR=6.9
Pod documented
845 0 my $line = shift;
846  
847 0 my(@match) = $line =~ m/HPC (\w+)=(\w+)/;
848 0 $DB::single=2;
849 0 my($t1, $t2) = ($match[0], $match[1]);
850 0 $self->$t1($t2);
851 }
852  
853 =head2 work
854  
855 Get the node #may be removed but we'll try it out
856 Process the batch
857 Submit to slurm
858 Take care of the counters
859  
860 =cut
861  
862 sub work{
863 0 ▶ my $self = shift;
Subroutine work: not called CC=3 SCAR=24.8
Pod documented
864  
865  
866 0 $DB::single=2;
867  
868 0 ▶ if($self->node_counter > (scalar @{ $self->nodelist }) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->node_counter > scalar @{$self->nodelist;}) 0 0 0
869 0 $self->reset_node_counter;
870 }
871 0 ▶ $self->node($self->nodelist()->[$self->node_counter]) if $self->nodelist;
Branch: 1 branch
BranchTrueFalseTotal
if $self->nodelist 0 0 0
872 0 $self->process_batch;
873  
874 0 $self->inc_batch_counter;
875 0 $self->clear_batch;
876 0 $self->inc_node_counter;
877  
878 0 $self->reset_cmd_counter;
879 }
880  
881 =head2 process_batch()
882  
883 Create the slurm submission script from the slurm template
884 Write out template, submission job, and infile for parallel runner
885  
886 =cut
887  
888 sub process_batch{
889 0 ▶ my $self = shift;
Subroutine process_batch: not called CC=8 SCAR=42.8
Pod documented
890 0 my($cmdfile, $slurmfile, $slurmsubmit, $fh, $command);
891  
892 0 $self->cmdfile($self->outdir."/".$self->jobname."_".$self->batch_counter.".in");
893 0 $self->slurmfile($self->outdir."/".$self->jobname."_".$self->batch_counter.".sh");
894  
895 0 ▶ $fh = IO::File->new( $self->cmdfile, q{>} ) or print "Error opening file ".$self->cmdfile." ".$!;
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::File"->new($self->cmdfile, ">") or print 'Error opening file ' . $self->cmdfile . ' ' . $! 0 0 0
896  
897 0 ▶ print $fh $self->batch if defined $fh && defined $self->batch;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fh and defined $self->batch 0 0 0
Condition0%0/3
defined $fh and defined $self->batch
!ll&&!rl&&r
000
MC/DC0%0/2
defined $fh and defined $self->batch
defined $fh defined $self->batch
Truth table0%0/3
A and B
A= defined $fhB= defined $self->batch
ABresult
0X0
100
111
898 0 $fh->close;
899  
900 0 my $ok;
901 0 ▶ if($self->wait){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->wait) 0 0 0
902 0 ▶ $ok = join(":", @{$self->jobref->[-2]}) if $self->jobref->[-2];
Branch: 1 branch
BranchTrueFalseTotal
if $self->jobref->[-2] 0 0 0
903 }
904  
905  
906 0 $command = $self->process_batch_command();
907 0 $DB::single=2;
908  
909 0 ▶ $self->template->process($self->template_file,
Branch: 1 branch
BranchTrueFalseTotal
$self->template->process($self->template_file, {"JOBNAME", $self->jobname . '_' . $self->batch_counter, "USER", $self->user, "NODE", $self->node, "CPU", $self->cpus_per_task, "PARTITION", $self->partition, "AFTEROK", $ok, "OUT", $self->logdir . '/' . $self->jobname . '_' . $self->batch_counter . '.log', "MODULE", $self->module, "self", $self, "COMMAND", $command}, $self->slurmfile) or die $self->template->error 0 0 0
910 { JOBNAME => $self->jobname."_".$self->batch_counter,
911 USER => $self->user,
912 NODE => $self->node,
913 CPU => $self->cpus_per_task,
914 PARTITION => $self->partition,
915 AFTEROK => $ok,
916 OUT => $self->logdir."/".$self->jobname."_".$self->batch_counter.".log",
917 MODULE => $self->module,
918 self => $self,
919 COMMAND => $command },
920 $self->slurmfile
921 ) || die $self->template->error;
922  
923 0 chmod 0777, $self->slurmfile;
924  
925 0 ▶ $self->submit_slurm if $self->submit_to_slurm;
Branch: 1 branch
BranchTrueFalseTotal
if $self->submit_to_slurm 0 0 0
926 }
927  
928 =head2 process_batch_command
929  
930 splitting this off from the main command
931  
932 =cut
933  
934 #TODO add support for custom commands
935  
936 sub process_batch_command {
937 0 ▶ my($self) = @_;
Subroutine process_batch_command: not called CC=5 SCAR=34.0
Pod documented
938 0 my $command;
939  
940 #Giving outdir/jobname doesn't work unless a full file path is supplied
941 #Need to get absolute path going on...
942 #$self->cmdfile($self->jobname."_batch".$self->batch_counter.".in");
943  
944 0 ▶ if($self->has_custom_command){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_custom_command) 0 0 0
945 0 $command = "cd ".getcwd()."\n";
946 0 $command .= $self->custom_command." --procs ".$self->procs." --infile ".$self->cmdfile." --outdir ".$self->outdir." --logname ".$self->jobname."_".$self->batch_counter;
947 }
948 0 ▶ if($self->use_gnuparallel){
Branch: 3 branches
BranchTrueFalseTotal
if ($self->use_gnuparallel) { } 0 0 0
elsif ($self->use_threads) { } 0 0 0
elsif ($self->use_processes) { } 0 0 0
949 0 $command = "cd ".getcwd()."\n";
950 0 $command .= "cat ".$self->cmdfile." | parallelparser.pl | parallel --joblog ".$self->outdir."/main.log --gnu -N 1 -q gnuparallelrunner.pl --command `echo {}` --outdir ".$self->outdir." --logname ".$self->jobname."_".$self->batch_counter."\n";
951 }
952 elsif($self->use_threads){
953 0 $command = "cd ".getcwd()."\n"; #Go back to the working directory just to be safe
954 0 $command .= "paralellrunner.pl --procs ".$self->procs." --infile ".$self->cmdfile." --outdir ".$self->outdir." --logname ".$self->jobname."_".$self->batch_counter;
955 }
956 elsif($self->use_processes){
957 0 $command = "cd ".getcwd()."\n";
958 0 $command .= "mcerunner.pl --procs ".$self->procs." --infile ".$self->cmdfile." --outdir ".$self->outdir." --logname ".$self->jobname."_".$self->batch_counter;
959 }
960 else{
961 0 die print "None of the job processes were chosen!\n";
962 }
963  
964 0 return $command;
965 }
966  
967 =head2 submit_slurm()
968  
969 Submit jobs to slurm queue using sbatch.
970  
971 This subroutine was just about 100% from the following perlmonks discussions. All that I did was add in some logging.
972  
973 http://www.perlmonks.org/?node_id=151886
974 You can use the script at the top to test the runner. Just download it, make it executable, and put it in the infile as
975  
976 perl command.pl 1
977 perl command.pl 2
978 #so on and so forth
979  
980 =cut
981  
982 sub submit_slurm{
983 0 ▶ my $self = shift;
Subroutine submit_slurm: not called CC=10 SCAR=47.0
Pod documented
984  
985 0 my ($infh,$outfh,$errfh);
986 0 $errfh = gensym(); # if you uncomment this line, $errfh will
987 # never be initialized for you and you
988 # will get a warning in the next print
989 # line.
990 0 my $cmdpid;
991 0 eval{
992 0 $cmdpid = open3($infh, $outfh, $errfh, "sbatch ".$self->slurmfile);
993 };
994 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
995  
996 0 my $sel = new IO::Select; # create a select object
997 0 $sel->add($outfh,$errfh); # and add the fhs
998  
999 0 my($stdout, $stderr, $jobid);
1000  
1001 0 ▶ while(my @ready = $sel->can_read) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ready) = $sel->can_read) 0 0 0
1002 0 foreach my $fh (@ready) { # loop through them
1003 0 my $line;
1004 # read up to 4096 bytes from this fh.
1005 0 my $len = sysread $fh, $line, 4096;
1006 0 ▶ if(not defined $len){
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $len) { } 0 0 0
elsif ($len == 0) { } 0 0 0
1007 # There was an error reading
1008 0 $self->log->fatal("Error from child: $!");
1009 } elsif ($len == 0){
1010 # Finished reading from this FH because we read
1011 # 0 bytes. Remove this handle from $sel.
1012 0 $sel->remove($fh);
1013 0 next;
1014 } else { # we read data alright
1015 0 ▶ if($fh == $outfh) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fh == $outfh) { } 0 0 0
elsif ($fh == $errfh) { } 0 0 0
1016 0 $stdout .= $line;
1017 0 $self->log->info($line);
1018 } elsif($fh == $errfh) {
1019 0 $stderr .= $line;
1020 0 $self->log->error($line);
1021 } else {
1022 0 $self->log->fatal("Shouldn't be here!\n");
1023 }
1024 }
1025 }
1026 }
1027  
1028 0 waitpid($cmdpid, 1);
1029 0 my $exitcode = $?;
1030  
1031 0 ▶ ($jobid) = $stdout =~ m/Submitted batch job (\d.*)$/ if $stdout;
Branch: 1 branch
BranchTrueFalseTotal
if $stdout 0 0 0
1032 0 ▶ if(!$jobid){
Branch: 1 branch
BranchTrueFalseTotal
if (not $jobid) { } 0 0 0
1033 0 print "No job was submitted! Please check to make sure you have loaded modules shared and slurm!\nFull error is:\t$stderr\n$stdout\nEnd Job error";
1034 0 print "Submit scripts will be written, but will not be submitted to the queue. Please look at your files in ".$self->outdir." for more information\n";
1035 0 $self->submit_to_slurm(0);
1036 }
1037 else{
1038 0 push(@{$self->jobref->[-1]}, $jobid);
1039 0 print "Submitting job ".$self->slurmfile."\n\tWith Slurm jobid $jobid\n";
1040 }
1041 }
1042  
1043 =head1 AUTHOR
1044  
1045 Jillian Rowe, C<< <jillian.e.rowe at gmail.com> >>
1046  
1047 =head1 BUGS
1048  
1049 Please report any bugs or feature requests to C<bug-runner-init at rt.cpan.org>, or through
1050 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HPC-Runner-Slurmm>. I will be notified, and then you'll
1051 automatically be notified of progress on your bug as I make changes.
1052  
1053  
1054 =head1 SUPPORT
1055  
1056 You can find documentation for this module with the perldoc command.
1057  
1058 perldoc HPC::Runner::Slurm
1059  
1060  
1061 You can also look for information at:
1062  
1063 =over 4
1064  
1065 =item * RT: CPAN's request tracker (report bugs here)
1066  
1067 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=HPC-Runner-Slurm>
1068  
1069 =item * AnnoCPAN: Annotated CPAN documentation
1070  
1071 L<http://annocpan.org/dist/HPC-Runner-Slurm>
1072  
1073 =item * CPAN Ratings
1074  
1075 L<http://cpanratings.perl.org/d/HPC-Runner-Slurm>
1076  
1077 =item * Search CPAN
1078  
1079 L<http://search.cpan.org/dist/HPC-Runner-Slurm/>
1080  
1081 =back
1082  
1083 =head1 Acknowledgements
1084  
1085 This module was originally developed at and for Weill Cornell Medical
1086 College in Qatar within ITS Advanced Computing Team. With approval from
1087 WCMC-Q, this information was generalized and put on github, for which
1088 the authors would like to express their gratitude.
1089  
1090 =head1 LICENSE AND COPYRIGHT
1091  
1092 Copyright 2014 Weill Cornell Medical College in Qatar.
1093  
1094 This program is free software; you can redistribute it and/or modify it
1095 under the terms of the the Artistic License (2.0). You may obtain a
1096 copy of the full license at:
1097  
1098 L<http://www.perlfoundation.org/artistic_license_2_0>
1099  
1100 Any use, modification, and distribution of the Standard or Modified
1101 Versions is governed by this Artistic License. By using, modifying or
1102 distributing the Package, you accept this license. Do not use, modify,
1103 or distribute the Package, if you do not accept this license.
1104  
1105 If your Modified Version has been derived from a Modified Version made
1106 by someone other than you, you are nevertheless required to ensure that
1107 your Modified Version complies with the requirements of this license.
1108  
1109 This license does not grant you the right to use any trademark, service
1110 mark, tradename, or logo of the Copyright Holder.
1111  
1112 This license includes the non-exclusive, worldwide, free-of-charge
1113 patent license to make, have made, use, offer to sell, sell, import and
1114 otherwise transfer the Package with respect to any patent claims
1115 licensable by the Copyright Holder that are necessarily infringed by the
1116 Package. If you institute patent litigation (including a cross-claim or
1117 counterclaim) against any party alleging that the Package constitutes
1118 direct or contributory patent infringement, then this Artistic License
1119 to you shall terminate on the date that such litigation is filed.
1120  
1121 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
1122 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
1123 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
1124 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
1125 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
1126 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
1127 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
1128 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1129  
1130  
1131 =cut
1132  
1133 1 __PACKAGE__->meta->make_immutable;
1134 #use namespace::autoclean;
1135  
1136 1 1;
↑ Summary Build »