I am trying to create a code of an iterative process while keeping the while inside the parallel region to minimize the parallelization overhead.
the code is something like this The problem is it never exits, so if possible i would like your thoughts about this
#include <stdio.h>
#include <omp.h>
int main(int argc, char **argv)
{
float error = 20;
#pragma omp parallel shared(error)
{
while (error > 5)
{
#pragma omp for reduction(-:error)
for (int i=0; i<10; ++i)
{
error -= 1;
}
}
}
fprintf(stderr, "Program terminated\n");
return 0;
}
this is a funny little problem. I have no overwhelming openmp experience, but after some experiments with your code I think the problem is caused by the lack of synchronisation when entering the parallel for loop (insert write statements to "watch" your code).
You can get the code to work by inserting a barrier just before the parallel for loop:
#pragma omp barrier
#pragma omp for reduction(-:error)
for(int i=0; i<10; ++i)
Without that barrier and running on 2 threads, one thread will enter the for loop for the second time and reduce error
to 5, when the other thread will not enter the second for loop at all, leaving the system in the strange state that a parallel for loop has been executed by one thread, but the other thread refuses to join. It is surely a warning about writing into shared variables inside parallel loops and using them as control variables elsewhere.