blib/lib/HPC/Runner/PBS.pm

filter: Statementstmt 49.0% 53 / 108 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 77.7% 14 / 18 Podpod 100% 3 / 3 totaltotal 47.6% 70 / 147 CC 11 SCAR 34.1
CC
11
Cov
42%
submit_slurm
90.0
run
6.0
get_nodes
6.0
CRAP 30.4
↑ Summary Build »
1 package HPC::Runner::PBS;
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 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 extends 'HPC::Runner';
21 1 extends 'HPC::Runner::Slurm';
22 1 with 'MooseX::SimpleConfig';
23  
24 1 our $VERSION = '0.05';
25  
26 # For pretty man pages!
27 1 $ENV{TERM}='xterm-256color';
28  
29 =encoding utf-8
30  
31 =head1 NAME
32  
33 HPC::Runner::PBS - Submit jobs to a PBS job scheduler.
34  
35 =head1 DESCRIPTION
36  
37 HPC::Runner::PBS is a wrapper around qsub and can be used to submit arbirtary bash commands to PBS.
38  
39 It has two levels of management. The first is the main qsub command, and the second is the actual job, which runs commands in parallel, controlled by HPC::Runner::Threads or HPC::Runner::MCE.
40  
41 It supports job dependencies. Put in the command 'wait' to tell PBS that some job or jobs depend on some other jobs completion. Put in the command 'newnode' to tell HPC::Runner::PBS to submit the job to a new node.
42  
43 The only necessary option is the --infile, and --queue if you wish to run on a queue besides s48.
44  
45 The bulk of this code is extended from HPC::Runner::Slurm.
46  
47 =head2 queue
48  
49 Same as the partition in HPC::Runner::Slurm
50  
51 =cut
52  
53 1 has 'queue' => (
54 is => 'rw',
55 isa => 'Str',
56 required => 1,
57 #default => 'adserial',
58 default => q(),
59 documentation => q{PBS queue for job submission. Defaults is none, pbs decides.},
60 predicate => 'has_queue',
61 clearer => 'clear_queue',
62 );
63  
64 =head2 walltime
65  
66 Define PBS walltime
67  
68 =cut
69  
70 1 has 'walltime' => (
71 is => 'rw',
72 isa => 'Str',
73 required => 1,
74 default => '04:00:00',
75 predicate => 'has_walltime',
76 clearer => 'clear_walltime,'
77 );
78  
79 =head2 mem
80  
81 =cut
82  
83 1 has 'mem' => (
84 is => 'rw',
85 isa => 'Str|Undef',
86 predicate => 'has_mem',
87 clearer => 'clear_mem',
88 required => 0,
89 documentation => q{Supply a memory limit},
90 );
91  
92 =head2 template_file
93  
94 actual template file
95  
96 One is generated here for you, but you can always supply your own with --template_file /path/to/template
97  
98 =cut
99  
100 has '+template_file' => (
101 traits => ['NoGetopt'],
102 is => 'rw',
103 isa => 'Str',
104 default => sub {
105 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
106  
107 0 my($fh, $filename) = tempfile();
108  
109 0 my $tt =<<EOF;
110 #!/bin/bash
111 #
112 #PBS -N [% JOBNAME %]
113 [% IF self.has_queue %]
114 #PBS -q [% self.queue %]
115 [% END %]
116 #PBS -l nodes=1:ppn=[% CPU %]
117 [% IF self.has_walltime %]
118 #PBS -l walltime=[% self.walltime %]
119 [% END %]
120 #PBS -j oe
121 #PBS -o localhost:[% OUT %]
122 [% IF self.has_mem %]
123 #PBS -l mem=[% self.mem %]
124 [% END %]
125  
126 [% IF AFTEROK %]
127 #PBS -W depend=afterok:[% AFTEROK %]
128 [% END %]
129  
130 [% IF MODULE %]
131 [% FOR d = MODULE %]
132 module load [% d %]
133 [% END %]
134 [% END %]
135  
136 [% COMMAND %]
137 EOF
138  
139 0 print $fh $tt;
140 0 $DB::single=2;
141 0 return $filename;
142 },
143 1 );
144  
145 =head1 SUBROUTINES/METHODS
146  
147 =cut
148  
149 =head2 run()
150  
151 First sub called
152 Calling system module load * does not work within a screen session!
153  
154 =cut
155  
156 sub run {
157 0 ▶ my $self = shift;
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
158  
159 0 $DB::single=2;
160  
161 0 $self->logname('pbs_logs');
162 0 $self->log($self->init_log);
163 0 $DB::single=2;
164  
165 0 ▶ if($self->serial){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->serial) 0 0 0
166 0 $self->procs(1);
167 }
168  
169 0 $self->check_files;
170 0 $self->parse_file_slurm;
171 0 $DB::single=2;
172 }
173  
174 sub get_nodes {
175 0 ▶ my($self) = @_;
Subroutine get_nodes: not called CC=2 SCAR=17.9
Pod documented
176  
177 0 ▶ $self->partition($self->queue) if $self->partition eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $self->partition eq "" 0 0 0
178  
179 0 $DB::single=2;
180  
181 0 $self->nodelist([]);
182  
183 0 $DB::single=2;
184 }
185  
186 =head2 submit_slurm()
187  
188 Submit jobs to PBS using qsub
189  
190 Uses almost the same logic as submit_slurm, so we'll keep that.
191  
192 This subroutine was just about 100% from the following perlmonks discussions. All that I did was add in some logging.
193  
194 http://www.perlmonks.org/?node_id=151886
195 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
196  
197 perl command.pl 1
198 perl command.pl 2
199 #so on and so forth
200  
201 =cut
202  
203 sub submit_slurm{
204 0 ▶ my $self = shift;
Subroutine submit_slurm: not called CC=9 SCAR=45.0
Pod documented
205  
206 0 my ($infh,$outfh,$errfh);
207 0 $errfh = gensym(); # if you uncomment this line, $errfh will
208 # never be initialized for you and you
209 # will get a warning in the next print
210 # line.
211 0 my $cmdpid;
212 0 eval{
213 0 $cmdpid = open3($infh, $outfh, $errfh, "qsub ".$self->slurmfile);
214 };
215 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
216  
217 0 my $sel = new IO::Select; # create a select object
218 0 $sel->add($outfh,$errfh); # and add the fhs
219  
220 0 my($stdout, $stderr, $jobid);
221  
222 0 ▶ while(my @ready = $sel->can_read) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ready) = $sel->can_read) 0 0 0
223 0 foreach my $fh (@ready) { # loop through them
224 0 my $line;
225 # read up to 4096 bytes from this fh.
226 0 my $len = sysread $fh, $line, 4096;
227 0 ▶ if(not defined $len){
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $len) { } 0 0 0
elsif ($len == 0) { } 0 0 0
228 # There was an error reading
229 0 $self->log->fatal("Error from child: $!");
230 } elsif ($len == 0){
231 # Finished reading from this FH because we read
232 # 0 bytes. Remove this handle from $sel.
233 0 $sel->remove($fh);
234 0 next;
235 } else { # we read data alright
236 0 ▶ if($fh == $outfh) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fh == $outfh) { } 0 0 0
elsif ($fh == $errfh) { } 0 0 0
237 0 $stdout .= $line;
238 0 $self->log->info($line);
239 } elsif($fh == $errfh) {
240 0 $stderr .= $line;
241 0 $self->log->error($line);
242 } else {
243 0 $self->log->fatal("Shouldn't be here!\n");
244 }
245 }
246 }
247 }
248  
249 0 waitpid($cmdpid, 1);
250 0 my $exitcode = $?;
251  
252 #($jobid) = $stdout =~ m/(\w.*)$/ if $stdout;
253 0 ($jobid) = $stdout;
254 0 ▶ if(!$jobid){
Branch: 1 branch
BranchTrueFalseTotal
if (not $jobid) { } 0 0 0
255 0 print "No job was submitted! Please check your things!\nFull error is:\t$stderr\n$stdout\nEnd Job error";
256 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";
257 0 $self->submit_to_slurm(0);
258 }
259 else{
260 0 push(@{$self->jobref->[-1]}, $jobid);
261 0 print "Submitting job ".$self->slurmfile."\n\tWith PBS jobid $jobid\n";
262 }
263 }
264  
265  
266 1 __PACKAGE__->meta->make_immutable;
267 1 1;
268  
269 __END__
270  
271  
272 =head1 SYNOPSIS
273  
274 use HPC::Runner::PBS;
275  
276 =head1 AUTHOR
277  
278 Jillian Rowe E<lt>jillian.e.rowe@gmail.comE<gt>
279  
280 =head1 Acknowledgements
281  
282 This module was originally developed at and for NYU Abu Dhabi in the Center for Genomics and Systems Biology.
283 With approval from NYUAD, this information was generalized and put on github, for which
284 the authors would like to express their gratitude.
285  
286 =head1 COPYRIGHT
287  
288 Copyright 2015- Jillian Rowe
289  
290 =head1 LICENSE
291  
292 This library is free software; you can redistribute it and/or modify
293 it under the same terms as Perl itself.
294  
295 =head1 SEE ALSO
296  
297 =cut
↑ Summary Build »