omp doesn't run properly with global variables?

General OpenMP discussion

omp doesn't run properly with global variables?

Postby ytlee » Sun Feb 03, 2013 3:47 pm

Hi Everyone,

I am adding omp command to run my program in a i-j nested loop. However, the result was very different from the original/serial job which I have no idea why that happened. I suspect if this issue has somethg to do with my global variable (it shouldn't be...). It will be great if I can get some help here. The following is part of my code.
(n_tot, xn, yn, zn, chk_radius, pow, G, m, de, rn and PEj are declared as global variable in the global module)

Code: Select all
subroutine selfg()
  use global
  implicit none

  integer :: i, j
  real :: delx, dely, delz, rr, r, netaccterm
  real, dimension(n_tot,n_tot) :: gxj, gyj, gzj, PEj

!$omp parallel do private(j,delx,dely,delz,rr,r,netaccterm) &
!$omp             num_threads(4)
  do i=1, n_tot
     do j=i+1, n_tot
        delx = xn(step,i)-xn(step,j)
        dely = yn(step,i)-yn(step,j)
        delz = zn(step,i)-zn(step,j)
        rr = delx**2+dely**2+delz**2
        r = sqrt(rr)
        rn(i,j) = r
        if (r < chk_radius) then
           !models of self-gravitation
           netaccterm = G*m*(-1/rr + de**(pow-2)/r**pow)/r  !inverse law
           gxj(i,j) = delx*netaccterm
           gyj(i,j) = dely*netaccterm           
           gzj(i,j) = delz*netaccterm           
           PEj(i,j) = G*m*(-1/r + de**(pow-2)*r**(1-pow)/(pow-1))
        endif
     enddo
  enddo
!$omp end parallel do
.
.
.
end subroutine selfg


I appreciate for your help...

-yt
ytlee
 
Posts: 2
Joined: Sun Feb 03, 2013 2:25 pm

Re: omp doesn't run properly with global variables?

Postby ftinetti » Wed Feb 06, 2013 6:23 am

Hi yt,

I've been looking at your code, and I didn't see anything wrong... maybe I lost something. Would you send a complete example that reproduces the wrong behaviour to play around with?

Fernando.
ftinetti
 
Posts: 581
Joined: Wed Feb 10, 2010 2:44 pm

Re: omp doesn't run properly with global variables?

Postby ytlee » Wed Feb 06, 2013 8:17 am

Hi Fernando,

After further checking, I think the code is now working... Seems like it works after initializing all the arrays' entries to zero. Apparently, without initializing give me some sort of error with(/without?) openMP. I can't believe I have to do this for Fortran. I assumed the entries are zero after doing: real, dimension(n_tot,n_tot) :: gxj, gyj, gzj, PEj but seems like it is not (sometimes it gives wield values)...

Really appreciate for looking at my code!

-yt
ytlee
 
Posts: 2
Joined: Sun Feb 03, 2013 2:25 pm

Re: omp doesn't run properly with global variables?

Postby ftinetti » Wed Feb 06, 2013 9:16 am

Hi again,

I see, I tend to always overlook this kind of errors. Actually, I would not wonder too much about
I can't believe I have to do this for Fortran. I assumed the entries are zero after doing: real, dimension(n_tot,n_tot) :: gxj, gyj, gzj, PEj

because I think that zero-initialization is not defined in any of the Fortran standards, even when many (most? all?) compilers actually implement such initialization.

It's good to know your code works now.

Fernando.
ftinetti
 
Posts: 581
Joined: Wed Feb 10, 2010 2:44 pm


Return to Using OpenMP

Who is online

Users browsing this forum: Google [Bot] and 5 guests