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:[email protected] 41 Buckmaster Road m: +44 7770 465 077 xmpp: [email protected] London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
signature.asc
Description: This is a digitally signed message part
