how to print progress of a long running parallel() loop?

Ali Çehreli acehreli at yahoo.com
Mon Dec 7 16:37:31 UTC 2020


On 12/7/20 4:48 AM, Dukc wrote:
> On Monday, 7 December 2020 at 08:16:50 UTC, mw wrote:
>> r = Parallel(n_jobs=2, verbose=10)(delayed(sleep)(.2) for _ in range(10))
>>
>> to print out the progress.
>>
>> How to do this in D's parallel loop?
>>
>> thanks.
> 
> Allocate a `shared int` before the foreach loop. In the loop when, let's 
> say `!(i & 0xFFF)`,  atomically increment the shared variable and print 
> it's number.
> 
> Disclaimer: no experience about using `shared`, but this is what I'd try.

Yes, that would be the way but I don't know the effect of using shared 
or not. But because the progress needs to be somehow reported, an atomic 
variable may not be sufficient (unless a separate thread is reading it, 
etc.)

This issue came up during one of my recent presentations at this point:

   https://www.youtube.com/watch?v=dRORNQIB2wA&feature=youtu.be&t=1527

I used a synchronized block in that code because it did not affect 
performance in my use case.

Ali



More information about the Digitalmars-d-learn mailing list