Cluster.pm

filter: Statementstmt 11.0% 26 / 235 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 36.3% 8 / 22 Podpod 42.8% 6 / 14 totaltotal 10.8% 40 / 370 CC 50 SCAR 45.9
CC
50
Cov
8%
qx
306.0
close
72.0
close2
72.0
CRAP 98.6
↑ Summary Cluster/Result.pm »
1 package GRID::Cluster;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use GRID::Machine;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use GRID::Cluster::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use GRID::Cluster::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use List::Util qw(sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Size of the buffer which is used to read from
13 # remote processes
14 1 ▶use constant BUFFERSIZE => 2048;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 our $VERSION = '0.04';
17  
18 # Constructor
19  
20 0 ▶sub new {
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
21 0 my $class = shift;
22 0 my %opts = @_;
23  
24 0 my $self;
25 0 ▶ if (exists($opts{config}) && -r $opts{config}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{'config'} and -r $opts{'config'}) { } 0 0 0
Condition0%0/3
exists $opts{"config"} and -r $opts{"config"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opts{"config"} and -r $opts{"config"}
exists $opts{"config"} -r $opts{"config"}
Truth table0%0/3
A and B
A= exists $opts{"config"}B= -r $opts{"config"}
ABresult
0X0
100
111
26 0 %$self = do $opts{config};
27 }
28 else {
29 $self = {
30 debug => $opts{debug},
31 max_num_np => $opts{max_num_np},
32 0 hosts => {},
33 };
34 }
35  
36 0 my @proposed_names = keys %{$self->{max_num_np}};
37 0 $self->{host_names} = [];
38  
39
40  
41 0 my $logic_id = 0;
42
43 0 for (@proposed_names) {
44 0 eval{
45 $self->{hosts}{$_} = GRID::Machine->new (
46 host => $_,
47 0 debug => $self->{debug}{$_},
48 logic_id => $logic_id,
49 );
50 };
51  
52 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
53 0 warn "Warning: Host $_ has not been initialized: $@";
54 0 delete $self->{debug}{$_};
55 0 delete $self->{max_num_np}{$_};
56 0 delete $self->{hosts}{$_};
57 }
58 else {
59 0 push @{$self->{host_names}}, $_;
60 0 $logic_id++;
61 }
62 }
63  
64 0 bless $self, $class;
65 0 ▶ return $self if (@{$self->{host_names}});
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"host_names"};} 0 0 0
66 0 return undef;
67 }
68  
69 # Getters
70  
71 0 ▶sub get_host {
Subroutine get_host: not called CC=2 SCAR=17.9
Pod undocumented
72 0 my ($self, $host_name) = @_;
73 0 ▶ return $self->{hosts}{$host_name} if defined ($self->{hosts}{$host_name});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"hosts"}{$host_name} 0 0 0
74 }
75  
76 0 ▶sub get_max_np {
Subroutine get_max_np: not called CC=1 SCAR=6.9
Pod undocumented
77 0 my $self = shift;
78 0 return sum values %{$self->{max_num_np}};
79 }
80  
81 0 ▶sub get_num_machines {
Subroutine get_num_machines: not called CC=1 SCAR=6.9
Pod undocumented
82 0 my $self = shift;
83 0 my $num = @{$self->{host_names}};
84 0 return $num;
85 }
86  
87 0 ▶sub get_machine_names {
Subroutine get_machine_names: not called CC=1 SCAR=6.9
Pod undocumented
88 0 my $self = shift;
89 0 return keys %{$self->{max_num_np}};
90 }
91  
92 # Methods
93  
94 0 ▶sub copyandmake {
Subroutine copyandmake: not called CC=2 SCAR=17.9
Pod documented
95 0 my $self = shift;
96 0 my %opts = @_;
97  
98 0 my $r = GRID::Cluster::Result->new();
99  
100 0 for (@{$self->{host_names}}) {
101 0 my $machine_result = $self->{hosts}{$_}->copyandmake(%opts);
102 0 $r->add(host_name => $_, machine_result => $machine_result);
103 }
104  
105 0 return $r;
106 }
107  
108 0 ▶sub chdir {
Subroutine chdir: not called CC=2 SCAR=17.9
Pod documented
109 0 my ($self, $dir) = @_;
110  
111 0 my $r = GRID::Cluster::Result->new();
112  
113 0 for (@{$self->{host_names}}) {
114 0 my $machine_result = $self->{hosts}{$_}->chdir($dir);
115 0 $r->add(host_name => $_, machine_result => $machine_result);
116 }
117  
118 0 return $r;
119 }
120  
121 0 ▶sub modput {
Subroutine modput: not called CC=2 SCAR=17.9
Pod documented
122 0 my $self = shift;
123 0 my @modules = @_;
124  
125 0 my $r = GRID::Cluster::Result->new();
126  
127 0 for (@{$self->{host_names}}) {
128 0 my $machine_result = $self->{hosts}{$_}->modput(@modules);
129 0 $r->add(host_name => $_, machine_result => $machine_result);
130 }
131  
132 0 return $r;
133 }
134  
135 0 ▶sub eval {
Subroutine eval: not called CC=3 SCAR=24.8
Pod documented
136 0 my $self = shift;
137 0 my ($code, @args) = @_;
138  
139 0 my $r = GRID::Cluster::Result->new();
140  
141 # First round: Send eval operations to each machine
142 0 for (@{$self->{host_names}}) {
143 0 $self->{hosts}{$_}->send_operation("GRID::Machine::EVAL", $code, \@args);
144 }
145  
146 # Second round: Receive different results from each machine
147 0 for (@{$self->{host_names}}) {
148 0 my $machine_result = $self->{hosts}{$_}->_get_result();
149 0 $r->add(host_name => $_, machine_result => $machine_result);
150 }
151  
152 0 return $r;
153 }
154  
155 0 ▶sub qx {
Subroutine qx: not called CC=17 SCAR=57.2
Pod documented
156 0 my $self = shift;
157 0 my @commands = map { "$_ | " } @_;
158  
159 0 my @proc;
160 my @pid;
161 0 my %map_id_machine;
162 0 my %id;
163  
164 0 my $counter = 0;
165
166 0 my $np = @commands;
167 0 my $lp = $np - 1;
168 0 my $readset = IO::Select->new();
169  
170 0 for (@{$self->{host_names}}) {
171 0 for my $actual_proc (0 .. $self->{max_num_np}{$_} - 1) {
172 0 my $m = $self->get_host($_);
173 0 ($proc[$counter], $pid[$counter]) = $m->open(shift @commands);
174 0 $proc[$counter]->blocking(1);
175  
176 0 $map_id_machine{$counter} = $_;
177 0 $readset->add($proc[$counter]);
178 0 my $address = 0 + $proc[$counter];
179 0 $id{$address} = $counter;
180  
181 0 $counter++;
182  
183 # See if all workers are busy, if so wait for one to finish
184 0 ▶ last if (($counter > $lp) || ($counter >= $self->get_max_np()));
Branch: 1 branch
BranchTrueFalseTotal
if $counter > $lp or $counter >= $self->get_max_np 0 0 0
Condition0%0/3
$counter > $lp or $counter >= $self->get_max_np
l!l&&r!l&&!r
000
MC/DC0%0/2
$counter > $lp or $counter >= $self->get_max_np
$counter > $lp $counter >= $self->get_max_np
Truth table0%0/3
A or B
A= $counter > $lpB= $counter >= $self->get_max_np
ABresult
1X1
011
000
185 }
186 0 ▶ last if (($counter > $lp) || ($counter >= $self->get_max_np()));
Branch: 1 branch
BranchTrueFalseTotal
if $counter > $lp or $counter >= $self->get_max_np 0 0 0
Condition0%0/3
$counter > $lp or $counter >= $self->get_max_np
l!l&&r!l&&!r
000
MC/DC0%0/2
$counter > $lp or $counter >= $self->get_max_np
$counter > $lp $counter >= $self->get_max_np
Truth table0%0/3
A or B
A= $counter > $lpB= $counter >= $self->get_max_np
ABresult
1X1
011
000
187 }
188
189 0 my $count = 0;
190 0 my @ready;
191 my @result;
192  
193 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
++$count == $np or do { do { push @ready, $readset->can_read unless @ready; my $handle = shift @ready; my $me = $id{0 + $handle}; my($aux, $bytes, $r); while (not defined $bytes or $bytes) { $bytes = sysread($handle, $aux, 2048); $r .= $aux if defined $bytes and $bytes; } ; $result[$me] = $r; $readset->remove($handle) if eof $handle; CORE::close $handle; if (@commands) { my $m = $self->get_host($map_id_machine{$me}); ($proc[$counter], $pid[$counter]) = $m->open(shift @commands); $proc[$counter]->blocking(1); $map_id_machine{$counter} = $map_id_machine{$me}; $readset->add($proc[$counter]); my $address = 0 + $proc[$counter]; $id{$address} = $counter; ++$counter; } } } 0 0 0
194 0 ▶ push @ready, $readset->can_read unless @ready;
Branch: 1 branch
BranchTrueFalseTotal
unless @ready 0 0 0
195  
196 0 my $handle = shift @ready;
197  
198 0 my $me = $id{0 + $handle};
199  
200 0 my ($aux, $bytes, $r);
201  
202 0 ▶ while ((!defined($bytes)) || ($bytes)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $bytes or $bytes) 0 0 0
Condition0%0/3
not defined $bytes or $bytes
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $bytes or $bytes
not defined $bytes $bytes
Truth table0%0/3
not(A) or B
A= not defined $bytesB= $bytes
ABresult
1X1
011
000
203 0 $bytes = sysread($handle, $aux, BUFFERSIZE);
204 0 ▶ $r .= $aux if ((defined($bytes)) && ($bytes));
Branch: 1 branch
BranchTrueFalseTotal
if defined $bytes and $bytes 0 0 0
Condition0%0/3
defined $bytes and $bytes
!ll&&!rl&&r
000
MC/DC0%0/2
defined $bytes and $bytes
defined $bytes $bytes
Truth table0%0/3
A and B
A= defined $bytesB= $bytes
ABresult
0X0
100
111
205 }
206  
207 0 $result[$me] = $r;
208  
209 0 ▶ $readset->remove($handle) if eof($handle);
Branch: 1 branch
BranchTrueFalseTotal
if eof $handle 0 0 0
210  
211 0 close $handle;
212  
213 0 ▶ if (@commands) {
Branch: 1 branch
BranchTrueFalseTotal
if (@commands) 0 0 0
214 0 my $m = $self->get_host($map_id_machine{$me});
215 0 ($proc[$counter], $pid[$counter]) = $m->open(shift @commands);
216 0 $proc[$counter]->blocking(1);
217  
218 0 $map_id_machine{$counter} = $map_id_machine{$me};
219 0 $readset->add($proc[$counter]);
220 0 my $address = 0 + $proc[$counter];
221 0 $id{$address} = $counter;
222  
223 0 $counter++;
224 }
225  
226 } until (++$count == $np);
227  
228 0 my @results;
229 0 my $i = 0;
230  
231 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
232 0 foreach (@result) {
233 0 $results[$i] = [ split /\n/, $_ ];
234 0 $i++;
235 }
236 0 return @results;
237 }
238 0 return \@result;
239 }
240  
241 0 ▶sub open {
Subroutine open: not called CC=5 SCAR=34.0
Pod undocumented
242 0 my ($self, @command) = @_;
243  
244 0 my @proc;
245 my @pid;
246 0 my %map_id_machine;
247 0 my %id;
248  
249 0 my $counter = 0;
250 0 my $np = @command;
251 0 my $lp = $np - 1;
252 0 my $readset = IO::Select->new();
253  
254 0 for (@{$self->{host_names}}) {
255 0 for my $actual_proc (0 .. $self->{max_num_np}{$_} - 1) {
256 0 my $m = $self->get_host($_);
257 0 ($proc[$counter], $pid[$counter]) = $m->open($command[$counter]);
258 0 $proc[$counter]->blocking(1);
259  
260 0 $map_id_machine{$counter} = $_;
261 0 $readset->add($proc[$counter]);
262 0 my $address = 0 + $proc[$counter];
263 0 $id{$address} = $counter;
264  
265 0 ▶ last if (++$counter > $lp);
Branch: 1 branch
BranchTrueFalseTotal
if ++$counter > $lp 0 0 0
266 }
267 0 ▶ last if ($counter > $lp);
Branch: 1 branch
BranchTrueFalseTotal
if $counter > $lp 0 0 0
268 }
269  
270 0 return GRID::Cluster::Handle->new (
271 readset => $readset,
272 proc => \@proc,
273 pid => \@pid,
274 id => \%id,
275 map_id_machine => \%map_id_machine
276 );
277 }
278  
279 0 ▶sub open2 {
Subroutine open2: not called CC=5 SCAR=34.0
Pod undocumented
280 0 my ($self, @command) = @_;
281  
282 0 my @rproc;
283 my @wproc;
284 0 my @pid;
285 0 my %map_id_machine;
286 0 my %id;
287  
288 0 my $counter = 0;
289 0 my $np = @command;
290 0 my $lp = $np - 1;
291 0 my $readset = IO::Select->new();
292  
293 0 for (@{$self->{host_names}}) {
294 0 for my $actual_proc (0 .. $self->{max_num_np}{$_} - 1) {
295 0 my $m = $self->get_host($_);
296 0 $wproc[$counter] = IO::Handle->new();
297 0 $rproc[$counter] = IO::Handle->new();
298 0 $pid[$counter] = $m->open2($rproc[$counter], $wproc[$counter], $command[$counter]);
299  
300 0 $map_id_machine{$counter} = $_;
301 0 $readset->add($rproc[$counter]);
302 0 my $address = 0 + $rproc[$counter];
303 0 $id{$address} = $counter;
304  
305 0 ▶ last if (++$counter > $lp);
Branch: 1 branch
BranchTrueFalseTotal
if ++$counter > $lp 0 0 0
306 }
307 0 ▶ last if ($counter > $lp);
Branch: 1 branch
BranchTrueFalseTotal
if $counter > $lp 0 0 0
308 }
309  
310 0 return GRID::Cluster::Handle->new (
311 readset => $readset,
312 rproc => \@rproc,
313 wproc => \@wproc,
314 pid => \@pid,
315 id => \%id,
316 map_id_machine => \%map_id_machine
317 );
318 }
319  
320 0 ▶sub close {
Subroutine close: not called CC=8 SCAR=42.8
Pod undocumented
321 0 my ($self, $cluster_handle) = @_;
322  
323 0 my $readset = $cluster_handle->get_readset();
324 0 my %id = %{$cluster_handle->get_id()};
325 0 my $np = values(%id);
326
327 0 my $count = 0;
328 0 my @ready;
329 my @result;
330  
331 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
++$count == $np or do { do { push @ready, $readset->can_read unless @ready; my $handle = shift @ready; my $me = $id{0 + $handle}; my($aux, $bytes, $r); while (not defined $bytes or $bytes) { $bytes = sysread($handle, $aux, 2048); $r .= $aux if defined $bytes and $bytes; } ; $result[$me] = $r; $readset->remove($handle) if eof $handle; CORE::close $handle } } 0 0 0
332 0 ▶ push @ready, $readset->can_read unless @ready;
Branch: 1 branch
BranchTrueFalseTotal
unless @ready 0 0 0
333  
334 0 my $handle = shift @ready;
335  
336 0 my $me = $id{0 + $handle};
337  
338 0 my ($aux, $bytes, $r);
339  
340 0 ▶ while ((!defined($bytes)) || ($bytes)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $bytes or $bytes) 0 0 0
Condition0%0/3
not defined $bytes or $bytes
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $bytes or $bytes
not defined $bytes $bytes
Truth table0%0/3
not(A) or B
A= not defined $bytesB= $bytes
ABresult
1X1
011
000
341 0 $bytes = sysread($handle, $aux, BUFFERSIZE);
342 0 ▶ $r .= $aux if ((defined($bytes)) && ($bytes));
Branch: 1 branch
BranchTrueFalseTotal
if defined $bytes and $bytes 0 0 0
Condition0%0/3
defined $bytes and $bytes
!ll&&!rl&&r
000
MC/DC0%0/2
defined $bytes and $bytes
defined $bytes $bytes
Truth table0%0/3
A and B
A= defined $bytesB= $bytes
ABresult
0X0
100
111
343 }
344  
345 0 $result[$me] = $r;
346  
347 0 ▶ $readset->remove($handle) if eof($handle);
Branch: 1 branch
BranchTrueFalseTotal
if eof $handle 0 0 0
348  
349 0 close $handle;
350  
351 } until (++$count == $np);
352  
353 0 return \@result;
354 }
355  
356 0 ▶sub close2 {
Subroutine close2: not called CC=8 SCAR=42.8
Pod undocumented
357 0 my ($self, $cluster_handle) = @_;
358  
359 0 my $readset = $cluster_handle->get_readset();
360 0 my @wproc = @{$cluster_handle->get_wproc()};
361 0 my %id = %{$cluster_handle->get_id()};
362 0 my $np = values(%id);
363  
364 0 my $count = 0;
365 0 my @ready;
366 my @result;
367  
368 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
++$count == $np or do { do { push @ready, $readset->can_read unless @ready; my $handle = shift @ready; my $me = $id{0 + $handle}; my($aux, $bytes, $r); while (not defined $bytes or $bytes) { $bytes = sysread($handle, $aux, 2048); $r .= $aux if defined $bytes and $bytes; } ; $result[$me] = $r; $readset->remove($handle) if eof $handle; $handle->close; $wproc[$count]->close } } 0 0 0
369 0 ▶ push @ready, $readset->can_read unless @ready;
Branch: 1 branch
BranchTrueFalseTotal
unless @ready 0 0 0
370  
371 0 my $handle = shift @ready;
372  
373 0 my $me = $id{0 + $handle};
374  
375 0 my ($aux, $bytes, $r);
376  
377 0 ▶ while ((!defined($bytes)) || ($bytes)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $bytes or $bytes) 0 0 0
Condition0%0/3
not defined $bytes or $bytes
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $bytes or $bytes
not defined $bytes $bytes
Truth table0%0/3
not(A) or B
A= not defined $bytesB= $bytes
ABresult
1X1
011
000
378 0 $bytes = sysread($handle, $aux, BUFFERSIZE);
379 0 ▶ $r .= $aux if ((defined($bytes)) && ($bytes));
Branch: 1 branch
BranchTrueFalseTotal
if defined $bytes and $bytes 0 0 0
Condition0%0/3
defined $bytes and $bytes
!ll&&!rl&&r
000
MC/DC0%0/2
defined $bytes and $bytes
defined $bytes $bytes
Truth table0%0/3
A and B
A= defined $bytesB= $bytes
ABresult
0X0
100
111
380 }
381  
382 0 $result[$me] = $r;
383  
384 0 ▶ $readset->remove($handle) if eof($handle);
Branch: 1 branch
BranchTrueFalseTotal
if eof $handle 0 0 0
385  
386 0 $handle->close();
387 0 $wproc[$count]->close();
388  
389 } until (++$count == $np);
390  
391 0 return \@result;
392 }
393  
394 1 1;
395 __END__
↑ Summary Cluster/Result.pm »