timer Derived Type

type, public :: timer

A timer instance stores private start times and state flags for the enabled timing backends. The most recent successful measurement from each backend is exposed through a public result component: elapsed_time, cpu_time, elapsed_dtime, and, when enabled, omp_time and mpi_time.

Start and stop calls must be matched by backend. For example, timer_start must be followed by timer_stop, and ctimer_start must be followed by ctimer_stop. Calling a stop routine before its matching start routine reports an error and leaves the stored result unchanged.


Components

Type Visibility Attributes Name Initial
real(kind=rk), public :: cpu_time = 0.0_rk

Most recent successful CPU-time measurement in seconds.

real(kind=rk), public :: elapsed_dtime = 0.0_rk

Most recent successful date_and_time elapsed time in seconds.

real(kind=rk), public :: elapsed_time = 0.0_rk

Most recent successful system_clock elapsed time in seconds.

integer(kind=ik), private :: clock_max = 0_ik

Cached maximum system_clock count used to handle one counter wrap.

integer(kind=ik), private :: clock_rate = 0_ik

Cached system_clock count rate in ticks per second.

integer(kind=ik), private :: clock_start = 0_ik

system_clock count captured at the start of the wall-clock interval.

real(kind=rk), private :: cpu_start = 0.0_rk

CPU time, in seconds, captured by ctimer_start.

real(kind=rk), private :: dtime_start_utc = 0.0_rk

UTC epoch seconds computed from date_and_time at dtimer_start.

logical, private :: is_cpu_started = .false.

True after ctimer_start and before a successful ctimer_stop.

logical, private :: is_dtime_started = .false.

True after dtimer_start and before a successful dtimer_stop.

logical, private :: is_paused = .false.

True after timer_pause and before a matching timer_resume.

logical, private :: is_started = .false.

True after timer_start and before a successful timer_stop.

integer(kind=ik), private :: pause_start = 0_ik

system_clock count captured when the wall-clock timer is paused.

integer(kind=ik), private :: paused_ticks = 0_ik

Total paused duration, in system_clock ticks, excluded from the result.


Type-Bound Procedures

procedure, public :: ctimer_start

Start a CPU-time measurement.

  • private subroutine ctimer_start(this)

    Author
    Seyed Ali Ghasemi
    License
    BSD 3-Clause

    Start the CPU-time timer.

    Read more…

    Arguments

    Type IntentOptional Attributes Name
    class(timer), intent(inout) :: this

    Timer instance whose CPU-time state is initialized.

procedure, public :: ctimer_stop

Stop a CPU-time measurement and update cpu_time.

  • private subroutine ctimer_stop(this, nloops, message, print, color, rfmt)

    Author
    Seyed Ali Ghasemi
    License
    BSD 3-Clause

    Stop the CPU-time timer and store elapsed CPU seconds.

    Read more…

    Arguments

    Type IntentOptional Attributes Name
    class(timer), intent(inout) :: this

    Timer instance previously started with ctimer_start.

    integer, intent(in), optional :: nloops

    Optional positive loop count used to compute average CPU time.

    character(len=*), intent(in), optional :: message

    Optional output label; defaults to CPU time:.

    logical, intent(in), optional :: print

    Optional output switch; defaults to .true..

    character(len=*), intent(in), optional :: color

    Optional FACE foreground color used for printed output.

    character(len=*), intent(in), optional :: rfmt

    Optional Fortran real edit descriptor used for printed output.

procedure, public :: ctimer_write

Append cpu_time to a text file.

  • private subroutine ctimer_write(this, file_name)

    Author
    Seyed Ali Ghasemi
    License
    BSD 3-Clause

    Append the last CPU-time result to a text file.

    Read more…

    Arguments

    Type IntentOptional Attributes Name
    class(timer), intent(in) :: this

    Timer instance whose cpu_time value is written.

    character(len=*), intent(in) :: file_name

    Path to the output text file.

procedure, public :: dtimer_start

Start a date_and_time civil-clock measurement.

  • private subroutine dtimer_start(this)

    Author
    Seyed Ali Ghasemi
    License
    BSD 3-Clause

    Start the date_and_time wall-clock timer.

    Read more…

    Arguments

    Type IntentOptional Attributes Name
    class(timer), intent(inout) :: this

    Timer instance whose date/time timing state is initialized.

procedure, public :: dtimer_stop

