Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!
26754 Discussions

Error when use variables in Fortran module

cheng__bin
Beginner
153 Views

I am encountering a very very strange error when I use variables in Fortran module with intel/18.0.2.

It's a quite simple code. Each column of 'F' is calculated by each column of 'X'. They are both variables in module global. Below is the code.

In global.f90: declaration of the module.

module global
implicit none
!  Parameters
real(8),parameter :: GravConst = 6.674184D-11
real(8),parameter :: PI = 3.141592653589793D0
integer :: N
real(8) :: X(3,2)
real(8) :: F(3,2)
real(8) :: omega
real(8) :: density
end module

In main.f90: calculation of 'F'.

program main
use global
implicit none

integer I,J,K
N = 2
F = 0.0D0
X = 0.0D0
omega = 1.0
density = 0.0

!  read positions
open (1,FILE="pos.txt")
do I = 1,N
    read(1,*) (X(K,I),K=1,3)
end do

write(*,*) 'X:',(X(J,2),J=1,3)
write(*,*) 'F:',(F(J,2),J=1,3)
write(*,*) 'F(1,2):',omega*omega*X(1,2)- 4.0D0/3.0D0*PI*GravConst*density*X(1,2)
write(*,*) 'F(2,2):',omega*omega*X(2,2)- 4.0D0/3.0D0*PI*GravConst*density*X(2,2)
do I = 1,N
    !yorp force
    do K = 1,2
        F(K,I) = F(K,I) + omega*omega*X(K,I)
    end do  
    !gravity      
    do K = 1,3
        F(K,I) = F(K,I) - 4.0D0/3.0D0*PI*GravConst*density*X(K,I)
    end do
end do
write(*,*) (F(J,2),J=1,3)
end

F(1,2) and F(2,2) calculated directly are printed before the loop. Note that omega=1, and density=0, so F(1,2) and F(2,2) should equal to X(1,2) and X(2,2), respectively. After the loop, the final values of F(:,2) are printed. Here is the result using intel/18.0.2. It's obvious that the result calculated by the loop is wrong.

X: -4.97716000000000       0.897938000000000       -11.4228000000000
F: 0.000000000000000E+000  0.000000000000000E+000  0.000000000000000E+000
F(1,2): -4.97716000000000
F(2,2): 0.897938000000000
-4.69400000000000       -4.97716000000000    0.000000000000000E+000

It seem like something strange happening in the loop... But if I change the order of the calculation in the loop, the result would be correct.

do I = 1,N 
    !gravity      
    do K = 1,3
        F(K,I) = F(K,I) - 4.0D0/3.0D0*PI*GravConst*density*X(K,I)
    end do
    !yorp force
    do K = 1,2
        F(K,I) = F(K,I) + omega*omega*X(K,I)
    end do 
end do

X: -4.97716000000000       0.897938000000000       -11.4228000000000
F: 0.000000000000000E+000  0.000000000000000E+000  0.000000000000000E+000
F(1,2): -4.97716000000000
F(2,2): 0.897938000000000
-4.97716000000000       0.897938000000000    0.000000000000000E+000

If I use local variables rather variables in module, or use intel/15.0.6, the result would also be correct.

 

Below are pos.txt and makefile I used.

2.64530000  -7.20517000     -4.69400000 
-4.97716000     0.89793800  -11.42280000

# Start of the makefile
COMPILER=ifort -O2
DEMBody: global.o main.o
    $(COMPILER)  -o DEMBody global.o main.o
global.mod: global.o global.f90
    $(COMPILER)  -c global.f90
global.o: global.f90
    $(COMPILER)  -c global.f90
main.o: global.mod main.f90
    $(COMPILER)  -c main.f90
clean:
    rm global.mod global.o main.o
# End of the makefile

 

Or, you could try this input. It would be more clear.

    X(1,1) = 1.0
    X(2,1) = 2.0
    X(3,1) = 3.0
    X(1,2) = 4.0
    X(2,2) = 5.0
    X(3,2) = 6.0

You would get a confusing result for 'F':

    F(:,1): 1.000        2.000       0.000E+000
    F(:,2): 3.000        4.000       0.000E+000

The correct result should be

    F(:,1): 1.000        2.000       0.000E+000
    F(:,2): 4.000        5.000       0.000E+000

It seems that the element of X/F has been moved by one cell.

 

 

0 Kudos
3 Replies
gib
New Contributor II
153 Views

This is indeed strange.  My old compiler (11.075) gives the correct results.

mecej4
Black Belt
153 Views

This appears to be an optimizer bug. Compiling with the /Od option leads to correct behavior.

Please report the bug to https://supporttickets.intel.com/servicecenter?lang=en-US .

cheng__bin
Beginner
153 Views

Yes, I tried '-fp-model strict' and got correct result. It seems that compilers after intel/17.0.7 has this bug. 

mecej4 wrote:

This appears to be an optimizer bug. Compiling with the /Od option leads to correct behavior.

Please report the bug to https://supporttickets.intel.com/servicecenter?lang=en-US .

Reply