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.

0

1 Answer 1

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
2
  • In your third example, should a15 be A?
    – Davislor
    Oct 23, 2015 at 20:24
  • No, a is fine there. It simply means use the length of the supplied character constant or variable.
    – Ian Bush
    Oct 25, 2015 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.