user1638145
user1638145

Reputation: 2049

ifort optimization O3 leads to inconsistency

I have a problem with my fortran code when using O3 optimization: The values calculated for the norm of an array changes with and without O3, and is incorrect with O3. The following shows a minimal example of my code

program main
use wavefunction
implicit none

integer(I4B) :: Na, Nb, Npes
complex(DPC), ALLOCATABLE, DIMENSION(:,:,:) :: phi 
real(DP), ALLOCATABLE, DIMENSION(:) :: normPerPes1
real(DP) :: sigma

integer(I4B) :: i,j
Na=100
Nb=100
Npes=4

ALLOCATE(phi(Na,Nb,Npes), normPerPes1(Npes))

sigma=10
phi=(0.D0,0.D0)
do i=1,Na
   do j=1,Nb
      !gaussian on pes 1
      phi(i,j,1)=1.D0/(sigma**2*2.D0*pi)*exp(-(dble(i-50)**2+dble(j-50)**2/(2.D0*sigma**2))
   end do
end do

!total norm
write(*,*) norm(Na,Nb,Npes,phi)
!norm on each pes
CALL normPerPes(Na,Nb,Npes,phi,NormPerPes1)
write(*,*) NormPerPes1

end program main

which uses the following module

module wavefunction
use nrtype
implicit none

contains
  function norm(Na,Nb,Npes, phi)
  implicit none
  INTEGER(I4B), INTENT(IN) :: Na, Nb, Npes
  COMPLEX(DPC), INTENT(IN) :: phi(Na,Nb,Npes)
  REAL(DP) :: norm

  INTEGER(I4B) :: i,j, pesNr

  norm=0.D0
  do i=1,Na
     do j=1,Nb
        do pesNr=1, Npes
           norm=norm+abs(phi(i,j,pesNr))**2
        end do
     end do
  end do

end function norm
!----------------------------------------------------------
subroutine normPerPes(Na, Nb, Npes, phi, normPerPes1)
  IMPLICIT none

  REAL(DP) :: normPerPes1(Npes)
  INTEGER(I4B), INTENT(IN) :: Na, Nb, Npes
  COMPLEX(DPC), INTENT(IN) :: phi(Na,Nb,Npes)
  INTEGER(I4B):: i,j,pesNr

  normPerPes1=0.0d0
  do i=1,Na
    do j=1,Nb
       do pesNr=1,Npes
          normPerPes1(pesNr)=normPerPes1(pesNr)+abs(phi(i,j,pesNr))**2
       end do
     end do
  end do
  return
end subroutine normPerPes
!-----------------------------------------------------------
end module wavefunction

if I compile with the following Makefile

# compiler
FC = ifort
# flags
FFLAGS = #  -O3

main: main.o nrtype.o wavefunction.o
main.o: main.f90 nrtype.o wavefunction.o
wavefunction.o: wavefunction.f90 nrtype.o
nrtype.o: nrtype.f90

%: %.o
    $(FC) $(FFLAGS) -o dynamic  $^ $(LDFLAGS)
%.o: %.f90
$(FC) $(FFLAGS) -c $<

clean:
rm -f *.o *.mod *_genmod.f90

I get the following correct output:

7.957747154568253E-004

7.957747154568242E-004 0.000000000000000E+000 0.000000000000000E+000 0.000000000000000E+000

However, if I use O3 then I obtain the following incorrect result

7.957747154568253E-004

1.989436788642788E-004 0.000000000000000E+000 0.000000000000000E+000 0.000000000000000E+000

This looks to me as there were something seriously fishy in my code, but I can't seem to find the problem. Thank you for your help!

Upvotes: 0

Views: 353

Answers (1)

user1638145
user1638145

Reputation: 2049

As confirmed by Intel (see https://software.intel.com/en-us/forums/topic/516819), this is a problem of the compiler version used ( Composer XE 2013 SP1 initial release (pkg. 080) ). They claim that upgrading to Update 2 or 3 helps - I was not able to try yet. In the mean while a workaround is to forget about O3 and use O2 optimization.

Upvotes: 1

Related Questions