| 1 | package Benchmark::Perl::Formance::Plugin::Mandelbrot::withmce; | ||||||||||
| 2 | 2 | our $AUTHORITY = 'cpan:SCHWIGON'; | |||||||||
| 3 | # ABSTRACT: benchmark - Generate Mandelbrot set portable bitmap file - using MCE | ||||||||||
| 4 | 2 | $Benchmark::Perl::Formance::Plugin::Mandelbrot::withmce::VERSION = '0.001'; | |||||||||
| 5 | # http://www.perlmonks.org/?node_id=1129370 | ||||||||||
| 6 | |||||||||||
| 7 | # http://benchmarksgame.alioth.debian.org/u64q/performance.php?test=mandelbrot | ||||||||||
| 8 | # based on Perl code contributed by Mykola Zubach | ||||||||||
| 9 | # parallelization via MCE by Mario Roy | ||||||||||
| 10 | |||||||||||
| 11 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | ############################################################# | ||||||||||
| 15 | # # | ||||||||||
| 16 | # Benchmark Code ahead - Don't touch without strong reason! # | ||||||||||
| 17 | # # | ||||||||||
| 18 | ############################################################# | ||||||||||
| 19 | |||||||||||
| 20 | 2 | ▶ | use Benchmark ':hireswallclock'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | 2 | ▶ | use MCE::Flow; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | 2 | ▶ | use constant MAXITER => 50; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 2 | ▶ | use constant LIMIT => 4.0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 2 | ▶ | use constant XMIN => -1.5; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 2 | ▶ | use constant YMIN => -1.0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 2 | ▶ | use constant WHITE => "\000"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 2 | ▶ | use constant BLACK => "\001"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | |||||||||||
| 31 | 2 | my ( $w, $h, $m, $invN ); | |||||||||
| 32 | |||||||||||
| 33 | 0 | ▶ | sub draw_line { | ||||||||
|
Subroutine
draw_line: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 34 | 0 | my ( $mce, $y, $chunk_id ) = @_; | |||||||||
| 35 | 0 | my ( $Cr, $Zr, $Zi, $Tr, $Ti ); | |||||||||
| 36 | 0 | my $Ci = $y * $invN + YMIN; | |||||||||
| 37 | 0 | my $line; | |||||||||
| 38 | |||||||||||
| 39 | 0 | LOOP: for my $x (0 .. $w - 1) { | |||||||||
| 40 | 0 | $Cr = $x * $invN + XMIN; | |||||||||
| 41 | 0 | $Zr = $Zi = $Tr = $Ti = 0.0; | |||||||||
| 42 | |||||||||||
| 43 | 0 | for (1 .. MAXITER) { | |||||||||
| 44 | 0 | $Zi = $Zi * 2 * $Zr + $Ci; | |||||||||
| 45 | 0 | $Zr = $Tr - $Ti + $Cr; | |||||||||
| 46 | 0 | $Ti = $Zi * $Zi; | |||||||||
| 47 | 0 | $Tr = $Zr * $Zr; | |||||||||
| 48 | 0 | ▶ | if ($Tr + $Ti > LIMIT) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 0 | $line .= WHITE; | |||||||||
| 50 | 0 | next LOOP; | |||||||||
| 51 | } | ||||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 0 | $line .= BLACK; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 0 | MCE->gather( $chunk_id, pack('B*', $line) ); | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | ## MAIN() | ||||||||||
| 61 | |||||||||||
| 62 | sub run | ||||||||||
| 63 | 1 | ▶ | { | ||||||||
|
Subroutine
run: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 64 | 1 | $w = $h = shift; | |||||||||
| 65 | 1 | $m = int( $h / 2 ); | |||||||||
| 66 | 1 | $invN = 2 / $w; | |||||||||
| 67 | |||||||||||
| 68 | # Compute upper-half only, gather lines | ||||||||||
| 69 | |||||||||||
| 70 | 1 | my %picture = mce_flow_s { chunk_size => 1 }, \&draw_line, 0, $m; | |||||||||
| 71 | |||||||||||
| 72 | # Output PBM image header | ||||||||||
| 73 | # Output upper half | ||||||||||
| 74 | # Remove first and last lines | ||||||||||
| 75 | # Output bottom half in reverse | ||||||||||
| 76 | |||||||||||
| 77 | 1 | binmode STDOUT; | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | sub main | ||||||||||
| 81 | 1 | ▶ | { | ||||||||
|
Subroutine
main: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||
| 82 | 1 | my ($options) = @_; | |||||||||
| 83 | |||||||||||
| 84 | 1 | ▶ | my $goal = $options->{fastmode} ? 400 : 2_000; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 1 | ▶ | my $count = $options->{fastmode} ? 1 : 5; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | |||||||||||
| 87 | 1 | my $result; | |||||||||
| 88 | 1 | ▶ | my $t = timeit $count, sub { $result = run($goal) }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 89 | return { | ||||||||||
| 90 | 1 | Benchmark => $t, | |||||||||
| 91 | goal => $goal, | ||||||||||
| 92 | count => $count, | ||||||||||
| 93 | }; | ||||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | 2 | 1; | |||||||||
| 97 | |||||||||||
| 98 | __END__ | ||||||||||