So i'm working on some homework related to fixing som buggy code when i ran into an interesting problem. I don't think it was one of the indended bugs because the lecturer was confused by it as well. For the sake of the homework i found a workaround but i wonder what the 'correct' way to fix it is. The code was structured something like this:
int i; //iterator
/* some other initilization code */
# pragma omp parallel private(i)
{
/* set up threads */
# pragma omp for
for (i=0;i<N;i++){
/* doing some stuff */
}
// for debugging:
printf("this thread's last index was %i", i);
} // end parallel
The problem is that when the code reaches the print statement, i isn't the last index but instead some random large number (same for each thread). The code inside the for loop seems to work fine, but ironically the debug print is broken (even worse in the actual code since it tried to index an array and promptly seg faulted).
What would be the proper way to do this? (either keep the index in general, or at least find out the final index of the thread)
private(i)of the parallel section. Same thing happens removing theprivate(i)and declaringiinside the parallel region.