3

I am using the function system_clock with Fortran90 (compiled with gfortran) in the following way :

   ! Variables for clock
   integer count_0, count_1
   integer count_rate, count_max
   double precision time_init, time_final, elapsed_time

   ! Starting time
   call system_clock(count_0, count_rate, count_max)
   time_init = count_0*1.0/count_rate

   .... Main code

   ! Ending time
   call system_clock(count_1, count_rate, count_max)
   time_final = count_1*1.0/count_rate
   ! Elapsed time
   elapsed_time = time_final - time_init

   ! Write elapsed time
   write(*,1003) int(elapsed_time),elapsed_time-int(elapsed_time)

1003 format('  Wall Clock = ',i0,f0.9)

I would like to know if I use correctly this function. Indeed, I didn't specify a value for count_rate and count_max but I guess there are default values. Moreover, It seems that I have to take into account the case where count_0 or count_1 are over count_max values, doesn't it. As you can see, for pretty formatting, I have split the seconds and the decimal part of elapsed time.

  • Please use tag [tag:fortran] for all Fortran questions. Did you encounter any problem? Was there any error message? Are the results wrong or correct? – Vladimir F Героям слава Oct 09 '17 at 05:55
  • `count_0` and `count_1` will never be greater than `count_max`. – francescalus Oct 09 '17 at 08:15
  • For GFortran specific information about SYSTEM_CLOCK, see https://gcc.gnu.org/onlinedocs/gfortran/SYSTEM_005fCLOCK.html – janneb Oct 11 '17 at 13:38
  • 1
    Also note that all the arguments to SYSTEM_CLOCK are INTENT(OUT). Which means that the intrinsic will never read from the arguments, only overwrite whatever was there before. – janneb Oct 11 '17 at 13:39

1 Answers1

3

It looks correct from reading. However it is very difficult to find out correctness of any code without seeing the output.

I recommend using larger integers (integer(int64)). int64 is defined in module iso_fortran_env, in Fortran 90 you can use selected_int_kind. It is compiler specific, but you are likely to get higher count_max and finer count_rate with larger integers. At least that happens for common compilers like gfortran and Intel.

I don't think you can monitor count and compare it with count_max usefully. And I never had the need when I used the larger integers as shown above. I can imagine some way (comparing with count_max/2), but it would be clumsy. You cannot restart the counter anyway.

What you can do is to write a small program that will print the maximum times

use iso_fortran_env, only: int32, int64, real64

integer(int32) :: count_max, count_rate

call system_clock(count_max=count_max, count_rate=count_rate)

write(*,*) "Maximum time:", real(count_max, real64) / count_rate

end

Try both integer(int32) and integer(int64) and also just integer above and observe the maximum times possible with those choices for your compiler.

For me with gfortran on Linux x86_64 the 32-bit integers give me almost 25 days of maximum time, while the 64-bit integers allow 292 years. The clock resolution is much finer with 64-bit integers too (1 ms vs. 1 ns).

  • -@Vladimir F: the code seems to give correct runtimes, I just wanted to get validation for a righ use of this function. My maximum runtimes are equal to few hours (approximately between 1 and 2 hours). I am going to see the `count_max' value with int32 and int64. Regards –  Oct 09 '17 at 12:31
  • It is normally almost impossible to guarantee correctness of any non-trivial code. But the call looks to be correct. – Vladimir F Героям слава Oct 11 '17 at 11:14