2

I want to time some subroutines. Here is the template I use to write the name and duration of execution:

SUBROUTINE get_sigma_vrelp

  ...declarations...

  real(8) :: starttime, endtime
  CHARACTER (LEN = 200) timebuf
  starttime = MPI_Wtime()

  ...do stuff...

  endtime = MPI_Wtime()
  write (timebuf, '(30a,e20.10e3)') 'get_sigma_vrelp',endtime-starttime
  call pout(timebuf)
END SUBROUTINE get_sigma_vrelp

And here is a sample output:

(thread  4):get_sigma_vrelp �>  

Why is a strange character printed instead of a numerical value for endtime-starttime? Incidentally, pout() simply writes the buffer to a process-specific file in a threadsafe manner. It shouldn't have anything to do with the problem, but if there is nothing else here that would cause the erroneous output then I can post its body.

1 Answers1

3

You have it the wrong way round! The line should read

write (timebuf, '(a30,e20.10e3)') 'get_sigma_vrelp',endtime-starttime

This way, you expect one string that is 30 characters long (a30) instead of 30 strings of arbitrary length (30a). The write statement does not receive characters after the first string, but the corresponding bytes of the float. Hence the garbage.

Your character literal is only 15 chars long, so you could write the line as

write (timebuf, '(a15,e20.10e3)') 'get_sigma_vrelp',endtime-starttime

or let the compiler decide the length on its own:

write (timebuf, '(a,e20.10e3)') 'get_sigma_vrelp',endtime-starttime
Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68