Limiter.pm 3.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
=pod 

=head1 NAME

    Bio::EnsEMBL::Hive::Limiter;

=head1 DESCRIPTION

    A simple data object that has a conditional capper/minimizer built in.
    Simple but very useful in the context of multi-parameter scheduling.

=head1 LICENSE

14
    Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Matthieu Muffato's avatar
Matthieu Muffato committed
15
    Copyright [2016-2018] EMBL-European Bioinformatics Institute
16 17 18

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
19

20 21 22 23 24 25 26 27
         http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License
    is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and limitations under the License.

=head1 CONTACT

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 31 32 33

=cut


package Bio::EnsEMBL::Hive::Limiter;
34 35

sub new {
36
    my ($class, $description, $available_capacity) = @_;
37 38

    my $self = bless {}, $class;
39
    $self->description( $description );
40 41 42 43 44 45 46 47 48
    $self->available_capacity( $available_capacity );

        # we fix the multiplier at 1 for direct limiters, but expect it to be (re)set later by reciprocal limiters:
    $self->multiplier( 1 );     

    return $self;
}


49 50 51 52 53 54 55 56 57 58
sub description {
    my $self = shift @_;

    if(@_) {
        $self->{_description} = shift @_;
    }
    return $self->{_description};
}


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
sub available_capacity {
    my $self = shift @_;

    if(@_) {
        $self->{_available_capacity} = shift @_;
    }
    return $self->{_available_capacity};
}


sub multiplier {
    my $self = shift @_;

    if(@_) {
        $self->{_multiplier} = shift @_;
    }
    return $self->{_multiplier};
}


sub reached {
    my $self = shift @_;

    return defined($self->available_capacity) && ($self->available_capacity <= 0.0);
}


sub preliminary_offer {
    my ($self, $slots_asked) = @_;

89 90 91 92
    my $available_capacity  = $self->available_capacity;
    my $multiplier          = $self->multiplier;

    if( defined($available_capacity) and defined($multiplier) and ($multiplier >= 0.0) ) {  # if multiplier is negative it is not limiting
Leo Gordon's avatar
Leo Gordon committed
93

94 95
        $available_capacity = 0 if($available_capacity<0);

Leo Gordon's avatar
Leo Gordon committed
96 97
        my $product = $available_capacity * $multiplier;
        my $slots_available = int( "$product" );            # stringification helps to round up things like 0.1*10 (instead of leaving them at 0.99999999)
98 99 100 101 102 103 104 105 106 107 108

        return ($slots_available<$slots_asked) ? $slots_available : $slots_asked;
    }

    return $slots_asked;
}


sub final_decision {
    my ($self, $slots_agreed) = @_;

109 110 111 112 113
    my $available_capacity  = $self->available_capacity;
    my $multiplier          = $self->multiplier;

    if( defined($available_capacity) and defined($multiplier) and ($multiplier > 0.0) ) {   # if multiplier is not positive capacity stays unaffected
                                                                                            # and we should not arrive here if $multiplier==0
114

115
        $self->available_capacity( $available_capacity - $slots_agreed/$multiplier );
116 117 118 119 120
    }
}


1;