monkfan has asked for the wisdom of the Perl Monks concerning the following question:
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.Total Sum = Function_1 + Function_2
Which yields: 73.33333333333333#!/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";
Thus, I humbly seek for enlightment from my fellow monks.#!/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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: My First POE - Simple Parallel Programming
by nothingmuch (Priest) on Jan 31, 2006 at 13:01 UTC | |
|
Re: My First POE - Simple Parallel Programming
by Ultra (Hermit) on Jan 31, 2006 at 15:09 UTC | |
by Ultra (Hermit) on Feb 01, 2006 at 06:28 UTC | |
|
Re: My First POE - Simple Parallel Programming
by diego_de_lima (Beadle) on Jan 31, 2006 at 11:58 UTC | |
|
Re: My First POE - Simple Parallel Programming
by BrowserUk (Patriarch) on Jan 31, 2006 at 17:09 UTC | |
by Anonymous Monk on Feb 01, 2006 at 00:17 UTC | |
by BrowserUk (Patriarch) on Feb 01, 2006 at 00:42 UTC | |
by monkfan (Curate) on Feb 03, 2006 at 14:31 UTC | |
by BrowserUk (Patriarch) on Feb 03, 2006 at 20:06 UTC |