No of threads

Ali Çehreli acehreli at yahoo.com
Tue Dec 19 18:42:01 UTC 2017


On 12/19/2017 02:24 AM, Vino wrote:
 > Hi All,
 >
 >    Request your help in clarifying the below. As per the document
 >
 > foreach (d; taskPool.parallel(xxx)) : The total number of threads that
 > will be created is total CPU -1 ( 2 processor with 6 core : 11 threads)
 >
 > foreach (d; taskPool.parallel(xxx,1)) : The total number of threads that
 > will be created is total CPU -1 ( 2 processor with 6 core : 12 threads)

That parameter is workUnitSize, meaning the number of elements each 
thread will process per work unit. So, when you set it to 100, each 
thread will work on 100 elements before they go pick more elements to 
work on. Experiment with different values to find out which is faster 
for your work load. If each element takes very short amount of time to 
work on, you need larger values because you don't want to stop a happy 
thread that's chugging along on elements. It really depends on each 
program, so try different values.

 > foreach (d; taskPool.parallel(xxx,20)) : As in Windows 2008 whatever
 > value is set for the parallel the total number of threads does not
 > increase more than 12.

taskPool is just for convenience. You need to create your own TaskPool 
if you want more threads:

import std.parallelism;
import core.thread;
import std.range;

void main() {
     auto t = new TaskPool(20);
     foreach (d; t.parallel(100.iota)) {
         // ...
     }
     Thread.sleep(5.seconds);
     t.finish();
}

Now there are 20 + 1 (main) threads.

Ali



More information about the Digitalmars-d-learn mailing list