-
Notifications
You must be signed in to change notification settings - Fork 0
/
iofifo.f90
282 lines (187 loc) · 6.03 KB
/
iofifo.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
! This file is part of futilities
!
! Copyright (C) 2013-2021 C. Ringeval
!
! futilities is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! futilities is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with futilities. If not, see <https://www.gnu.org/licenses/>.
module iofifo
use fifo, only : ends, qval, node
implicit none
private
public dump_queue_vals, read_queue_vals
public write_queue_vals, check_queue_file
integer, parameter :: lenIoRank = 4
integer, parameter :: lenIoMax = 64
logical, parameter :: display = .true.
contains
subroutine int2char_zero(icount,clen,ccount)
implicit none
integer, intent(in) :: icount
integer, intent(in) :: clen
character(len=clen), intent(out) :: ccount
character(len=lenIoMax) :: numToStrg
character(len=clen) :: strg
if (clen.gt.lenIoMax) then
stop 'step2char: clen > lenIoMax!'
endif
write(numToStrg,*) icount
strg = trim(adjustl(numToStrg))
strg = adjustr(strg)
call replace_char(strg,' ','0')
ccount(1:clen) = strg(1:clen)
end subroutine int2char_zero
subroutine replace_char(strg,charold,charnew)
implicit none
character(len=*), intent(inout) :: strg
character, intent(in) :: charold, charnew
integer :: position
position = 1
do
position = scan(strg,charold)
if (position.ne.0) then
strg(position:position) = charnew
else
exit
endif
enddo
end subroutine replace_char
function queue_filename(rank)
implicit none
character(len=lenIoMax) :: queue_filename
integer, intent(in), optional :: rank
character(len=lenIoRank) :: cRank
if (present(rank)) then
call int2char_zero(rank,lenIoRank,cRank)
queue_filename = 'queue'//cRank//'.bin'
else
queue_filename = 'queue.bin'
endif
end function queue_filename
subroutine dump_queue_vals(qbounds, rank)
implicit none
type(ends), intent(in) :: qbounds
integer, intent(in), optional :: rank
type(node), pointer :: ptrnode
character(len=lenIoMax) :: filename
integer, parameter :: nunit = 300
logical, parameter :: goforward = .true.
if (qbounds%size.eq.0) stop 'write_queue_vals: queue empty!'
if (present(rank)) then
filename = queue_filename(rank)
else
filename = queue_filename()
endif
open(file=filename,unit=nunit,form='unformatted',status='unknown')
write(nunit) qbounds%size
if (goforward) then
ptrnode => qbounds%front
do
write(nunit) ptrnode%val
if (.not.associated(ptrnode%next)) exit
ptrnode => ptrnode%next
end do
else
ptrnode => qbounds%back
do
write(nunit)ptrnode%val
if (.not.associated(ptrnode%prev)) exit
ptrnode => ptrnode%prev
end do
endif
close(nunit)
ptrnode => null()
end subroutine dump_queue_vals
function check_queue_file(rank)
implicit none
integer, intent(in), optional :: rank
logical :: check_queue_file
integer, parameter :: nunit = 301
character(len=lenIoMax) :: filename
logical :: ishere
integer :: nsize,i
if (present(rank)) then
filename = queue_filename(rank)
else
filename = queue_filename()
endif
inquire(file=filename,exist=ishere)
check_queue_file = ishere
end function check_queue_file
subroutine read_queue_vals(ptrval, rank)
implicit none
type(qval), dimension(:), intent(inout), pointer :: ptrval
integer, intent(in), optional :: rank
integer, parameter :: nunit = 301
character(len=lenIoMax) :: filename
logical :: ishere
integer :: nsize,i
if (associated(ptrval)) stop 'read_queue: ptrval already full!'
if (present(rank)) then
filename = queue_filename(rank)
else
filename = queue_filename()
endif
inquire(file=filename,exist=ishere)
if (.not.ishere) then
write(*,*)'searching file ',filename
stop 'read_queue_vals: file not found!'
endif
open(file=filename,unit=nunit,form='unformatted',status='old')
read(nunit) nsize
if (display) then
write(*,*)'read_queue_vals: reading queue... ',nsize
endif
allocate(ptrval(nsize))
do i=1,nsize
read(nunit) ptrval(i)
enddo
close(nunit)
end subroutine read_queue_vals
subroutine write_queue_vals(qbounds, rank)
implicit none
type(ends), intent(in) :: qbounds
integer, intent(in), optional :: rank
type(node), pointer :: ptrnode
character(len=lenIoMax) :: filename
integer, parameter :: nunit = 302
logical, parameter :: goforward = .true.
if (qbounds%size.eq.0) stop 'write_queue_vals: queue empty!'
if (present(rank)) then
filename = 'asc'//queue_filename(rank)
else
filename = 'asc'//queue_filename()
endif
print *,'filename ', filename, qbounds%size
open(file=filename,unit=nunit,form='formatted',status='new')
write(nunit,*) qbounds%size
if (goforward) then
print *,'val',associated(qbounds%front)
ptrnode => qbounds%front
do
print *,'val',ptrnode%val
write(nunit,*) ptrnode%val
if (.not.associated(ptrnode%next)) exit
ptrnode => ptrnode%next
end do
else
ptrnode => qbounds%back
do
write(nunit,*) ptrnode%val
if (.not.associated(ptrnode%prev)) exit
ptrnode => ptrnode%prev
end do
endif
close(nunit)
ptrnode => null()
end subroutine write_queue_vals
end module iofifo