« blib/lib/Benchmark/Perl/Formance/Plugin/Shootout/knucleotide.pm
↑ Summary
blib/lib/Benchmark/Perl/Formance/Plugin/Threads.pm »
| 1 | package Benchmark::Perl::Formance::Plugin::Shootout::mandelbrot; | ||
| 2 | our $AUTHORITY = 'cpan:SCHWIGON'; | ||
| 3 | # ABSTRACT: benchmark - Generate Mandelbrot set portable bitmap file | ||
| 4 | |||
| 5 | # COMMAND LINE: | ||
| 6 | # /usr/bin/perl mandelbrot.perl 16000 | ||
| 7 | |||
| 8 | # The Computer Language Benchmarks Game | ||
| 9 | # http://shootout.alioth.debian.org/ | ||
| 10 | # implemented by Greg Buchholz | ||
| 11 | # streamlined by Kalev Soikonen | ||
| 12 | # parallelised by Philip Boulain | ||
| 13 | # modified by Jerry D. Hedden | ||
| 14 | # Benchmark::Perl::Formance plugin by Steffen Schwigon | ||
| 15 | # - nr of threads now dynamically | ||
| 16 | |||
| 17 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 0 | ▶ | use threads; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 20 | |||
| 21 | our $VERSION = "0.002"; | ||
| 22 | |||
| 23 | ############################################################# | ||
| 24 | # # | ||
| 25 | # Benchmark Code ahead - Don't touch without strong reason! # | ||
| 26 | # # | ||
| 27 | ############################################################# | ||
| 28 | |||
| 29 | use Benchmark ':hireswallclock'; | ||
| 30 | |||
| 31 | use constant ITER => 50; | ||
| 32 | use constant LIMITSQR => 2.0 ** 2; | ||
| 33 | use constant MAXPIXEL => 524288; # Maximum pixel buffer per thread | ||
| 34 | |||
| 35 | my ($w, $h); | ||
| 36 | my $threads; | ||
| 37 | |||
| 38 | # Generate pixel data for a single dot | ||
| 39 | sub dot($$) { ## no critic | ||
| 40 | my ($Zr, $Zi, $Tr, $Ti) = (0.0,0.0,0.0,0.0); | ||
| 41 | my $i = ITER; | ||
| 42 | my $Cr = 2 * $_[0] / $w - 1.5; | ||
| 43 | my $Ci = 2 * $_[1] / $h - 1.0; | ||
| 44 | ( | ||
| 45 | $Zi = 2.0 * $Zr * $Zi + $Ci, | ||
| 46 | $Zr = $Tr - $Ti + $Cr, | ||
| 47 | $Ti = $Zi * $Zi, | ||
| 48 | $Tr = $Zr * $Zr | ||
| 49 | ) until ($Tr + $Ti > LIMITSQR || !$i--); | ||
| 50 | return ($i == -1); | ||
| 51 | } | ||
| 52 | |||
| 53 | # Generate pixel data for range of lines, inclusive | ||
| 54 | sub lines($$) { ## no critic | ||
| 55 | map { my $y = $_; | ||
| 56 | pack 'B*', pack 'C*', map dot($_, $y), 0..$w-1; | ||
| 57 | } $_[0]..$_[1] | ||
| 58 | } | ||
| 59 | |||
| 60 | sub num_cpus { | ||
| 61 | open my $fh, '<', '/proc/cpuinfo' or return; | ||
| 62 | my $cpus; | ||
| 63 | while (<$fh>) { | ||
| 64 | $cpus ++ if /^processor[\s]+:/; # 0][]0]; # for emacs cperl-mode indent bug | ||
| 65 | } | ||
| 66 | return $cpus; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub run | ||
| 70 | { | ||
| 71 | $w = $h = shift; | ||
| 72 | $threads = num_cpus() + 1; # Workers; ideally slightly overshoots number of processors | ||
| 73 | |||
| 74 | # Decide upon roughly equal batching of workload, within buffer limits | ||
| 75 | $threads = $h if $threads > $h; | ||
| 76 | my $each = int($h / $threads); | ||
| 77 | $each = int(MAXPIXEL / $w) if ($each * $w) > MAXPIXEL; | ||
| 78 | $each = 1 if $each < 1; | ||
| 79 | |||
| 80 | # Work as long as we have lines to spawn for or threads to collect from | ||
| 81 | $| = 1; | ||
| 82 | #print "P4\n$w $h\n"; | ||
| 83 | my $y = 0; | ||
| 84 | my @workers; | ||
| 85 | while (@workers or ($y < $h)) { | ||
| 86 | # Create workers up to requirement | ||
| 87 | while ((@workers < $threads) and ($y < $h)) { | ||
| 88 | my $y2 = $y + $each; | ||
| 89 | $y2 = $h if $y2 > $h; | ||
| 90 | push(@workers, threads->create('lines', $y, $y2 - 1)); | ||
| 91 | $y = $y2; | ||
| 92 | } | ||
| 93 | # Block for result from the leading thread (to keep output in order) | ||
| 94 | my $next = shift @workers; | ||
| 95 | |||
| 96 | $next->join(); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | sub main | ||
| 101 | { | ||
| 102 | my ($options) = @_; | ||
| 103 | |||
| 104 | my $goal = $options->{fastmode} ? 100 : 1_000; | ||
| 105 | my $count = $options->{fastmode} ? 1 : 5; | ||
| 106 | |||
| 107 | my $result; | ||
| 108 | my $t = timeit $count, sub { $result = run($goal) }; | ||
| 109 | return { | ||
| 110 | Benchmark => $t, | ||
| 111 | goal => $goal, | ||
| 112 | count => $count, | ||
| 113 | # result => $result, # useless here | ||
| 114 | threads => $threads, | ||
| 115 | w => $w, | ||
| 116 | h => $h, | ||
| 117 | }; | ||
| 118 | } | ||
| 119 | |||
| 120 | 1; | ||
| 121 | |||
| 122 | __END__ | ||