-
Notifications
You must be signed in to change notification settings - Fork 0
/
pputil_yj.f90
396 lines (392 loc) · 12.1 KB
/
pputil_yj.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
MODULE pputil
use constants, only : p_
IMPLICIT NONE
PRIVATE
PUBLIC :: ppinit, ppexit, init_pmove, end_pmove, pmove, pmove2
!
INTEGER, SAVE :: me, nvp,GCLR,TCLR
INTEGER, SAVE :: pmove_tag=0
INTEGER, SAVE :: TUBE_COMM,GRID_COMM
REAL(P_), DIMENSION(:), ALLOCATABLE, SAVE :: s_buf, r_buf
logical, DIMENSION(:), ALLOCATABLE, SAVE :: s_buf2, r_buf2 !yj added
INTEGER, DIMENSION(:), ALLOCATABLE, SAVE :: s_counts, s_displ
INTEGER, DIMENSION(:), ALLOCATABLE, SAVE :: r_counts, r_displ
INTEGER, DIMENSION(:), ALLOCATABLE, SAVE :: ipsend, iphole
!
CONTAINS
!
!===========================================================================
SUBROUTINE init_pmove(xp, np, lz, ierr)
!
INCLUDE 'mpif.h'
!
REAL(P_), DIMENSION(:), INTENT(in) :: xp
INTEGER, INTENT(in) :: np
REAL(P_), INTENT(in) :: lz
INTEGER, INTENT(out) :: ierr
!
! Local vars
INTEGER :: nsize, ksize
INTEGER :: i, ip, iz, ih, iwork
REAL(P_) :: dzz, xt
INTEGER, DIMENSION(0:nvp-1) :: isb
!
!----------------------------------------------------------------------
! 0. Allocate fixed size arrays
!
IF( .not. ALLOCATED(s_counts) ) ALLOCATE(s_counts(0:nvp-1))
IF( .not. ALLOCATED(s_displ) ) ALLOCATE(s_displ(0:nvp-1))
IF( .not. ALLOCATED(r_counts) ) ALLOCATE(r_counts(0:nvp-1))
IF( .not. ALLOCATED(r_displ) ) ALLOCATE(r_displ(0:nvp-1))
!
!----------------------------------------------------------------------
! 1. Construct send buffer
!
dzz = lz / nvp
s_counts = 0
DO ip = 1,np
!yj xt = MODULO(xp(ip), lz) !!! Assume periodicity
xt = xp(ip)+ lz/2 !!!yjhu
iz = INT(xt/dzz)
IF( iz .ne. GCLR )THEN
s_counts(iz) = s_counts(iz)+1
END IF
END DO
s_displ(0) = 0
DO i=1,nvp-1
s_displ(i) = s_displ(i-1) + s_counts(i-1)
END DO
!
nsize = sum(s_counts)
IF( .not. ALLOCATED(s_buf) ) THEN
ksize=2*nsize ! To prevent too much futur reallocations
ALLOCATE(s_buf(1:ksize))
ALLOCATE(s_buf2(1:ksize))
ALLOCATE(ipsend(1:ksize))
ALLOCATE(iphole(1:ksize))
ELSE IF ( SIZE(s_buf) .LT. nsize ) THEN
DEALLOCATE(s_buf)
DEALLOCATE(s_buf2)
DEALLOCATE(ipsend)
DEALLOCATE(iphole)
ALLOCATE(s_buf(1:nsize))
ALLOCATE(s_buf2(1:nsize))
ALLOCATE(ipsend(1:nsize))
ALLOCATE(iphole(1:nsize))
END IF
!
!----------------------------------------------------------------------
! 2. Construct (sorted) pointers to holes
!
isb(0:nvp-1) = s_displ(0:nvp-1)
ih = 0
DO ip=1,np
!yj xt = MODULO(xp(ip), lz) !!! Assume periodicity
xt = xp(ip)+ lz/2 !!!yjhu
iz = INT(xt/dzz)
IF( iz .ne. GCLR ) THEN
isb(iz) = isb(iz)+1
ipsend(isb(iz)) = ip
ih = ih+1
iphole(ih) = ip
END IF
END DO
!
!----------------------------------------------------------------------
! 3. Construct receive buffer
!
CALL MPI_ALLTOALL(s_counts, 1, MPI_INTEGER, &
& r_counts, 1, MPI_INTEGER, TUBE_COMM, ierr)
r_displ(0) = 0
DO i=1,nvp-1
r_displ(i) = r_displ(i-1) + r_counts(i-1)
END DO
!
nsize = sum(r_counts)
IF( .not. ALLOCATED(r_buf) ) THEN
ksize=2*nsize ! To prevent too much futur reallocations
ALLOCATE(r_buf(1:ksize))
ALLOCATE(r_buf2(1:ksize))
ELSE IF ( SIZE(r_buf) .LT. nsize ) THEN
DEALLOCATE(r_buf)
DEALLOCATE(r_buf2)
ALLOCATE(r_buf(1:nsize))
ALLOCATE(r_buf2(1:nsize))
END IF
!
! Check for part. array overflow
ierr = 0
nsize = np - sum(s_counts) + sum(r_counts)
if( nsize .gt. size(xp) ) then
write(*,*) 'PE', me, 'Particle array overflow'
ierr = 1
end if
! call ppsum(ierr)
!
!----------------------------------------------------------------------
! 4. Initialize tag
!
pmove_tag = 101
!
END SUBROUTINE init_pmove
!===========================================================================
SUBROUTINE pmove(xp, np_old, np_new, ierr)
!
INCLUDE 'mpif.h'
!
REAL(P_), DIMENSION(:), INTENT(inout) :: xp
INTEGER, INTENT(in) :: np_old
INTEGER, INTENT(out) :: np_new
INTEGER, INTENT(out) :: ierr
!
! Local vars
INTEGER :: nsize
INTEGER :: i, ip, iz, ih, isrc
INTEGER :: nhole, mhole, nrrecv, nrsend, nptot_old, nptot
INTEGER :: ind, count, tot_count, iwork
!
! Local arrays
INTEGER, DIMENSION(1:nvp) :: s_requ, r_requ, id_source
INTEGER :: isrt, iend
INTEGER :: status(MPI_STATUS_SIZE), arr_status(MPI_STATUS_SIZE,nvp)
!
!----------------------------------------------------------------------
! 1. Fill send buffer
!
DO i=0,nvp-1
IF( s_counts(i) .GT. 0 ) THEN
isrt = s_displ(i)+1
iend = s_displ(i)+s_counts(i)
s_buf(isrt:iend) = xp(ipsend(isrt:iend))
END IF
END DO
!----------------------------------------------------------------------
! 2. Initiate non-blocking send/receive
!
pmove_tag = pmove_tag+1
nrrecv=0 !......... Start non-blocking receive
DO i=0,nvp-1
IF(r_counts(i) .GT. 0 ) THEN
nrrecv=nrrecv+1
id_source(nrrecv) = i
isrt = r_displ(i)+1
CALL MPI_IRECV(r_buf(isrt), r_counts(i), MPI_REAL8,&
& i, pmove_tag, TUBE_COMM, r_requ(nrrecv), ierr)
END IF
END DO
nrsend=0 !......... Start non-blocking SYNCHRONOUS send
DO i=0,nvp-1
IF(s_counts(i) .GT. 0 ) THEN
nrsend=nrsend+1
isrt = s_displ(i)+1
CALL MPI_ISSEND(s_buf(isrt), s_counts(i), MPI_REAL8,&
& i, pmove_tag, TUBE_COMM, s_requ(nrsend), ierr)
END IF
END DO
!
!----------------------------------------------------------------------
! 3. Remove holes and compress part. arrays
!
nhole = sum(s_counts)
ip = np_old
DO ih = nhole, 1, -1
xp(iphole(ih)) = xp(ip)
ip = ip-1
END DO
np_new = ip
!
!----------------------------------------------------------------------
! 4. Store incoming part. to the part. arrays
!
tot_count = 0
DO i=1,nrrecv
CALL MPI_WAITANY(nrrecv, r_requ, ind, status, ierr)
isrc = id_source(ind)
CALL MPI_GET_COUNT(status, MPI_REAL8, count, ierr)
IF( count .ne. r_counts(isrc) ) THEN
WRITE(*,*) 'PE',me, ' Counts mismatched from PE',isrc,&
& count, r_counts(isrc)
END IF
tot_count = tot_count+count
END DO
!
IF( tot_count .GT. 0 ) THEN
isrt = np_new + 1
iend = np_new + tot_count
xp(isrt:iend) = r_buf(1:tot_count)
np_new = iend
END IF
!
!----------------------------------------------------------------------
! 5. Epilogue
!
!... Wait for any non-blocking comm. requests
IF( nrsend.gt.0) CALL MPI_WAITALL(nrsend, s_requ, arr_status, ierr)
!
!... Check consistency
ierr = 0
CALL MPI_ALLREDUCE(np_old, nptot_old, 1, MPI_INTEGER,&
& MPI_SUM, MPI_COMM_WORLD, ierr)
CALL MPI_ALLREDUCE(np_new, nptot, 1, MPI_INTEGER,&
& MPI_SUM, MPI_COMM_WORLD, ierr)
IF( nptot.ne.nptot_old ) THEN
IF(me.eq.0) WRITE(*,*) 'PMOVE: mismatch in total numbers:',&
& nptot_old, nptot
ierr = 1
END IF
! call ppsum(ierr)
!
!----------------------------------------------------------------------!
END SUBROUTINE pmove
SUBROUTINE pmove2(xp, np_old, np_new, ierr) !differ from pmove() in that it deal with logical array, instead of real array, by yjhu
!
INCLUDE 'mpif.h'
!
logical, DIMENSION(:), INTENT(inout) :: xp
INTEGER, INTENT(in) :: np_old
INTEGER, INTENT(out) :: np_new
INTEGER, INTENT(out) :: ierr
!
! Local vars
INTEGER :: nsize
INTEGER :: i, ip, iz, ih, isrc
INTEGER :: nhole, mhole, nrrecv, nrsend, nptot_old, nptot
INTEGER :: ind, count, tot_count, iwork
!
! Local arrays
INTEGER, DIMENSION(1:nvp) :: s_requ, r_requ, id_source
INTEGER :: isrt, iend
INTEGER :: status(MPI_STATUS_SIZE), arr_status(MPI_STATUS_SIZE,nvp)
!
!----------------------------------------------------------------------
! 1. Fill send buffer
!
DO i=0,nvp-1
IF( s_counts(i) .GT. 0 ) THEN
isrt = s_displ(i)+1
iend = s_displ(i)+s_counts(i)
s_buf2(isrt:iend) = xp(ipsend(isrt:iend))
END IF
END DO
!----------------------------------------------------------------------
! 2. Initiate non-blocking send/receive
!
pmove_tag = pmove_tag+1
nrrecv=0 !......... Start non-blocking receive
DO i=0,nvp-1
IF(r_counts(i) .GT. 0 ) THEN
nrrecv=nrrecv+1
id_source(nrrecv) = i
isrt = r_displ(i)+1
CALL MPI_IRECV(r_buf2(isrt), r_counts(i), MPI_logical,&
& i, pmove_tag, TUBE_COMM, r_requ(nrrecv), ierr)
END IF
END DO
nrsend=0 !......... Start non-blocking SYNCHRONOUS send
DO i=0,nvp-1
IF(s_counts(i) .GT. 0 ) THEN
nrsend=nrsend+1
isrt = s_displ(i)+1
CALL MPI_ISSEND(s_buf2(isrt), s_counts(i), MPI_logical,&
& i, pmove_tag, TUBE_COMM, s_requ(nrsend), ierr)
END IF
END DO
!
!----------------------------------------------------------------------
! 3. Remove holes and compress part. arrays
!
nhole = sum(s_counts)
ip = np_old
DO ih = nhole, 1, -1
xp(iphole(ih)) = xp(ip)
ip = ip-1
END DO
np_new = ip
!
!----------------------------------------------------------------------
! 4. Store incoming part. to the part. arrays
!
tot_count = 0
DO i=1,nrrecv
CALL MPI_WAITANY(nrrecv, r_requ, ind, status, ierr)
isrc = id_source(ind)
CALL MPI_GET_COUNT(status, MPI_logical, count, ierr)
IF( count .ne. r_counts(isrc) ) THEN
WRITE(*,*) 'PE',me, ' Counts mismatched from PE',isrc,&
& count, r_counts(isrc)
END IF
tot_count = tot_count+count
END DO
!
IF( tot_count .GT. 0 ) THEN
isrt = np_new + 1
iend = np_new + tot_count
xp(isrt:iend) = r_buf2(1:tot_count)
np_new = iend
END IF
!
!----------------------------------------------------------------------
! 5. Epilogue
!
!... Wait for any non-blocking comm. requests
IF( nrsend.gt.0) CALL MPI_WAITALL(nrsend, s_requ, arr_status, ierr)
!
!... Check consistency
ierr = 0
CALL MPI_ALLREDUCE(np_old, nptot_old, 1, MPI_INTEGER,&
& MPI_SUM, MPI_COMM_WORLD, ierr)
CALL MPI_ALLREDUCE(np_new, nptot, 1, MPI_INTEGER,&
& MPI_SUM, MPI_COMM_WORLD, ierr)
IF( nptot.ne.nptot_old ) THEN
IF(me.eq.0) WRITE(*,*) 'PMOVE: mismatch in total numbers:',&
& nptot_old, nptot
ierr = 1
END IF
! call ppsum(ierr)
!
!----------------------------------------------------------------------!
END SUBROUTINE pmove2
!===========================================================================
SUBROUTINE end_pmove(ierr)
!
INCLUDE 'mpif.h'
INTEGER, INTENT(OUT) :: ierr
!
! Local vars
!----------------------------------------------------------------------!
ierr=1 !yj
END SUBROUTINE end_pmove
!
!
SUBROUTINE ppinit(ntube,com1,com2)
use domain_decomposition, only : myid, numprocs
!INCLUDE 'mpif.h'
! use petscksp
use mpi
!#include <petsc/finclude/petscksp.h>
INTEGER, INTENT(IN) :: ntube
INTEGER, INTENT(OUT) :: com1,com2
INTEGER :: ierr
!call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
me = myid
me=myid
GCLR=INT(me/ntube)
TCLR=MOD(me,ntube)
CALL MPI_COMM_SPLIT(MPI_COMM_WORLD,GCLR,TCLR,GRID_COMM,ierr)
CALL MPI_COMM_SPLIT(MPI_COMM_WORLD,TCLR,GCLR,TUBE_COMM,ierr)
com1=TUBE_COMM
com2=GRID_COMM
nvp=numprocs/ntube
END SUBROUTINE ppinit
!
!===========================================================================
SUBROUTINE ppexit
INTEGER :: ierr
CALL MPI_FINALIZE(ierr)
STOP
END SUBROUTINE ppexit
!
END MODULE pputil
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!