1 | #!/usr/bin/perl |
---|
2 | # |
---|
3 | # 2011/11/27 gabriel |
---|
4 | |
---|
5 | use strict; |
---|
6 | |
---|
7 | use Getopt::Long(); |
---|
8 | use Pod::Usage; |
---|
9 | use Coro; |
---|
10 | use Coro::Semaphore; |
---|
11 | use Coro::Signal; |
---|
12 | use Coro::Channel; |
---|
13 | use Coro::Handle; |
---|
14 | use IO::File; |
---|
15 | use POSIX qw( WNOHANG WEXITSTATUS ); |
---|
16 | use Cwd qw( getcwd ); |
---|
17 | |
---|
18 | my $file = ''; |
---|
19 | my $verbose; |
---|
20 | my $nodefile = $ENV{OAR_NODE_FILE} || ''; |
---|
21 | my $masterio; |
---|
22 | my $switchio; |
---|
23 | my $help; |
---|
24 | my $oarsh = 'oarsh -q -T'; |
---|
25 | |
---|
26 | Getopt::Long::GetOptions( |
---|
27 | 'file=s' => \$file, |
---|
28 | 'verbose' => \$verbose, |
---|
29 | 'help' => \$help, |
---|
30 | 'oarsh=s' => \$oarsh, |
---|
31 | 'nodefile=s' => \$nodefile, |
---|
32 | 'masterio=s' => \$masterio, |
---|
33 | 'switchio' => \$switchio, |
---|
34 | ) || pod2usage( -verbose => 0 ); |
---|
35 | pod2usage( -verbose => 2 ) if $help; |
---|
36 | pod2usage( -verbose => 2 ) if not -e $file; |
---|
37 | |
---|
38 | my @job = (); |
---|
39 | open( JOB_LIST, '<', "$file" ) or die "can't open $file: $!"; |
---|
40 | while (<JOB_LIST>) { |
---|
41 | chomp; |
---|
42 | next if m/^#/; |
---|
43 | next if m/^\s*$/; |
---|
44 | push @job, $_ ; |
---|
45 | } |
---|
46 | close JOB_LIST; |
---|
47 | |
---|
48 | my $stderr = $ENV{OAR_STDERR} || ''; |
---|
49 | $stderr =~ s/\.stderr$//; |
---|
50 | $stderr = $masterio if $masterio; |
---|
51 | my $stdout = $ENV{OAR_STDOUT} || ''; |
---|
52 | $stdout =~ s/\.stdout$//; |
---|
53 | $stdout = $masterio if $masterio; |
---|
54 | |
---|
55 | my $current_dir = getcwd(); |
---|
56 | |
---|
57 | my $finished = new Coro::Signal; |
---|
58 | my $job_todo = new Coro::Semaphore 0; |
---|
59 | $job_todo->up for (@job); |
---|
60 | |
---|
61 | my $ressources = new Coro::Channel; |
---|
62 | open( NODE_FILE, '<', "$nodefile" ) |
---|
63 | or die "can't open $nodefile: $!"; |
---|
64 | while (<NODE_FILE>) { |
---|
65 | chomp; |
---|
66 | next if m/^#/; |
---|
67 | next if m/^\s*$/; |
---|
68 | $ressources->put($_); |
---|
69 | } |
---|
70 | close NODE_FILE; |
---|
71 | |
---|
72 | my $job_num = 0; |
---|
73 | my %scheduled = (); |
---|
74 | |
---|
75 | async { |
---|
76 | for my $job (@job) { |
---|
77 | my $node = $ressources->get; |
---|
78 | |
---|
79 | $job_num++; |
---|
80 | |
---|
81 | my $fh = IO::File->new(); |
---|
82 | my $job_pid = $fh->open("| $oarsh $node >/dev/null 2>&1") |
---|
83 | or die "don't start subjob: $!"; |
---|
84 | |
---|
85 | $fh->autoflush; |
---|
86 | $fh = unblock $fh; |
---|
87 | |
---|
88 | $scheduled{$job_pid} = { fh => $fh, node => $node, num => $job_num }; |
---|
89 | |
---|
90 | printf "start job %5i / %5i on node %s at %s\n", |
---|
91 | $job_num, $job_pid, $node, time |
---|
92 | if $verbose; |
---|
93 | |
---|
94 | my ( $job_stdout, $job_stderr ); |
---|
95 | $job_stdout = "> $stdout-$job_num.stdout" if $stdout ne '' and $switchio; |
---|
96 | $job_stderr = "2> $stderr-$job_num.stderr" if $stderr ne '' and $switchio; |
---|
97 | |
---|
98 | $fh->print("cd $current_dir\n"); |
---|
99 | $fh->print("$job $job_stdout $job_stderr\n"); |
---|
100 | $fh->print("exit\n"); |
---|
101 | cede; |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | async { |
---|
106 | while () { |
---|
107 | for my $job_pid ( keys %scheduled ) { |
---|
108 | if ( waitpid( $job_pid, WNOHANG ) ) { |
---|
109 | printf "end job %5i / %5i on node %s at %s\n", |
---|
110 | $scheduled{$job_pid}->{num}, |
---|
111 | $job_pid, $scheduled{$job_pid}->{node}, time |
---|
112 | if $verbose; |
---|
113 | close $scheduled{$job_pid}->{fh}; |
---|
114 | $ressources->put( $scheduled{$job_pid}->{node} ); |
---|
115 | $job_todo->down; |
---|
116 | delete $scheduled{$job_pid}; |
---|
117 | } |
---|
118 | cede; |
---|
119 | } |
---|
120 | |
---|
121 | $finished->send if $job_todo->count == 0; |
---|
122 | cede; |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | cede; |
---|
127 | |
---|
128 | $finished->wait; |
---|
129 | |
---|
130 | __END__ |
---|
131 | |
---|
132 | =head1 NAME |
---|
133 | |
---|
134 | oar-parexec - parallel execute lot of small job |
---|
135 | |
---|
136 | =head1 SYNOPSIS |
---|
137 | |
---|
138 | oar-parexec --file filecommand [--verbose] [--nodefile filenode] [--masterio basefileio] [--switchio] [--oarsh sssh] |
---|
139 | oar-parexec --help |
---|
140 | |
---|
141 | =head1 DESCRIPTION |
---|
142 | |
---|
143 | C<oar-parexec> execute lot of small job.in parallel inside a cluster. |
---|
144 | Number of parallel job at one time cannot excede core number in the node file. |
---|
145 | C<oar-parexec> is easier to use inside an OAR job environment |
---|
146 | which define automatically theses strategics parameters... |
---|
147 | |
---|
148 | Option C<--file> is the only mandatory one. |
---|
149 | |
---|
150 | Small job will be launch in the same folder as the master job. |
---|
151 | |
---|
152 | |
---|
153 | =head1 OPTIONS |
---|
154 | |
---|
155 | =over 12 |
---|
156 | |
---|
157 | =item B<-f|--file filecommand> |
---|
158 | |
---|
159 | File name which content job list. |
---|
160 | |
---|
161 | =item B<-v|--verbose> |
---|
162 | |
---|
163 | =item B<-n|nodefile filenode> |
---|
164 | |
---|
165 | File name that list all the node to launch job. |
---|
166 | By defaut, it's define automatically by OAR via |
---|
167 | environment variable C<OAR_NODE_FILE>. |
---|
168 | |
---|
169 | For example, if you want to use 6 core on your cluster node, |
---|
170 | you need to put 6 times the hostname node in this file, |
---|
171 | one per line... |
---|
172 | It's a very common file in MPI process ! |
---|
173 | |
---|
174 | =item B<-m|--masterio basefileio> |
---|
175 | |
---|
176 | The C<basefileio> will be use in place of environment variable |
---|
177 | C<OAR_STDOUT> and C<OAR_STDERR> (without extension) to build the base name of the small job standart output |
---|
178 | (only when option C<swithio> is activated). |
---|
179 | |
---|
180 | =item B<-s|--switchio> |
---|
181 | |
---|
182 | Each small job will have it's own output STDOUT and STDERR |
---|
183 | base on master OAR job with C<JOB_NUM> inside |
---|
184 | (or base on C<basefileio> if option C<masterio>). |
---|
185 | Example : |
---|
186 | |
---|
187 | OAR.151524.stdout -> OAR.151524-JOB_NUM.stdout |
---|
188 | |
---|
189 | where 151524 here is the master C<OAR_JOB_ID> |
---|
190 | and C<JOB_NUM> is the small job nnumber. |
---|
191 | |
---|
192 | =item B<-o|-oarsh command> |
---|
193 | |
---|
194 | Command use to launch a shell on a node. |
---|
195 | By default |
---|
196 | |
---|
197 | oarsh -q -T |
---|
198 | |
---|
199 | =item B<-h|--help> |
---|
200 | |
---|
201 | =back |
---|
202 | |
---|
203 | |
---|
204 | =head1 EXAMPLE |
---|
205 | |
---|
206 | Content for the job file (option C<--file>) could have: |
---|
207 | |
---|
208 | - empty line |
---|
209 | - comment line begin with # |
---|
210 | - valid shell command |
---|
211 | |
---|
212 | Example where F<$HOME/test/subjob1.sh> is a shell script (executable). |
---|
213 | |
---|
214 | $HOME/test/subjob1.sh |
---|
215 | $HOME/test/subjob2.sh |
---|
216 | $HOME/test/subjob3.sh |
---|
217 | $HOME/test/subjob4.sh |
---|
218 | ... |
---|
219 | $HOME/test/subjob38.sh |
---|
220 | $HOME/test/subjob39.sh |
---|
221 | $HOME/test/subjob40.sh |
---|
222 | |
---|
223 | These jobs could be launch by |
---|
224 | |
---|
225 | oarsub -n test -l /core=6,walltime=00:35:00 "oar-parexec -f ./subjob.list.txt" |
---|
226 | |
---|
227 | |
---|
228 | =head1 SEE ALSO |
---|
229 | |
---|
230 | oar-dispatch, mpilauncher |
---|
231 | |
---|
232 | |
---|
233 | =head1 AUTHORS |
---|
234 | |
---|
235 | Written by Gabriel Moreau, Grenoble - France |
---|
236 | |
---|
237 | |
---|
238 | =head1 LICENSE AND COPYRIGHT |
---|
239 | |
---|
240 | GPL version 2 or later and Perl equivalent |
---|
241 | |
---|
242 | Copyright (C) 2011 Gabriel Moreau / LEGI - CNRS UMR 5519 - France |
---|
243 | |
---|