Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Threads From Hell #1: How To Share A Hash [SOLVED]

by BrowserUk (Patriarch)
on May 15, 2015 at 18:00 UTC ( [id://1126800]=note: print w/replies, xml ) Need Help??


in reply to Threads From Hell #1: How To Share A Hash [SOLVED]

Corion kindly advised my not to use Thread::Semaphore and pointed me to ... Thread::Queue.

I finally got around to putting together proof of the wisdom of Corion's advice.

This is a version of your Thread::Semaphore code, limiting to 4 concurrent threads and calculating factorials 1000! .. 2000!:

Total runtime: Took 47.511607 seconds

This version does the same calculations using the same number of concurrent threads, but ditches Thread::Semaphore in favour of Thread::Queue to queue the 1000 numbers to 4 reused threads, thus saving the startup and teardown costs of 996 threads:

Total runtime: Took 31.290966 seconds; giving a 33% saving of time.

But the biggest lesson of threading, is when not to use it. This version ditches threads altogether and uses the obvious optimisation:

Total runtime: Took 0.685503 seconds. Same results, but a 98.5% time saving over the first version above.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

Replies are listed 'Best First'.
Re^2: Threads From Hell #1: How To Share A Hash [SOLVED]
by karlgoethebier (Abbot) on May 15, 2015 at 18:34 UTC
    "But the biggest lesson...the obvious optimisation..."
    "Du sprichst ein großes Wort gelassen aus." (Johann Wolfgang von Goethe)

    Very impressive and instructional. What else should i say?

    Update:

    There are two little things i would like to add:

    My basic theme wasn't to calculate factorials using threads.

    I wanted to explore multithreading and thought the best idea would be to use some "expensive" calculation for this. Like in a classic homework.

    Unfortunately you provided a much better algorithm that avoids multithreading ;-)

    I fear i hit a basic problem in this context.

    And apropos little quirks:

    I wrote:

    sub process { my $number = shift; # lock %result; $result{ threads->tid() } = shared_clone( { $number => factorial($number) } ); $semaphore->up; }

    It seems like i really need to say something like:

    for my $key ( sort { $a <=> $b } keys %result ) { my $ref = $result{$key}; while ( my ( $k, $v ) = each %{$ref} ) { say qq($k => $v); } }

    ...to iterate over the results. From the docs:

    "each HASH does not work properly on shared references embedded in shared structures"

    Thanks for this advice and my best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re^2: Threads From Hell #1: How To Share A Hash [SOLVED]
by karlgoethebier (Abbot) on May 16, 2015 at 11:31 UTC

    I'm struggling with this:

    $Q->enqueue( 1000..2000, (undef) x 4 );

    The docs say:

    ->enqueue(LIST) Adds a list of items onto the end of the queue.

    I dumped it:

    karls-mac-mini:monks karl$ ./queue.pl bless({ # tied threads::shared::tie queue => [ # tied threads::shared::tie 1000 .. 2000, undef, undef, undef, undef, ], }, "Thread::Queue") Took 13.820966 seconds

    What are the four undefs good for?

    Best regards, Karl

    P.S.: But my box is faster than yours ;-)

    «The Crux of the Biscuit is the Apostrophe»

      What are the four undefs good for?

      They cause these loops to terminate:

      while( my $number = $Q->dequeue ) {
      P.S.: But my box is faster than yours ;-)

      No surprise there. My Q6600 is over 7 years old.

      Mind you, I make better use of it than most people do of their Haswells :)


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
      In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
        "Mind you, I make better use of it than most people do of their Haswells"

        Indeed.

        "They cause these loops to terminate"

        I guess it works like this:

        | 1000 1001 1002 1003 | | 4 threads ------------ | | 1004 1005 1006 1007 | | 4 threads ------------ | ... until they meet undef | undef undef undef undef| | 4 threads ------------ |

        Best regards, Karl (still locked/blocked)

        «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1126800]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-16 18:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found