parallel foreach stuck on 8 workUnitSize

Ali Çehreli acehreli at yahoo.com
Sun Jan 9 03:26:50 UTC 2022


On 1/8/22 7:18 PM, Booster wrote:
 > 
https://dlang.org/library/std/parallelism/task_pool.parallel.html#workUnitSize 

 >
 >
 > Basically have no difference than the above but when I change the
 > workUnitSize to 1, 4, whatever it is always running 8 parallel loops.
 >
 > Either a bug or workUnitSize is not what I think it is. I simply want to
 > execute n iterations of the loop at the time, not fixed at 8.

Work unit size is the number of elements each thread will work 
sequentially before causing a context switch. You need to create a 
TaskPool object to configure the number of threads because the default 
TaskPool uses all cores.

I have a slide about this in my DConf Online 2020 presentation right here:

   https://www.youtube.com/watch?v=dRORNQIB2wA&t=1700s

import std.parallelism;

void main() {
     int[] elements;

     auto tp = new TaskPool(totalCPUs / 2); // 1. Thread count
     foreach (e; tp.parallel(elements, 1)) { // 2. Work unit size
         // ...
     }
     tp.finish(); // Don't forget
}

Ali



More information about the Digitalmars-d-learn mailing list