fsiesta_mpi.F90: Reopening /dev/stdout doesn't work with position="append" with gfortran

Bug #1844530 reported by Jonas L. B.
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Siesta
New
Undecided
Unassigned

Bug Description

As the title says: I am trying to use the fsiesta_mpi.F90 interface to use Siesta as a subroutine. When compiled with gfortran (it does not happen with ifort), I get a crash with the error "Fortran runtime error: Illegal seek".

The particular error occurs at line 501 in fsiesta_mpi.F90, where the code reads:

```
#ifdef MPI
! Go back to parent directory and reset output file
  call chdir('..')
  close(unit=6)
  open(unit=6, file=trim(mainOutFile), position='append') ! (this is line 501)
#endif
```
It closes the temporary output file and reopens the previous one (which is stdout, at least in my case). I can reproduce it in a minimal fortran example:

```
program MyTest
implicit none
close(unit=6)
open(unit=6, file="/dev/stdout", position="append")
write(6, *) "testtest"
end program
```
which shows the same behaviour of crashing when compiled with gfortran but not ifort. Removing the position="append" part works, and I have done that for myself now, but I guess that if an actual file is specified as mainOutFile, it'd be overwritten. So perhaps another solution is needed.

Revision history for this message
Alberto Garcia (albertog) wrote :

Which version of gfortran are you using?
Your test runs fine on my Mac with

  $ gfortran --version
  GNU Fortran (Homebrew GCC 7.2.0) 7.2.0

Revision history for this message
Jonas L. B. (jonaslb) wrote :

I am using 9.1.0
    $ gfortran --version
    GNU Fortran (GCC) 9.1.0

Revision history for this message
Jonas L. B. (jonaslb) wrote :

I can also reproduce on gfortran 8.3.0.

Revision history for this message
Jonas L. B. (jonaslb) wrote :

I later also reproduced it on gfortran 6.4 and 7.3, so this must be a platform dependent thing (I used different Linuxes).
A fix that should work both when main output is a file or device, is to use `call fseek(6,0,2)` rather than `position="append"`.
I can try to send an MR with this small fix on monday when everything is moved to Gitlab :)

Revision history for this message
Nick Papior (nickpapior) wrote :

I think we should solve this in another way than using seek.

Is this on HPC or Niflheim?

PS. I think the correct way would be to use iostat to check if the first append fails, if so do a compatible thing that might not be optimal.

Revision history for this message
Jonas L. B. (jonaslb) wrote :

It happens on HPC, Niflheim and my own Ubuntu PC.

I tested with iostat and it is set to 29, so we could check for that of course. But seeking seems to work regardless, so why not just do that?

Revision history for this message
Nick Papior (nickpapior) wrote :

fseek is not in the fortran standard. :(

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.