| 1 | package Math::Matlab::Pool; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use vars qw($VERSION $MEMBERS $SYNC_FILE); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 1 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | $VERSION = sprintf "%d.%03d", q$Revision: 1.4 $ =~ /: (\d+)\.(\d+)/; | |||||||||||||||||||
| 8 | } | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 1 | ▶ | use Math::Matlab; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 1 | ▶ | use base qw( Math::Matlab ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | ▶ | use Fcntl qw(:DEFAULT :flock); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | ##----- assign defaults, unless already set externally ----- | ||||||||||||||||||||
| 16 | 1 | ▶ | $MEMBERS = [] unless defined $MEMBERS; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 17 | 1 | ▶ | $SYNC_FILE = '/tmp/MatlabPool.lock' unless defined $SYNC_FILE; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | ##----- Public Class Methods ----- | ||||||||||||||||||||
| 20 | 1 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||
| 21 | 1 | my ($class, $href) = @_; | |||||||||||||||||||
| 22 | my $self = { | ||||||||||||||||||||
| 23 | members => defined($href->{members}) ? $href->{members} : $MEMBERS, | ||||||||||||||||||||
| 24 | 1 | ▶ | sync_file => defined($href->{sync_file}) ? $href->{sync_file} : $SYNC_FILE, | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 25 | err_msg => '', | ||||||||||||||||||||
| 26 | result => '' | ||||||||||||||||||||
| 27 | }; | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 1 | bless $self, $class; | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | ## create objects from config as necessary | ||||||||||||||||||||
| 32 | 1 | foreach my $i ( 0..$#{$self->members} ) { | |||||||||||||||||||
| 33 | 3 | my $member = $self->members->[$i]; | |||||||||||||||||||
| 34 | 3 | ▶ | next unless ref $member eq 'HASH'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | 3 | my $class = $member->{class}; | |||||||||||||||||||
| 36 | 3 | $self->members->[$i] = $class->new( $member->{args} ); | |||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 1 | return $self; | |||||||||||||||||||
| 40 | } | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | ##----- Public Object Methods ----- | ||||||||||||||||||||
| 43 | 0 | ▶ | sub execute { | ||||||||||||||||||
|
Subroutine
execute: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 44 | 0 | my ($self, $code, $rel_mwd) = @_; | |||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 0 | my $matlab = $self->members->[ $self->next_index ]; | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 0 | ▶ | if ($matlab->execute($code, $rel_mwd)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | 0 | $self->{'result'} = $matlab->fetch_raw_result; | |||||||||||||||||||
| 50 | 0 | return 1; | |||||||||||||||||||
| 51 | } else { | ||||||||||||||||||||
| 52 | 0 | $self->err_msg( $matlab->err_msg ); | |||||||||||||||||||
| 53 | 0 | return 0; | |||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 0 | ▶ | sub next_index { | ||||||||||||||||||
|
Subroutine
next_index: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||
| 58 | 0 | my ($self) = @_; | |||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | ## the following code from p. 247 of Perl Cookbook | ||||||||||||||||||||
| 61 | 0 | ▶ | sysopen(FH, $self->sync_file, O_RDWR|O_CREAT) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 62 | or die "can't open syncfile: $!"; | ||||||||||||||||||||
| 63 | 0 | ▶ | flock(FH, LOCK_EX) or die "can't lock syncfile: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | ## now we have the lock, let's do our stuff | ||||||||||||||||||||
| 65 | 0 | ▶ | my $num = <FH> || $#{$self->members}; | ||||||||||||||||||
|
Condition0%0/3
readline FH || $#{$self->members;}
MC/DC0%0/2
readline FH || $#{$self->members;}
readline FH
$#{$self->members;}
Truth table0%0/3
A || B
A= readline FHB= $#{$self->members;}
| |||||||||||||||||||||
| 66 | 0 | ▶ | seek(FH, 0, 0) or die "can't rewind syncfile: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | 0 | ▶ | truncate(FH, 0) or die "can't truncate syncfile: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | 0 | $num++; | |||||||||||||||||||
| 69 | 0 | ▶ | $num = 0 if $num > $#{$self->members}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | 0 | ▶ | print FH $num, "\n" or die "can't write syncfile: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | 0 | ▶ | close(FH) or die "can't close syncfile: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 0 | return $num; | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 7 | ▶ | sub members { my $self = shift; return $self->_getset('members', @_); } | ||||||||||||||||||
|
Subroutine
members: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 77 | 0 | ▶ | sub sync_file { my $self = shift; return $self->_getset('sync_file', @_); } | ||||||||||||||||||
|
Subroutine
sync_file: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 1 | 1; | |||||||||||||||||||
| 80 | __END__ | ||||||||||||||||||||