1 | #!/usr/bin/perl |
---|
2 | # |
---|
3 | # 2011/11/03 gabriel |
---|
4 | |
---|
5 | use strict; |
---|
6 | |
---|
7 | use Getopt::Long(); |
---|
8 | use Pod::Usage; |
---|
9 | use Coro; |
---|
10 | use Coro::Signal; |
---|
11 | use Coro::Semaphore; |
---|
12 | |
---|
13 | my $task = 0; |
---|
14 | my $overload = 1.1; |
---|
15 | my $file = ''; |
---|
16 | my $verbose; |
---|
17 | my $help; |
---|
18 | |
---|
19 | Getopt::Long::GetOptions( |
---|
20 | 'task=i' => \$task, |
---|
21 | 'overload=f' => \$overload, |
---|
22 | 'file=s' => \$file, |
---|
23 | 'verbose' => \$verbose, |
---|
24 | 'help' => \$help, |
---|
25 | ) || pod2usage(-verbose => 0); |
---|
26 | pod2usage(-verbose => 2) if $help; |
---|
27 | |
---|
28 | if ($task == 0) { |
---|
29 | open(NODE_FILE, '<', "$ENV{OAR_NODE_FILE}") or die "can't open ENV{OAR_NODE_FILE}: $!"; |
---|
30 | $task++ while <NODE_FILE>; |
---|
31 | close NODE_FILE; |
---|
32 | } |
---|
33 | |
---|
34 | my @job = (); |
---|
35 | open (JOB_LIST, '<', "$file") or die "can't open $file: $!"; |
---|
36 | while (<JOB_LIST>) { |
---|
37 | chomp; |
---|
38 | next if m/^#/; |
---|
39 | push @job, $_ if m/^\s*oarsub/; |
---|
40 | } |
---|
41 | close JOB_LIST; |
---|
42 | |
---|
43 | my $container_id=$ENV{OAR_JOB_ID}; |
---|
44 | my $insert_oar_option = "-t inner=$container_id"; |
---|
45 | |
---|
46 | # interactive job |
---|
47 | if (not $container_id > 1) { |
---|
48 | $insert_oar_option = ''; |
---|
49 | $overload = 1; |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | my $finished = new Coro::Signal; |
---|
54 | my $job_active = new Coro::Semaphore 0; |
---|
55 | my $job_todo = new Coro::Semaphore 0; |
---|
56 | $job_todo->up for (@job); |
---|
57 | |
---|
58 | my %scheduled = (); |
---|
59 | |
---|
60 | async { |
---|
61 | for my $job (@job) { |
---|
62 | while ($job_active->count >= $task*$overload) { |
---|
63 | cede; |
---|
64 | } |
---|
65 | $job =~ s/^\s*oarsub//; |
---|
66 | print "oarsub $insert_oar_option $job" if $verbose; |
---|
67 | my $job_id = `oarsub $insert_oar_option $job|grep ^OAR_JOB_ID|cut -f 2 -d '='`; |
---|
68 | chomp $job_id; |
---|
69 | if ($job_id > 1) { |
---|
70 | $scheduled{$job_id}++; |
---|
71 | $job_active->up; |
---|
72 | } |
---|
73 | cede; |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | async { |
---|
78 | while () { |
---|
79 | for my $job_id (keys %scheduled) { |
---|
80 | my $is_finish = `oarstat -s -j $job_id`; |
---|
81 | chomp $is_finish; |
---|
82 | if ($is_finish =~ m/Terminated/) { |
---|
83 | delete $scheduled{$job_id}; |
---|
84 | $job_active->down; |
---|
85 | $job_todo->down; |
---|
86 | } |
---|
87 | cede; |
---|
88 | } |
---|
89 | |
---|
90 | $finished->send if $job_todo->count == 0; |
---|
91 | cede; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | cede; |
---|
96 | |
---|
97 | $finished->wait; |
---|
98 | |
---|
99 | |
---|
100 | __END__ |
---|
101 | |
---|
102 | =head1 NAME |
---|
103 | |
---|
104 | oar-dispatch - dispatch lot of small oar job |
---|
105 | |
---|
106 | =head1 SYNOPSIS |
---|
107 | |
---|
108 | oar-dispatch [--task integer] [--overload real] --file filecommand [--verbose] |
---|
109 | oar-dispatch --help |
---|
110 | |
---|
111 | =head1 OPTIONS |
---|
112 | |
---|
113 | =over 12 |
---|
114 | |
---|
115 | =item B<[-t|--task integer]> |
---|
116 | |
---|
117 | Number of task to do in parallel. |
---|
118 | Default to the line number of the file OAR_NODE_FILE. |
---|
119 | |
---|
120 | =item B<[-o|--overload real]> |
---|
121 | |
---|
122 | Number of OAR job to create / number of task. |
---|
123 | Some job are create in advance to start whenever it's possible. |
---|
124 | 1.1 by default. |
---|
125 | |
---|
126 | =item B<[-f|--file filecommand]> |
---|
127 | |
---|
128 | File name which content OAR job list |
---|
129 | |
---|
130 | =item B<[-v|--verbose]> |
---|
131 | |
---|
132 | =item B<[-h|--help]> |
---|
133 | |
---|
134 | =back |
---|
135 | |
---|
136 | Input job file name content can have |
---|
137 | |
---|
138 | - empty line |
---|
139 | - comment line begin with # |
---|
140 | - oarsub command without -t option |
---|
141 | |
---|
142 | C<oar-dispatch> will add C<-t inner=container_id> in this command line, |
---|
143 | just after C<oarsub>. |
---|
144 | |
---|
145 | =head1 EXAMPLE |
---|
146 | |
---|
147 | Example where the file F<$HOME/test/subjob.txt> is a list of OAR script job (and can be executable but not need here). |
---|
148 | |
---|
149 | oarsub -n test -l /core=1,walltime=00:05:00 $HOME/test/subjob1.oar |
---|
150 | oarsub -n test -l /core=1,walltime=00:05:00 $HOME/test/subjob2.oar |
---|
151 | oarsub -n test -l /core=1,walltime=00:05:00 $HOME/test/subjob3.oar |
---|
152 | oarsub -n test -l /core=1,walltime=00:05:00 $HOME/test/subjob4.oar |
---|
153 | ... |
---|
154 | oarsub -n test -l /core=1,walltime=00:05:00 $HOME/test/subjob38.oar |
---|
155 | oarsub -n test -l /core=1,walltime=00:05:00 $HOME/test/subjob39.oar |
---|
156 | oarsub -n test -l /core=1,walltime=00:05:00 $HOME/test/subjob40.oar |
---|
157 | |
---|
158 | These jobs could be launch with |
---|
159 | |
---|
160 | oarsub -t container -n test-container -l /core=6,walltime=00:35:00 "oar-dispatch -f ./subjob.list.txt" |
---|
161 | |
---|
162 | Total C<walltime> is defined by the formula: |
---|
163 | |
---|
164 | total_walltime = subjob_walltime * total_subjob / core + global_delay |
---|
165 | |
---|
166 | In practise, C<oar-dispatch> take few second and each subjob run in less than it's walltime so |
---|
167 | |
---|
168 | total_walltime < subjob_walltime * total_subjob / core |
---|
169 | |
---|
170 | If launch in interactif, C<overload> parameter is equal to 1, |
---|
171 | C<task> must be define |
---|
172 | and no inner container is add to the C<oarsub> command line. |
---|
173 | |
---|
174 | |
---|
175 | =head1 SEE ALSO |
---|
176 | |
---|
177 | oar-parexec, mpilauncher |
---|
178 | |
---|
179 | |
---|
180 | =head1 AUTHORS |
---|
181 | |
---|
182 | Written by Gabriel Moreau, Grenoble - France |
---|
183 | |
---|
184 | |
---|
185 | =head1 LICENSE AND COPYRIGHT |
---|
186 | |
---|
187 | GPL version 2 or later and Perl equivalent |
---|
188 | |
---|
189 | Copyright (C) 2011 Gabriel Moreau / LEGI - CNRS UMR 5519 - France |
---|
190 | |
---|