www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to specify number of worker threads for Taskpool

reply "Sparsh Mittal" <sparsh0mittal gmail.com> writes:
I am seeing




I am running the example of parallel foreach given in that 
section.

Sorry if it is too obvious, but my question is when using


foreach(i, ref elem; taskPool.parallel(logs)) { elem = log(i + 
1.0); }

where taskPool is not explicitly defined, how do I specify number 
of worker threads? I don't know when is constructor of TaskPool 
called and hence when

///////   trusted this(size_t nWorkers);
///////   Allows for custom number of worker threads.

function which allows for setting number of threads, is called.

I will be thankful for your help.
Feb 17 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 02/17/2013 08:23 AM, Sparsh Mittal wrote:
 I am seeing


Please also consider: http://ddili.org/ders/d.en/parallelism.html
 I am running the example of parallel foreach given in that section.

 Sorry if it is too obvious, but my question is when using


 foreach(i, ref elem; taskPool.parallel(logs)) { elem = log(i + 1.0); }

 where taskPool is not explicitly defined,
That is a pre-defined object of the std.parallelism module.
 how do I specify number o
 worker threads?
You cannot change taskPool. It is started with one less than the total cores of the system.
 I don't know when is constructor of TaskPool called and
 hence when

 ///////  trusted this(size_t nWorkers);
 /////// Allows for custom number of worker threads.

 function which allows for setting number of threads, is called.

 I will be thankful for your help.
You must construct a separate TaskPool object and use that one for your special needs. The following example from the link above uses just 2 cores: import std.stdio; import std.parallelism; void main() { auto workers = new TaskPool(2); foreach (i; workers.parallel([1, 2, 3, 4])) { writefln("Working on %s", i); } workers.finish(); } Ali
Feb 17 2013
parent "Sparsh Mittal" <sparsh0mittal gmail.com> writes:
Thanks a lot for your reply. It was very helpful.
Feb 17 2013