Juan José Gómez-Navarro's homepage

Fortran Timers

I wrote a very simple Fortran module to estimate the time elapsed between two lines of a program. It is very basic but allows fast and dirty benchmarks to help you estimate where the bottleneck of you program is. It basically consists of a wrapper for the system_clock built-in subroutine in standard fortran.

Implementation

The module is designed having in mind an OOP, and it is implemented in Fortran 2003. It provides the class Timer, and two methods, Tic and Toc. That is it. The state of a Timer object is defined by two variables: the rate of the CPU and the starting clock time, like this:

  type Timer
    private
    integer(KINT4) :: start, rate=-1
  contains
    procedure, public :: Tic, Tac
  end type Timer

The Tic method sets the state of these two variables in the state of the object:

  subroutine Tic(self)
    class (Timer), intent(inout) :: self
    integer(KINT4) :: start, rate

    call system_clock(count_rate=rate)
    call system_clock(start)
    self%start=start
    self%rate=rate
  end subroutine

The Toc method reads the current time and calculates, in serons, the time elapsed between the calls to Tic and Toc. It also prints out the result in the standard output. Beforehand it performs a basic check in case you are calling by mistake to Toc before Tic.

  subroutine Tac(self)
    class (Timer), intent(in) :: self
    integer(KINT4) :: finish

    if(self%rate<0) then
      print*, 'Call to ''Tac'' subroutine must come after call to ''Tic'''
      stop
    endif
    call system_clock(finish)
    print*, 'Elapsed time in seconds:', float(finish-self%start)/self%rate
  end subroutine

Example of application

Using this module is pretty simple and intuitive.

program testtimers
  use Timers
  implicit none

  type(Timer) :: crono1

  call crono1%Tic()

  ! some heavy stuff to be benchmarked

  call crono1%Toc()

end program testtimers

You can get this module checking out my github repositories. Let me know if you find a bug or figure out an easy improvement. However have in mind that the simplicity is one important aspect of the aim of this module.

Tags: fortran module

Categories: Computation

subscribe via atom