Using multiple processors

Russel Winder russel at winder.org.uk
Fri Dec 28 05:15:13 PST 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 at ekiga.net
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel at winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20121228/8d42087f/attachment.pgp>


More information about the Digitalmars-d-learn mailing list