-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForcedPlatform.pm
98 lines (72 loc) · 2.34 KB
/
ForcedPlatform.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
package ForcedPlatform;
use parent 'BestEffortPlatform';
use strict;
use warnings;
use Data::Dumper;
use Switch;
use ProcessorRange;
use BestEffortPlatform qw(DEFAULT SMALLEST_FIRST BIGGEST_FIRST);
sub new {
my ($class, @remaining_parameters) = @_;
my $self = $class->SUPER::new(@remaining_parameters);
bless $self, $class;
return $self;
}
sub reduce {
my ($self, $job, $left_processors) = @_;
my $available_cpus = $self->{platform}->available_cpus_in_clusters($left_processors);
my $cpus_structure = $self->{platform}->build_structure($available_cpus);
my $chosen_ranges = $self->choose_cpus($cpus_structure, $job->requested_cpus());
if (defined $chosen_ranges) {
$left_processors->affect_ranges(ProcessorRange::sort_and_fuse_contiguous_ranges($chosen_ranges));
} else {
$left_processors->remove_all();
}
return;
}
sub choose_cpus {
my ($self, $cpus_structure, $target_number) = @_;
my @suitable_levels = grep {$_->[0]->{total_original_size} >= $target_number} (@{$cpus_structure});
# The first level is the only one that can be used since this is a
# forced variant
my @sorted_blocks;
switch ($self->{mode}) {
case SMALLEST_FIRST {
@sorted_blocks = sort { $a->{total_size} <=> $b->{total_size} } (@{$suitable_levels[0]});
}
case BIGGEST_FIRST {
@sorted_blocks = sort { $b->{total_size} <=> $a->{total_size} } (@{$suitable_levels[0]});
}
else {
@sorted_blocks = @{$suitable_levels[0]};
}
}
my $chosen_block;
for my $cpus_block (@sorted_blocks) {
if ($cpus_block->{total_size} >= $target_number) {
$chosen_block = $cpus_block;
last;
}
}
# If this is undefined means there are no minimum sized blocks that
# have enough available CPUs for the job
return unless (defined $chosen_block);
my @chosen_ranges;
my $range_start = shift @{$chosen_block->{cpus}};
my $range_end = $range_start;
my $taken_cpus = 0;
while ($taken_cpus < $target_number) {
my $cpu_number = shift @{$chosen_block->{cpus}};
while ((defined $cpu_number) and ($cpu_number == $range_end + 1)
and ($taken_cpus + $range_end - $range_start + 1 < $target_number)) {
$range_end = $cpu_number;
$cpu_number = shift @{$chosen_block->{cpus}};
}
push @chosen_ranges, [$range_start, $range_end];
$taken_cpus += $range_end - $range_start + 1;
$range_start = $cpu_number;
$range_end = $range_start;
}
return \@chosen_ranges;
}
1;