Yet another parallel foreach + continue question

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Jul 20 00:37:56 UTC 2021


On Tue, Jul 20, 2021 at 12:07:10AM +0000, seany via Digitalmars-d-learn 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 ...
>         }
>     }
> 
> 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.
> 
> 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? I am having a bit of difficulty to understand this.
[...]

I didn't test this, but I'm pretty sure `continue` inside a parallel
foreach loop simply terminates that iteration early; I don't think it
will skip to the next iteration.

Basically, what .parallel does under the hood is to create N jobs (where
N is the number of items to iterate over), representing N instances of
the loop body, and assign them to M worker threads to execute. Then it
waits until all N jobs have been completed before it returns.  Which
order the worker threads will pick up the loop body instances is not
specified, and generally is not predictable from user code.

The loop body in this case is translated into a delegate that gets
passed to the task pool's .opApply method; each worker thread that picks
up a job simply invokes the delegate with the right value of the loop
variable. A `continue` translates to returning a specific magic value
from the delegate that tells .opApply that the loop body finished early.
AFAIK, the task pool does not act on this return value, i.e., the other
instances of the loop body will execute regardless.


T

-- 
Time flies like an arrow. Fruit flies like a banana.


More information about the Digitalmars-d-learn mailing list