Idiomatic way to share mutable data?

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Sun Dec 22 14:00:21 PST 2013


On 22/12/13 22:42, Joseph Rushton Wakeling wrote:
> 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).

I should clarify what I mean there.  By splitting up x and vx into two separate 
arrays, you get the benefit that in the inner loops, you can have a situation 
where you have one purely read-only array, and one which is write-only.

Then, as Frustrated says, you can split up the write-only array into manageable 
chunks which can be dealt with by separate threads (using separate RNGs if 
that's necessary: note that the default random generator rndGen is thread-local, 
so calls to it in different threads should generate sufficiently independent 
pseudo-random values).

But depending on what your total system size is, you might find that your CPU's 
ability to vectorize loop operations is actually more efficient than any split 
into different threads (this has certainly happened to me).

But if you don't have any random number generation inside the inner loops, 
again, you'll probably find it better just to use std.parallelism.


More information about the Digitalmars-d-learn mailing list