use MCE;

G

gamo

Hello

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
 
R

Rainer Weikusat

[...]
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 ;

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)

means

$result == $another_var

in general and that the solution for your specific case is

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

gamo

El 04/11/13 13:37, Rainer Weikusat escribió:
[...]
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 ;

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)

means

$result == $another_var

in general and that the solution for your specific case is

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

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
 
M

marioeroy

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) = @_;
return;
}

## 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();
}

return;
};

## Display the answer.

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


- mario
 
G

gamo

El 03/02/14 15:51, Mario escribió:
The MCE 1.509 release now contains a simulation based off the one from "gamo". The speedup is incredible.

https://metacpan.org/pod/MCE::Examples#MONTE-CARLO-SIMULATION

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.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top