MCE::Candy - Sugar methods for Many-Core Engine
This document describes MCE::Candy version 1.603
This module provides a collection of sugar methods created for MCE.
The methods described below were created prior to the 1.5 release which added MCE Models. This module is loaded automatically upon calling a "for" method.
Forchunk, foreach, and forseq are sugar methods in MCE. Workers are automatically spawned, the code block is executed in parallel, and shutdown is called. Do not use these methods if workers must persist afterwards.
Specifying options is optional. Valid options are the same as for the process method.
## Declare a MCE instance. my $mce = MCE->new( chunk_size => 20, max_workers => $max_workers ); ## Arguments inside the code block are the same as for user_func. $mce->forchunk(\@input_data, sub { my ($mce, $chunk_ref, $chunk_id) = @_; foreach ( @{ $chunk_ref } ) { MCE->print("$chunk_id: $_\n"); } }); ## Passing chunk_size as an option. $mce->forchunk(\@input_data, { chunk_size => 30 }, sub { ... });
Foreach implies chunk_size => 1 and cannot be overwritten. Arguments inside the code block are the same as for user_func. This is true even if chunk_size is set to 1. MCE is both a chunking engine plus a parallel engine all in one. Arguments within the block are the same whether calling foreach or forchunk.
my $mce = MCE->new( max_workers => $max_workers ); $mce->foreach(\@input_data, sub { my ($mce, $chunk_ref, $chunk_id) = @_; my $row = $chunk_ref->[0]; MCE->print("$chunk_id: $row\n"); });
Below, passing an anonymous array as input data. For example, wanting to parallelize a serial loop with MCE.
## Serial loops. for (my $i = 0; $i < $max; $i++) { ... ## Runs serially } for my $i (0 .. $max - 1) { ... ## Runs serially } ## Parallel loop via MCE. $mce->foreach([ (0 .. $max - 1) ], sub { my ($mce, $chunk_ref, $chunk_id) = @_; my $i = $chunk_ref->[0]; ... ## Runs in parallel });
Sequence can be defined using an array or hash reference.
my $mce = MCE->new( max_workers => 3 ); $mce->forseq([ 20, 40 ], sub { my ($mce, $n, $chunk_id) = @_; my $result = `ping 192.168.1.${n}`; ... }); $mce->forseq({ begin => 15, end => 10, step => -1 }, sub { my ($mce, $n, $chunk_id) = @_; print $n, " from ", MCE->wid, "\n"; });
When chunking, the $n_seq variable is a reference pointing to an array of sequences. Chunking reduces IPC overhead behind the scene. Chunk size defaults to 1 when not specified.
$mce->forseq([ 20, 80 ], { chunk_size => 10 }, sub { my ($mce, $n_seq, $chunk_id) = @_; for my $n ( @{ $n_seq } ) { my $result = `ping 192.168.1.${n}`; ... } });
MCE
Mario E. Roy, <marioeroy AT gmail DOT com>
To install MCE, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MCE
CPAN shell
perl -MCPAN -e shell install MCE
For more information on module installation, please visit the detailed CPAN module installation guide.