blib/lib/HPC/Runner/Slurm.pm

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