forked from ESCOMP/CDEPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshr_sys_mod.F90
331 lines (247 loc) · 10.9 KB
/
shr_sys_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
!===============================================================================
! SVN $Id: shr_sys_mod.F90 66411 2014-12-19 22:40:08Z santos@ucar.edu $
! SVN $URL: https://svn-ccsm-models.cgd.ucar.edu/csm_share/trunk_tags/share3_150116/shr/shr_sys_mod.F90 $
!===============================================================================
! Currently supported by all compilers
#define HAVE_GET_ENVIRONMENT
#define HAVE_SLEEP
! Except this combination?
#if defined CPRPGI && defined CNL
#undef HAVE_GET_ENVIRONMENT
#endif
#if defined CPRNAG
#define HAVE_EXECUTE
#endif
MODULE shr_sys_mod
use shr_kind_mod ! defines real & integer kinds
use shr_log_mod, only: s_loglev => shr_log_Level
use shr_log_mod, only: s_logunit => shr_log_Unit
use shr_abort_mod, only: shr_sys_abort => shr_abort_abort
use shr_abort_mod, only: shr_sys_backtrace => shr_abort_backtrace
#ifdef CPRNAG
! NAG does not provide these as intrinsics, but it does provide modules
! that implement commonly used POSIX routines.
use f90_unix_dir, only: chdir
use f90_unix_proc, only: abort, sleep
#endif
implicit none
! PUBLIC: Public interfaces
private
public :: shr_sys_system ! make a system call
public :: shr_sys_chdir ! change current working dir
public :: shr_sys_getenv ! get an environment variable
public :: shr_sys_irtc ! returns real-time clock tick
public :: shr_sys_sleep ! have program sleep for a while
public :: shr_sys_flush ! flush an i/o buffer
! Imported from shr_abort_mod and republished with renames. Other code that wishes to
! use these routines should use these shr_sys names rather than directly using the
! routines from shr_abort_abort. (This is for consistency with older code, from when
! these routines were defined in shr_sys_mod.)
public :: shr_sys_abort ! abort a program
public :: shr_sys_backtrace ! print a backtrace, if possible
!===============================================================================
CONTAINS
!===============================================================================
!===============================================================================
!===============================================================================
SUBROUTINE shr_sys_system(str,rcode)
IMPLICIT none
!----- arguments ---
character(*) ,intent(in) :: str ! system/shell command string
integer(SHR_KIND_IN),intent(out) :: rcode ! function return error code
!----- functions -----
#if (defined LINUX && !defined CPRGNU)
integer(SHR_KIND_IN),external :: system ! function to envoke shell command
#endif
!----- formats -----
character(*),parameter :: subName = '(shr_sys_system) '
character(*),parameter :: F00 = "('(shr_sys_system) ',4a)"
!-------------------------------------------------------------------------------
! PURPOSE: an architecture independent system call
!-------------------------------------------------------------------------------
rcode = 0
#ifdef HAVE_EXECUTE
call execute_command_line(str,exitstat=rcode) ! Intrinsic as of F2008
#else
#if (defined AIX)
call system(str,rcode)
#elif (defined CPRGNU || defined LINUX)
rcode = system(str)
#else
write(s_logunit,F00) 'ERROR: no implementation of system call for this architecture'//trim(str)
call shr_sys_abort(subName//'no implementation of system call for this architecture')
#endif
#endif
END SUBROUTINE shr_sys_system
!===============================================================================
!===============================================================================
SUBROUTINE shr_sys_chdir(path, rcode)
IMPLICIT none
!----- arguments -----
character(*) ,intent(in) :: path ! chdir to this dir
integer(SHR_KIND_IN),intent(out) :: rcode ! return code
!----- local -----
integer(SHR_KIND_IN) :: lenpath ! length of path
#if (defined AIX || (defined LINUX && !defined CPRGNU && !defined CPRNAG) || defined CPRINTEL)
integer(SHR_KIND_IN),external :: chdir ! AIX system call
#endif
!----- formats -----
character(*),parameter :: subName = '(shr_sys_chdir) '
character(*),parameter :: F00 = "('(shr_sys_chdir) ',4a)"
!-------------------------------------------------------------------------------
! PURPOSE: an architecture independent system call
!-------------------------------------------------------------------------------
rcode = 0
lenpath=len_trim(path)
#if (defined AIX)
rcode = chdir(%ref(path(1:lenpath)//'\0'))
#elif (defined Darwin || (defined LINUX && !defined CPRNAG))
rcode=chdir(path(1:lenpath))
#elif (defined CPRNAG)
call chdir(path(1:lenpath), errno=rcode)
#else
write(s_logunit,F00) 'ERROR: no implementation of chdir for this architecture'
call shr_sys_abort(subname//'no implementation of chdir for this machine')
#endif
END SUBROUTINE shr_sys_chdir
!===============================================================================
!===============================================================================
SUBROUTINE shr_sys_getenv(name, val, rcode)
IMPLICIT none
!----- arguments -----
character(*) ,intent(in) :: name ! env var name
character(*) ,intent(out) :: val ! env var value
integer(SHR_KIND_IN),intent(out) :: rcode ! return code
!----- local -----
#ifndef HAVE_GET_ENVIRONMENT
integer(SHR_KIND_IN) :: lenname ! length of env var name
integer(SHR_KIND_IN) :: lenval ! length of env var value
character(SHR_KIND_CL) :: tmpval ! temporary env var value
#endif
!----- formats -----
character(*),parameter :: subName = '(shr_sys_getenv) '
character(*),parameter :: F00 = "('(shr_sys_getenv) ',4a)"
!-------------------------------------------------------------------------------
! PURPOSE: an architecture independent system call
!-------------------------------------------------------------------------------
!$OMP master
#ifdef HAVE_GET_ENVIRONMENT
call get_environment_variable(name=name,value=val,status=rcode) ! Intrinsic in F2003
#else
lenname=len_trim(name)
#if (defined AIX || defined LINUX)
call getenv(trim(name),tmpval)
val=trim(tmpval)
rcode = 0
if (len_trim(val) == 0 ) rcode = 1
if (len_trim(val) > SHR_KIND_CL) rcode = 2
#else
write(s_logunit,F00) 'ERROR: no implementation of getenv for this architecture'
call shr_sys_abort(subname//'no implementation of getenv for this machine')
#endif
#endif
!$OMP end master
END SUBROUTINE shr_sys_getenv
!===============================================================================
!===============================================================================
integer(SHR_KIND_I8) FUNCTION shr_sys_irtc( rate )
IMPLICIT none
!----- arguments -----
integer(SHR_KIND_I8), optional :: rate
!----- local -----
integer(SHR_KIND_IN) :: count
integer(SHR_KIND_IN) :: count_rate
integer(SHR_KIND_IN) :: count_max
integer(SHR_KIND_IN),save :: last_count = -1
integer(SHR_KIND_I8),save :: count_offset = 0
!$OMP THREADPRIVATE (last_count, count_offset)
!----- formats -----
character(*),parameter :: subName = '(shr_sys_irtc) '
character(*),parameter :: F00 = "('(shr_sys_irtc) ',4a)"
!-------------------------------------------------------------------------------
! emulates Cray/SGI irtc function (returns clock tick since last reboot)
!
! This function is not intended to measure elapsed time between
! multi-threaded regions with different numbers of threads. However,
! use of the threadprivate declaration does guarantee accurate
! measurement per thread within a single multi-threaded region as
! long as the number of threads is not changed dynamically during
! execution within the multi-threaded region.
!
!-------------------------------------------------------------------------------
call system_clock(count=count,count_rate=count_rate, count_max=count_max)
if ( present(rate) ) rate = count_rate
shr_sys_irtc = count
!--- adjust for clock wrap-around ---
if ( last_count /= -1 ) then
if ( count < last_count ) count_offset = count_offset + count_max
end if
shr_sys_irtc = shr_sys_irtc + count_offset
last_count = count
END FUNCTION shr_sys_irtc
!===============================================================================
!===============================================================================
SUBROUTINE shr_sys_sleep(sec)
IMPLICIT none
!----- arguments -----
real (SHR_KIND_R8),intent(in) :: sec ! number of seconds to sleep
!----- local -----
integer(SHR_KIND_IN) :: isec ! integer number of seconds
#ifndef HAVE_SLEEP
integer(SHR_KIND_IN) :: rcode ! return code
character(90) :: str ! system call string
#endif
!----- formats -----
character(*),parameter :: subName = '(shr_sys_sleep) '
character(*),parameter :: F00 = "('(shr_sys_sleep) ',4a)"
character(*),parameter :: F10 = "('sleep ',i8 )"
!-------------------------------------------------------------------------------
! PURPOSE: Sleep for approximately sec seconds
!-------------------------------------------------------------------------------
isec = nint(sec)
if (isec < 0) then
if (s_loglev > 0) write(s_logunit,F00) 'ERROR: seconds must be > 0, sec=',sec
else if (isec == 0) then
! Don't consider this an error and don't call system sleep
else
#ifdef HAVE_SLEEP
call sleep(isec)
#else
write(str,FMT=F10) isec
call shr_sys_system( str, rcode )
#endif
endif
END SUBROUTINE shr_sys_sleep
!===============================================================================
!===============================================================================
SUBROUTINE shr_sys_flush(unit)
IMPLICIT none
!----- arguments -----
integer(SHR_KIND_IN) :: unit ! flush output buffer for this unit
!----- local -----
!----- formats -----
character(*),parameter :: subName = '(shr_sys_flush) '
character(*),parameter :: F00 = "('(shr_sys_flush) ',4a)"
!-------------------------------------------------------------------------------
! PURPOSE: an architecture independent system call
!
! This is probably no longer needed; the "flush" statement is supported by
! all compilers that CESM supports for years now.
!
!-------------------------------------------------------------------------------
!$OMP SINGLE
flush(unit)
!$OMP END SINGLE
!
! The following code was originally present, but there's an obvious issue.
! Since shr_sys_flush is usually used to flush output to a log, when it
! returns an error, does it do any good to print that error to the log?
!
! if (ierr > 0) then
! write(s_logunit,*) subname,' Flush reports error: ',ierr
! endif
!
END SUBROUTINE shr_sys_flush
!===============================================================================
!===============================================================================
END MODULE shr_sys_mod