Recommendation for parallelism with nested for loops?

Ali Çehreli acehreli at yahoo.com
Fri Aug 19 02:49:15 UTC 2022


On 8/18/22 18:49, Shriramana Sharma wrote:
 > Hello. I want to parallelize a computation which has two for loops

An option is to add tasks individually but I am not sure how wise doing 
this and I don't know how to determine whether all tasks are completed.

In any case, Roy Margalit's DConf 2022 presentation is very much on 
topic. :)

   http://dconf.org/2022/index.html#roym

And the following program cannot show any benefit because the tasks are 
so short.

import std.stdio;
import std.parallelism;
import std.conv;

enum I = 1_000;
enum J = 1_000;

void main() {
   auto results = new int[I * J];

   // In case you want a new TaskPool:
   // auto tp = new TaskPool(totalCPUs);
   // (And use tp. below instead of taskPool.)

   foreach (i; 0 .. I) {
     foreach (j; 0 .. J) {
       taskPool.put(task!foo(i, j, results));
     }
   }

   // WARNING: I am not sure whether one can trust the results are
   //          ready yet. (?)
   //
   // parallel() does call yieldForce() on each task but we don't seem
   // to have that option for tasks that are .put() into the pool. (?)

   enum toPrint = 10;
   writeln(results[0..toPrint]);
   writeln("[...]");
   writeln(results[$-toPrint..$]);
}

void foo(size_t i, size_t j, int[] results) {
   results[i * J + j] = to!int(i * J + j);
}

Ali



More information about the Digitalmars-d-learn mailing list