Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29261 ディスカッション

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

CRquantum
新規コントリビューター I
4,301件の閲覧回数

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/1903/7?u=crquantum

0 件の賞賛
13 返答(返信)
Arjen_Markus
名誉コントリビューター II
4,251件の閲覧回数

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
名誉コントリビューター III
4,219件の閲覧回数

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
名誉コントリビューター II
4,212件の閲覧回数

It is initialised:

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

I overlooked that at first myself

jimdempseyatthecove
名誉コントリビューター III
4,203件の閲覧回数

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

oops i=0

Jim Dempsey

mecej4
名誉コントリビューター III
4,180件の閲覧回数

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
従業員
4,071件の閲覧回数

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
従業員
3,995件の閲覧回数

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
新規コントリビューター I
3,891件の閲覧回数

Thank you very much for your reply and the workaround!

Would you submit a request to fix this issue? 

Barbara_P_Intel
従業員
3,860件の閲覧回数

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



CRquantum
新規コントリビューター I
3,831件の閲覧回数
Barbara_P_Intel
従業員
3,077件の閲覧回数

The compiler developers looked this case over carefully and decided not to fix it due to a high risk of a regression. Please use one of the 2 workarounds. I suggest using ifx as that is the Intel Fortran compiler of the future.



mecej4
名誉コントリビューター III
3,059件の閲覧回数

Here is another work-around, one that does not depend on changing compiler flags:

Replace the line

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

by

call rk4_ti_step_mod3 ( (x(i-1)), x(i) )
CRquantum
新規コントリビューター I
3,054件の閲覧回数

Thank you all Barbara and mecej4, I appreciate your endeavor! 

返信