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

runtime I/O error in recursive function.

woshiwuxin
Novice
2,069 Views
I have a piece of Fortran 90 code which uses a recursive function for testing purpose.
I use both ifort (v 11.1) and gfortran (v 4.3.2) on an AMD64 Debian/Linux 5.0 machine.
The source code is shown below, and it's quite simple --- an integer "n" is gradually reduced until "n==1". Moreover, the intermediate results are also printed onto the standard output. However, The binary obtained by ifort would produce nothing, but an recursive I/O error.
Source code:
[fortran]program ex
  implicit none
  integer::n=4
  integer,external::reduce
  write(*,*) n, reduce(n)
  stop
end program ex

recursive function reduce(n) result(ans)
  implicit none
  integer::n,ans
  if (n==1) then
    ans=1
    write(*,*) "n=", n
    return
  endif
  ans=reduce(n-1)
  write(*,*) "n=", n
  return
end function reduce[/fortran]
Execution of binary compiled by gfortran produces:
[bash]           4 n=           1
 n=           2
 n=           3
 n=           4
           1
[/bash]
Execution of binary compiled by ifort produces:
[bash]forrtl: severe (40): recursive I/O operation, unit -1, file unknown
Image              PC                Routine            Line        Source             
a.out              000000000047303D  Unknown               Unknown  Unknown
a.out              0000000000471B45  Unknown               Unknown  Unknown
a.out              0000000000448C19  Unknown               Unknown  Unknown
a.out              00000000004359BF  Unknown               Unknown  Unknown
a.out              00000000004124D3  Unknown               Unknown  Unknown
a.out              0000000000402D15  Unknown               Unknown  Unknown
a.out              0000000000402C68  Unknown               Unknown  Unknown
a.out              0000000000402C68  Unknown               Unknown  Unknown
a.out              0000000000402B7A  Unknown               Unknown  Unknown
a.out              0000000000402ADC  Unknown               Unknown  Unknown
libc.so.6          00007FD7289AD1A6  Unknown               Unknown  Unknown
a.out              00000000004029D9  Unknown               Unknown  Unknown[/bash]
The problem could be remedied by replacing "write(*,*)" with "write(6,*)" ineither themain program or the recursive function reduce.
I personally think this might be a bug in ifort. Any comments are welcome.
0 Kudos
1 Solution
Steven_L_Intel1
Employee
2,069 Views
It's not a bug in ifort. The Fortran standard prohibits starting an I/O operation on a unit while another operation on that same unit is in progress. gfortran evidently has an extension which allows you to do this, Intel Fortran does not.

What I would recommend is replacing the use of reduce(n) in the main program with an assignment to a variable and then print the variable.

View solution in original post

0 Kudos
2 Replies
Steven_L_Intel1
Employee
2,070 Views
It's not a bug in ifort. The Fortran standard prohibits starting an I/O operation on a unit while another operation on that same unit is in progress. gfortran evidently has an extension which allows you to do this, Intel Fortran does not.

What I would recommend is replacing the use of reduce(n) in the main program with an assignment to a variable and then print the variable.
0 Kudos
woshiwuxin
Novice
2,069 Views

Thank you, Steve! Your solution works!

0 Kudos
Reply