ensembl-hive  2.6
Limiter.pm
Go to the documentation of this file.
1 =pod
2 
3 =head1 NAME
4 
6 
7 =head1 DESCRIPTION
8 
9  A simple data object that has a conditional capper/minimizer built in.
10  Simple but very useful in the context of multi-parameter scheduling.
11 
12 =head1 LICENSE
13 
14  Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
15  Copyright [2016-2024] EMBL-European Bioinformatics Institute
16 
17  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
18  You may obtain a copy of the License at
19 
20  http://www.apache.org/licenses/LICENSE-2.0
21 
22  Unless required by applicable law or agreed to in writing, software distributed under the License
23  is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24  See the License for the specific language governing permissions and limitations under the License.
25 
26 =head1 CONTACT
27 
28  Please subscribe to the Hive mailing list: http://listserver.ebi.ac.uk/mailman/listinfo/ehive-users to discuss Hive-related questions or to be notified of our updates
29 
30 =cut
31 
32 
33 package Bio::EnsEMBL::Hive::Limiter;
34 
35 use strict;
36 use warnings;
37 
38 sub new {
39  my ($class, $description, $available_capacity) = @_;
40 
41  my $self = bless {}, $class;
42  $self->description( $description );
43 
44  $self->original_capacity( $available_capacity );
45  $self->available_capacity( $available_capacity );
46 
47  # we fix the multiplier at 1 for direct limiters, but expect it to be (re)set later by reciprocal limiters:
48  $self->multiplier( 1 );
49 
50  return $self;
51 }
52 
53 
54 sub description {
55  my $self = shift @_;
56 
57  if(@_) {
58  $self->{_description} = shift @_;
59  }
60  return $self->{_description};
61 }
62 
63 
64 sub original_capacity {
65  my $self = shift @_;
66 
67  if(@_) {
68  $self->{_original_capacity} = shift @_;
69  }
70  return $self->{_original_capacity};
71 }
72 
73 
74 sub available_capacity {
75  my $self = shift @_;
76 
77  if(@_) {
78  $self->{_available_capacity} = shift @_;
79  }
80  return $self->{_available_capacity};
81 }
82 
83 
84 sub multiplier {
85  my $self = shift @_;
86 
87  if(@_) {
88  $self->{_multiplier} = shift @_;
89  }
90  return $self->{_multiplier};
91 }
92 
93 
94 sub reached {
95  my $self = shift @_;
96 
97  return defined($self->available_capacity) && ($self->available_capacity <= 0.0);
98 }
99 
100 
101 sub preliminary_offer {
102  my ($self, $slots_asked) = @_;
103 
104  my $available_capacity = $self->available_capacity;
105  my $multiplier = $self->multiplier;
106 
107  if( defined($available_capacity) and defined($multiplier) and ($multiplier >= 0.0) ) { # if multiplier is negative it is not limiting
108 
109  $available_capacity = 0 if($available_capacity<0);
110 
111  my $product = $available_capacity * $multiplier;
112  my $slots_available = int( "$product" ); # stringification helps to round up things like 0.1*10 (instead of leaving them at 0.99999999)
113 
114  my $hit_the_limit = $slots_available<$slots_asked;
115 
116  return ($hit_the_limit ? $slots_available : $slots_asked , $hit_the_limit);
117  }
118 
119  return ($slots_asked, 0);
120 }
121 
122 
123 sub final_decision {
124  my ($self, $slots_agreed) = @_;
125 
126  my $available_capacity = $self->available_capacity;
127  my $multiplier = $self->multiplier;
128 
129  if( defined($available_capacity) and defined($multiplier) and ($multiplier > 0.0) ) { # if multiplier is not positive capacity stays unaffected
130  # and we should not arrive here if $multiplier==0
131 
132  $self->available_capacity( $available_capacity - $slots_agreed/$multiplier );
133  }
134 }
135 
136 
137 1;
Bio::EnsEMBL::Hive::Version
Definition: Version.pm:19
Bio::EnsEMBL::Hive::Limiter
Definition: Limiter.pm:10
Bio::EnsEMBL::Hive
Definition: Hive.pm:38