6 use mom_coms,
only : sum_across_pes, pe_here, num_pes
8 use mom_io,
only : open_file, append_file, ascii_file, writeonly_file
12 implicit none ;
private
14 public write_cputime, mom_write_cputime_init, write_cputime_start_clock
18 integer :: clocks_per_sec = 1000
19 integer :: max_ticks = 1000
28 type(time_type) :: start_time
30 real :: startup_cputime
31 real :: prev_cputime = 0.0
32 real :: dn_dcpu_min = -1.0
33 real :: cputime2 = 0.0
34 integer :: previous_calls = 0
36 integer :: filecpu_ascii
37 character(len=200) :: cpufile
43 subroutine write_cputime_start_clock(CS)
46 integer :: new_cputime
47 if (.not.
associated(cs))
allocate(cs)
49 call system_clock(new_cputime, clocks_per_sec, max_ticks)
50 cs%prev_cputime = new_cputime
51 end subroutine write_cputime_start_clock
54 subroutine mom_write_cputime_init(param_file, directory, Input_start_time, CS)
56 character(len=*),
intent(in) :: directory
57 type(time_type),
intent(in) :: input_start_time
62 integer :: new_cputime
64 #include "version_variable.h"
65 character(len=40) :: mdl =
'MOM_write_cputime'
67 if (.not.
associated(cs))
then
69 call system_clock(new_cputime, clocks_per_sec, max_ticks)
70 cs%prev_cputime = new_cputime
75 call get_param(param_file, mdl,
"MAXCPU", cs%maxcpu, &
76 "The maximum amount of cpu time per processor for which "//&
77 "MOM should run before saving a restart file and "//&
78 "quitting with a return value that indicates that a "//&
79 "further run is required to complete the simulation. "//&
80 "If automatic restarts are not desired, use a negative "//&
81 "value for MAXCPU. MAXCPU has units of wall-clock "//&
82 "seconds, so the actual CPU time used is larger by a "//&
83 "factor of the number of processors used.", &
84 units=
"wall-clock seconds", default=-1.0)
85 call get_param(param_file, mdl,
"CPU_TIME_FILE", cs%CPUfile, &
86 "The file into which CPU time is written.",default=
"CPU_stats")
87 cs%CPUfile = trim(directory)//trim(cs%CPUfile)
88 call log_param(param_file, mdl,
"directory/CPU_TIME_FILE", cs%CPUfile)
90 cs%CPUfile = trim(cs%CPUfile)//
"."//trim(adjustl(statslabel))
93 cs%Start_time = input_start_time
95 end subroutine mom_write_cputime_init
99 subroutine write_cputime(day, n, nmax, CS)
100 type(time_type),
intent(inout) :: day
101 integer,
intent(in) :: n
102 integer,
intent(inout) :: nmax
110 integer :: new_cputime
112 character(len=256) :: mesg
113 integer :: start_of_day, num_days
115 if (.not.
associated(cs))
call mom_error(fatal, &
116 "write_energy: Module must be initialized before it is used.")
118 call system_clock(new_cputime, clocks_per_sec, max_ticks)
124 if (new_cputime < cs%prev_cputime-(10.0*clocks_per_sec))
then
125 d_cputime = new_cputime - cs%prev_cputime + max_ticks
127 d_cputime = new_cputime - cs%prev_cputime
130 call sum_across_pes(d_cputime)
131 if (cs%previous_calls == 0) cs%startup_cputime = d_cputime
133 cs%cputime2 = cs%cputime2 + d_cputime
135 if ((cs%previous_calls >= 1) .and. (cs%maxcpu > 0.0))
then
137 if ((n > cs%prev_n) .and. (d_cputime > 0.0) .and. &
138 ((cs%dn_dcpu_min*d_cputime < (n - cs%prev_n)) .or. &
139 (cs%dn_dcpu_min < 0.0))) &
140 cs%dn_dcpu_min = (n - cs%prev_n) / d_cputime
141 if (cs%dn_dcpu_min >= 0.0)
then
143 nmax = n + int( cs%dn_dcpu_min * &
144 (0.95*cs%maxcpu * real(num_pes())*clocks_per_sec - &
145 (cs%startup_cputime + cs%cputime2)) )
150 cs%prev_cputime = new_cputime ; cs%prev_n = n
152 call get_time(day, start_of_day, num_days)
153 reday = real(num_days)+ (real(start_of_day)/86400.0)
156 if ((cs%previous_calls == 0) .and. (is_root_pe()))
then
157 if (day > cs%Start_time)
then
158 call open_file(cs%fileCPU_ascii, trim(cs%CPUfile), &
159 action=append_file, form=ascii_file, nohdrs=.true.)
161 call open_file(cs%fileCPU_ascii, trim(cs%CPUfile), &
162 action=writeonly_file, form=ascii_file, nohdrs=.true.)
166 if (is_root_pe())
then
167 if (cs%previous_calls == 0)
then
168 write(cs%fileCPU_ascii, &
169 '("Startup CPU time: ", F12.3, " sec summed across", I5, " PEs.")') &
170 (cs%startup_cputime / clocks_per_sec), num_pes()
171 write(cs%fileCPU_ascii,*)
" Day, Step number, CPU time, CPU time change"
173 write(cs%fileCPU_ascii,
'(F12.3,", "I11,", ", F12.3,", ", F12.3)') &
174 reday, n, (cs%cputime2 / real(clocks_per_sec)), &
175 d_cputime / real(clocks_per_sec)
177 cs%previous_calls = cs%previous_calls + 1
179 end subroutine write_cputime