-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackfilling.pm
372 lines (272 loc) · 9.28 KB
/
Backfilling.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package Backfilling;
use strict;
use warnings;
use Exporter qw(import);
use Time::HiRes qw(time);
use Data::Dumper;
use List::Util qw(max min shuffle sum);
use Switch;
use POSIX qw(pow);
use Log::Log4perl qw(:no_extra_logdie_message get_logger);
use Util qw($config);
use ExecutionProfile;
use Heap;
use Event;
use Platform;
use Job;
use constant {
SUBMISSION_EVENT => 0,
JOB_START_EVENT => 1,
JOB_COMPLETION_EVENT => 2,
};
# Constructors
sub new {
my (
$class,
$reduction_algorithm,
$platform,
$trace,
$penalty_function,
$penalty_factor,
) = @_;
my $self = {
cmax => 0,
current_time => 0,
execution_profile => ExecutionProfile->new(
$platform->processors_number(),
$reduction_algorithm,
),
platform => $platform,
trace => $trace,
penalty_function => $penalty_function,
penalty_factor => $penalty_factor,
};
die "not enough processors: ", $self->{trace}->needed_cpus() if
$self->{trace}->needed_cpus() > $self->{platform}->processors_number();
bless $self, $class;
return $self;
}
# Getters and setters
sub trace {
my ($self) = @_;
return $self->{trace};
}
sub cmax {
my ($self) = @_;
return max map {$_->real_ending_time()} (@{$self->{trace}->jobs()});
}
sub sum_flow_time {
my ($self) = @_;
return sum map {$_->flow_time()} @{$self->{trace}->jobs()};
}
sub max_flow_time {
my ($self) = @_;
return max map {$_->flow_time()} @{$self->{trace}->jobs()};
}
sub mean_flow_time {
my ($self) = @_;
return $self->sum_flow_time() / @{$self->{trace}->jobs()};
}
sub sum_stretch {
my ($self) = @_;
return sum map {$_->bounded_stretch()} @{$self->{trace}->jobs()};
}
sub max_stretch {
my ($self) = @_;
return max map {$_->bounded_stretch()} @{$self->{trace}->jobs()};
}
sub mean_stretch {
my ($self) = @_;
return (sum map {$_->stretch()} @{$self->{trace}->jobs()}) / @{$self->{trace}->jobs()};
}
sub bounded_stretch {
my ($self, $bound) = @_;
$bound = 10 unless defined $bound;
my $jobs_number = scalar @{$self->{trace}->jobs()};
my $total_bounded_stretch = sum map {$_->bounded_stretch($bound)} (@{$self->{trace}->jobs()});
return $total_bounded_stretch/$jobs_number;
}
sub stretch_sum_of_squares {
my ($self) = @_;
return sqrt(sum map {$_->bounded_stretch(10) ** 2} (@{$self->{trace}->jobs()}));
}
sub stretch_with_cpus_squared {
my ($self) = @_;
return sum map {$_->bounded_stretch_with_cpus_squared(10)} (@{$self->{trace}->jobs()});
}
sub stretch_with_cpus_log {
my ($self) = @_;
return sum map {$_->bounded_stretch_with_cpus_log(10)} (@{$self->{trace}->jobs()});
}
sub flow_time_pnorm {
my ($self) = @_;
my $pnorm = $config->param('parameters.pnorm');
return pow((sum map {defined $_->flow_time() ? pow($_->flow_time(), $pnorm) : 0} @{$self->{trace}->jobs()}), 1/$pnorm);
}
sub stretch_pnorm {
my ($self) = @_;
my $pnorm = $config->param('parameters.pnorm');
return pow((sum map {defined $_->bounded_stretch() ? pow($_->bounded_stretch(), $pnorm) : 0} @{$self->{trace}->jobs()}), 1/$pnorm);
}
sub contiguous_jobs_number {
my ($self) = @_;
return sum map {$self->{platform}->job_contiguity($_->assigned_processors())} (@{$self->{trace}->jobs()});
}
sub contiguity_factor {
my ($self) = @_;
my $total_contiguity_factor = sum map {$self->{platform}->job_contiguity_factor($_->assigned_processors())} (@{$self->{trace}->jobs()});
return $total_contiguity_factor/scalar @{$self->{trace}->jobs()};
}
sub local_jobs_number {
my ($self) = @_;
return sum map {$self->{platform}->job_locality($_->assigned_processors())} (@{$self->{trace}->jobs()});
}
sub locality_factor {
my ($self) = @_;
my $total_locality_factor = sum map {$self->{platform}->job_locality_factor($_->assigned_processors())} (@{$self->{trace}->jobs()});
return $total_locality_factor/scalar @{$self->{trace}->jobs()};
}
sub platform_level_factor {
my ($self) = @_;
my $job_level_distances = sum map {$self->{platform}->job_relative_level_distance($_->assigned_processors(), $_->requested_cpus())} (@{$self->{trace}->jobs()});
return $job_level_distances / scalar @{$self->{trace}->jobs()};
}
sub job_success_rate {
my ($self) = @_;
return sum map {($_->status() == JOB_STATUS_COMPLETED) ? 1 : 0} (@{$self->{trace}->jobs()});
}
sub run_time {
my ($self) = @_;
return $self->{run_time};
}
# Functions
sub run {
my ($self) = @_;
my $logger = get_logger('Backfilling.run');
$self->{reserved_jobs} = []; # jobs not started yet
$self->{started_jobs} = {}; # jobs that have already started
$self->{events} = Heap->new(Event->new(-1, -1));
$self->{events}->add(
Event->new(
SUBMISSION_EVENT,
$_->submit_time(),
$_
)
) for (@{$self->{trace}->jobs()});
$self->{run_time} = time();
while (my @events = $self->{events}->retrieve_all()) {
$self->{current_time} = $events[0]->timestamp();
$self->{execution_profile}->set_current_time($self->{current_time});
$logger->trace("current time: $self->{current_time}");
my @typed_events;
push @{$typed_events[$_->type()]}, $_ for @events;
$logger->trace("submission events: @{$typed_events[SUBMISSION_EVENT]}") if $typed_events[SUBMISSION_EVENT];
$logger->trace("ending events: @{$typed_events[JOB_COMPLETION_EVENT]}") if $typed_events[JOB_COMPLETION_EVENT];
for my $event (@{$typed_events[JOB_COMPLETION_EVENT]}) {
my $job = $event->payload();
$self->{execution_profile}->remove_job($job, $self->{current_time}) unless $job->requested_time() == $job->run_time();
}
# reassign all reserved jobs if any job finished
$self->reassign_jobs() if $config->param('backfilling.reassign_jobs') and scalar @{$typed_events[JOB_COMPLETION_EVENT]};
for my $event (@{$typed_events[SUBMISSION_EVENT]}) {
my $job = $event->payload();
$self->assign_job($job);
$logger->logdie("job " . $job->job_number() . " was not assigned") unless defined $job->starting_time();
push @{$self->{reserved_jobs}}, $job;
$self->{events}->add(
Event->new(
JOB_START_EVENT,
$job->starting_time(),
$job
)
);
}
$self->start_jobs();
}
# all jobs should be scheduled and started
$logger->logdie('there are still jobs in the reserved queue') if @{$self->{reserved_jobs}};
$self->{execution_profile}->free_profiles();
$self->{run_time} = time() - $self->{run_time};
return;
}
sub start_jobs {
my ($self) = @_;
my @remaining_reserved_jobs;
my $logger = get_logger('Backfilling::start_jobs');
for my $job (@{$self->{reserved_jobs}}) {
if ($job->starting_time() == $self->{current_time}) {
$logger->trace('job ' . $job->job_number() . ' starting');
$self->{events}->add(
Event->new(
JOB_COMPLETION_EVENT,
$job->real_ending_time(),
$job
)
);
} else {
push @remaining_reserved_jobs, $job;
}
}
$self->{reserved_jobs} = \@remaining_reserved_jobs;
return;
}
sub reassign_jobs {
my ($self) = @_;
my $logger = get_logger('Backfilling:reassign_jobs');
for my $job (@{$self->{reserved_jobs}}) {
$logger->trace('trying to reassign job ' . $job->job_number());
next unless $self->{execution_profile}->available_processors($self->{current_time}) >= $job->requested_cpus();
$logger->trace('available processors');
my $job_starting_time = $job->starting_time();
my $assigned_processors = $job->assigned_processors();
$self->{execution_profile}->remove_job($job, $self->{current_time});
unless ($self->{execution_profile}->could_start_job($job, $self->{current_time})) {
$self->{execution_profile}->add_job($job_starting_time, $job);
next;
}
$logger->trace('could start job');
my $new_processors = $self->{execution_profile}->get_free_processors($job, $self->{current_time});
unless (defined $new_processors) {
$self->{execution_profile}->add_job($job_starting_time, $job);
next;
}
$logger->trace('new processors defined');
$job->assign($self->{current_time}, $new_processors);
$self->{execution_profile}->add_job($self->{current_time}, $job);
}
return;
}
sub assign_job {
my ($self, $job) = @_;
my $logger = get_logger('Backfilling::assign_job');
$logger->trace('assigning job ' . $job->job_number());
my ($starting_time, $chosen_processors) = $self->{execution_profile}->find_first_profile($job);
$logger->trace("chose starting time:processors $starting_time:$chosen_processors");
my $job_platform_level = $self->{platform}->job_level_distance($chosen_processors);
my $job_minimum_level = $self->{platform}->job_minimum_level_distance($job->requested_cpus());
$logger->trace("job levels $job_platform_level/$job_minimum_level");
if ($job_platform_level != $job_minimum_level) {
my $new_job_run_time;
switch ($self->{penalty_function}) {
case 'linear' {
$new_job_run_time = $job->run_time() * (1 + ($job_platform_level - $job_minimum_level) * $self->{penalty_factor});
}
case 'quadratic' {
$new_job_run_time = $job->run_time() * $self->{penalty_factor} ** ($job_platform_level - $job_minimum_level)
}
}
$job->original_run_time($job->run_time());
$logger->trace("using penalty function $self->{penalty_function} and factor $self->{penalty_factor}");
$logger->trace("new job run time: $new_job_run_time (from " . $job->run_time() . ')');
if ($new_job_run_time > $job->requested_time()) {
$job->run_time($job->requested_time());
$job->status(JOB_STATUS_FAILED);
} else {
$job->run_time($new_job_run_time);
}
}
$job->assign($starting_time, $chosen_processors);
$self->{execution_profile}->add_job($starting_time, $job);
return;
}
1;