Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: Is it possible to create a single Hash-of-Hash.. with multiple threads..

by BrowserUk (Pope)
on Apr 25, 2014 at 10:21 UTC ( #1083769=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Is it possible to create a single Hash-of-Hash.. with multiple threads..
in thread Is it possible to create a single Hash-of-Hash.. with multiple threads..

Perl Threads implementation so far, seems very inadequate - it is good for primary school student exhibiting how parallel programming works.

That is a wrong conclusion drawn through a lack of understanding.

If you would care to describe your application in detail -- not just generics, but rather scales and reasons; with examples of data and processing requirements -- then you would (probably) get help with finding definitive, practical and efficient solutions to those requirements.

Your OP asks a simplistic question with an obvious answer: Yes, of course a multi-level hash can be constructed using multiple threads. It is simple and trivial:

#! perl -slw use strict; use threads; use threads::shared; sub displayHash { my( $ref, $pad ) = @_; return "\n" unless keys %{ $ref }; my $buf = ''; for my $key ( sort keys %{ $ref } ) { $buf .= "$pad" if length $buf; $buf .= "{$key}" . displayHash( $ref->{ $key }, $pad . ' ' ) +; } return $buf; } sub worker { my( $ref, $reps ) = @_; for( 1 .. $reps ) { my $copyRef = $ref; for my $step ( map chr( 97 + rand 26 ), 1 .. int( rand 10 ) ) +{ if( exists $copyRef->{ $step } ) { $copyRef = $copyRef->{ $step }; } else { lock %{ $copyRef }; $copyRef = $copyRef->{ $step } = &share( {} ); } } } } our $REPS //= 20; our $THREADS //= 4; my %HoHos : shared; my @threads = map threads->create( \&worker, \%HoHos, $REPS ), 1 .. $T +HREADS; $_->join for @threads; print displayHash( \%HoHos, '' ); __END__ C:\test>junk91 -REPS=7 {b}{l}{w}{g} {d}{t}{k}{f}{g}{g}{g}{i}{j} {f}{j}{v} {g}{s}{g}{h}{g} {i}{f}{j}{r}{m}{v}{u}{v}{b} {k}{d}{f}{q}{k} {z}{t}{m}{h}{e}{i} {m}{b}{l} {l}{u}{p}{t}{d}{h} {q}{k}{y} {n}{f}{u}{v}{z}{z}{l} {o}{f} {p}{u}{s}{w}{n}{t} {q}{h} {y}{k}{d}{l}{a}{n}{a}{i} {r}{q}{t}{g} {v}{o}{n}{x}{b}{g}{c} {w}{a} {y}{a} {d}{y}{j} {z}{a}{p}{t}{i}{r}{t} {y}{i}{g}{c}{v}{z}{z}{k}

But, it is also probably not very helpful to your real application.


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".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re^3: Is it possible to create a single Hash-of-Hash.. with multiple threads..
Download Code
Replies are listed 'Best First'.
Re^4: Is it possible to create a single Hash-of-Hash.. with multiple threads..
by gsat (Initiate) on May 02, 2014 at 16:18 UTC

    Dear Monk BrowserUk! Please take a bow!!

    Thank You so much - for enlightening me :) Indeed - it was my lack of understanding.

    I have found this entire discussion from everybody - very helpful.

    -=-=-=-=-=-=-=-

    Now, I need more :) It seems - me and my code is tired of doing so many .. if exists.. else share.. kinda additives before I do any structure change - and I think no way to avoid that. Now a couple of Questions here:

    Q1. Is there any easier method?

    Q2. Is it possible to grow a local tree or branch data-structure, then create a shared_clone - and graft the new tree to s sub-branch of main shared tree?

    Q3. I have few subroutines to be called many times - based on data - from inside my thread_worker. So far I am lacking depth in understanding or visualizing - how/when multiple threads work on different set of data, while calling a subroutine - does the code-inside-subroutine-block gets a replicated inside the thread uniquely? - else how the sanity is retained? - sorry if I am asking too basic question - maybe it is the case, I am confused - and not clear enough - where is my confusion too..

    Q4. Inside subroutine - do I have to use every variable as local and not-shared?

    Thanks a lot! Again & Again!

      1. No.

        You can replace:

        if( exists $copyRef->{ $step } ) { $copyRef = $copyRef->{ $step }; } else { lock %{ $copyRef }; $copyRef = $copyRef->{ $step } = &share( {} ); }

        With:

        lock %{ $copyRef }; $copyRef = $copyRef->{ $step } //= &share( {} )

        Which is more compact, but not easier nor more efficient.

      2. Yes.

        Two possibilities:

        1. You can build a normal (non-shared) subhash and when complete, use threads::shared::shared_clone() to make a shared copy of it and assign a reference to the copy to a key/value pair in the main shared hash:
          ... my %subhash; ... # populate it lock %mainSharedHash; $mainSharedHash{ $key } = shared_clone( \%subhash ); ...
        2. You can create a shared subhash and assign a ref to it without needing to copy:
          ... my %subhash :shared: ... # populate it lock %mainSharedHash; $mainSharedHash{ $key } = \%subhash;
      3. I do not understand the question.

        You'll get better answers if you describe your actual application. Ie. Real requirements garner working responses.

      4. Again, your question makes no sense.

        In a subroutine (or anywhere else), you can use unshared variables and/or shared variables as required.


      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".
      In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (12)
As of 2015-07-07 22:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls