use MCE;

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View


I usually do a lot of simulations and heuristics in a linear funcional  
way. Now I want to send random candidates to the cores or process, get  
the results for each one and when the acceptable result is reached,  
rerun the calculus, print a report and exit.

I take a look on the modules Parallel::* (Simple, ForkManager and Loops)  
but none is both easy and could do the things well. Then I see MCE, wich  
sure could do that things, but is complex and the MCE::Examples provided  
don't cover a simple simulation scenario.

Are there somebody wich could guide me in the parallelization of
something like

$var = 1;
$foo = 2;
$bar = 3;

while (1) {

    $r = rand();

    $result = $r * ($var+$foo+$bar);

    $anothervar = sqrt($var+$foo+$bar);

    last if ($result < $anothervar+0.001 && $result > $anothervar-0.001);


print "$r -> $result\n";

exit ;

Thanks in advance

Re: use MCE;


Quoted text here. Click to load it

It is somewhat unclear what is supposed to be parallelized how here, eg,
are the $(bar|foo|var) really compile-time constant and if they're not,
how often to they change? And what's the point of this algorithm,
anyway? From the above, it follows that  

$r == sqrt($var + $foo + bar) / ($var + $foo + $bar)


$result == $another_var

in general and that the solution for your specific case is

[rw@sable]~#perl -e 'print sqrt(6)/6, "\n";'

Re: use MCE;

El 04/11/13 13:37, Rainer Weikusat escribió:
Quoted text here. Click to load it

It's only an example. Suppose I have common variables ($var, $foo, $bar)  
to be used in an algorithm. The algorithm takes these and a
random input ($r), and knows when to stop its calculations (comparison  
with $anothervar). Then it has a result ($result).

What I want to do is parallelize the algorithm (could be a sub), and
get the best $r and/or $result. This gather of information, and the need
to share input vars is difficult. AFAIK the most independent the
algorithm is, the easyest parallelization could be done.

I tought in threads, too, but there are limitations: a random is not
thread-safe or the creation of each thread need to be by hand via
create, as is by hand the finish of a thread. Last but not least after
all I want to reach better performance, in speed or precision.

Thanks for your help

Re: use MCE;

Below, you will find the simulation performed in parallel using MCE.

use MCE::Loop;

## Callback functions.

my ($r, $result);

sub get_rand {
    return if defined $result;
    return rand();

sub recv_result {
    return if defined $result;
    ($r, $result) = @_;

## Configure MCE and common variables.

MCE::Loop::init {
    chunk_size => 1, max_workers => 8,
    input_data => \&get_rand,
    gather => \&recv_result

my $var = 1;  
my $foo = 2;  
my $bar = 3;  

## Compute in parallel.

mce_loop {
 # my ($mce, $chunk_ref, $chunk_id) = @_;
    my $r = $_;

    my $result = $r * ($var+$foo+$bar);  
    my $anothervar = sqrt($var+$foo+$bar);  

    if ($result < $anothervar+0.001 && $result > $anothervar-0.001) {
        MCE->gather($r, $result); MCE->abort();


## Display the answer.

print "$r -> $result\n";  

- mario

Re: use MCE;

The MCE example above is possible with MCE 1.508 or later releases.

Re: use MCE;

The MCE 1.509 release now contains a simulation based off the one from "gamo". The speedup is incredible.

Re: use MCE;

El 03/02/14 15:51, Mario escribió:
Quoted text here. Click to load it

Thank you very much for replying, Mario.
I'm very interested in paralellyzing scripts, and that's is only an
example to figure out how to do it. It's not trivial how to.
If you are interested, I could post more real small examples in which
I wish to do the job using all the cpu resources.



Re: use MCE;

Hi Gamo,

I'm delighted to know that you've received the news on MCE 1.509 and hoping that the Monte Carlo simulation using MCE is helpful.

In addition to , various examples are documented under .

And sure, why not... we can see if MCE is able to maximize on all available cores for a couple more examples.  No problem.


Site Timeline