Stop a date_and_time civil-clock measurement and update elapsed_dtime.

  • private subroutine dtimer_stop(this, nloops, message, print, color, rfmt)

    Author
    Seyed Ali Ghasemi
    License
    BSD 3-Clause

    Stop the date_and_time timer and store elapsed seconds.

    Read more…

    Arguments

    Type IntentOptional Attributes Name
    class(timer), intent(inout) :: this

    Timer instance previously started with dtimer_start.

    integer, intent(in), optional :: nloops

    Optional positive loop count used to compute average date/time duration.

    character(len=*), intent(in), optional :: message

    Optional output label; defaults to Elapsed time:.

    logical, intent(in), optional :: print

    Optional output switch; defaults to .true..

    character(len=*), intent(in), optional :: color

    Optional FACE foreground color used for printed output.

    character(len=*), intent(in), optional :: rfmt

    Optional Fortran real edit descriptor used for printed output.

procedure, public :: dtimer_write

Append elapsed_dtime to a text file.

  • private subroutine dtimer_write(this, file_name)

    Author
    Seyed Ali Ghasemi
    License
    BSD 3-Clause

    Append the last date_and_time result to a text file.

    Read more…

    Arguments

    Type IntentOptional Attributes Name
    class(timer), intent(in) :: this

    Timer instance whose elapsed_dtime value is written.

    character(len=*), intent(in) :: file_name

    Path to the output text file.

procedure, public :: timer_pause

Pause a running system_clock wall-clock measurement.

  • private subroutine timer_pause(this)

    Author
    Seyed Ali Ghasemi
    License
    BSD 3-Clause

    Pause a running system_clock wall-clock timer.

    Read more…

    Arguments

    Type IntentOptional Attributes Name
    class(timer), intent(inout) :: this

    Running wall-clock timer instance to pause.

procedure, public :: timer_resume

Resume a paused system_clock wall-clock measurement.

  • private subroutine timer_resume(this)

    Author
    Seyed Ali Ghasemi
    License
    BSD 3-Clause

    Resume a paused system_clock wall-clock timer.

    Read more…

    Arguments

    Type IntentOptional Attributes Name
    class(timer), intent(inout) :: this

    Paused wall-clock timer instance to resume.

procedure, public :: timer_start

Start a system_clock wall-clock measurement.

  • private subroutine timer_start(this)

    Author
    Seyed Ali Ghasemi
    License
    BSD 3-Clause

    Start the system_clock wall-clock timer.

    Read more…

    Arguments

    Type IntentOptional Attributes Name
    class(timer), intent(inout) :: this

    Timer instance whose wall-clock state is initialized.

procedure, public :: timer_stop

Stop a system_clock wall-clock measurement and update elapsed_time.

  • private subroutine timer_stop(this, nloops, message, print, color, rfmt)

    Author
    Seyed Ali Ghasemi
    License
    BSD 3-Clause

    Stop the system_clock wall-clock timer and store elapsed seconds.

    Read more…

    Arguments

    Type IntentOptional Attributes Name
    class(timer), intent(inout) :: this

    Timer instance previously started with timer_start.

    integer, intent(in), optional :: nloops

    Optional positive loop count used to compute average elapsed time.

    character(len=*), intent(in), optional :: message

    Optional output label; defaults to Elapsed time:.

    logical, intent(in), optional :: print

    Optional output switch; defaults to .true..

    character(len=*), intent(in), optional :: color

    Optional FACE foreground color used for printed output.

    character(len=*), intent(in), optional :: rfmt

    Optional Fortran real edit descriptor used for printed output.

procedure, public :: timer_write

Append elapsed_time to a text file.

  • private subroutine timer_write(this, file_name)

    Author
    Seyed Ali Ghasemi
    License
    BSD 3-Clause

    Append the last system_clock result to a text file.

    Read more…

    Arguments

    Type IntentOptional Attributes Name
    class(timer), intent(in) :: this

    Timer instance whose elapsed_time value is written.

    character(len=*), intent(in) :: file_name

    Path to the output text file.

