fortran array, shared or reduction?

General OpenMP discussion

fortran array, shared or reduction?

Postby ntlam » Mon Sep 12, 2011 9:51 am

Hi all,

I just started using OpenMP for my fortran codes. I have to do some calculation with arrays and want to make the code parallel. Here is a simple example. Suppose I want to calculate the interaction energy of Natom atoms which I have already grouped them in pairs: pair(1--->Npair). In a serial code I would do like following:
-----------------------------
do n=1,npair
i=atom1(n)
j=atom2(n)
energ(i)=energ(i)+some_function
energ(j)=energ(j)+some_function
end do
---------------------------
My problem when make this code parallel is to know whether to have variable energ shared or reduction? When make it reduction I get bus error. When make it shared the results are not consistent even I put the step of energy calculation in Critical block.

Please give me some hint if you can. Thanks,

Lam
ntlam
 
Posts: 3
Joined: Mon Sep 12, 2011 9:27 am

Re: fortran array, shared or reduction?

Postby ftinetti » Mon Sep 12, 2011 1:15 pm

Hi,

Please send more details/information:
1) Code with the directives, even when it fails
2) Compiler and compiler options you use.
3) Other information (I'm just curious about this): computer hardware, OS
ftinetti
 
Posts: 487
Joined: Wed Feb 10, 2010 2:44 pm

Re: fortran array, shared or reduction?

Postby ntlam » Mon Sep 12, 2011 1:58 pm

Ok, here is a piece of code:


!$omp parallel do &
!$omp private(n,i,j,kbond,lbond,XI,YI,ZI,XJ,YJ,ZJ,dx,dy,dz,dist,E0,E) &
!$omp shared(BOND,nbond,bontyp,delta,x,y,z,fx,fy,fz) &
!$omp reduction (+:ENG)
DO N=1,NBOND

I=BOND(N,1)
J=BOND(N,2)

IF(BONTYP(N)==1)THEN
KBOND=K_G
LBOND=L_G
ELSE
KBOND=K_P
LBOND=L_P
END IF

XI=X(I);YI=Y(I);ZI=Z(I)
XJ=X(J);YJ=Y(J);ZJ=Z(J)
DX=XI-XJ
DY=YI-YJ
DZ=ZI-ZJ

DIST=SQRT(DX**2+DY**2+DZ**2)
E0=KBOND*(DIST-LBOND)**2/2

XI=X(I)+DELTA
DX=XI-XJ
! DY=YI-YJ
! DZ=ZI-ZJ
DIST=SQRT(DX**2+DY**2+DZ**2)
E=KBOND*(DIST-LBOND)**2/2

!$omp CRITICAL
FX(I)=FX(I)-(E-E0)/DELTA
FX(J)=FX(J)+(E-E0)/DELTA
!$OMP END CRITICAL

XI=X(I)
YI=Y(I)+DELTA
! DX=XI-XJ
DY=YI-YJ
! DZ=ZI-ZJ
DIST=SQRT(DX**2+DY**2+DZ**2)
E=KBOND*(DIST-LBOND)**2/2

!$OMP CRITICAL
FY(I)=FY(I)-(E-E0)/DELTA
FY(J)=FY(J)+(E-E0)/DELTA
!$OMP END CRITICAL

YI=Y(I)
ZI=Z(I)+DELTA
! DX=XI-XJ
! DY=YI-YJ
DZ=ZI-ZJ

DIST=SQRT(DX**2+DY**2+DZ**2)

E=KBOND*(DIST-LBOND)**2/2
!$OMP CRITICAL
FZ(I)=FZ(I)-(E-E0)/DELTA
FZ(J)=FZ(J)+(E-E0)/DELTA
!$OMP END CRITICAL

ZI=Z(I)
ENG=ENG+E0

END DO
!$omp end parallel do

----------------------------

I use ifort, get different results for FX,FY,FZ when using option -openmp and when not.

I have Dell Precision T7500, Intel Xeon E5620, with Ubuntu 11.04 if this is a piece of info you want to know.

Thanks,

Lam
ntlam
 
Posts: 3
Joined: Mon Sep 12, 2011 9:27 am

Re: fortran array, shared or reduction?

Postby ftinetti » Mon Sep 12, 2011 4:23 pm

I do not see anything I could identify as a problem...

What do you mean by

I use ifort, get different results for FX,FY,FZ when using option -openmp and when not.

Remember that due to rounding and FP precision issues different order of operations leads to different results (though negligible most of the times).

HTH.
ftinetti
 
Posts: 487
Joined: Wed Feb 10, 2010 2:44 pm

Re: fortran array, shared or reduction?

Postby ntlam » Mon Sep 12, 2011 5:00 pm

I just meant I use intel compiler. And there is a small difference in my results when compiling in the serial mode compared to the parallel mode.

Maybe the small difference is due to the precision. Is there any way to increase the precision so I can verify this is the cause?

Thanks,
ntlam
 
Posts: 3
Joined: Mon Sep 12, 2011 9:27 am


Return to Using OpenMP

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 2 guests

cron