PerlStone2015/spectralnorm.pm

filter: Statementstmt 33.6% 33 / 98 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 44.4% 8 / 18 Podpod 0.0% 0 / 9 totaltotal 27.7% 41 / 148 CC 19 SCAR 28.6
CC
19
Cov
28%
do_parallel
56.0
num_cpus
20.0
init
12.6
CRAP 17.5
« PerlStone2015/revcomp.pm ↑ Summary PerlStone2015/binarytrees.pm »
1 package Benchmark::Perl::Formance::Plugin::PerlStone2015::spectralnorm;
2 1 our $AUTHORITY = 'cpan:SCHWIGON';
3 # ABSTRACT: benchmark - Eigenvalue using the power method
4 1 $Benchmark::Perl::Formance::Plugin::PerlStone2015::spectralnorm::VERSION = '0.002';
5 # COMMAND LINE:
6 # /usr/bin/perl spectralnorm.perl-3.perl 5500
7  
8 # The Computer Language Benchmarks Game
9 # http://shootout.alioth.debian.org/
10 #
11 # Contributed by Andrew Rodland
12 # Benchmark::Perl::Formance plugin by Steffen Schwigon
13  
14 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 #############################################################
18 # #
19 # Benchmark Code ahead - Don't touch without strong reason! #
20 # #
21 #############################################################
22  
23 1 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Benchmark ':hireswallclock';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 our $n;
27 1 our $size_of_float;
28 1 our $threads;
29 1 our @ranges;
30 1 our $begin;
31 1 our $end;
32  
33 0 ▶sub eval_A {
Subroutine eval_A: not called CC=1 SCAR=0.2
Pod undocumented
34 1 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 0 my $div = ( ($_[0] + $_[1]) * ($_[0] + $_[1] + 1) / 2) + $_[0] + 1;
36 1 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 0 1 / $div;
38 }
39  
40 0 ▶sub multiplyAv {
Subroutine multiplyAv: not called CC=2 SCAR=17.9
Pod undocumented
41 return map {
42 0 my ($i, $sum) = ($_);
43 0 $sum += eval_A($i, $_) * $_[$_] for 0 .. $#_;
44 0 $sum;
45 } $begin .. $end;
46 }
47  
48 0 ▶sub multiplyAtv {
Subroutine multiplyAtv: not called CC=2 SCAR=17.9
Pod undocumented
49 return map {
50 0 my ($i, $sum) = ($_);
51 0 $sum += eval_A($_, $i) * $_[$_] for 0 .. $#_;
52 0 $sum;
53 } $begin .. $end;
54 }
55  
56 0 ▶sub do_parallel {
Subroutine do_parallel: not called CC=7 SCAR=40.3
Pod undocumented
57 0 my $func = shift;
58  
59 0 my @out;
60 0 my (@fd, @ptr, %fh2proc);
61 0 for my $proc (0 .. $threads - 1) {
62 0 ($begin, $end) = @{ $ranges[$proc] };
63 0 my $pid = open $fd[$proc], "-|";
64 0 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) { } 0 0 0
65 0 print pack "F*", $func->( @_ );
66 0 exit 0;
67 } else {
68 0 $fh2proc{ $fd[$proc] } = $proc;
69 0 $ptr[$proc] = $begin;
70 }
71 }
72  
73 0 my $select = IO::Select->new(@fd);
74  
75 0 ▶ while ($select->count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($select->count) 0 0 0
76 0 for my $fh ($select->can_read) {
77 0 my $proc = $fh2proc{$fh};
78 0 ▶ while (read $fh, my $data, $size_of_float) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $fh, my $data, $Benchmark::Perl::Formance::Plugin::PerlStone2015::spectralnorm::size_of_float) 0 0 0
79 0 $out[ $ptr[$proc] ++ ] = unpack "F", $data;
80 }
81 0 ▶ $select->remove($fh) if eof($fh);
Branch: 1 branch
BranchTrueFalseTotal
if eof $fh 0 0 0
82 }
83 }
84  
85 0 return @out;
86 }
87  
88 0 ▶sub multiplyAtAv {
Subroutine multiplyAtAv: not called CC=1 SCAR=6.9
Pod undocumented
89 0 my @array = do_parallel(\&multiplyAv, @_);
90 0 return do_parallel(\&multiplyAtv, @array);
91 }
92  
93 0 ▶sub num_cpus {
Subroutine num_cpus: not called CC=4 SCAR=30.0
Pod undocumented
94 0 ▶ open my $fh, '<', '/proc/cpuinfo' or return; # '
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", "/proc/cpuinfo" or return 0 0 0
95 0 my $cpus;
96 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
97 0 ▶ $cpus ++ if /^processor[\s]+:/; # 0][]0]; # for emacs cperl-mode indent bug
Branch: 1 branch
BranchTrueFalseTotal
if /^processor[\s]+:/ 0 0 0
98 }
99 0 return $cpus;
100 }
101  
102 0 ▶sub init {
Subroutine init: not called CC=4 SCAR=25.3
Pod undocumented
103 0 ($n) = @_;
104  
105 0 $size_of_float = length pack "F", 0;
106  
107 0 ▶ $threads = num_cpus() || 1;
Condition0%0/2
num_cpus() || 1
l!l
00
MC/DC0%0/1
num_cpus() || 1
num_cpus()
Truth table0%0/2
A
A= num_cpus()
Aresult
11
00
108  
109 0 ▶ if ($threads > $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Benchmark::Perl::Formance::Plugin::PerlStone2015::spectralnorm::threads > $Benchmark::Perl::Formance::Plugin::PerlStone2015::spectralnorm::n) 0 0 0
110 0 $threads = $n;
111 }
112  
113 0 for my $i (0 .. $threads - 1) {
114 1 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 0 $ranges[$i][0] = $n * $i / $threads;
116 0 $ranges[$i][1] = $n * ($i + 1) / $threads - 1;
117 1 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
118 }
119 }
120  
121 sub run
122 0 ▶{
Subroutine run: not called CC=3 SCAR=24.8
Pod undocumented
123 0 my ($goal) = @_;
124  
125 0 init($goal);
126  
127 0 my @u = (1) x $n;
128 0 my @v;
129 0 for (0 .. 9) {
130 0 @v = multiplyAtAv( @u );
131 0 @u = multiplyAtAv( @v );
132 }
133  
134 0 my ($vBv, $vv);
135 0 for my $i (0 .. $#u) {
136 0 $vBv += $u[$i] * $v[$i];
137 0 $vv += $v[$i] ** 2;
138 }
139  
140 0 return sprintf( "%0.9f\n", sqrt( $vBv / $vv ) );
141 }
142  
143  
144 sub main
145 0 ▶{
Subroutine main: not called CC=3 SCAR=24.8
Pod undocumented
146 0 my ($options) = @_;
147  
148 0 ▶ my $goal = $options->{fastmode} ? 250 : 750;
Branch: 1 branch
BranchTrueFalseTotal
$options->{'fastmode'} ? : 0 0 0
149 0 ▶ my $count = $options->{fastmode} ? 1 : 5;
Branch: 1 branch
BranchTrueFalseTotal
$options->{'fastmode'} ? : 0 0 0
150  
151 0 my $result;
152 0 ▶ my $t = timeit $count, sub { $result = run($goal) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
153 return {
154 0 Benchmark => $t,
155 goal => $goal,
156 count => $count,
157 result => $result,
158 n => $n,
159 size_of_float => $size_of_float,
160 threads => $threads,
161 ranges => [ @ranges ],
162 begin => $begin,
163 end => $end,
164 };
165 }
166  
167 1 1;
168  
169 __END__
« PerlStone2015/revcomp.pm ↑ Summary PerlStone2015/binarytrees.pm »