Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop sequence in OpenMP Collapse performance advise

I found Intel's performance suggestion on Xeon Phi on Collapse clause in OpenMP.

#pragma omp parallel for collapse(2) 
  for (i = 0; i < imax; i++) { 
    for (j = 0; j < jmax; j++) a[ j + jmax*i] = 1.; 
  } 

Modified example for better performance:

#pragma omp parallel for collapse(2) 
  for (i = 0; i < imax; i++) { 
     for (j = 0; j < jmax; j++) a[ k++] = 1.; 
  }

I test both case in Fortran with similar code on regular CPU using GFortran 4.8, they both get correct result. Test using similar Fortran Code with later code does not pass for GFortran5.2.0 and Intel 14.0

But as far as I understand, the loop body for OpenMP should avoid "loop sequence dependent" variable, for this case is k, so why in the later case it can get correct result and even better performance?

like image 477
Francium Avatar asked Sep 11 '26 14:09

Francium


1 Answers

Here's the equivalent code for the two approaches when using collapse clause. You could see the second one is better.

for(int k=0; k<imax*jmax; k++) {
  int i = k / jmax;
  int j = k % jmax;
  a[j + jmax*i]=1.;
}

for(int k=0; k<imax*jmax; k++) {
  a[k]=1.;
}
like image 55
kangshiyin Avatar answered Sep 13 '26 14:09

kangshiyin