Parallel For

jfondren julian.fondren at gmail.com
Wed Jun 16 08:20:11 UTC 2021


On Wednesday, 16 June 2021 at 06:29:21 UTC, z wrote:
> On Tuesday, 15 June 2021 at 06:39:24 UTC, seany wrote:
>>...
>
> This is the best I could do: https://run.dlang.io/is/dm8LBP
> For some reason, LDC refuses to vectorize or even just unroll 
> the nonparallel version, and more than one `parallel` corrupts 
> the results.

The same trick as before is useful here: insert a write(i+ii);
before the assignment and see if the output looks reasonable.

Instead of only unique indices, there are many repeats, as of
course (i=0)+(ii=1) == (i=1)+(ii=0)

```d
import std;

void main() {
     int[] a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
     int[] b = [11, 12, 13, 14, 15, 16, 17, 18];
     int[] c = new int[a.length * b.length];
     foreach (i, aa; parallel(a)) {
         foreach (ii, bb; parallel(b)) {
             c[i * b.length + ii] = aa + bb;
         }
     }
     writeln(c);
}
```


More information about the Digitalmars-d-learn mailing list