amun-code/sources/timers.F90
Grzegorz Kowal 81de98d9e2 Update the copyright year to 2023.
Signed-off-by: Grzegorz Kowal <grzegorz@amuncode.org>
2023-02-01 18:36:37 -03:00

370 lines
10 KiB
Fortran

!!******************************************************************************
!!
!! This file is part of the AMUN source code, a program to perform
!! Newtonian or relativistic magnetohydrodynamical simulations on uniform or
!! adaptive mesh.
!!
!! Copyright (C) 2008-2023 Grzegorz Kowal <grzegorz@amuncode.org>
!!
!! This program is free software: you can redistribute it and/or modify
!! it under the terms of the GNU General Public License as published by
!! the Free Software Foundation, either version 3 of the License, or
!! (at your option) any later version.
!!
!! This program is distributed in the hope that it will be useful,
!! but WITHOUT ANY WARRANTY; without even the implied warranty of
!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
!! GNU General Public License for more details.
!!
!! You should have received a copy of the GNU General Public License
!! along with this program. If not, see <http://www.gnu.org/licenses/>.
!!
!!******************************************************************************
!!
!! module: TIMERS
!!
!! This module handles the execution time counting. Its general
!! implementation allows to insert up to 128 counters which will measure
!! time spent on the execution of the bounded code block. Each timer
!! can be described.
!!
!!******************************************************************************
!
module timers
implicit none
integer , parameter :: ntimers = 32
integer , save :: ntimer
logical , dimension(ntimers), save :: tenabled, tlocked
character(len=32), dimension(ntimers), save :: description
integer , dimension(ntimers), save :: tcount
integer(kind=8) , dimension(ntimers), save :: times, tstart, tstop
integer(kind=8) , save :: ticks, tbegin
real (kind=8) , save :: conv = 1.0d+00
private
public :: initialize_timers, finalize_timers
public :: set_timer, start_timer, stop_timer
public :: get_timer, get_count, get_timer_total
public :: ntimers, timer_enabled, timer_description
!- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
!
contains
!
!===============================================================================
!
! subroutine INITIALIZE_TIMERS:
! ----------------------------
!
! Subroutine initializes module TIMERS and allocates memory to store
! execution times.
!
!
!===============================================================================
!
subroutine initialize_timers()
implicit none
!-------------------------------------------------------------------------------
!
call system_clock(count=tbegin, count_rate=ticks)
tenabled(:) = .false.
tlocked(:) = .false.
description(:) = ''
ntimer = 1
times(:) = 0
tstart(:) = 0
tstop(:) = 0
tcount(:) = 0
if (ticks > 0) conv = 1.0d+00 / ticks
!-------------------------------------------------------------------------------
!
end subroutine initialize_timers
!
!===============================================================================
!
! subroutine FINALIZE_TIMERS:
! --------------------------
!
! Subroutine finalizes module.
!
!===============================================================================
!
subroutine finalize_timers()
implicit none
!-------------------------------------------------------------------------------
!
!-------------------------------------------------------------------------------
!
end subroutine finalize_timers
!
!===============================================================================
!
! subroutine SET_TIMER:
! --------------------
!
! Subroutine sets the timer by giving its desciption.
!
! Arguments:
!
! string - the timer description;
! timer - the timer index;
!
!===============================================================================
!
subroutine set_timer(string, timer)
use helpers, only : print_message
implicit none
character(len=*), intent(in) :: string
integer , intent(out) :: timer
character(len=*), parameter :: loc = 'TIMERS::set_timer()'
!-------------------------------------------------------------------------------
!
ntimer = ntimer + 1
if (ntimer <= ntimers) then
description(ntimer) = trim(adjustl(string))
tenabled(ntimer) = .true.
timer = ntimer
else
call print_message(loc, "The maximum number of counters exceeded! " // &
"Increase the parameter 'ntimers' in this module and recompile.")
end if
!-------------------------------------------------------------------------------
!
end subroutine set_timer
!
!===============================================================================
!
! subroutine START_TIMER:
! ----------------------
!
! Subroutine starts accounting time for a specified timer.
!
! Arguments:
!
! timer - the timer index;
!
!===============================================================================
!
subroutine start_timer(timer)
use helpers, only : print_message
implicit none
integer, intent(in) :: timer
character(len=*), parameter :: loc = 'TIMERS::start_timer()'
!-------------------------------------------------------------------------------
!
if (tlocked(timer)) then
call print_message(loc, "Timer '" // trim(description(timer)) // &
"' already locked!")
else
tlocked(timer) = .true.
call system_clock(tstart(timer))
end if
!-------------------------------------------------------------------------------
!
end subroutine start_timer
!
!===============================================================================
!
! subroutine STOP_TIMER:
! ---------------------
!
! Subroutine stops accounting time of the specified timer.
!
! Arguments:
!
! timer - the timer index;
!
!===============================================================================
!
subroutine stop_timer(timer)
use helpers, only : print_message
implicit none
integer, intent(in) :: timer
character(len=*), parameter :: loc = 'TIMERS::stop_timer()'
!-------------------------------------------------------------------------------
!
if (tlocked(timer)) then
call system_clock(tstop(timer))
times(timer) = times(timer) + (tstop(timer) - tstart(timer))
tcount(timer) = tcount(timer) + 1
tlocked(timer) = .false.
else
call print_message(loc, "Timer '" // trim(description(timer)) // &
"' already unlocked!")
end if
!-------------------------------------------------------------------------------
!
end subroutine stop_timer
!
!===============================================================================
!
! function GET_TIMER:
! ------------------
!
! Function returns accounted time of the specified timer.
!
! Arguments:
!
! timer - the timer index;
!
!===============================================================================
!
real(kind=8) function get_timer(timer)
implicit none
integer, intent(in) :: timer
!-------------------------------------------------------------------------------
!
get_timer = max(0.0d+00, conv * times(timer))
return
!-------------------------------------------------------------------------------
!
end function get_timer
!
!===============================================================================
!
! function GET_COUNT:
! ------------------
!
! Function returns the call count for the specified timer.
!
! Arguments:
!
! timer - the timer index;
!
!===============================================================================
!
integer(kind=4) function get_count(timer)
implicit none
integer, intent(in) :: timer
!-------------------------------------------------------------------------------
!
get_count = tcount(timer)
return
!-------------------------------------------------------------------------------
!
end function get_count
!
!===============================================================================
!
! function TIMER_ENABLED:
! ----------------------
!
! Function returns logical flag determining if the specified timer is enabled.
!
! Arguments:
!
! timer - the timer index;
!
!===============================================================================
!
logical function timer_enabled(timer)
implicit none
integer, intent(in) :: timer
!-------------------------------------------------------------------------------
!
timer_enabled = tenabled(timer)
return
!-------------------------------------------------------------------------------
!
end function timer_enabled
!
!===============================================================================
!
! function TIMER_DESCRIPTION:
! --------------------------
!
! Function returns the the specified timer description.
!
! Arguments:
!
! timer - the timer index;
!
!===============================================================================
!
character(len=32) function timer_description(timer)
implicit none
integer, intent(in) :: timer
!-------------------------------------------------------------------------------
!
timer_description = description(timer)
return
!-------------------------------------------------------------------------------
!
end function timer_description
!
!===============================================================================
!
! function GET_TIMER_TOTAL:
! ------------------------
!
! Function returns the total execution time.
!
!===============================================================================
!
real(kind=8) function get_timer_total()
implicit none
integer(kind=8) :: tend
!-------------------------------------------------------------------------------
!
call system_clock(count=tend)
get_timer_total = max(0.0d+00, conv * (tend - tbegin))
return
!-------------------------------------------------------------------------------
!
end function get_timer_total
!===============================================================================
!
end module