Source Code

   type timer
      !! author: Seyed Ali Ghasemi
      !! summary: Timing object with independent state for each supported backend.
      !!
      !! A `timer` instance stores private start times and state flags for the
      !! enabled timing backends. The most recent successful measurement from
      !! each backend is exposed through a public result component:
      !! `elapsed_time`, `cpu_time`, `elapsed_dtime`, and, when enabled,
      !! `omp_time` and `mpi_time`.
      !!
      !! Start and stop calls must be matched by backend. For example,
      !! `timer_start` must be followed by `timer_stop`, and `ctimer_start` must
      !! be followed by `ctimer_stop`. Calling a stop routine before its matching
      !! start routine reports an error and leaves the stored result unchanged.

      integer(ik), private :: clock_rate = 0_ik
         !! Cached `system_clock` count rate in ticks per second.
      integer(ik), private :: clock_max = 0_ik
         !! Cached maximum `system_clock` count used to handle one counter wrap.
      integer(ik), private :: clock_start = 0_ik
         !! `system_clock` count captured at the start of the wall-clock interval.

      integer(ik), private :: pause_start = 0_ik
         !! `system_clock` count captured when the wall-clock timer is paused.
      integer(ik), private :: paused_ticks = 0_ik
         !! Total paused duration, in `system_clock` ticks, excluded from the result.

      logical, private :: is_started = .false.
         !! True after `timer_start` and before a successful `timer_stop`.
      logical, private :: is_paused = .false.
         !! True after `timer_pause` and before a matching `timer_resume`.

      real(rk), private :: cpu_start = 0.0_rk
         !! CPU time, in seconds, captured by `ctimer_start`.
      logical, private :: is_cpu_started = .false.
         !! True after `ctimer_start` and before a successful `ctimer_stop`.

#if defined(USE_OMP)
      real(rk), private :: omp_start = 0.0_rk
         !! OpenMP wall-clock time, in seconds, captured by `otimer_start`.
      logical, private :: is_omp_started = .false.
         !! True after `otimer_start` and before a successful `otimer_stop`.
#endif

#if defined(USE_MPI)
      real(rk), private :: mpi_start = 0.0_rk
         !! MPI wall-clock time, in seconds, captured by `mtimer_start`.
      logical, private :: is_mpi_started = .false.
         !! True after `mtimer_start` and before a successful `mtimer_stop`.
#endif

      real(rk), private :: dtime_start_utc = 0.0_rk
         !! UTC epoch seconds computed from `date_and_time` at `dtimer_start`.
      logical, private :: is_dtime_started = .false.
         !! True after `dtimer_start` and before a successful `dtimer_stop`.

      real(rk), public :: elapsed_time = 0.0_rk
         !! Most recent successful `system_clock` elapsed time in seconds.
      real(rk), public :: cpu_time = 0.0_rk
         !! Most recent successful CPU-time measurement in seconds.
#if defined(USE_OMP)
      real(rk), public :: omp_time = 0.0_rk
         !! Most recent successful OpenMP wall-clock measurement in seconds.
#endif
#if defined(USE_MPI)
      real(rk), public :: mpi_time = 0.0_rk
         !! Most recent successful MPI wall-clock measurement in seconds.
#endif
      real(rk), public :: elapsed_dtime = 0.0_rk
         !! Most recent successful `date_and_time` elapsed time in seconds.

   contains
      procedure, public :: timer_start
         !! Start a `system_clock` wall-clock measurement.
      procedure, public :: timer_stop
         !! Stop a `system_clock` wall-clock measurement and update `elapsed_time`.
      procedure, public :: timer_write
         !! Append `elapsed_time` to a text file.
      procedure, public :: timer_pause
         !! Pause a running `system_clock` wall-clock measurement.
      procedure, public :: timer_resume
         !! Resume a paused `system_clock` wall-clock measurement.

      procedure, public :: ctimer_start
         !! Start a CPU-time measurement.
      procedure, public :: ctimer_stop
         !! Stop a CPU-time measurement and update `cpu_time`.
      procedure, public :: ctimer_write
         !! Append `cpu_time` to a text file.

#if defined(USE_OMP)
      procedure, public :: otimer_start
         !! Start an OpenMP wall-clock measurement.
      procedure, public :: otimer_stop
         !! Stop an OpenMP wall-clock measurement and update `omp_time`.
      procedure, public :: otimer_write
         !! Append `omp_time` to a text file.
#endif

#if defined(USE_MPI)
      procedure, public :: mtimer_start
         !! Start an MPI wall-clock measurement.
      procedure, public :: mtimer_stop
         !! Stop an MPI wall-clock measurement and update `mpi_time`.
      procedure, public :: mtimer_write
         !! Append `mpi_time` to a text file.
#endif

      procedure, public :: dtimer_start
         !! Start a `date_and_time` civil-clock measurement.
      procedure, public :: dtimer_stop
         !! Stop a `date_and_time` civil-clock measurement and update `elapsed_dtime`.
      procedure, public :: dtimer_write
         !! Append `elapsed_dtime` to a text file.
   end type timer