use MCE;

Discussion in 'Perl Misc' started by gamo, Oct 31, 2013.

  1. gamo

    gamo Guest


    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
    gamo, Oct 31, 2013
    1. Advertisements

  2. [...]
    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";'
    Rainer Weikusat, Nov 4, 2013
    1. Advertisements

  3. gamo

    gamo Guest

    El 04/11/13 13:37, Rainer Weikusat escribió:
    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
    gamo, Nov 4, 2013
  4. gamo

    marioeroy Guest

    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
    marioeroy, Feb 2, 2014
  5. gamo

    marioeroy Guest

    marioeroy, Feb 2, 2014
  6. gamo

    Mario Guest

    Mario, Feb 3, 2014
  7. gamo

    gamo Guest

    El 03/02/14 15:51, Mario escribió:
    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.

    gamo, Feb 3, 2014
  8. gamo

    Mario Guest

    Mario, Feb 6, 2014
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.