use strict;
use warnings;
use MCE;
use MCE::Loop;
use MCE::Shared;
my $volume = 1e9; # crank it up
my $max_workers = MCE::Util::get_ncpu;
my $chunk_size = int $volume / $max_workers;
MCE::Loop::init {
max_workers => $max_workers,
chunk_size => $chunk_size,
bounds_only => 1,
};
my $fu = MCE::Shared-> scalar( 0 );
mce_loop_s { # note "s"
my $partial_result = 0;
my ( $begin, $end ) = @$_;
$partial_result += $_ for $begin .. $end;
print 'Worker', MCE-> wid, ' here, my partial result is ',
$partial_result, "\n";
$fu-> incrby( $partial_result );
} 1, $volume;
print "\nDone: @{[ $fu-> get ]}\n";
__END__
Worker4 here, my partial result is 218750000125000000
Worker1 here, my partial result is 31250000125000000
Worker3 here, my partial result is 156250000125000000
Worker2 here, my partial result is 93750000125000000
Done: 500000000500000000
Hi, not sure if it explains for dummies or not. Feel free to ask further, if it doesn't. Important:
- it's required to provide way to communicate results back from workers which are just separate processes (or threads) -- i.e. to arrange for IPC. You didn't, thus your variable remained zero. One possibility, through explicitly shared variable, is shown above;
- you really don't want to build an enormous list (1e7 elements in your code, or moreover 1e9 above) before any work is even started. For sequences, the MCE provides built-in methods. For other kind of input, think about e.g. setting up iterator as source.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.