Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Good question! I've been working on it to see what the real issue is. I don't have an answer for you; I'm just documenting what I've found for everyone's benefit.

First, I presume that "inherently recursive" means "cannot be rewritten to be tail-recursive". Fibonacci is an example of an inherently recursive algorithm.

sub fibonacci { my ($n) = @_; return 0 if $n == 0; return 1 if $n == 1; return sum fibonacci($n-2), fibonacci($n-1); }

At face value, it's quite easy to make it threaded:

sub fibonacci { my ($n) = @_; return 0 if $n == 0; return 1 if $n == 1; return sum map $_->join(), async { fibonacci($n-2) }, async { fibonacci($n-1) }; }

But what if you wanted to limit the number of threads (perhaps to limit overhead)? One solution would be to have a pool of threads and to use them if available.

sub fibonacci { my ($n) = @_; return 0 if $n == 0; return 1 if $n == 1; return sum map $_->get(), async_maybe { fibonacci($n-2) }, async_maybe { fibonacci($n-1) }; }

(The above assumes closures can be shared, but it can be rewritten to not use closures.)

When implementing async_maybe (and the get of the object it returns), one must be extra careful to avoid the situation where a thread is waiting to have it's result collected.

But what if you want a worker model (perhaps to distribute the work to other machines)? Now, that's hard. One would need some kind of a callback system.

sub fibonacci { my ($n) = @_; my $result; process_and_wait(sub { fibonacci_task(sub { $result = $_[0]; }, $n ); }); return $result; } sub fibonacci_task { my ($on_complete, $n) = @_; return $on_complete->(0) if $n == 0; return $on_complete->(1) if $n == 1; my ($x,$y); process(sub { fibonacci_task(sub { $x = $_[0] }, $n-2) }); process(sub { fibonacci_task(sub { $y = $_[0] }, $n-1) }); #TODO: The last of the two tasks to complete # must call $on_complete->($x+$y). }

The TODO item is hard to do cleanly. And of course, I'm still using closures even though I think that's not an option for threads threads. Rewriting to avoid closures will likely make the code even longer.


In reply to Re: [OT]: threading recursive subroutines. by ikegami
in thread [OT]: threading recursive subroutines. by BrowserUk

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (6)
    As of 2014-09-19 03:57 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (129 votes), past polls