« blib/lib/Benchmark/Perl/Formance/Plugin/FibMoose.pm
↑ Summary
blib/lib/Benchmark/Perl/Formance/Plugin/FibOOSig.pm »
| 1 | package Benchmark::Perl::Formance::Plugin::Shootout::spectralnorm; | ||||||||||||
| 2 | 522 | our $AUTHORITY = 'cpan:SCHWIGON'; | |||||||||||
| 3 | # ABSTRACT: benchmark - Eigenvalue using the power method | ||||||||||||
| 4 | |||||||||||||
| 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 | 522 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 522 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | |||||||||||||
| 17 | 522 | our $VERSION = "0.001"; | |||||||||||
| 18 | |||||||||||||
| 19 | ############################################################# | ||||||||||||
| 20 | # # | ||||||||||||
| 21 | # Benchmark Code ahead - Don't touch without strong reason! # | ||||||||||||
| 22 | # # | ||||||||||||
| 23 | ############################################################# | ||||||||||||
| 24 | |||||||||||||
| 25 | 522 | ▶ | use IO::Select; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 26 | 522 | ▶ | use Benchmark ':hireswallclock'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 27 | |||||||||||||
| 28 | 522 | ▶ | use vars qw($n $size_of_float $threads @ranges $begin $end); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 29 | |||||||||||||
| 30 | 100000 | ▶ | sub eval_A { | ||||||||||
|
Subroutine
eval_A: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 31 | 522 | ▶ | use integer; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 32 | 100000 | my $div = ( ($_[0] + $_[1]) * ($_[0] + $_[1] + 1) / 2) + $_[0] + 1; | |||||||||||
| 33 | 522 | ▶ | no integer; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 34 | 100000 | 1 / $div; | |||||||||||
| 35 | } | ||||||||||||
| 36 | |||||||||||||
| 37 | 260 | ▶ | sub multiplyAv { | ||||||||||
|
Subroutine
multiplyAv: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 38 | return map { | ||||||||||||
| 39 | 260 | my ($i, $sum) = ($_); | |||||||||||
| 40 | 1000 | $sum += eval_A($i, $_) * $_[$_] for 0 .. $#_; | |||||||||||
| 41 | 1000 | $sum; | |||||||||||
| 42 | } $begin .. $end; | ||||||||||||
| 43 | } | ||||||||||||
| 44 | |||||||||||||
| 45 | 260 | ▶ | sub multiplyAtv { | ||||||||||
|
Subroutine
multiplyAtv: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 46 | return map { | ||||||||||||
| 47 | 260 | my ($i, $sum) = ($_); | |||||||||||
| 48 | 1000 | $sum += eval_A($_, $i) * $_[$_] for 0 .. $#_; | |||||||||||
| 49 | 1000 | $sum; | |||||||||||
| 50 | } $begin .. $end; | ||||||||||||
| 51 | } | ||||||||||||
| 52 | |||||||||||||
| 53 | 10700 | ▶ | sub do_parallel { | ||||||||||
|
Subroutine
do_parallel: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||
| 54 | 10700 | my $func = shift; | |||||||||||
| 55 | |||||||||||||
| 56 | 10700 | my @out; | |||||||||||
| 57 | 10700 | my (@fd, @ptr, %fh2proc); | |||||||||||
| 58 | 10700 | for my $proc (0 .. $threads - 1) { | |||||||||||
| 59 | 135980 | ($begin, $end) = @{ $ranges[$proc] }; | |||||||||||
| 60 | 135980 | my $pid = open $fd[$proc], "-|"; | |||||||||||
| 61 | 135980 | ▶ | if ($pid == 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | 520 | print pack "F*", $func->( @_ ); | |||||||||||
| 63 | 520 | exit 0; | |||||||||||
| 64 | } else { | ||||||||||||
| 65 | 135460 | $fh2proc{ $fd[$proc] } = $proc; | |||||||||||
| 66 | 135460 | $ptr[$proc] = $begin; | |||||||||||
| 67 | } | ||||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | 10180 | my $select = IO::Select->new(@fd); | |||||||||||
| 71 | |||||||||||||
| 72 | 10180 | ▶ | while ($select->count) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 73 | 94089 | for my $fh ($select->can_read) { | |||||||||||
| 74 | 132340 | my $proc = $fh2proc{$fh}; | |||||||||||
| 75 | 132340 | ▶ | while (read $fh, my $data, $size_of_float) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 76 | 509000 | $out[ $ptr[$proc] ++ ] = unpack "F", $data; | |||||||||||
| 77 | } | ||||||||||||
| 78 | 132340 | ▶ | $select->remove($fh) if eof($fh); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | } | ||||||||||||
| 80 | } | ||||||||||||
| 81 | |||||||||||||
| 82 | 10180 | return @out; | |||||||||||
| 83 | } | ||||||||||||
| 84 | |||||||||||||
| 85 | 5480 | ▶ | sub multiplyAtAv { | ||||||||||
|
Subroutine
multiplyAtAv: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 86 | 5480 | my @array = do_parallel(\&multiplyAv, @_); | |||||||||||
| 87 | 5220 | return do_parallel(\&multiplyAtv, @array); | |||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | 521 | ▶ | sub num_cpus { | ||||||||||
|
Subroutine
num_cpus: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||
| 91 | 521 | ▶ | open my $fh, '<', '/proc/cpuinfo' or return; # ' | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 92 | 521 | my $cpus; | |||||||||||
| 93 | 521 | ▶ | while (<$fh>) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 94 | 182871 | ▶ | $cpus ++ if /^processor[\s]+:/; # 0][]0]; # for emacs cperl-mode indent bug | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 95 | } | ||||||||||||
| 96 | 521 | return $cpus; | |||||||||||
| 97 | } | ||||||||||||
| 98 | |||||||||||||
| 99 | 521 | ▶ | sub init { | ||||||||||
|
Subroutine
init: called CC=4 SCAR=14.5
Pod
undocumented
| |||||||||||||
| 100 | 521 | ($n) = @_; | |||||||||||
| 101 | |||||||||||||
| 102 | 521 | $size_of_float = length pack "F", 0; | |||||||||||
| 103 | |||||||||||||
| 104 | 521 | ▶ | $threads = num_cpus() || 1; | ||||||||||
|
Condition50%1/2
num_cpus() || 1
MC/DC0%0/1
num_cpus() || 1
num_cpus()
Truth table50%1/2
A
A= num_cpus()
| |||||||||||||
| 105 | |||||||||||||
| 106 | 521 | ▶ | if ($threads > $n) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 107 | 0 | $threads = $n; | |||||||||||
| 108 | } | ||||||||||||
| 109 | |||||||||||||
| 110 | 0 | for my $i (0 .. $threads - 1) { | |||||||||||
| 111 | 522 | ▶ | use integer; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 112 | 6773 | $ranges[$i][0] = $n * $i / $threads; | |||||||||||
| 113 | 6773 | $ranges[$i][1] = $n * ($i + 1) / $threads - 1; | |||||||||||
| 114 | 522 | ▶ | no integer; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 115 | } | ||||||||||||
| 116 | } | ||||||||||||
| 117 | |||||||||||||
| 118 | sub run | ||||||||||||
| 119 | 521 | ▶ | { | ||||||||||
|
Subroutine
run: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 120 | 521 | my ($goal) = @_; | |||||||||||
| 121 | |||||||||||||
| 122 | 521 | init($goal); | |||||||||||
| 123 | |||||||||||||
| 124 | 521 | my @u = (1) x $n; | |||||||||||
| 125 | 521 | my @v; | |||||||||||
| 126 | 521 | for (0 .. 9) { | |||||||||||
| 127 | 2870 | @v = multiplyAtAv( @u ); | |||||||||||
| 128 | 2610 | @u = multiplyAtAv( @v ); | |||||||||||
| 129 | } | ||||||||||||
| 130 | |||||||||||||
| 131 | 1 | my ($vBv, $vv); | |||||||||||
| 132 | 1 | for my $i (0 .. $#u) { | |||||||||||
| 133 | 50 | $vBv += $u[$i] * $v[$i]; | |||||||||||
| 134 | 50 | $vv += $v[$i] ** 2; | |||||||||||
| 135 | } | ||||||||||||
| 136 | |||||||||||||
| 137 | 1 | return sprintf( "%0.9f\n", sqrt( $vBv / $vv ) ); | |||||||||||
| 138 | } | ||||||||||||
| 139 | |||||||||||||
| 140 | |||||||||||||
| 141 | sub main | ||||||||||||
| 142 | 521 | ▶ | { | ||||||||||
|
Subroutine
main: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||
| 143 | 521 | my ($options) = @_; | |||||||||||
| 144 | |||||||||||||
| 145 | 521 | ▶ | my $goal = $options->{fastmode} ? 50 : 750; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 146 | 521 | ▶ | my $count = $options->{fastmode} ? 1 : 5; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 147 | |||||||||||||
| 148 | 521 | my $result; | |||||||||||
| 149 | 521 | ▶ | my $t = timeit $count, sub { $result = run($goal) }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 150 | return { | ||||||||||||
| 151 | 1 | Benchmark => $t, | |||||||||||
| 152 | goal => $goal, | ||||||||||||
| 153 | count => $count, | ||||||||||||
| 154 | result => $result, | ||||||||||||
| 155 | n => $n, | ||||||||||||
| 156 | size_of_float => $size_of_float, | ||||||||||||
| 157 | threads => $threads, | ||||||||||||
| 158 | ranges => [ @ranges ], | ||||||||||||
| 159 | begin => $begin, | ||||||||||||
| 160 | end => $end, | ||||||||||||
| 161 | }; | ||||||||||||
| 162 | } | ||||||||||||
| 163 | |||||||||||||
| 164 | 522 | 1; | |||||||||||
| 165 | |||||||||||||
| 166 | __END__ | ||||||||||||