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; |
---|
31 | pod2usage( -verbose => 2 ) if not -e $file; |
---|
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 | |
---|
150 | =head1 DESCRIPTION |
---|
151 | |
---|
152 | C<oar-parexec> need to be executed inside an OAR job environment. |
---|
153 | because it need the two environment variable that OAR define by |
---|
154 | default: |
---|
155 | |
---|
156 | OAR_NODE_FILE path to a file which content one node by line |
---|
157 | |
---|
158 | OAR_WORKDIR dir to launch job and do a chdir inside |
---|
159 | |
---|
160 | Content for the job file (option C<--file>) could have: |
---|
161 | |
---|
162 | - empty line |
---|
163 | - comment line begin with # |
---|
164 | - valid shell command |
---|
165 | |
---|
166 | Example where F<$HOME/test/subjob1.sh> is a shell script (executable). |
---|
167 | |
---|
168 | $HOME/test/subjob1.sh |
---|
169 | $HOME/test/subjob2.sh |
---|
170 | $HOME/test/subjob3.sh |
---|
171 | $HOME/test/subjob4.sh |
---|
172 | |
---|
173 | $HOME/test/subjob38.sh |
---|
174 | $HOME/test/subjob39.sh |
---|
175 | $HOME/test/subjob40.sh |
---|
176 | |
---|
177 | These jobs could be launch by |
---|
178 | |
---|
179 | oarsub -n test -l /core=6,walltime=00:35:00 "oar-parexec -f ./subjob.list.txt" |
---|
180 | |
---|
181 | |
---|
182 | =head1 SEE ALSO |
---|
183 | |
---|
184 | oar-dispatch, mpilauncher |
---|
185 | |
---|
186 | |
---|
187 | =head1 AUTHORS |
---|
188 | |
---|
189 | Written by Gabriel Moreau, Grenoble - France |
---|
190 | |
---|
191 | |
---|
192 | =head1 LICENSE AND COPYRIGHT |
---|
193 | |
---|
194 | GPL version 2 or later and Perl equivalent |
---|
195 | |
---|
196 | Copyright (C) 2011 Gabriel Moreau / LEGI - CNRS UMR 5519 - France |
---|
197 | |
---|