Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Perl Threads and multi-core CPUs

by misterwhipple (Monk)
on Sep 10, 2008 at 03:27 UTC ( [id://710249]=note: print w/replies, xml ) Need Help??


in reply to Perl Threads and multi-core CPUs

Some random thoughts:

(Oops, you already said you're using Linux.) What Perl module/function are you using for fork()ing?

The forks module seems encouraging on this subject.

This is outside my experience, but could you accomplish what you need by forking before you generate your AI model, when the parent process is small, then using shared memory to make the model available to the child processes?

cat >~/.sig </dev/interesting

Replies are listed 'Best First'.
Re^2: Perl Threads and multi-core CPUs
by BrowserUk (Patriarch) on Sep 10, 2008 at 04:23 UTC
    The forks module seems encouraging on this subject.

    Can you imagine the time and memory costs of sharing 1GB of data, by converting it to Storable format and then transmitting it to each of your 'forks' via a socket?


    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.
      OK, just to confirm - does the Perlmonks community agree that Perl threads are actually runnable on separate CPUs/cores? Can anybody tell me on how to check that separate Perl threads are running on different cores in case "top is foolong you" as suggested already? I did a test to measure the time it takes to run a single process that processes all data and then another test for 8 Perl threads that each process 1/8 of the data set and the time measurement was almost exactly the same. I am doing something wrong?

        I'll confirm, and guarentee, that Perl threads are scheduled by the system scheduler and are eligible to run on all available cores unless you takes specific steps to prevent them from doing so. But then I guess you'd expect me to say that :)

        I am doing something wrong?

        On the basis of your description of your observations: yes. But without some idea of what you are doing, it's pretty much impossible to suggest what that might be.

        If, for example, your process is cpu-bound and spends most of it's time interacting with your 1GB of shared data, and your threaded version is applying indiscriminate locks to that shared data, then you could effectively be serialising access to it and 7 of your threads spend their time waiting for the 8th thread to release the lock. That conceivably might produce the symptoms you are seeing.

        By way of demonstration. The following code will show almost no benefit from the threading or multiple cores:

        #! perl -slw use strict; use threads; use threads::shared; my %bighash :shared = map{ $_ => 1; } 'aaa' .. 'zzz'; sub thread { while( 1 ) { lock %bighash; 1 while each %bighash; } } my @threads = map{ threads->create( \&thread ); } 1 .. 8; $_->join for @threads;

        Because all the threads are competing for a lock on the same single data structure, so only one thread will ever doing anything useful at any given time.

        There are simple ways of avoiding this problem, but which is applicable depends upon what you are doing in your program.


        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.
        Just a small nit, but the Perlmonks community doesn't have a voice :) on the other hand brother monk BrowserUk does have an extensive knowledge of how threads work :)

        If they run on different CPU's, then why do you have to yield() to get other threads to run, on an SMP kernel?

        Update... I guess you don't. But in Coro I did have to call cede() in looping threads to let any other thread run.

        This works:

        use 5.20.0; use threads; sub loop1 { do { print "loop1\n"; sleep 1 } while 1 } sub loop2 { do { print "loop2\n"; sleep 1 } while 1 } my $thr1 = threads->create('loop1'); my $thr2 = threads->create('loop2'); do { print "main\n"; sleep 1 } while 1;

        This also works:

        use 5.20.0; use threads; my $thr1 = async { do { print "loop1\n"; sleep 1 } while 1 }; my $thr2 = async { do { print "loop2\n"; sleep 1 } while 1 }; do { print "main\n"; sleep 1 } while 1;

        This does not work:

        use 5.20.0; use Coro; async { do { print "loop1\n"; sleep 1 } while 1 } async { do { print "loop2\n"; sleep 1 } while 1 } do { print "main\n"; sleep 1 } while 1;

        Top is not fooling, all threads of one process share the same memory segement and hence its shown as one process.regrding time the top shows its cumulative of all the threads.u can verify this by putting a start time and end time statemetns in your script. this will be half o what you see in top

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-03-19 06:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found