/
qsub_condense
executable file
·228 lines (189 loc) · 5.83 KB
/
qsub_condense
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/perl -w
use strict;
use Cwd 'abs_path';
use IPC::Open2;
use File::Basename;
use File::Spec::Functions;
my $processors_per_node = 8;
sub usage {
my $p = $processors_per_node;
print <<EOF
Usage: $0 [options] job1.sh [job2.sh [job3.sh ...]]
Submits all your jobs scripts, but tries to combine smaller jobs
together into groups of $p to ensure that you allocate the minimum
number of nodes possible.
To use this script effectively, you must set the following line in
your job script:
#PBS -l nodes=N:ppn=P
... where N*P is the total number of processors you'd like to run your
script on.
This script also recognizes the following options in each job file:
name=name - set with '#PBS -N name'
output=out - set with '#PBS -o out'
error=err - set with '#PBS -e err'
join - set with '#PBS -j oe'
These options take the default values supplied by PBS if not specified.
EOF
;
die join("\n",@_) . "\n" if @_;
}
usage("Please supply a job file") if not (@ARGV);
if ($ARGV[0] eq '-h' or $ARGV[0] eq '--help') {
usage();
exit(0);
}
# collect info about the files
my @files;
my %procs;
my %names;
my %output_directives;
my %directories;
my @qsub_options;
foreach my $option (@ARGV) {
# see if the item passed in is a file
if (not (-e $option and -f $option and -r $option)) {
push @qsub_options, $option;
next;
}
my $file = $option;
# get the full path of the file
my $absfile = abs_path($file);
my ($filename, $directory, $suffix) = fileparse($absfile);
$directories{$absfile} = $directory;
#search through the file and get the number of processes it needs.
open(SCRIPT, $file) or die "Can't open file $file: $!\n";
$names{$absfile}='';
my $this_proc_count = 0;
my $join_std_err = 0;
my $output = '';
my $error = '';
while (my $line = <SCRIPT>) {
if ($line =~ /^#PBS -l nodes=(\d+):ppn=(\d+)/) {
my $nodes = $1;
my $ppn = $2;
$this_proc_count = $nodes*$ppn;
}
$names{$absfile} = $1 if $line =~ /^#PBS -N (.*)/;
$join_std_err = 1 if $line =~ /^#PBS -j oe/;
$output = $1 if $line =~ /^#PBS -o (.*)/;
$error = $1 if $line =~ /^#PBS -e (.*)/;
}
$procs{$absfile} = $this_proc_count;
$output_directives{$absfile} = "";
if ($output) {
if (file_name_is_absolute($output)) {
$output_directives{$absfile} .= " >| $output";
} else {
$output_directives{$absfile} .= " >| $directory/$output";
}
} else {
if ($names{$absfile}) {
$output_directives{$absfile} .= " >| $directory/$names{$absfile}.o\$PBS_JOBID";
} else {
$output_directives{$absfile} .= " >| $absfile.o\$PBS_JOBID";
}
}
if ($error) {
if (file_name_is_absolute($error)) {
$output_directives{$absfile} .= " 2>| $error";
} else {
$output_directives{$absfile} .= " 2>| $directory/$error";
}
} elsif ($join_std_err) {
$output_directives{$absfile} .= " 2>&1";
} else {
if ($names{$absfile}) {
$output_directives{$absfile} .= " 2>| $directory/$names{$absfile}.e\$PBS_JOBID";
} else {
$output_directives{$absfile} .= " 2>| $absfile.e\$PBS_JOBID";
}
}
$names{$absfile} = $file if not $names{$absfile};
push @files, $absfile;
}
# sort the file list by processor count mod 8. 8 => 8, not 0.
@files = sort { leftover($procs{$b}) <=> leftover($procs{$a}); } @files;
sub leftover {
my ($x) = @_;
return (($x-1) % $processors_per_node)+1;
}
# bin the jobs together. Try to minimize the total number of
#processors allocated while maximizing the number of jobs created.
my $bins = [];
foreach my $file (@files) {
#search through all the existing bins for a place to put this job.
my $found_free_space=0;
foreach my $bin (@$bins) {
if ($bin->{free} >= leftover($procs{$file})) {
$bin->{free} -= leftover($procs{$file});
push @{$bin->{files}}, $file;
$bin->{proc_count} += $procs{$file};
$found_free_space=1;
last;
}
}
if (not $found_free_space) {
#make a new bin.
my $bin = {};
$bin->{free} = $processors_per_node-leftover($procs{$file});
$bin->{files} = [$file];
$bin->{proc_count} = $procs{$file};
push @$bins, $bin;
}
}
# print out jobs for each bin.
my $counter = 0;
foreach my $bin (@$bins) {
my @bin_files = @{$bin->{files}};
my $job_name = join(":", @names{@bin_files});
# limit the job name to 250 characters
$job_name = substr($job_name, 0, 250);
my $node_count = int(($bin->{proc_count}+$processors_per_node-1)/$processors_per_node);
# write out the actual qsub script
#open(OUTPUT, ">test$counter"); $counter++;
open2(\*INPUT, \*OUTPUT, 'qsub '.join(" ",@qsub_options));
print OUTPUT <<EOF
#!/bin/bash
##################################
## YOU CAN SET THESE PARAMETERS ##
# Join output and error into one file
#PBS -j oe
# Name of the job that shows up in qstat
#PBS -N $job_name
# How many nodes and processes per node to use.
# Always use 8 to avoid wasting processors
#PBS -l nodes=$node_count:ppn=8
# Set the job to run for 10 days maximum of one day.
# Otherwise, the maximum walltime is ridiculously short.
#PBS -l walltime=24000:00:00
##################################
cd \$PBS_O_WORKDIR
export original_pbs_nodefile="\$PBS_NODEFILE"
exec 9<\$original_pbs_nodefile
nodefiles=( )
EOF
;
foreach my $file (@bin_files) {
#print out host files for each of the subscripts.
print OUTPUT "\nexport PBS_NODEFILE=`mktemp`\n";
print OUTPUT "nodefiles[\${#nodefiles[*]}]=\$PBS_NODEFILE\n";
for (my $ii=0; $ii<$procs{$file}; $ii++) {
print OUTPUT "read -u9 line; echo \$line >> \$PBS_NODEFILE\n";
}
#set the PBS_O_WORKDIR to the location of the script.
print OUTPUT qq|export PBS_O_WORKDIR="$directories{$file}"\n|;
#run the script.
print OUTPUT "bash $file $output_directives{$file} &\n";
}
print OUTPUT <<EOF
wait
for ii in "\${nodefiles[@]}"
do
rm -f \$ii
done
EOF
;
close(OUTPUT);
print <INPUT>;
close(INPUT);
}