[13] | 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 | |
---|
| 17 | my $file = ''; |
---|
| 18 | my $verbose; |
---|
| 19 | my $switchio; |
---|
| 20 | my $help; |
---|
| 21 | my $oarsh = 'oarsh -q -T'; |
---|
| 22 | |
---|
| 23 | Getopt::Long::GetOptions( |
---|
| 24 | 'file=s' => \$file, |
---|
| 25 | 'verbose' => \$verbose, |
---|
| 26 | 'help' => \$help, |
---|
| 27 | 'oarsh' => \$oarsh, |
---|
| 28 | 'switchio' => \$switchio, |
---|
| 29 | ) || pod2usage( -verbose => 0 ); |
---|
| 30 | pod2usage( -verbose => 2 ) if $help; |
---|
[19] | 31 | pod2usage( -verbose => 2 ) if -e $file; |
---|
[13] | 32 | |
---|
| 33 | my @job = (); |
---|
| 34 | open( JOB_LIST, '<', "$file" ) or die "can't open $file: $!"; |
---|
| 35 | while (<JOB_LIST>) { |
---|
| 36 | chomp; |
---|
| 37 | next if m/^#/; |
---|
| 38 | push @job, $_ if not m/^\s*$/; |
---|
| 39 | } |
---|
| 40 | close JOB_LIST; |
---|
| 41 | |
---|
| 42 | my $stderr = $ENV{OAR_STDERR}; |
---|
| 43 | $stderr =~ s/\.stderr$//; |
---|
| 44 | my $stdout = $ENV{OAR_STDOUT}; |
---|
| 45 | $stdout =~ s/\.stdout$//; |
---|
| 46 | |
---|
| 47 | my $finished = new Coro::Signal; |
---|
| 48 | my $job_todo = new Coro::Semaphore 0; |
---|
| 49 | $job_todo->up for (@job); |
---|
| 50 | |
---|
| 51 | my $ressources = new Coro::Channel; |
---|
| 52 | open( NODE_FILE, '<', "$ENV{OAR_NODE_FILE}" ) |
---|
| 53 | or die "can't open ENV{OAR_NODE_FILE}: $!"; |
---|
| 54 | while (<NODE_FILE>) { |
---|
| 55 | chomp; |
---|
| 56 | $ressources->put($_); |
---|
| 57 | } |
---|
| 58 | close NODE_FILE; |
---|
| 59 | |
---|
| 60 | my $job_num = 0; |
---|
| 61 | my %scheduled = (); |
---|
| 62 | |
---|
| 63 | async { |
---|
| 64 | for my $job (@job) { |
---|
| 65 | my $node = $ressources->get; |
---|
| 66 | |
---|
| 67 | $job_num++; |
---|
| 68 | |
---|
| 69 | my $fh = IO::File->new(); |
---|
| 70 | my $job_pid = $fh->open("| $oarsh $node >/dev/null 2>&1") |
---|
| 71 | or die "don't start subjob: $!"; |
---|
| 72 | |
---|
| 73 | $fh->autoflush; |
---|
| 74 | $fh = unblock $fh; |
---|
| 75 | |
---|
| 76 | $scheduled{$job_pid} = { fh => $fh, node => $node, num => $job_num }; |
---|
| 77 | |
---|
| 78 | printf "start job %5i / %5i on node %s at %s\n", |
---|
| 79 | $job_num, $job_pid, $node, time |
---|
| 80 | if $verbose; |
---|
| 81 | |
---|
| 82 | my ( $job_stdout, $job_stderr ); |
---|
| 83 | $job_stdout = "> $stdout-$job_num.stdout" if $stdout ne '' and $switchio; |
---|
| 84 | $job_stderr = "2> $stderr-$job_num.stderr" if $stderr ne '' and $switchio; |
---|
| 85 | |
---|
| 86 | $fh->print("cd $ENV{OAR_WORKDIR}\n"); |
---|
| 87 | $fh->print("$job $job_stdout $job_stderr\n"); |
---|
| 88 | $fh->print("exit\n"); |
---|
| 89 | cede; |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | async { |
---|
| 94 | while () { |
---|
| 95 | for my $job_pid ( keys %scheduled ) { |
---|
| 96 | if ( waitpid( $job_pid, WNOHANG ) ) { |
---|
| 97 | printf "end job %5i / %5i on node %s at %s\n", |
---|
| 98 | $scheduled{$job_pid}->{num}, |
---|
| 99 | $job_pid, $scheduled{$job_pid}->{node}, time |
---|
| 100 | if $verbose; |
---|
| 101 | close $scheduled{$job_pid}->{fh}; |
---|
| 102 | $ressources->put( $scheduled{$job_pid}->{node} ); |
---|
| 103 | $job_todo->down; |
---|
| 104 | delete $scheduled{$job_pid}; |
---|
| 105 | } |
---|
| 106 | cede; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | $finished->send if $job_todo->count == 0; |
---|
| 110 | cede; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | cede; |
---|
| 115 | |
---|
| 116 | $finished->wait; |
---|
| 117 | |
---|
| 118 | __END__ |
---|
| 119 | |
---|
| 120 | =head1 NAME |
---|
| 121 | |
---|
| 122 | oar-parexec - parallel execute lot of small job |
---|
| 123 | |
---|
| 124 | =head1 SYNOPSIS |
---|
| 125 | |
---|
| 126 | oar-parexec --file filepath [--verbose] [--switchio] [--oarsh sssh] |
---|
| 127 | oar-parexec --help |
---|
| 128 | |
---|
| 129 | =head1 OPTIONS |
---|
| 130 | |
---|
| 131 | --file file name which content job list |
---|
| 132 | |
---|
| 133 | --verbose |
---|
| 134 | |
---|
| 135 | --switchio each small job will have it's own output STDOUT and STDERR |
---|
| 136 | base on master OAR job with JOB_NUM inside. Example : |
---|
| 137 | |
---|
| 138 | OAR.151524.stdout -> OAR.151524-JOB_NUM.stdout |
---|
| 139 | |
---|
| 140 | where 151524 here is the master OAR_JOB_ID |
---|
| 141 | |
---|
| 142 | -oarsh command use to connect a shell on a node |
---|
| 143 | by default |
---|
| 144 | |
---|
| 145 | oarsh -q -T |
---|
| 146 | |
---|
| 147 | --help |
---|
| 148 | |
---|
| 149 | File name content can have |
---|
| 150 | |
---|
| 151 | - empty line |
---|
| 152 | - comment line begin with # |
---|
| 153 | - valid shell command |
---|
| 154 | |
---|
| 155 | Example where F<$HOME/test/subjob1.sh> is a shell script (executable). |
---|
| 156 | |
---|
| 157 | $HOME/test/subjob1.sh |
---|
| 158 | $HOME/test/subjob2.sh |
---|
| 159 | $HOME/test/subjob3.sh |
---|
| 160 | $HOME/test/subjob4.sh |
---|
| 161 | |
---|
| 162 | $HOME/test/subjob38.sh |
---|
| 163 | $HOME/test/subjob39.sh |
---|
| 164 | $HOME/test/subjob40.sh |
---|
| 165 | |
---|
| 166 | These jobs could be launch by |
---|
| 167 | |
---|
| 168 | oarsub -n test -l /core=6,walltime=00:35:00 "oar-parexec -f ./subjob.list.txt" |
---|
| 169 | |
---|
| 170 | =head1 AUTHORS |
---|
| 171 | |
---|
| 172 | Gabriel Moreau (C) 2011 |
---|
| 173 | |
---|