use MCE;

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

  1. gamo

    gamo Guest

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

  2. gamo <> writes:

    [...]

    > 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
    Rainer Weikusat, Nov 4, 2013
    #2
    1. Advertising

  3. gamo

    gamo Guest

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

    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) = @_;
    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
    , Feb 2, 2014
    #4
  5. gamo

    Guest

    , Feb 2, 2014
    #5
  6. gamo

    Mario Guest

    Mario, Feb 3, 2014
    #6
  7. gamo

    gamo Guest

    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

    --
    http://www.telecable.es/personales/gamo/
    gamo, Feb 3, 2014
    #7
  8. gamo

    Mario Guest

    Mario, Feb 6, 2014
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    7,104
  2. M K
    Replies:
    11
    Views:
    7,040
    woods_jemmy
    Apr 9, 2008
  3. Simon
    Replies:
    1
    Views:
    444
    Henri
    Nov 9, 2004
  4. =?Utf-8?B?S2hhaXJvbA==?=
    Replies:
    0
    Views:
    352
    =?Utf-8?B?S2hhaXJvbA==?=
    Dec 10, 2004
  5. =?Utf-8?B?SmVmZiBLZXNpbmdlcg==?=

    Common Web functions dll (Used to use MTS, what do I use in .NET

    =?Utf-8?B?SmVmZiBLZXNpbmdlcg==?=, Dec 28, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    460
    charlie
    Apr 30, 2005
Loading...

Share This Page