How many std.concurrency receivers?

Russel Winder russel at winder.org.uk
Thu Oct 11 11:43:26 PDT 2012


On Thu, 2012-10-11 at 20:04 +0200, thedeemon wrote:
[…]
> Here's how I would try to approach a task of having thousands of 
> independent agents with current std.concurrency. Each agent 
> (cell) is represented by some data structure and its main 
> function which gets one message as input, reacts (possibly 
> changing its state and sending other messages) and returns 
> without blocking. Then I'd create say 16 threads (or 8, anyway a 
> power of 2 which is close to actual number of cores), each of 
> them will have its own message queue, that's given by 
> std.concurrency. Let's say each cell has its own id. I would 
> place cell with id N to the thread number N mod 16. Each thread 
> will have an array of cells mapped to it. Then if some cell sends 
> a message to cell X, it makes sure the message contains cell id 
> of recipient and then sends it to thread X mod 16. Each worker 
> thread runs a loop where it receives next message from its queue, 
> finds the target cell by its id in this thread's array of cells 
> (we can use X / 16 as index) and calls its reaction function. 
> This way all agents are evenly distributed between threads, we're 
> using just 16 threads and 16 queues which work in parallel, and 
> it all acts as if thousands of agents work independently. However 
> this approach does not guarantee even work distribution between 
> cores.

Can't this be done now using tasks and a threadpool from std.parallel?

And I believe (in that I can't point you at explicit data just now),
that it is generally best to have 1 or 2 more threads than there are
cores to get optimal performance.

-- 
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/20121011/ee4e9de1/attachment.pgp>


More information about the Digitalmars-d-learn mailing list