-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogFileModule.f90
342 lines (249 loc) · 8.31 KB
/
LogFileModule.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
332
333
! ____ _ __ ____ __ ____
! / __/___(_) / ___ ____/ __ \__ _____ ___ / /_ / _/__ ____
! _\ \/ __/ / _ \/ -_) __/ /_/ / // / -_|_-</ __/ _/ // _ \/ __/
! /___/\__/_/_.__/\__/_/ \___\_\_,_/\__/___/\__/ /___/_//_/\__(_)
!
!
! Copyright 2010 SciberQuest Inc.
!
! No permission is granted to reproduce this software.
!
! This is experimental software and is provided ‘‘as is’’, with no
! warranties of any kind whatsoever, no support, no promise of updates,
! or printed documentation.
!==============================================================================
module LogFileModule
use DoubleVectorModule
use PosixFileModule
use mpi
!============================================================================
type LogFile
type(PosixFile),pointer :: File
type(DoubleVector),pointer :: EventStart
type(DoubleVector),pointer :: EventEnd
end type
!----------------------------------------------------------------------------
interface NewLogFile
module procedure NewLogFileDefault
module procedure NewLogFileCopy
end interface
!----------------------------------------------------------------------------
interface LogFileWrite
module procedure LogFileWriteChar
module procedure LogFileWriteString
end interface
contains
!----------------------------------------------------------------------------
function NewLogFileDefault() result(fh)
implicit none
type(LogFile), pointer :: fh
integer iErr
allocate(fh,stat=iErr)
if (iErr.ne.0) then
write(0,*)"Error: Could not allocate LogFile."
stop
end if
fh%File => NewPosixFile()
fh%EventStart => NewDoubleVector()
fh%EventEnd => NewDoubleVector()
end function
!----------------------------------------------------------------------------
function NewLogFileCopy(other) result(fh)
implicit none
type(LogFile) other
type(LogFile), pointer :: fh
integer iErr
allocate(fh,stat=iErr)
if (iErr.ne.0) then
write(0,*)"Error: Could not allocate LogFile."
stop
end if
! copy the file handle
fh%File => NewPosixFile(other%File)
! not copying event states
fh%EventStart => NewDoubleVector()
fh%EventEnd => NewDoubleVector()
end function
!----------------------------------------------------------------------------
subroutine DeleteLogFile(fh)
implicit none
type(LogFile), pointer :: fh
integer iErr
integer nLeft
if (.not.associated(fh)) return
call PosixFileClose(fh%File,iErr)
call DeletePosixFile(fh%File)
nLeft=DoubleVectorGetSize(fh%EventStart)
if (nLeft.ne.0) then
write(0,*)'Error: EventStart stack is not empty.',nLeft
end if
nLeft=DoubleVectorGetSize(fh%EventStart)
if (nLeft.ne.0) then
write(0,*)'Error: EventEnd stack is not empty.',nLeft
end if
call DeleteDoubleVector(fh%EventStart)
call DeleteDoubleVector(fh%EventEnd)
deallocate(fh)
nullify(fh)
end subroutine
!----------------------------------------------------------------------------
! A communicator should be set prior to open if one wants comm based logging.
! The default is for all processes to open the file.
subroutine LogFileOpen(fh,fileName,comm,iErr)
implicit none
type(LogFile) fh
integer comm
character(len=*) fileName
integer iErr
iErr=0
call PosixFileSetComm(fh%File,comm,iErr)
if (iErr.ne.0) return
iErr=PosixFileOpen( &
fh%File, &
fileName, &
-1, &
"unknown", &
"write", &
"append", &
"unformatted", &
"stream")
if (iErr.ne.0) return
end subroutine
!----------------------------------------------------------------------------
subroutine LogFileWriteHeader(fh,label,iErr)
implicit none
type(LogFile) fh
character(len=*) label
character(len=1024) buffer
character(len=8) date
character(len=10) time
character(len=256) eStr
integer iErr
iErr=0
date=""
time=""
if (LogFileRankIsActive(fh)) then
call date_and_time(date, time)
write(buffer,fmt='(13A)',iostat=ierr,iomsg=eStr) &
"#################################################",char(10), &
"# ",trim(label),char(10), &
"# ",trim(date),",",trim(time),char(10), &
"#################################################",char(10)
if (iErr.ne.0) then
write(0,*)"Error: Failed to write string."
write(0,*)"Error: ",eStr
return
end if
call LogFileWrite(fh,buffer,iErr)
end if
end subroutine
!---------------------------------------------------------------------------
subroutine LogFileWriteChar(fh,text,iErr)
implicit none
type(LogFile) fh
character(len=*) text
integer iErr
iErr=0
call PosixFileWrite(fh%File,text,iErr)
end subroutine
!---------------------------------------------------------------------------
subroutine LogFileWriteString(fh,str,iErr)
implicit none
type(LogFile) fh
type(String) str
integer iErr
iErr=0
call PosixFileWrite(fh%File,str,iErr)
end subroutine
!---------------------------------------------------------------------------
subroutine LogFileMarkEvent(time,iErr)
use mpi
implicit none
real*8 time
integer iErr
iErr=0
time=MPI_Wtime()
end subroutine
!---------------------------------------------------------------------------
subroutine LogFileMarkEventStart(fh,iErr)
implicit none
type(LogFile) fh
real*8 time
integer iErr
call LogFileMarkEvent(time,iErr)
call DoubleVectorPush(fh%EventStart,time)
end subroutine
!---------------------------------------------------------------------------
subroutine LogFileMarkEventEnd(fh,iErr)
implicit none
type(LogFile) fh
real*8 time
integer iErr
call LogFileMarkEvent(time,iErr)
call DoubleVectorPush(fh%EventEnd,time)
end subroutine
!----------------------------------------------------------------------------
subroutine LogFileWriteEvent(fh,eventName,iErr)
implicit none
type(LogFile) fh
character(len=*) eventName
real*8 startt,endt,ellapsedt
character(len=512) buffer
integer iErr
iErr=0
startt=DoubleVectorPop(fh%EventStart)
endt=DoubleVectorPop(fh%EventEnd)
ellapsedt=endt-startt
if (PosixFileRankIsActive(fh%File)) then
write(buffer,iostat=iErr,fmt='(A24,I24,3E24.15,A)') &
trim(eventName), &
PosixFileGetWorldRank(fh%File), &
startt, &
endt, &
ellapsedt, &
char(10)
if (iErr.ne.0) return
call PosixFileWrite(fh%File,buffer,iErr)
end if
end subroutine
!----------------------------------------------------------------------------
subroutine LogFileClose(fh,iErr)
implicit none
type(LogFile) fh
integer iErr
call PosixFileClose(fh%File,iErr)
end subroutine
!---------------------------------------------------------------------------
function LogFileRankIsActive(fh) result(isActive)
implicit none
type(LogFile) fh
logical isActive
isActive=PosixFileRankIsActive(fh%File)
end function
!----------------------------------------------------------------------------
subroutine LogFileSetComm(fh,comm,iErr)
implicit none
type(LogFile) fh
integer comm
integer iErr
iErr=0
! note, it's OK to change the comm on a connected file.
! close runs on all processes.
call PosixFileSetComm(fh%File,comm,iErr)
end subroutine
!----------------------------------------------------------------------------
subroutine LogFileSetFileName(fh,fileName,iErr)
implicit none
type(LogFile) fh
character(len=*) fileName
integer iErr
call PosixFileSetFileName(fh%File,fileName,iErr)
end subroutine
!----------------------------------------------------------------------------
function LogFileGetFileName(fh) result(fileName)
implicit none
type(LogFile) fh
character(len=512) fileName
fileName=PosixFileGetFileName(fh%File)
end function
end module