How to call stop from parallel foreach

seany seany at uni-bonn.de
Fri Jun 25 13:53:17 UTC 2021


On Thursday, 24 June 2021 at 21:19:19 UTC, Ali Çehreli wrote:
> On 6/24/21 1:41 PM, seany wrote:
>
> > Is there any way to control the number of CPU cores used in
> > parallelization ?
>
> Yes. You have to create a task pool explicitly:
>
> import std.parallelism;
>
> void main() {
>   enum threadCount = 2;
>   auto myTaskPool = new TaskPool(threadCount);
>   scope (exit) {
>     myTaskPool.finish();
>   }
>
>   enum workUnitSize = 1; // Or 42 or something else. :)
>   foreach (e; myTaskPool.parallel([ 1, 2, 3 ], workUnitSize)) {
>     // ...
>   }
> }
>
> I've touched on a few parallelism concepts at this point in a 
> presentation:
>
>   https://www.youtube.com/watch?v=dRORNQIB2wA&t=1332s
>
> Ali

I tried this .

                 int[][] pnts ;
		pnts.length = fld.length;

		enum threadCount = 2;
		auto prTaskPool = new TaskPool(threadCount);

		scope (exit) {
			prTaskPool.finish();
		}

		enum workUnitSize = 1;

		foreach(i, fLine; prTaskPool.parallel(fld, workUnitSize)) {
                   //....
                 }


This is throwing random segfaults.
CPU has 2 cores, but usage is not going above 37%

Even much deeper down in program, much further down the line...
And the location of segfault is random.




More information about the Digitalmars-d-learn mailing list