source: trunk/oarutils/oar-parexec @ 75

Last change on this file since 75 was 75, checked in by g7moreau, 12 years ago
  • Add optin --kill and --transmit for signal handling
File size: 14.6 KB
Line 
1#!/usr/bin/perl
2#
3# 2011/11/27 gabriel
4
5use strict;
6
7use Getopt::Long();
8use Pod::Usage;
9use Coro;
10use Coro::Semaphore;
11use Coro::Signal;
12use Coro::Channel;
13use Coro::Handle;
14use IO::File;
15use POSIX qw( WNOHANG WEXITSTATUS );
16use Cwd qw( getcwd );
17
18my $file;
19my $dir;
20my $cmd;
21my $logtrace;
22my $verbose;
23my $job_np         = 1;
24my $nodefile       = $ENV{OAR_NODE_FILE} || '';
25my $masterio;
26my $switchio;
27my $help;
28my $oarsh          = 'oarsh -q -T';
29my $sig_transmit;
30my $sig_checkpoint = 'USR2';
31
32Getopt::Long::GetOptions(
33   'file=s'     => \$file,
34   'dir=s'      => \$dir,
35   'cmd=s'      => \$cmd,
36   'logtrace=s' => \$logtrace,
37   'verbose'    => \$verbose,
38   'help'       => \$help,
39   'oarsh=s'    => \$oarsh,
40   'jobnp=i'    => \$job_np,
41   'nodefile=s' => \$nodefile,
42   'masterio=s' => \$masterio,
43   'switchio'   => \$switchio,
44   'transmit'   => \$sig_transmit,
45   'kill=s'     => \$sig_checkpoint,
46   ) || pod2usage(-verbose => 0);
47pod2usage(-verbose => 2) if $help;
48pod2usage(-verbose => 2) if not (
49 (-e "$file")
50 or (-d "$dir" and $cmd ne '')
51 );
52
53# re-run, keep trace of job already done
54my %state;
55my $log_h = IO::File->new();
56if (-e "$logtrace") {
57   $log_h->open("< $logtrace")
58      or die "error: can't read log file: $!";
59   while (<$log_h>) {
60      $state{$1} = 'start' if m/^start\s+job\s+([^\s]+)\s/;
61      $state{$1} = 'end'   if m/^end\s+job\s+([^\s]+)\s/;
62      }
63   $log_h->close();
64   }
65if ($logtrace) {
66   $log_h->open(">> $logtrace")
67      or die "error: can't append log file $logtrace: $!";
68   $log_h->autoflush;
69   $log_h = unblock $log_h;
70   }
71
72# job to run
73my @job = ();
74if (-e "$file") {
75   my $job_num = 0;
76   open(JOB_LIST, '<', "$file") or die "error: can't open job file $file: $!";
77   while (<JOB_LIST>) {
78      chomp;
79      next if m/^#/;
80      next if m/^\s*$/;
81      $job_num++;
82      push @job, { name => $job_num, cmd => "$_" };
83      }
84   close JOB_LIST;
85   }
86else {
87   opendir(DIR, $dir) or die "error: can't open folder $dir: $!";
88   while (my $item = readdir(DIR)) {
89      next if $item =~ m/^\./;
90      next if $item =~ m/:/;
91      next if $item =~ m/\.old$/;
92      next if $item =~ m/\.sav$/;
93      next if $item =~ m/\.bak$/;
94      next if $item =~ m/\.no$/;
95      next unless (-d "$dir/$item");
96      push @job, { name => $item, cmd => "( cd $dir/$item/; $cmd )" };
97      }
98   closedir DIR;
99   }
100
101# ressources available
102my @ressources = ();
103open(NODE_FILE, '<', "$nodefile")
104   or die "can't open $nodefile: $!";
105while (<NODE_FILE>) {
106   chomp;
107   next if m/^#/;
108   next if m/^\s*$/;
109   push @ressources, $_;
110   }
111close NODE_FILE;
112
113my $ressource_size = scalar(@ressources);
114die "error: not enought ressources jobnp $job_np > ressources $ressource_size"
115   if $job_np > $ressource_size;
116
117my $current_dir = getcwd();
118
119my $stderr = $ENV{OAR_STDERR} || '';
120$stderr =~ s/\.stderr$//;
121$stderr = $masterio if $masterio;
122my $stdout = $ENV{OAR_STDOUT} || '';
123$stdout =~ s/\.stdout$//;
124$stdout = $masterio if $masterio;
125
126my $finished = new Coro::Signal;
127my $job_todo = new Coro::Semaphore 0;
128my $job_name_maxlen;
129for (@job) {
130   $job_todo->up;
131   $job_name_maxlen = length($_->{name}) if length($_->{name}) > $job_name_maxlen;
132   }
133
134# slice of ressources for parallel job
135my $ressources = new Coro::Channel;
136for my $slot (1 .. int($ressource_size / $job_np)) {
137   $ressources->put(
138      join(',',
139         @ressources[ (($slot - 1) * $job_np) .. (($slot * $job_np) - 1) ])
140         );
141   }
142
143my %scheduled = ();
144
145# OAR checkpoint and default signal SIGUSR2
146my $oar_checkpoint = new Coro::Semaphore 0;
147$SIG{$sig_checkpoint} = sub {
148   print "warning: receive checkpoint at "
149      . time
150      . ", no new job, just finishing running job\n"
151      if $verbose;
152   $oar_checkpoint->up();
153   kill $sig_checkpoint => keys %scheduled if $sig_transmit;
154   };
155
156# asynchrone start job block
157async {
158        JOB:
159   for my $job (@job) {
160      my $job_name = $job->{name};
161      my $job_cmd  = $job->{cmd};
162
163      # job has been already run ?
164      if (exists $state{$job_name}) {
165         if ($state{$job_name} eq 'start') {
166            print "warning: job $job_name was not clearly finished, relaunching...\n"
167               if $verbose;
168            }
169         elsif ($state{$job_name} eq 'end') {
170            delete $state{$job_name}; # free memory
171            $job_todo->down;
172            print "warning: job $job_name already run\n" if $verbose;
173            cede;
174            next JOB;
175            }
176         }
177
178      # take job ressource
179      my $job_ressource = $ressources->get;
180
181      # no more launch job when OAR checkpointing
182      last JOB if $oar_checkpoint->count() > 0;
183
184      my ($node_connect) = split ',', $job_ressource;
185      my $fh = IO::File->new();
186      my $job_pid = $fh->open("| $oarsh $node_connect >/dev/null 2>&1")
187         or die "error: can't start subjob: $!";
188
189      $fh->autoflush;
190      $fh = unblock $fh;
191
192      $scheduled{$job_pid} = {
193         fh           => $fh,
194         node_connect => $node_connect,
195         ressource    => $job_ressource,
196         name         => $job_name
197         };
198
199      my $msg = sprintf "start job %${job_name_maxlen}s / %5i at %s on node %s\n",
200         $job_name, $job_pid, time, $job_ressource;
201      $log_h->print($msg) if $logtrace;
202      print($msg) if $verbose;
203
204      my ($job_stdout, $job_stderr);
205      $job_stdout = ">  $stdout-$job_name.stdout" if $stdout ne '' and $switchio;
206      $job_stderr = "2> $stderr-$job_name.stderr" if $stderr ne '' and $switchio;
207
208      my $job_nodefile = "/tmp/oar-parexec-$ENV{LOGNAME}-$job_name";
209
210     # set job environment, run it and clean
211      if ($job_np > 1) {
212         $fh->print("printf \""
213               . join('\n', split(',', $job_ressource,))
214               . "\" > $job_nodefile\n");
215         $fh->print("OAR_NODE_FILE=$job_nodefile\n");
216         $fh->print("OAR_NP=$job_np\n");
217         $fh->print("export OAR_NODE_FILE\n");
218         $fh->print("export OAR_NP\n");
219         $fh->print("unset OAR_MSG_NODEFILE\n");
220         }
221      $fh->print("cd $current_dir\n");
222      $fh->print("$job_cmd $job_stdout $job_stderr\n");
223      $fh->print("rm -f $job_nodefile\n") if $job_np > 1;
224      $fh->print("exit\n");
225      cede;
226      }
227   }
228
229# asynchrone end job block
230async {
231   while () {
232      for my $job_pid (keys %scheduled) {
233                        # non blocking PID test
234         if (waitpid($job_pid, WNOHANG)) {
235            my $msg = sprintf "end   job %${job_name_maxlen}s / %5i at %s on node %s\n",
236               $scheduled{$job_pid}->{name},
237               $job_pid, time, $scheduled{$job_pid}->{ressource};
238            $log_h->print($msg) if $logtrace;
239            print($msg) if $verbose;
240            close $scheduled{$job_pid}->{fh};
241            # leave ressources for another job
242            $ressources->put($scheduled{$job_pid}->{ressource});
243            $job_todo->down;
244            delete $scheduled{$job_pid};
245            }
246         cede;
247         }
248
249      # checkpointing ! just finishing running job and quit
250      $finished->send if $oar_checkpoint->count() > 0 and scalar(keys(%scheduled)) == 0;
251
252      $finished->send if $job_todo->count() == 0;
253      cede;
254      }
255   }
256
257cede;
258
259# all job have been done
260$finished->wait;
261
262# close log trace file
263$log_h->close() if $logtrace;
264
265__END__
266
267=head1 NAME
268
269oar-parexec - parallel execution of many small job
270
271=head1 SYNOPSIS
272
273 oar-parexec --file filecommand \
274    [--logtrace tracefile] [--verbose] \
275    [--jobnp integer] [--nodefile filenode] [--oarsh sssh] \
276    [--switchio] [--masterio basefileio]
277
278 oar-parexec --dir foldertoiterate --cmd commandtolaunch \
279    [--logtrace tracefile] [--verbose] \
280    [--jobnp integer] [--nodefile filenode] [--oarsh sssh] \
281    [--switchio] [--masterio basefileio]
282
283 oar-parexec --help
284
285=head1 DESCRIPTION
286
287C<oar-parexec> can execute lot of small job in parallel inside a cluster.
288Number of parallel job at one time cannot exceed the number of core define in the node file
289C<oar-parexec> is easier to use inside an OAR job environment
290which define automatically these strategics parameters...
291However, it can be used outside OAR.
292
293Option C<--file> or C<--dir> and C<--cmd> are the only mandatory parameters.
294
295Small job will be launch in the same folder as the master job.
296Two environment variable are defined for each small job
297and only in case of parallel small job (option C<--jobnp> > 1).
298
299 OAR_NODE_FILE - file that list node for parallel computing
300 OAR_NP        - number of processor affected
301
302The file define by OAR_NODE_FILE is created  in /tmp
303on the node before launching the small job
304and this file will be delete after job complete.
305C<oar-parexec> is a simple script,
306OAR_NODE_FILE will not be deleted in case of crash of the master job.
307
308OAR define other variable that are equivalent to OAR_NODE_FILE:
309OAR_NODEFILE, OAR_FILE_NODES, OAR_RESOURCE_FILE...
310You can use in your script the OAR original file ressources
311by using these variable if you need it.
312 
313
314=head1 OPTIONS
315
316=over 12
317
318=item B<-f|--file filecommand>
319
320File name which content job list.
321For the JOB_NAME definition,
322the first valid job in the list will have the number 1 and so on...
323
324=item B<-d|--dir foldertoiterate>
325
326Command C<--cmd> will be launch in all sub-folder of this master folder.
327Files in this folder will be ignored.
328Sub-folder name which begin with F<.>
329or finish with F<.old>, F<.sav>, F<.bak>, F<.no> will either be ignored...
330
331The JOB_NAME is simply the Sub-folder name.
332
333=item B<-c|--cmd commandtolaunch>
334
335Command (and argument to it) tha will be launch in all sub-folder
336parameter folfer C<--dir>
337
338=item B<-l|--logtrace tracefile>
339
340File which log and trace running job.
341In case of running the same master command (after crash for example),
342only job that are not mark as done will be run again.
343Be careful, job mark as running (start but not finish) will be run again.
344Tracing is base on the JOB_NAME between multiple run.
345
346This option is very usefull in case of crash
347but also for checkpointing and idempotent OAR job.
348
349=item B<-v|--verbose>
350
351=item B<-j|--jobnp integer>
352
353Number of processor to allocated for each small job.
3541 by default.
355
356=item B<-n|--nodefile filenode>
357
358File name that list all the node where job could be launch.
359By defaut, it's define automatically by OAR via
360environment variable C<OAR_NODE_FILE>.
361
362For example, if you want to use 6 core on your cluster node,
363you need to put 6 times the hostname node in this file,
364one per line...
365It's a very common file in MPI process !
366
367=item B<-o|-oarsh command>
368
369Command use to launch a shell on a node.
370By default
371
372 oarsh -q -T
373
374Change it to C<ssh> if you are not using an OAR cluster...
375
376=item B<-s|--switchio>
377
378Each small job will have it's own output STDOUT and STDERR
379base on master OAR job with C<JOB_NAME> inside
380(or base on C<basefileio> if option C<masterio>).
381Example :
382
383 OAR.151524.stdout -> OAR.151524-JOB_NAME.stdout
384
385where 151524 here is the master C<OAR_JOB_ID>
386and C<JOB_NAME> is the small job name.
387
388=item B<-m|--masterio basefileio>
389
390The C<basefileio> will be use in place of environment variable
391C<OAR_STDOUT> and C<OAR_STDERR> (without extension) to build the base name of the small job standart output
392(only use when option C<swithio> is activated).
393
394=item B<-h|--help>
395
396=back
397
398
399=head1 EXAMPLE
400
401=head2 Simple list of sequential job
402
403Content for the job file command (option C<--file>) could have:
404
405 - empty line
406 - comment line begin with #
407 - valid shell command
408
409Example where F<$HOME/test/subjob1.sh> is a shell script (executable).
410
411 $HOME/test/subjob1.sh
412 $HOME/test/subjob2.sh
413 $HOME/test/subjob3.sh
414 $HOME/test/subjob4.sh
415 ...
416 $HOME/test/subjob38.sh
417 $HOME/test/subjob39.sh
418 $HOME/test/subjob40.sh
419
420These jobs could be launch by:
421
422 oarsub -n test -l /core=6,walltime=04:00:00 \
423   "oar-parexec -f ./subjob.list.txt"
424
425=head2 Folder job
426
427In a folder F<subjob.d>, create sub-folder with your data inside : F<test1>, <test2>...
428The same command will be executed in every sub-folder.
429C<oar-parexec> change the current directory to the sub-folder before launching it.
430
431A very simple job could be:
432
433 oarsub -n test -l /core=6,walltime=04:00:00 \
434   "oar-parexec -d ./subjob.d -c 'sleep 10; env'"
435
436The command C<env> will be excuted in all folder F<test1>, F<test2>... after a 10s pause.
437
438Sometime, it's simpler to use file list command,
439sometime, jobs by folder with the same command run is more relevant.
440
441=head2 Parallel job
442
443You need to put the number of core each small job need with option C<--jobnp>.
444If your job is build on OpenMP or MPI,
445you can use OAR_NP and OAR_NODE_FILE variables to configure them.
446On OAR cluster, you need to use C<oarsh> or a wrapper like C<oar-envsh>
447for connexion between node instead of C<ssh>.
448
449Example with parallel small job on 2 core:
450
451 oarsub -n test -l /core=6,walltime=04:00:00 \
452   "oar-parexec -j 2 -f ./subjob.list.txt"
453
454=head2 Tracing and master crash
455
456If the master node crash after hours of calculus, everything is lost ?
457No, with option C<--logtrace>,
458it's possible to remember older result
459and not re-run these job the second and next time.
460
461 oarsub -n test -l /core=6,walltime=04:00:00 \
462   "oar-parexec -f ./subjob.list.txt -l ./subjob.list.log"
463
464After a crash or an C<oardel> command,
465you can then re-run the same command that will end to execute the jobs in the list
466
467 oarsub -n test -l /core=6,walltime=04:00:00 \
468   "oar-parexec -f ./subjob.list.txt -l ./subjob.list.log"
469
470C<logtrace> file are just plain file.
471We use the extension '.log' because these files are automatically
472eliminate from our backup system!
473
474=head2 Checkpointing and Idempotent
475
476C<oar-parexec> is compatible with the OAR checkpointing.
477Il you have 2000 small jobs that need 55h to be done on 6 cores,
478you can cut this in small parts.
479
480For this example, we suppose that each small job need about 10min...
481So, we send a checkpoint 12min before the end of the process
482to let C<oar-parexec> finish the jobs started.
483After being checkpointed, C<oar-parexec> do not start any new small job.
484
485 oarsub -t idempotent -n test \
486   -l /core=6,walltime=04:00:00 \
487   --checkpoint 720 \
488   "oar-parexec -f ./subjob.list.txt -l ./subjob.list.log"
489
490After 3h48min, the OAR job will begin to stop launching new small job.
491When all running small job are finished, it's exit.
492But as the OAR job is type C<idempotent>,
493OAR will re-submit it as long as all small job are not executed...
494
495This way, we let other users a chance to use the cluster!
496
497In this last exemple, we use moldable OAR job with idempotent
498to reserve many core for a small time or a few cores for a long time:
499
500 oarsub -t idempotent -n test \
501   -l /core=50,walltime=01:05:00 \
502   -l /core=6,walltime=04:00:00 \
503   --checkpoint 720 \
504   "oar-parexec -f ./subjob.list.txt -l ./subjob.list.log"
505
506
507=head1 SEE ALSO
508
509oar-dispatch, mpilauncher,
510orsh, oar-envsh, ssh
511
512
513=head1 AUTHORS
514
515Written by Gabriel Moreau, Grenoble - France
516
517
518=head1 LICENSE AND COPYRIGHT
519
520GPL version 2 or later and Perl equivalent
521
522Copyright (C) 2011 Gabriel Moreau / LEGI - CNRS UMR 5519 - France
523
Note: See TracBrowser for help on using the repository browser.