Getting wrong results with a nested loop

General OpenMP discussion

Getting wrong results with a nested loop

Postby schwalbs » Tue Aug 21, 2012 11:05 pm

Hey everyone,

I have a very strange problem that I'm try to solve and understand. I have a nested for loop of the following form:

Code: Select all
#pragma omp parallel for schedule(guided) shared(Array) collapse(3)
for (int i=istart; i<iend; i++)
{
  for (int j=jstart; j<jend; j++)
  {
    for(int k=kstart; k<kend; k++)
    {
       Int IJK = (i*(jend-jstart) + (j-jstart))*(kend-kstart) + (k-kstart);
       Array[3*IJK + 2] = an operation with some shared values;
    }
  }
}


There are three loops of this form, with Array[3*IJK] , Array[3*IJK + 1] and Array[3*IJK+2] respectively. Array is also actually a shared pointer and for the value of IJK, a function is actually called (inlined).

I first tried parallelizing all loops and the program runs through, but the results are different compared to my serial results.

Now come the strange parts.

The for loops that are of this same structure, but have Array[3*IJK + 1] and Array[3*IJK ] instead, produce correct results when parallelized (the other loop is serial in this case). But as soon as I parallelize the Array[3*IJK+2] loop, I get different results.

Also, If I don't use collapse, or collapse(2) instead of collapse(3), I get different results. Only with the #pragma statement as above, I get correct results in the Array[3*IJK + 1] and Array[3*IJK ] loops.

I thought it might have something to do with the order in which Array was written to, but with an ordered clause and construct, I still get wrong results.

With num_threads(1) I get wrong results as well.

What can be the cause of this?
schwalbs
 
Posts: 2
Joined: Tue Aug 21, 2012 12:02 pm

Re: Getting wrong results with a nested loop

Postby MarkB » Wed Aug 22, 2012 7:33 am

Hi there,

I can't see anything wrong with your code. If you print out the values of IJK are they incorrect?
It would be worth trying a different compiler to test whether you see the same problems.

Hope that might help,
Mark.
MarkB
 
Posts: 408
Joined: Thu Jan 08, 2009 10:12 am

Re: Getting wrong results with a nested loop

Postby schwalbs » Fri Aug 24, 2012 1:25 pm

The values of IJK seem to be correct. I looked at the values more closely, and there is a very small difference. For example instead of 3.5394040414085617, I receive 3.5394040414085612.
Is it possible that OpenMP can cause a different rounding of values?
schwalbs
 
Posts: 2
Joined: Tue Aug 21, 2012 12:02 pm

Re: Getting wrong results with a nested loop

Postby MarkB » Mon Aug 27, 2012 2:53 am

schwalbs wrote:Is it possible that OpenMP can cause a different rounding of values?


In general, yes, though the usual cause of this is reductions, which is not the case here. However, adding OpenMP directives can cause the compiler to do its optimisations slightly differently, which can result in different rounding behaviour (for example by changing if/when values are stored to memory instead of being held in registers). On some compilers, adding the OpenMP flag automatically enables some level of optimisation. What compiler and optimisation flags are you using?
MarkB
 
Posts: 408
Joined: Thu Jan 08, 2009 10:12 am


Return to Using OpenMP

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 4 guests