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.
| [reply] [Watch: Dir/Any] |
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?
| [reply] [Watch: Dir/Any] |
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.
| [reply] [Watch: Dir/Any] [d/l] |
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 :)
| [reply] [Watch: Dir/Any] |
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;
| [reply] [Watch: Dir/Any] [d/l] [select] |
| [reply] [Watch: Dir/Any] |