views:

867

answers:

3

I am running a loop in a Fortran 90 program that outputs numerical values to an output file for each iteration of the loop. The problem is that the output is not saved to the file but every so many steps. How do I get it to flush each step?

Example code:

open(unit=1,file='output')

do i = 1, 1000
 write(1,*) i
end do

close(unit=1)

Thanks in advance.

+2  A: 

You need to make the output unbuffered. Try setting the GFORTRAN_UNBUFFERED_ALL environment variable to 'y', 'Y' or 1.

ire_and_curses
Would I do this from bash as follows:GFORTRAN_UNBUFFERED_ALL='y'export $GFORTRAN_UNBUFFERED_ALL?Just curious?
Patrick Hogan
Yes, although you don't need the '$' in the export line. This will work until you exit the current shell. If you want this behaviour permanently you may want to add those lines to your `.bashrc` file.
ire_and_curses
I have tried the following prescription and I have typed:GFORTRAN_UNBUFFERED_ALL='y'export GFORTRAN_UNBUFFERED_ALLecho $GFORTRAN_UNBUFFERED_ALLEcho printed the proper value.I have tried this with 'y','Y', and 1. None of the solved the problem. Thank you for the suggestion, though.
Patrick Hogan
A: 

Hi

The other way, if gfortran implements it, is to call the non-standard subroutine flush. Not all compilers do implement this.

Cheers

Mark

High Performance Mark
`FLUSH` as a subroutine (as in `call FLUSH()`) is nonstandard, but the `FLUSH` statement is valid Fortran 2003: `FLUSH (10)`
FX
A: 

When I need to flush, I just close the file and reopen. This is clumsy and slow, but I don't know of a better way in fortran 90 that'll work with all compilers.

gusgw