1! ---
2! Copyright (C) 1996-2016	The SIESTA group
3!  This file is distributed under the terms of the
4!  GNU General Public License: see COPYING in the top directory
5!  or http://www.gnu.org/copyleft/gpl.txt .
6! See Docs/Contributors.txt for a list of contributors.
7! ---
8!!@LICENSE
9!
10module m_walltime
11!
12! Implements a wall_time routine to return the wall time, instead of
13! the cpu time. It tries to deal with possible wraparounds of the system
14! clock's counter.
15!
16implicit none
17
18public :: wall_time
19
20integer, parameter, private :: dp = selected_real_kind(14,200)
21integer, parameter, private :: i8 = selected_int_kind(12)
22
23integer(i8), private, save     :: last_count
24integer(i8), private, save     :: max_count
25real(dp), private, save    :: last_time
26real(dp), private, save    :: count_rate
27logical, private, save     :: first = .true.
28
29
30private
31
32CONTAINS
33
34subroutine wall_time(t)
35real(dp), intent(out)  :: t
36
37real(dp)  :: elapsed_time
38
39integer(i8)       :: count_rate_int
40integer(i8)       :: count
41
42      if (first) then
43         CALL system_clock (count_rate=count_rate_int)
44         CALL system_clock (count_max=max_count)
45         count_rate = real(count_rate_int,kind=dp)
46         first = .false.
47         CALL system_clock (last_count)
48         t = 0.0_dp
49         last_time = t
50         RETURN
51      endif
52
53CALL system_clock (count)
54
55! Watch out for wrap-around...
56! This works if the system counter wrapped around just once...
57! ... be liberal in your use of this routine.
58
59if (count < last_count) then
60   elapsed_time = (max_count - last_count + count)/count_rate
61else
62   elapsed_time = (count-last_count)/count_rate
63endif
64
65t = last_time + elapsed_time
66last_time = t
67last_count = count
68
69end subroutine wall_time
70
71end module m_walltime
72
73