Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Update: See this post for a faster version. A MCE + Inline::C demonstration is also included.

Parallelism is possible with just one line of code for the next demonstration. As in the previous examples, am using the sequence generator with chunk_size set to 1. Thus, $_ is the next sequence number when specifying chunk_size => 1.

mce_flow_s sub { draw_line($_) }, 0, $h - 1;

Folks will be pleased to know that this will work for non-threads in the upcoming MCE 1.7 release with MCE::Shared. One simply replaces ( use threads; use threads::shared ) with ( use MCE::Shared ) and ( my @picture : shared; ) with ( my @picture : Shared; ). That's all.

The code outputs the image in pbm format.

# synopsis: perl mandelbrot.pl 1024 > image.pbm # # https://github.com/nbraud/benchmarksgame/blob/master/bench/ # mandelbrot/mandelbrot.perl # # The Computer Language Benchmarks Game # http://benchmarksgame.alioth.debian.org/ # # contributed by Mykola Zubach # shorten by Mario Roy from using MCE use strict; use threads; use threads::shared; use MCE::Flow chunk_size => 1, max_workers => 'auto'; use constant MAXITER => 50; use constant LIMIT => 4.0; use constant XMIN => -1.5; use constant YMIN => -1; use constant WHITE => "\0"; use constant BLACK => "\001"; my ($w, $h, $invN); my @picture : shared; sub draw_line($) { my $y = shift; my $line; my $Ci = $y * $invN + YMIN; X: for my $x (0 .. $w - 1) { my ($Zr, $Zi, $Tr, $Ti); my $Cr = $x * $invN + XMIN; for (1 .. MAXITER) { $Zi = $Zi * 2 * $Zr + $Ci; $Zr = $Tr - $Ti + $Cr; $Ti = $Zi * $Zi; $Tr = $Zr * $Zr; if ($Tr + $Ti > LIMIT) { $line .= WHITE; next X; } } $line .= BLACK; } $picture[$y] = pack 'B*', $line; } ## MAIN() $w = $h = shift || 200; $invN = 2 / $w; mce_flow_s sub { draw_line($_) }, 0, $h - 1; binmode STDOUT; print "P4\n$w $h\n"; # PBM image header print @picture;

In reply to Re^3: Threads From Hell #3: Missing Some Basic Prerequisites by marioroy
in thread Threads From Hell #3: Missing Some Basic Prerequisites [Solved] by karlgoethebier

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-03-28 21:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found