Wrong output of a nested loop (3 loops!)

General OpenMP discussion

Wrong output of a nested loop (3 loops!)

Postby kazempour » Wed Aug 01, 2012 8:24 am

Hi everyone,

I am implementing openMP in a subrutine named black_box_1 and the subrutine gets a number and variable "b" as inputs. Below you can find a part of my code:
Code: Select all
!$omp parallel
!$omp& shared ( n, a, b, main )
!$omp& shared ( index_old )
!$omp& private ( i, j, k )
!$OMP DO REDUCTION(+:index_new)
      DO i=1,(n/2+1) 
      DO j=1,(n+1)*2
      DO k=1,2
      index_new=index_new+1
      main(index_new)=main(index_new)+
     %conjg(a(i,j,index_old,k))*b(index_old)     
      ENDDO
      ENDDO
      ENDDO
!$omp end do
!$omp end parallel


As I compile this subroutine this part of code gives wrong output? Do you have any idea where is the problem exactly? (my code is in Fortran 77 environment!!!)
Thanks,
Mahdi
kazempour
 
Posts: 13
Joined: Wed Jul 25, 2012 4:11 am

Re: Wrong output of a nested loop (3 loops!)

Postby MarkB » Wed Aug 01, 2012 8:55 am

The problem is with the variable index_new: declaring it as a reduction variable means each thread gets a private copy, initialised to zero, and the sum of these copies is not performed until the end of the parallel loop. This means that every thread is writing to the same part of main.

To fix this you need to calculate index_new as a function of the 3 loop indices (i, ,j and k), instead of using the value from the previous loop iteration.
MarkB
 
Posts: 447
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh


Return to Using OpenMP

Who is online

Users browsing this forum: Exabot [Bot], Yahoo [Bot] and 9 guests