module mytimers use precision implicit none integer, parameter :: starttime = 1 integer, parameter :: sumtime = 2 integer, parameter :: starttime_cpu = 3 integer, parameter :: sumtime_cpu = 4 ! integer, parameter :: mytimer_t_tot = 1 integer, parameter :: mytimer_t_bc = 2 integer, parameter :: mytimer_t_df = 3 integer, parameter :: mytimer_t_tr = 4 integer, parameter :: mytimer_t_rest= 5 integer, parameter :: mynumtimers = 5 real(hp), dimension(:,:) :: usedcp character(20), dimension(:) :: names logical :: initialized = .false. contains ! ! ! !============================================================================== subroutine mytimers_init () implicit none ! ! locals integer :: istat ! ! body allocate (usedcp(mynumtimers,4), stat = istat) if (istat == 0) allocate (names (mynumtimers) , stat = istat) if (istat /= 0) then write(*,*) "ERROR:mytimers:allocation problem" stop endif names(mytimer_t_tot) = 'tram2 Total' names(mytimer_t_bc) = 'tram2 bedbc2004' names(mytimer_t_df) = 'tram2 calseddf2004' names(mytimer_t_tr) = 'tram2 bedtr2004' names(mytimer_t_rest) = 'tram2 Rest' usedcp(:, starttime ) = -1.0_hp usedcp(:, sumtime ) = 0.0_hp usedcp(:, starttime_cpu) = 0.0_hp usedcp(:, sumtime_cpu ) = 0.0_hp initialized = .true. end subroutine mytimers_init ! ! ! !============================================================================== subroutine mytimer_start (timnum) implicit none integer, intent(in) :: timnum ! ! local integer(long) :: tcount integer(long) :: trate integer(long) :: tmax real(sp) :: cputim character(80) :: message ! ! body if (.not. initialized) return ! if ( usedcp(timnum,starttime) > 0.0_hp ) then write (*,'(3a)') 'ERROR:mytimer:Timer_start: Timer ', trim(gdp%gdtimers%names(timnum)), ' has already been started' endif call system_clock(tcount, trate, tmax) call cpu_time(cputim) usedcp(timnum,starttime_cpu) = real(cputim,hp) usedcp(timnum,starttime) = real(tcount,hp) / real(trate,hp) end subroutine mytimer_start ! ! ! !============================================================================== function mytimer_sum (timnum) implicit none integer, intent(in) :: timnum ! ! locals integer(long) :: tcount integer(long) :: trate integer(long) :: tmax real(hp) :: timer_sum character(80) :: message ! ! body if (.not. initialized) then timer_sum = 0.0_hp return endif ! if ( usedcp(timnum,starttime) < 0.0_hp ) then timer_sum = usedcp(timnum,sumtime) else call system_clock(tcount, trate, tmax) timer_sum = usedcp(timnum,sumtime) & & + (real(tcount,hp)/real(trate,hp) - usedcp(timnum,starttime)) endif end function mytimer_sum ! ! ! !============================================================================== subroutine mytimer_stop (timnum) implicit none integer, intent(in) :: timnum ! ! locals integer(long) :: tcount integer(long) :: trate integer(long) :: tmax real(sp) :: cputim character(80) :: message ! ! body if (.not. initialized) return ! if ( usedcp(timnum,starttime) < 0.0_hp ) then write (*,'(3a)') 'ERROR:mytimers:Timer_stop: Timer ', trim(gdp%gdtimers%names(timnum)), ' has not been started' call system_clock(tcount, trate, tmax) call cpu_time(cputim) usedcp(timnum,starttime_cpu) = real(cputim,hp) usedcp(timnum,starttime) = real(tcount,hp) / real(trate,hp) endif call system_clock(tcount, trate, tmax) call cpu_time(cputim) usedcp(timnum,sumtime_cpu) = usedcp(timnum,sumtime_cpu) & & + (real(cputim,hp) - usedcp(timnum,starttime_cpu)) usedcp(timnum,starttime_cpu) = 0.0_hp usedcp(timnum,sumtime) = usedcp(timnum,sumtime) & & + (real(tcount,hp)/real(trate,hp) - usedcp(timnum,starttime)) usedcp(timnum,starttime) = -1.0_hp end subroutine mytimer_stop ! ! ! !============================================================================== subroutine mytimers_finish () implicit none ! ! locals integer :: i integer :: istat integer , pointer :: lundia integer , pointer :: nmax integer , pointer :: mmax integer , pointer :: kmax integer , pointer :: lmax integer :: timesteps real(hp) :: simper real(hp) , dimension(:,:), pointer :: usedcp real(hp) , dimension(4) :: timetot character(20), dimension(:) , pointer :: names character(80) :: message logical , pointer :: addtim ! ! body if (.not. initialized) then write (*,'(a)') 'ERROR:mytimers:Timers_finish: Timers not initialized; can not produce timer output.' return endif ! do i = 1, mynumtimers if ( usedcp(i,starttime) > 0.0_hp ) then ! ! Only show a warning when there is no (more serious) error ! write (*,'(3a)') 'ERROR:mytimers:Timer_finish: Timer ', trim(gdp%gdtimers%names(i)), ' has not been stopped' call timer_stop(i) endif ! ! Location 'startime' in array usedcp is used to store the percentage of the total time ! usedcp(i,starttime) = (usedcp(i,sumtime) / usedcp(timer_total,sumtime)) * 100.0_hp usedcp(i,starttime_cpu) = (usedcp(i,sumtime_cpu) / usedcp(timer_total,sumtime_cpu)) * 100.0_hp enddo write(lundia,'(a)') 'mytimers:' write(lundia,'(a)') '|---------------------------------------------------------------------|' write(lundia,'(a)') '|Timer name | wall clock | CPU time |' write(lundia,'(a)') '| |-----------------------|-----------------------|' write(lundia,'(a)') '| | sec | % | sec | % |' write(lundia,'(a)') '|---------------------------------------------------------------------|' do i = 1, mynumtimers write(lundia,111) names(i), & & usedcp(i,sumtime) , usedcp(i,starttime) , & & usedcp(i,sumtime_cpu), usedcp(i,starttime_cpu) enddo write(lundia,'(a)') '|---------------------------------------------------------------------|' write(lundia,*) ! deallocate(usedcp, stat = istat) deallocate(names, stat = istat) initialized = .false. 111 format('|',a,' | ',2(f12.2,' |',f5.1,' | ')) end subroutine mytimers_finish end module mytimers