Template error and parallel foreach bug?

Timon Gehr timon.gehr at gmx.ch
Sat Jun 4 11:04:56 PDT 2011


I just found Project Euler, and tried to solve the first problem.
https://gist.github.com/1007840
simendsjo wrote:
> I did four implementations: template, ctfe, parallel foreach and
> parallel map.
>
> The template implementation works on low numbers, but not on 1000
> (haven't checked when it fails). It gives me the following error:
> euler1.d(23): Error: template instance
> euler1.SumMultiple3Or5(499LU,Below,57918LU) recursive expansion
>
> The parallel foreach loop works good on low numbers, but when increasing
> BOLOW it starts giving wrong results. The higher the number, the more
> often it calculates wrong results.
>
> This is tested on dmd 2.053 on a dual core Win7.

ulong SumMultiple3Or5_parallel(uint below) {
    ulong sum;
    foreach(i; parallel(iota(below))) {
        if(i % 3 == 0 || i % 5 == 0)
            sum += i; // low level data race here.
    }
    return sum;
}

Loop iterations in a parallel foreach loop must be independent of each other.

Timon


More information about the Digitalmars-d-learn mailing list