How to call stop from parallel foreach

Bastiaan Veelo Bastiaan at Veelo.net
Thu Jun 24 21:05:28 UTC 2021


On Thursday, 24 June 2021 at 20:41:40 UTC, seany wrote:
> On Thursday, 24 June 2021 at 20:33:00 UTC, Bastiaan Veelo wrote:
>
>> By the way, nesting parallel `foreach` does not make much 
>> sense, as one level already distributes the load across all 
>> cores (but one). Additional parallelisation will likely just 
>> add overhead, and have a net negative effect.
>>
>> — Bastiaan.
>
> Okey. So consider :
>
>     foreach(array_elem; parallel(an_array)) {
>       dothing(array_elem);
>     }
>
>
> and then in `dothing()` :
>
>     foreach(subelem; array_elem) {
>
>       dootherthing(subelem);
>     }
>
> - Will this ALSO cause the same overhead?

You can nest multiple `foreach`, but only parallelise one like so:
```d
foreach(array_elem; parallel(an_array))
       foreach(subelem; array_elem)
           dootherthing(subelem);
```
So there is no need to hide one of them in a function.

> Is there any way to control the number of CPU cores used in 
> parallelization ?
>
> E.g : take 3 cores for the first parallel foreach - and then 
> for the second one, take 3 cores each -> so 3 + 3 * 3 = 12 
> cores out of a 16 core system? Thank you.

There might be, by using various `TaskPool`s with a smaller 
number of work threads: 
https://dlang.org/phobos/std_parallelism.html#.TaskPool.this.2. 
But I cannot see the benefit of doing this. It will just 
distribute the same amount of work in a different way.

— Bastiaan.


More information about the Digitalmars-d-learn mailing list