Limiter.pm 3.19 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-2014] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
15 16 17

    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
18

19 20 21 22 23 24 25 26
         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

27
  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
28 29 30 31 32

=cut


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

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

    my $self = bless {}, $class;
38
    $self->description( $description );
39 40 41 42 43 44 45 46 47
    $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;
}


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

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


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
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) = @_;

88 89 90 91
    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
92 93 94

        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)
95 96 97 98 99 100 101 102 103 104 105

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

    return $slots_asked;
}


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

106 107 108 109 110
    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
111

112
        $self->available_capacity( $available_capacity - $slots_agreed/$multiplier );
113 114 115 116 117
    }
}


1;