Thread overview
Using multiple processors
Dec 23, 2012
n00b
Dec 24, 2012
thedeemon
Dec 27, 2012
n00b
Dec 28, 2012
Russel Winder
December 23, 2012
Hello.
My program has a great deal of computation to do, so I thought I'd create several threads in order to use multiple processors. It worked fine with a simple "test" program, but when I try to do this with the real program, only 1 processor is used.
I'm using D1 and windows 7 64bit.
What could cause that difference? I don't use synchronized methods, diffrent threads don't access the same data ( every thread gets a slice of an object array with a "compute()" method to be called ). I tried to disable the garbage collector and reduce memory allocation/deallocation.

Thanks for any help.
December 24, 2012
On Sunday, 23 December 2012 at 08:00:56 UTC, n00b wrote:
> Hello.
> My program has a great deal of computation to do, so I thought I'd create several threads in order to use multiple processors. It worked fine with a simple "test" program, but when I try to do this with the real program, only 1 processor is used.

How much memory allocation is happening there? High allocation rate and often GCs can kill parallelism since they're happening with a lock. Anyway, too hard to tell anything certain without knowing what your program is doing.
December 27, 2012
Le 24/12/2012 05:18, thedeemon a écrit :
> On Sunday, 23 December 2012 at 08:00:56 UTC, n00b wrote:
>> Hello.
>> My program has a great deal of computation to do, so I thought I'd
>> create several threads in order to use multiple processors. It worked
>> fine with a simple "test" program, but when I try to do this with the
>> real program, only 1 processor is used.
>
> How much memory allocation is happening there? High allocation rate and
> often GCs can kill parallelism since they're happening with a lock.
> Anyway, too hard to tell anything certain without knowing what your
> program is doing.


I tried to disable GC and reduce memory allocation. It didn't work but I guess allocation rate is still pretty high, I'll try to reduce it even more, and post more details if it still doesn't work.
Thanks for your answer.
December 28, 2012
On Thu, 2012-12-27 at 13:39 -0500, n00b wrote:
> Le 24/12/2012 05:18, thedeemon a écrit :
> > On Sunday, 23 December 2012 at 08:00:56 UTC, n00b wrote:
> >> Hello.
> >> My program has a great deal of computation to do, so I thought I'd
> >> create several threads in order to use multiple processors. It worked
> >> fine with a simple "test" program, but when I try to do this with the
> >> real program, only 1 processor is used.
> >
> > How much memory allocation is happening there? High allocation rate and often GCs can kill parallelism since they're happening with a lock. Anyway, too hard to tell anything certain without knowing what your program is doing.
> 
> 
> I tried to disable GC and reduce memory allocation. It didn't work but I
> guess allocation rate is still pretty high, I'll try to reduce it even
> more, and post more details if it still doesn't work.
> Thanks for your answer.

Simply creating threads to try and harness parallelism is generally the wrong approach. Multi-threading should be treated as infrastructure, in the same way stack and heap are. Most programmers (unless they are severely resource constrained) never need to worry about stack and heap management, they leave it to the compiler and runtime system. Threads should be treated the exact same way, they are infrastructure to be managed by the runtime system. Use a higher level parallelism model such as actors (cf. D's spawn), dataflow (I think D has no offering here yet), CSP (D definitely doesn't have an offering here as yet, even though Go does), data parallelism (D's std.parallelism is your friend here).

Map your solution to your problem to one of these models and you harness thread pools that manage the threads for you. This way you do not have to manage locks, semaphores, monitors, etc. all of which are tools required for operating systems or writing actor, dataflow, CSP, data parallelism, agents, etc. toolkits.

-- 
Russel. ============================================================================= Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder@ekiga.net 41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel@winder.org.uk London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder