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

Non-advancing write to console

David_Kinniburgh
Beginner
373 Views
I am trying to make a simple progress bar from a line of dots advancing across the screen. The following program tests what I want to do. It works as I want it to with gfortran but with ivf 11.1.051 the line of dots appears to be buffered and a full line of dots is output after every thousand iterations rather than one dot at a time as I would like. I have tried opening unit 6 with BUFFERED='NO' to no avail - the documentation for OPEN refers only to files not to the pre-connected console. Any suggestions, please?


program test_advance
implicit none
integer :: i, n = 5000
integer :: now, start, count_rate, count_max, waitms = 10

call system_clock(start, count_rate, count_max)
now = start

! best if console width is greater than 103 characters wide
! one dot for every 10 iterations
! new line for every 1000 iterations

do i = 1, n
if (mod(i,10).eq.0) write (*,'(".")',advance='no')
if (mod(i,1000).eq.0) write (*,'(1X,I0,"k")') i/1000
! slow down - wait in msec
do while (now-start .lt. waitms)
call system_clock(now, count_rate, count_max)
enddo
start = now
enddo

end program test_advance

0 Kudos
4 Replies
Steven_L_Intel1
Employee
373 Views
It's nice that gfortran does this for you but the standard doesn't require it and our implementation doesn't flush the partial line. You can get the effect with Fortran carriage control and a combination of the '+' carriage control character and the $ edit descriptor, but it's messy. I've already suggested to our library folks that they do what gfortran does here.
0 Kudos
IanH
Honored Contributor II
373 Views
It would be nice if FLUSH(OUTPUT_UNIT) gave the runtime an appropriate nudge.
0 Kudos
mecej4
Honored Contributor III
373 Views
That would work, but cause some inconvenience since the flush statements would need to be written after every point where a prompt was written out and a flush is needed.

An option to open the console unit UNBUFFERED would not have this disadvantage, but performance would suffer if the user intended to use a mix of buffered and unbuffered output statements.
0 Kudos
David_Kinniburgh
Beginner
373 Views
Ah, thanks Steve. I thought ADVANCE had got rid of the necessity for the carriage control stuff.
Actually the solution is not that messy

if (mod(i,10).eq.0) write (*,'("."$)')
if (mod(i,1000).eq.0) write (*,'(1X,I0,"k")') i/1000

(\ can be used instead of $)

That also works with gfortran though the timings are different because of different count_rate's. I should have divided the count by the count_rate to get proper timings.
0 Kudos
Reply