Looking for writing parallel foreach kind of statement for nested for-loops

FG home at fgda.pl
Sat Feb 9 23:45:40 PST 2013


On 2013-02-10 02:22, Sparsh Mittal wrote:
> The current reply answers my question, but I was just curious. Can we have a
> method which divides the 2d region as follows: 8*12 divided into 4*6 to each of
> 4 threads.

Think again if you need that. Things start getting pretty ugly. :)

     const uint parts = 2; // in each dimension
     foreach (block; parallel(iota(parts*parts))) {
         uint p1 = block / parts;
         foreach (i; (p1*N/parts)..(p1==parts-1 ? N : (p1+1)*N/parts)) {
             uint p2 = block % parts;
             foreach (j; (p2*M/parts)..(p2==parts-1 ? M : (p2+1)*M/parts)) {
                 func(i, j, p1, p2, block);
             }
         }
     }




More information about the Digitalmars-d-learn mailing list