in reply to My First POE - Simple Parallel Programming

You've got a POE solution. Here's what the threads solution looks like:

#!/usr/bin/perl use warnings; use strict; use threads; 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; } my $up_bound = 10; my $t = threads->new( \&Function_1, $up_bound ); my $sum = Function_2( $up_bound ) + $t->join; print "The total is $sum"; __END__ c:\Perl\test>junk6 The total is 73.3333333333333

However, unless you have multiple CPUs, that will not run any faster than simply doing the two sequentially. This is a cpu bound process, and unless you have multiple cpus, both calculations will be time-sliced on the same cpu, and that will probably take longer than running them sequentially due to the overhead of task switching.

That said, the POE solution will never run more quickly. Regardless of whether you have multiple cpus or not. Even if there are multiple cpus, it will not make use of them. It will simply be time-slicing your single threaded process, and will run more slowly because of it, and all that extra complexity will have bought you nothing. POE will only benefit you performance-wise if your process is doing a lot of IOwaits which it can utilise to do other processing. POE is very clever, but it isn't designed for cpu intensive processing.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: My First POE - Simple Parallel Programming
by Anonymous Monk on Feb 01, 2006 at 00:17 UTC
    Well, the POE::Wheel::Run solution will take advantage of multiple CPUs and run faster. Not a single process POE solution of course.

      Okay. I guess I didn't read the POE example closely enough and was assuming a 'normal' POE event driven, single process approach--and it does actually mention forking at the top.

      That said, it would be an interesting exercise to see how much calculation the asynced sub would have to be doing in order to offset the overhead of spawning (two?) new processes and all the IPC etc. before it would show benefit over just calling them serially. I would imagine the breakeven point would be pretty high.

      That said, this application isn't really what POE is designed for. I think POE is very clever--just rather complex. Going by the list of around 400 POE related modules on CPAN, it seems as if every new problem requires a unique solution tailored to that specific problem? Even then, the applications using those modules hardly seem simple, and the use of POE is far from transparent. Threads just seem easier to use to me :)


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: My First POE - Simple Parallel Programming
by monkfan (Curate) on Feb 03, 2006 at 14:31 UTC
    However, unless you have multiple CPUs, that will not run any faster than simply doing the two sequentially.
    Dear BrowserUK,
    Indeed using threads is much simpler than POE. Thanks for introducing it to me. BTW does the forking feature of that code above automatically take effect if I run that code of yours in a Linux Cluster?

    I mean I have a Linux Cluster with 40 CPUs. Can I run your code above with the forking effect just by saving this in bash script (let's call it my_bash.sh):
    perl junk6.pl
    Then run it the following way with qsub command:
    my_home_node $ qsub my_bash.sh
    It will submit the job into a particular node of the 40 CPUs. Or is there any other way to do it?

    Regards,
    Edward

      No it won't. Had you mentioned clustering in your OP I would not have offered a threaded solution.

      That said, a clustering solution based around threads would be far simpler to both implement and use, but I do not have one to offer you so for your situation, POE is the right (only) Perl solution.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.