How to use D parallel functions/library
thedeemon via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Nov 24 22:20:14 PST 2015
On Tuesday, 24 November 2015 at 18:49:25 UTC, Bishop120 wrote:
> I figured this would be a simple parallel foreach function with
> an iota range of sizeX and just making int X declared inside
> the function so that I didnt have to worry about shared
> variable but I cant get around the alive++ reduction and I dont
> understand enough about D's reduction/parallel library.
>
> Any ideas? Thanks in advance for yalls patience and assistance!
Incrementing often the same variable from different parallel
threads is a very bad idea in terms of performance. I would
suggest counting number of alive cells for each row independently
(in a local non-shared variable) and storing it to an array (one
value per row), then after the loop sum them up.
auto aliveCellsPerRow = new int[N];
foreach(i; iota(N).parallel) {
int aliveHere;
//...process a row...
aliveCellsPerRow[i] = aliveHere;
}
alive = aliveCellsPerRow.sum;
Then everything will be truly parallel, correct and fast.
More information about the Digitalmars-d-learn
mailing list