Idiomatic way to share mutable data?

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Sun Dec 22 13:42:06 PST 2013


On 22/12/13 22:07, Charles McAnany wrote:
> So here's the conundrum: How do I parallelize this efficiently?

Does your simulation rely at all on pseudo-random number generation _inside_ the 
loop?  That is, apart from for generation of the initial array of atoms?

If it's purely deterministic, it ought to suffice to use std.parallelism.  If 
not, then things can be more tricky.

OTOH you might find it best to declare the array of atoms as shared.  I don't 
have good personal experience here so don't know how to advise you.

You might also find it beneficial -- since in each of the inner loops you're 
reading from one set of values and writing to another -- to split up your array 
of atoms into two arrays: double[] x and double[] vx -- and to find another way 
of doing the interaction calculation (e.g. do it as interaction(size_t i, size_t 
j) where i, j are array indexes).

> On that note, how do you pass a reference to a thread (via send) without the
> compiler yelling at you? Do you cast(immutable Atom[]) on send and cast(Atom[])
> on receive?

This can be one way of handling things -- of course, you have to be careful to 
treat the data with respect, e.g. to not modify it even though you have cast it 
away from immutable.



More information about the Digitalmars-d-learn mailing list