I’d like to know the progress of a for-loop using OpenMP.
I know reduction directive doesn’t work, but I wrote like this:
#pragma omp for reduction (+:sum)
for (int i=0; i < size; i++){
// do something that takes about 10seconds
sum++;
#pragma omp critical
cout << sum << " / " << size << endl;
}
this will return something like this:
1 / 100
1 / 100
2 / 100
1 / 100
...
but I want this:
1 / 100
2 / 100
3 / 100
. ..
Is there any way to get the correct sum value during reduction directive?
or should I use another method?
The
reductionclause has a very well defined meaning, explained in detail in section 2.9.3.6 of the latest OpenMP standard. I doubt you will be able to use it for the purpose you described above.Anyhow, it may be possible to implement that behavior with only slight modifications to your source:
In this way you are ensured that only one thread at a time is trying to increment ‘sum’ and print it on screen. Given the long time each iteration takes, this synchronization should not give rise to performance issues.