Yet another parallel foreach + continue question

Ali Çehreli acehreli at yahoo.com
Tue Jul 20 05:56:08 UTC 2021


On 7/19/21 5:07 PM, seany wrote:

 > Consider :
 >
 >      for (int i = 0; i < max_value_of_i; i++) {
 >          foreach ( j, dummyVar; myTaskPool.parallel(array_to_get_j_from,
 > my_workunitSize) {
 >
 >              if ( boolean_function(i,j) ) continue;
 >              double d = expensiveFunction(i,j);
 >              // ... stuff ...
 >          }
 >      }

Arranging the code to its equivalent may reveal the answer:

if (!boolean_function(i, j)) {
   double d = expensiveFunction(i, j);
   // ... stuff ...
}

We removed 'continue' and nothing changed and your question disappeared. :)

 > I understand, that the parallel iterator will pick lazily values of `j`
 > (up to `my_workunitsize`), and execute the for loop for those values in
 > its own thread.

Yes.

 > Say, values of `j` from `10`to `20` is filled where `my_workunitsize` =
 > 11. Say, at `j = 13` the `boolean_function` returns true.
 >
 > Will then the for loop just jump to the next value of `j = 14` like a
 > normal for loop?

Yes.

 > I am having a bit of difficulty to understand this.
 > Thank you.

parallel is only for performance gain. The 2 knobs that it provides are 
also for performance reasons:

1) "Use just this many cores, not all"

2) "Process this many elements, not 100 (the default)" because otherwise 
context switches are too expensive

Other than that, it shouldn't be any different from running the loop 
regularly.

Ali



More information about the Digitalmars-d-learn mailing list