-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpbs_mod.f90
179 lines (179 loc) · 5.14 KB
/
pbs_mod.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
module pbs_mod
!
use module_kind_types
use, intrinsic :: iso_c_binding, only : pbs_int => c_long
!
implicit none
!
private
!
public :: pbs_remaining_walltime
!
#ifdef PBS_ENV
interface
subroutine pbs_time_left(seconds_left) bind(c,name="pbs_time_left_")
import :: pbs_int
integer(pbs_int) :: seconds_left
end subroutine pbs_time_left
end interface
#endif
!
integer(pbs_int), save :: initial_wtl
integer(pbs_int), save :: previous_wtl
real(wp), save :: initial_wtime
real(wp), save :: previous_wtime
logical(lk), save :: pbs_mod_needs_initializing = true
!
contains
!
!###############################################################################
!
subroutine pbs_remaining_walltime(walltime_expiring,wall_time_remaining)
!
!.. Formal Arguments ..
logical(lk), intent(out) :: walltime_expiring
!
!.. Optional Arguments ..
real(wp), optional, intent(out) :: wall_time_remaining
!
!.. Local Scalars ..
integer(pbs_int) :: current_wtl
real(wp) :: current_wtime
real(wp) :: time_elapsed
real(wp) :: next_time_est
real(wp) :: wall_time_left
!
!.. Local Arrays ..
real(wp) :: time_values(1:2)
!
!.. Local Parameters ..
character(len=*), parameter :: pname = "pbs_remaining_walltime"
real(wp), parameter :: max_sec = real( 1000*3600-1 , kind=wp )
real(wp), parameter :: five_min = real( 300 , kind=wp )
!
continue
!
current_wtl = 0_pbs_int
current_wtime = zero
!
if (mypnum == 0) then
!
if (pbs_mod_needs_initializing) then
call initialize_pbs_mod(walltime_expiring)
end if
!
#ifdef PBS_ENV
!call pbs_time_left(current_wtl)
! pbs_time_left seems to not be reliable enough to use it each time
current_wtl = previous_wtl
current_wtime = mpi_wtime()
!write (iout,1) current_wtl,current_wtime
!
if (previous_wtl-current_wtl == 0_pbs_int) then
!
! If the time between calls to pbs_time_left was too small such that
! the returned integers are the same, use the mpi_wtime() function to
! compute the time elapsed between the current time and the previous
! time this subroutine was called.
!
! NOTE : previous_wtime should be less than current_wtime since
! mpi_wtime counts up as time progresses
!
time_elapsed = current_wtime - previous_wtime
time_elapsed = min( time_elapsed , max_sec )
wall_time_left = real(initial_wtl,kind=kind(wall_time_left)) - &
(current_wtime-initial_wtime)
!
else
!
! Use integers returned from pbs_time_left to compute the time
! elapsed since the previous time this subroutine was called
!
! NOTE : current_wtl should be less than previous_wtl since pbs_time_left
! counts down the remaining time for the job as time progresses
!
time_elapsed = real(previous_wtl-current_wtl,kind=kind(time_elapsed))
time_elapsed = min( time_elapsed , max_sec )
wall_time_left = real(current_wtl,kind=kind(wall_time_left))
!
end if
!
next_time_est = time_elapsed * (two+half)
!
walltime_expiring = (wall_time_left-next_time_est <= zero) .or. &
(wall_time_left-five_min <= zero)
!
previous_wtl = current_wtl
previous_wtime = current_wtime
!
#else
wall_time_left = huge(zero)
walltime_expiring = .false.
#endif
!
!write (iout,1) current_wtl,wall_time_left
time_values(1) = merge(-one,one,walltime_expiring)
time_values(2) = wall_time_left
!
end if
!
if (ncpu > 1) then
call mpi_bcast(time_values,size(time_values,kind=int_mpi), &
mpi_flttyp,0_int_mpi,MPI_COMM_WORLD,mpierr)
end if
!
walltime_expiring = (time_values(1) < zero)
if (present(wall_time_remaining)) then
wall_time_remaining = time_values(2)
!if (mypnum == 0) write (iout,2) wall_time_remaining
end if
!
1 format (/,2x," current_wtl = ",i0,/, &
2x," wall_time_left = ",es25.17)
2 format (2x, "wall_time_remaining = ",es25.17,/)
!
end subroutine pbs_remaining_walltime
!
!###############################################################################
!
subroutine initialize_pbs_mod(walltime_expiring)
!
!.. Formal Arguments ..
logical(lk), intent(out) :: walltime_expiring
!
!.. Local Scalars ..
integer(pbs_int) :: current_wtl
!
!.. Local Parameters ..
character(len=*), parameter :: pname = "initialize_pbs_mod"
!
continue
!
walltime_expiring = fals
pbs_mod_needs_initializing = fals
!
#ifdef PBS_ENV
call pbs_time_left(current_wtl)
#else
current_wtl = huge(int(0,kind=kind(current_wtl)))
#endif
write (iout,1) current_wtl,ncpu
!
! Initialize the module variables storing the previous wall time left
! and previous mpi_wtime
!
initial_wtl = current_wtl
initial_wtime = mpi_wtime()
!
previous_wtl = initial_wtl
previous_wtime = initial_wtime
!
! Format Statements
!
1 format (/," Allocated Wall Time = ",i0," s. running on ",i0," CPUs",/)
!
end subroutine initialize_pbs_mod
!
!###############################################################################
!
end module pbs_mod