- Contrassegnare come Nuovo
- Preferito
- Iscriversi
- Disattiva notifiche
- Iscriversi a feed RSS
- Evidenziare
- Stampare
- Segnalare contenuto inappropriato
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 integer, parameter :: 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 integer, parameter :: 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,
Link copiato
- Contrassegnare come Nuovo
- Preferito
- Iscriversi
- Disattiva notifiche
- Iscriversi a feed RSS
- Evidenziare
- Stampare
- Segnalare contenuto inappropriato
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.
- Contrassegnare come Nuovo
- Preferito
- Iscriversi
- Disattiva notifiche
- Iscriversi a feed RSS
- Evidenziare
- Stampare
- Segnalare contenuto inappropriato
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
- Contrassegnare come Nuovo
- Preferito
- Iscriversi
- Disattiva notifiche
- Iscriversi a feed RSS
- Evidenziare
- Stampare
- Segnalare contenuto inappropriato
It is initialised:
do j = 1, np
i = 0
x(i) = 20.0
I overlooked that at first myself
- Contrassegnare come Nuovo
- Preferito
- Iscriversi
- Disattiva notifiche
- Iscriversi a feed RSS
- Evidenziare
- Stampare
- Segnalare contenuto inappropriato
The above initializes x(1:np) but not x(0)
oops i=0
Jim Dempsey
- Contrassegnare come Nuovo
- Preferito
- Iscriversi
- Disattiva notifiche
- Iscriversi a feed RSS
- Evidenziare
- Stampare
- Segnalare contenuto inappropriato
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.
- Contrassegnare come Nuovo
- Preferito
- Iscriversi
- Disattiva notifiche
- Iscriversi a feed RSS
- Evidenziare
- Stampare
- Segnalare contenuto inappropriato
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.
- Contrassegnare come Nuovo
- Preferito
- Iscriversi
- Disattiva notifiche
- Iscriversi a feed RSS
- Evidenziare
- Stampare
- Segnalare contenuto inappropriato
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.
- Contrassegnare come Nuovo
- Preferito
- Iscriversi
- Disattiva notifiche
- Iscriversi a feed RSS
- Evidenziare
- Stampare
- Segnalare contenuto inappropriato
Thank you very much for your reply and the workaround!
Would you submit a request to fix this issue?
- Contrassegnare come Nuovo
- Preferito
- Iscriversi
- Disattiva notifiche
- Iscriversi a feed RSS
- Evidenziare
- Stampare
- Segnalare contenuto inappropriato
I filed a bug report, CMPLRIL0-34271. I'll keep you posted on its progress to a fix.
- Contrassegnare come Nuovo
- Preferito
- Iscriversi
- Disattiva notifiche
- Iscriversi a feed RSS
- Evidenziare
- Stampare
- Segnalare contenuto inappropriato
- Contrassegnare come Nuovo
- Preferito
- Iscriversi
- Disattiva notifiche
- Iscriversi a feed RSS
- Evidenziare
- Stampare
- Segnalare contenuto inappropriato
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.
- Contrassegnare come Nuovo
- Preferito
- Iscriversi
- Disattiva notifiche
- Iscriversi a feed RSS
- Evidenziare
- Stampare
- Segnalare contenuto inappropriato
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) )
- Contrassegnare come Nuovo
- Preferito
- Iscriversi
- Disattiva notifiche
- Iscriversi a feed RSS
- Evidenziare
- Stampare
- Segnalare contenuto inappropriato
Thank you all Barbara and mecej4, I appreciate your endeavor!

- Iscriversi a feed RSS
- Contrassegnare la discussione come nuova
- Contrassegnare la discussione come letta
- Sposta questo Discussione per l'utente corrente
- Preferito
- Iscriversi
- Pagina in versione di stampa