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 Debug verbosity. (Default 0)
80 Display brief usage information.
95 use Cwd qw(getcwd abs_path);
97 use List::Util qw(min);
100 my %options = (nodes => 1,
111 GetOptions(\%options,
116 'array_from|array-from=s',
117 'array_per_job|array-per-job=i',
118 'array_slot_limit|array-slot-limit=i',
119 'array_all_in_one_job|array-all-in-one-job!',
120 'ppn|processors-per-node=i',
124 'time|walltime=s','cputime|cput=s','host=s',
125 'pmem|process_mem|process-mem=s',
126 'pvmem|process_virtual_mem|process-virtiual-mem=s',
127 'max_file|max-file|file=s',
130 'debug|d+','help|h|?','man|m');
132 # pod2usage() if $options{help};
133 # pod2usage({verbose=>2}) if $options{man};
135 $DEBUG = $options{debug};
138 if (not @ARGV and not $options{interactive}) {
139 push @USAGE_ERRORS,"You must provide a command to run";
141 if (defined $options{array} and $options{array} !~ /^(?:|chdir|xargs)$/i) {
142 push @USAGE_ERRORS,"--array must be one of chdir, xargs or '' if provided";
143 $options{array} = lc($options{array});
144 if ($options{array} eq '') {
145 $options{array} = undef;
148 if ($options{interactive} and @ARGV) {
149 push @USAGE_ERRORS,"Don't provide commands when you're asking for an interactive shell";
152 # pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
153 print STDERR join("\n",@USAGE_ERRORS) and exit 1 if @USAGE_ERRORS;
155 # OK. Generate the options to qsub which we'll be using
156 my @qsub_options = generate_qsub_options(\%options,\@ARGV);
158 if ($options{interactive}) {
159 print STDERR 'running: qsub '.join(' ',@qsub_options) if $DEBUG;
160 exec('qsub',@qsub_options);
163 if ($options{array}) {
164 @array = read_array_options(\%options) if $options{array};
165 # the -t option gives the range of elements for an array job
166 if ($options{array_all_in_one_job}) {
167 $options{array_per_job} = scalar @array;
169 push @qsub_options,'-t','1-'. ceil(scalar @array / $options{array_per_job});
170 if ($options{array_slot_limit}) {
171 $qsub_options[$#qsub_options] .= '%'.$options{array_slot_limit};
175 call_qsub(\@qsub_options,write_qsub_script(\%options,\@ARGV,\@array));
178 sub generate_qsub_options{
179 my ($options,$args) = @_;
181 if (defined $options->{queue} and length $options->{queue}) {
182 push @qo,'-q',$options->{queue};
184 if (defined $options->{dir}) {
185 push @qo,'-d',abs_path($options->{dir});
187 push @qo,'-d',getcwd;
189 ## handle the -l options
191 push @l, 'nodes='.$options->{nodes};
192 if (defined $options->{ppn}) {
193 $l[$#l] .= ':ppn='.$options->{ppn};
195 if (defined $options->{account}) {
196 push @qo,'-A',$options->{account};
207 for my $k (keys %l_options) {
208 if ($options->{$k}) {
209 push @l,$l_options{$k}.'='.$options{$k};
212 push @qo,'-l',join(',',@l) if @l;
213 if ($options->{interactive}) {
216 if ($options->{name}) {
217 push @qo,'-N',$options->{name};
219 push @qo,'-N',join('_',@{$args}[0..min($#{$args},2)]);
221 # join error and output streams
222 if ($options->{join}) {
228 sub read_array_options{
231 if (defined $options->{array_from}) {
232 $fh = IO::File->new(defined $options->{array_from}) or
233 die "Unable to open $options->{array_from} for reading: $!";
244 my ($qsub_options,$script) = @_;
246 open $qsub_fh,'|-','qsub',@{$qsub_options},'-' or
247 die "Unable to start qsub: $!";
248 print {$qsub_fh} $script or
249 die "Unable to print to qsub: $!";
251 die "Unable to close qsub filehandle: $!";
254 sub write_qsub_script {
255 my ($opt,$arg,$array) = @_;
257 my $script = "#!/bin/bash\n";
258 my $command = join(' ',map {qq('$_')} @{$arg});
260 # this script was written by dqsub
262 if (defined $opt->{array}) {
263 my @subshell = ('','');
264 my $array_opt = join("\n",@{$array});
265 my $max_array = scalar @{$array};
266 my $apjm1 = $opt->{array_per_job} - 1;
267 if ($opt->{array_per_job} > 1) {
268 # we will use subshells if there are more than one array
270 @subshell = ('(',')');
272 for i in \$(seq 1 $opt->{array_per_job}); do
273 # in some cases, the jobs aren't going to come out evenly. Handle that.
274 JOBNUM=\$(( \${PBS_ARRAYID:=1} * $opt->{array_per_job} + \$i - $opt->{array_per_job} ))
275 if [ \$JOBNUM -le $max_array ]; then
276 OPT=\$(sed -n -e "\$JOBNUM p"<<'_HERE_DOC_END_'
280 OPT=\$(sed -n -e "\${PBS_ARRAYID:=1} p"<<'_HERE_DOC_END_'
288 if ($opt->{array} eq 'chdir') {
298 exec ${command} "\$OPT";
302 if ($opt->{array_per_job} > 1) {
310 # there's no array, so just executing the command with arguments