Dear Fellow Monks,

I have the a task that sums up the results of two functions. It looks like this:
Total Sum = Function_1 + Function_2
Now these two functions can be executed in parallel way. Meaning that the output of Function_1 doesn't depend on Function_2 or vice versa. The problem is that these functions require a very time consuming process. That's why I need it to run in parallel.

For simplicity sake, the function basically takes a number as an upperbound of series of value and perform some arithmetic on them. The overall non-parallel and very time consuming process can be seen in the current code of mine:

#!/usr/bin/perl -w use strict; sub Function_1 { my $up_bound1 = shift; my $total_f1; foreach my $in (0..$up_bound1) { $total_f1 += $in/3; } return $total_f1; } sub Function_2 { my $up_bound2 = shift; my $total_f2; foreach my $in (0 .. $up_bound2) { $total_f2 += $in; } return $total_f2; } ## Begin main process my $up_bound = 10; my $sum = Function_1($up_bound)+Function_2($up_bound); print "$sum\n";
Which yields: 73.33333333333333
To speed up the code above, I am trying to use POE, especially running F1 and F2 in parallel. The main idea is this: And I would like to pack them inside a subroutine that returns the single final summed value.

This is the first time I am using it. So please kindly bear with me. Here is the sample code I have, in which I'm totally lost. Not knowing how to go about it:
#!/usr/bin/perl use warnings; use strict; use POE; my $up_bound = 10; my $final_result = parallel_with_poe($up_bound); sub parallel_with_poe { $ubound = shift; POE::Session->create( inline_states => { # How can I pass the parameter correctly here? _start => Function_1($_KERNEL,$ubound); to_wait => sub { $_[KERNEL]->(delay (tick=>2)); } run_function_2 => Function_2($_KERNEL,$ubound); # This ain't right also, I don't know # how to pass the above result into the function _stop => final_sum($_KERNEL,$ubound); }, ); $poe_kernel->run(); return; # how to return the final value? #exit(0); } sub final_sum { my ($kernel,$ans_of_f1, $ans_of_f2) = @_; my $sum = $ans_of_f1+$ans_of_f2; return $sum; } sub Function_1 { my ($kernel_f1, $up_bound1) = @_; my $total_f1; foreach my $in ( 0 .. $up_bound1) { $total_f1 += $in/3; } return $total_f1; } sub Function_2 { my ($kernel_2,$up_bound2) = @_; my $total_f2; foreach my $in (0 .. $up_bound2) { $total_f2 += $in; } return $total_f2;
Thus, I humbly seek for enlightment from my fellow monks.

Regards,
Edward

In reply to My First POE - Simple Parallel Programming by monkfan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.