Parallel For

Ali Çehreli acehreli at yahoo.com
Tue Jun 15 09:09:29 UTC 2021


On 6/14/21 11:39 PM, seany wrote:

 > I know that D has parallel foreach [like
 > this](http://ddili.org/ders/d.en/parallelism.html).

I gave an example of it in my DConf Online 2020 presentation as well:

   https://www.youtube.com/watch?v=dRORNQIB2wA&t=1324s

 >                  int[] c ;
 >                  foreach(aa; parallel (a)) {
 >                          foreach (bb; parallel(b)) {
 >
 >                                  c ~= aa+bb;

That is violating a parallelism requirement that loop bodies must be 
independent. (I use a similar example during my presentation above.) You 
need to either pre-allocate the array (as jfondren said) or not store 
the elements at all but use them independently in the loop body.

Yes, std.concurrency is another option. I show a "recipe" of usage here:

   https://www.youtube.com/watch?v=dRORNQIB2wA&t=1737s

Ali



More information about the Digitalmars-d-learn mailing list