2 # dqsub submits jobs using qsub with better options
3 # and is released under the terms of the GNU GPL version 3, or any
4 # later version, at your option. See the file README and COPYING for
6 # Copyright 2014 by Don Armstrong <don@donarmstrong.com>.
17 dqsub - submits jobs using qsub with better options
24 --queue, -q Queue to use
25 --interactive, -I call qsub interactively
27 --array array mode (one of 'chdir' or 'xargs' or '')
28 --array-from file to read arrays from (default STDIN)
29 --array-per-job number of array items to handle in each job (default 1)
30 --array-all-in-one-job Run all of the array items in one job
31 --ppn processors per node to use
32 --mem memory to request
33 --dir Directory to run the script in (default current directory)
34 --account, -A Account name to use
35 --join, -J join error and output streams (default)
36 --name, -N Name of the job
37 --debug, -d debugging level (Default 0)
38 --help, -h display this help
39 --man, -m display manual
47 This describes how dqsub will generate array jobs.
49 If no B<--array> is given, then the command and any additional
50 arguments given will be run using qsub.
52 If B<--array> is C<chdir>, then each line of the input given in
53 B<--array-from> will be used as a directory and the command and any
54 additional arguments given will run in each directory.
56 IF B<--array> is C<xargs>, then each line of the input given will be
57 considered to be an additional argument which will be given to the
58 command run in the current directory.
62 File to read array arguments from. If not provided, and B<--array> is
63 given, arguments will be read from STDIN.
65 =item B<--account, -A>
71 Whether to join STDOUT and STDERR. On by default; disable with
76 Which batch system to use. If sbatch exists, assume it's slurm,
81 Debug verbosity. (Default 0)
85 Display brief usage information.
100 use Cwd qw(getcwd abs_path);
102 use List::Util qw(min);
105 my %options = (nodes => 1,
116 GetOptions(\%options,
122 'array_from|array-from=s',
123 'array_per_job|array-per-job=i',
124 'array_slot_limit|array-slot-limit=i',
125 'array_all_in_one_job|array-all-in-one-job!',
126 'ppn|cpus|processors-per-node=i',
130 'time|walltime=s','cputime|cput=s','host=s',
131 'pmem|process_mem|process-mem=s',
132 'pvmem|process_virtual_mem|process-virtiual-mem=s',
133 'max_file|max-file|file=s',
136 'debug|d+','help|h|?','man|m');
138 # pod2usage() if $options{help};
139 # pod2usage({verbose=>2}) if $options{man};
141 $DEBUG = $options{debug};
144 if (not @ARGV and not $options{interactive}) {
145 push @USAGE_ERRORS,"You must provide a command to run";
147 if (defined $options{array} and $options{array} !~ /^(?:|chdir|xargs)$/i) {
148 push @USAGE_ERRORS,"--array must be one of chdir, xargs or '' if provided";
149 $options{array} = lc($options{array});
150 if ($options{array} eq '') {
151 $options{array} = undef;
154 if ($options{interactive} and @ARGV) {
155 push @USAGE_ERRORS,"Don't provide commands when you're asking for an interactive shell";
158 if (not defined $options{batch}) {
161 $options{batch} = 'slurm'
163 $options{batch} = 'pbs'
167 if ($options{batch} !~ /^pbs|slurm$/) {
168 push @USAGE_ERRORS,"Unsupported batch system '$options{batch}'; ".
169 "supported systems are pbs or slurm";
172 # pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
174 print STDERR map {"$_\n"} @USAGE_ERRORS;
179 my $JOB_SUBMITTER = 'qsub';
180 # OK. Generate the options to qsub which we'll be using
182 if ($options{batch} eq 'pbs') {
183 @qsub_options = generate_qsub_options(\%options,\@ARGV);
184 $JOB_SUBMITTER = 'qsub';
185 } elsif ($options{batch} eq 'slurm') {
186 @qsub_options = generate_slurm_options(\%options,\@ARGV);
187 $JOB_SUBMITTER = 'sbatch';
189 die "Unsupported batch system '$options{batch}'";
193 if ($options{interactive}) {
194 print STDERR 'running: qsub '.join(' ',@qsub_options) if $DEBUG;
195 exec('qsub',@qsub_options);
198 if ($options{array}) {
199 @array = read_array_options(\%options) if $options{array};
200 # the -t option gives the range of elements for an array job
201 if ($options{array_all_in_one_job}) {
202 $options{array_per_job} = scalar @array;
204 push @qsub_options,'-t','1-'. ceil(scalar @array / $options{array_per_job});
205 if ($options{array_slot_limit}) {
206 $qsub_options[$#qsub_options] .= '%'.$options{array_slot_limit};
210 call_qsub(\@qsub_options,write_qsub_script(\%options,\@ARGV,\@array));
213 sub generate_qsub_options{
214 my ($options,$args) = @_;
216 if (defined $options->{queue} and length $options->{queue}) {
217 push @qo,'-q',$options->{queue};
219 ## handle the -l options
221 push @l, 'nodes='.$options->{nodes};
222 if (defined $options->{ppn}) {
223 $l[$#l] .= ':ppn='.$options->{ppn};
225 if (defined $options->{account}) {
226 push @qo,'-A',$options->{account};
237 for my $k (keys %l_options) {
238 if ($options->{$k}) {
239 push @l,$l_options{$k}.'='.$options{$k};
242 push @qo,'-l',join(',',@l) if @l;
243 if ($options->{interactive}) {
246 if ($options->{name}) {
247 push @qo,'-N',$options->{name};
249 push @qo,'-N',join('_',
250 map {my $a = $_; $a =~ s/[^a-zA-Z0-9]*//g; $a;}
251 @{$args}[0..min($#{$args},2)]);
253 # join error and output streams
254 if ($options->{join}) {
260 sub generate_slrum_options{
261 my ($options,$args) = @_;
263 if (defined $options->{queue} and length $options->{queue}) {
264 push @qo,'-p',$options->{queue};
266 ## handle the -l options
267 if (defined $options->{account}) {
268 push @qo,'-A',$options->{account};
272 ppn => 'cpus-per-task',
280 for my $k (keys %options_map) {
281 if ($options->{$k}) {
282 push @qo,'--'.$options_map{$k}.'=',$options{$k};
286 push @qo,'--mem=',$options{mem};
288 if ($options->{interactive}) {
291 if ($options->{name}) {
292 push @qo,'-J',$options->{name};
294 push @qo,'-J',join('_',
295 map {my $a = $_; $a =~ s/[^a-zA-Z0-9]*//g; $a;}
296 @{$args}[0..min($#{$args},2)]);
298 # join error and output streams
299 if ($options->{join}) {
305 sub read_array_options{
308 if (defined $options->{array_from}) {
309 $fh = IO::File->new(defined $options->{array_from}) or
310 die "Unable to open $options->{array_from} for reading: $!";
321 my ($qsub_options,$script) = @_;
323 open $qsub_fh,'|-',$JOB_SUBMITTER,@{$qsub_options},'-' or
324 die "Unable to start $JOB_SUBMITTER: $!";
325 print {$qsub_fh} $script or
326 die "Unable to print to $JOB_SUBMITTER: $!";
328 die "Unable to close $JOB_SUBMITTER filehandle: $!";
331 sub write_qsub_script {
332 my ($opt,$arg,$array) = @_;
334 my $script = "#!/bin/bash\n";
335 my $command = join(' ',map {$_ =~ /\s/?qq('$_'):$_} @{$arg});
337 # this script was written by dqsub
339 my $directory = getcwd;
340 if (defined $opt->{dir}) {
341 $directory = abs_path($opt->{dir});
343 # we really should be quoting this instead
345 # change to the working directory
348 if (defined $opt->{array}) {
349 my @subshell = ('','');
350 my $array_opt = join("\n",@{$array});
351 my $max_array = scalar @{$array};
352 my $apjm1 = $opt->{array_per_job} - 1;
354 if [ -n "\$PBS_ARRAYID" ]; then
355 MYARRAYID="\${PBS_ARRAYID:=1}"
357 MYARRAYID="\${SLURM_ARRAY_TASK_ID:=1}"
360 if ($opt->{array_per_job} > 1) {
361 # we will use subshells if there are more than one array
363 @subshell = ('(',')');
365 for i in \$(seq 1 $opt->{array_per_job}); do
366 # in some cases, the jobs aren't going to come out evenly. Handle that.
367 JOBNUM=\$(( \${MYARRAYID:=1} * $opt->{array_per_job} + \$i - $opt->{array_per_job} ))
368 if [ \$JOBNUM -le $max_array ]; then
369 OPT=\$(sed -n -e "\$JOBNUM p"<<'_HERE_DOC_END_'
373 OPT=\$(sed -n -e "\${MYARRAYID:=1} p"<<'_HERE_DOC_END_'
381 if ($opt->{array} eq 'chdir') {
391 exec ${command} "\$OPT";
395 if ($opt->{array_per_job} > 1) {
403 # there's no array, so just executing the command with arguments