Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
27326 Discussions

-O2, -O3 gives obvious wrong results, simple, 30 lines MWE attached.

CRquantum
New Contributor I
1,123 Views

Dear all, 

 

I am a problem about Intel OneAPI Fortran, the version I am using is 2021.3. 

The problem is repeatable on both linux and windows. 

gfortran does not have problem. 

 

Thing is, with -O3 or -O2 compile flag, the output array xout is wrong, it gives zeros, which should not be zeros. 

However, with -O0 flag, result is correct. 

 

Furthermore, at  line 31, if I set np<=8, result is correct. But >8 result is wrong.

Also, if I add some write statement, the result become correct again.

 

MWE is about 30 lines below. 

 

The problem is at line 17, 

 

      call rk4_ti_step_mod3 ( x(i-1), x(i) )

 

somehow the -O2 or -O3 flag seems decided that they just do nothing here.

 

Could anyone have a look and identify if it is a compiler bug or not? 

 

Thank you very much in advance!

 

 

 

    module tests 
    implicit none
    contains
    subroutine test01
    integerparameter :: n = 10, np = 10
    integer :: i, itot, istart, istep, j, k
    real  :: x(0:n), xout(5,np)
    itot = n
    istart = itot/5
    istep = istart
    do j = 1, np
	    i = 0
        x(i) = 20.0
	    do i = 1, n
	        call rk4_ti_step_mod3 ( x(i-1), x(i) ) ! check n=10 bug.
	        !write ( *, '(2x,i8,2x,f14.6,2x,g14.6)' ) i, x(i-1), x(i)
	    end do
	    !write (6,'(''x = '',t20, 5(f15.7,1x))') x(istart:itot:istep)
	    xout(1:5:1,j) = x(istart:itot:istep)
    enddo    
    write (6,'(5(f15.7,1x))') xout
    return
    end subroutine test01
    subroutine rk4_ti_step_mod3 ( x, xstar ) 
    real :: x, xstar
    xstar = x 
    !write(6,*) 'x', x 
    xstar = xstar + x
    return
    end subroutine rk4_ti_step_mod3
    end module tests    
    program main
    use tests
    implicit none  
    call test01
    stop ('Program end normally.')
    end program main

 

On the other hand, if I delete the only one module tests in the above code, and place the two subroutines in the main program, the problem is gone. Just do not know why. 

 

See code below, without using module, it works. 

 

    program main
    implicit none  
    call test01
    stop ('Program end normally.')
    contains
    subroutine test01
    integerparameter :: n = 10, np = 10
    integer :: i, itot, istart, istep, j, k
    real  :: x(0:n), xout(5,np)
    itot = n
    istart = itot/5
    istep = istart
    do j = 1, np
	    i = 0
        x(i) = 20.0
	    do i = 1, n
	        call rk4_ti_step_mod3 ( x(i-1), x(i) ) ! check n=10 bug.
	        !write ( *, '(2x,i8,2x,f14.6,2x,g14.6)' ) i, x(i-1), x(i)
	    end do
	    !write (6,'(''x = '',t20, 5(f15.7,1x))') x(istart:itot:istep)
	    xout(1:5:1,j) = x(istart:itot:istep)
    enddo    
    write (6,'(5(f15.7,1x))') xout
    return
    end subroutine test01
    subroutine rk4_ti_step_mod3 ( x, xstar ) 
    real :: x, xstar
    xstar = x 
    !write(6,*) 'x', x 
    xstar = xstar + x
    return
    end subroutine rk4_ti_step_mod3
    end program main 
 
	
	
	

 

A even concise 20s line MWE which reproduce this -O2 or -O3 issue can be found here, 

https://fortran-lang.discourse.group/t/why-a-simple-code-intel-fortran-o3-o2-gives-wrong-results/190...

0 Kudos
10 Replies
Arjen_Markus
Valued Contributor III
1,073 Views

I built the first program with Intel oneAPI 2021.01 as well as Intel Fortran 18 and got even weirder results: unless I specify -debug, the output of the program is all zeroes. -O0 also gives zeroes. I do not see anything wrong with the program myself.

jimdempseyatthecove
Black Belt
1,041 Views

x(0) is never initialized. You cannot assume uninitialized variables are 0.

I have not tried your program with initializing x(0) to 0.0 so I cannot say their are further errors.

rk4_ti_step_mod3 ( x, xstar ) contains
xstar = xstar + x with x dummy referencing x(0) when i==1

IOW reading an uninitialized variable... then passing it on to the next cell in the array (propagating junk data).

 

Jim Dempsey

Arjen_Markus
Valued Contributor III
1,034 Views

It is initialised:

    do j = 1, np
            i = 0
        x(i) = 20.0
 

I overlooked that at first myself

jimdempseyatthecove
Black Belt
1,025 Views

The above initializes x(1:np) but not x(0)

oops i=0

Jim Dempsey

mecej4
Black Belt
1,002 Views

I can reproduce the error with IFort 2013SP1, IFort 16.0.8 and Ifort 19.1.3 using just /Ot, generating 64-bit code for the 25-line MWE in CRquantum's link to fortran-lang.discourse.group.

What I can see in the assembly code is that the generated code for subroutine rk4_ti_step_mod3 is correct, but that subroutine is never called. Instead,  bad inline code is generated in the caller.

Barbara_P_Intel
Moderator
893 Views

I compiled like this and ran.  Are these the answers you expect?

$ ifort -fno-inline forum.f90
$ a.out
     80.0000000     320.0000000    1280.0000000    5120.0000000   20480.0000000
     80.0000000     320.0000000    1280.0000000    5120.0000000   20480.0000000
     80.0000000     320.0000000    1280.0000000    5120.0000000   20480.0000000
     80.0000000     320.0000000    1280.0000000    5120.0000000   20480.0000000
     80.0000000     320.0000000    1280.0000000    5120.0000000   20480.0000000
     80.0000000     320.0000000    1280.0000000    5120.0000000   20480.0000000
     80.0000000     320.0000000    1280.0000000    5120.0000000   20480.0000000
     80.0000000     320.0000000    1280.0000000    5120.0000000   20480.0000000
     80.0000000     320.0000000    1280.0000000    5120.0000000   20480.0000000
     80.0000000     320.0000000    1280.0000000    5120.0000000   20480.0000000
Program end normally.

 Thanks, @mecej4 , for the inline tip. 

 

Barbara_P_Intel
Moderator
817 Views

I also compiled with ifx. I get the same answers I reported above.

So you have two workarounds.

(1) Compile with this compiler option

$ ifort -fno-inline forum.f90

(2) Compile with ifx. .o and .mod files are interchangeable between the compilers as long as you don't use -ipo.


CRquantum
New Contributor I
713 Views

Thank you very much for your reply and the workaround!

Would you submit a request to fix this issue? 

Barbara_P_Intel
Moderator
682 Views

I filed a bug report, CMPLRIL0-34271. I'll keep you posted on its progress to a fix.



CRquantum
New Contributor I
653 Views
Reply