Progress update in Parallel.For() loops
Asked Answered
Y

2

5

As you can guess the index of the Parallel.For() loop jumps from one value to the other. How can I estimate the amount of the work done?

Thanks.

Youngman answered 21/10, 2010 at 9:29 Comment(0)
A
7

By keeping a counter rather than looking at the index? For example:

int counter  = 0;
Parallel.For(4, 500, i => {
    // TODO: something useful...         
    int progress = Interlocked.Increment(ref counter);
    Console.WriteLine("{0}: {1}", progress, i);
});

(the Interlocked usage is essential to avoid getting race-conditions on accessing counter)

Acromegaly answered 21/10, 2010 at 9:33 Comment(1)
And here is the same wrapped.Arithmetic
T
3
int progress = 0;
Parallel.For( from, to, i => {
// do the job
Interlocked.Increment(ref progress);
});

now actual progress is (float)(to - from) / progress

Tuchun answered 21/10, 2010 at 9:33 Comment(5)
No way to get this value without interfering with thread synchronization?Youngman
@devdept no. but what is the problem using this method?Tuchun
I thought the the Parallel.For() included a more elegant way to get this value.Youngman
@devdept you have to add two lines of code, what is not elegant here?Tuchun
@Alberto, here is a wrapper that incapsulates threading stuff. It is important to use value returned by 'Increment' though, otherwise you may still get race conditions.Arithmetic

© 2022 - 2024 — McMap. All rights reserved.