-
Notifications
You must be signed in to change notification settings - Fork 12
/
halo_common.f90
425 lines (400 loc) · 13.7 KB
/
halo_common.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
!=======================================================================
! This is part of the 2DECOMP&FFT library
!
! 2DECOMP&FFT is a software framework for general-purpose 2D (pencil)
! decomposition. It also implements a highly scalable distributed
! three-dimensional Fast Fourier Transform (FFT).
!
! Copyright (C) 2009-2011 Ning Li, the Numerical Algorithms Group (NAG)
!
!=======================================================================
! This file contain common code to be included by subroutines
! 'update_halo_...' in halo.f90
if (present(opt_decomp)) then
decomp = opt_decomp
else
decomp = decomp_main
end if
if (present(opt_global)) then
global = opt_global
else
global = .false.
end if
s1 = size(in,1)
s2 = size(in,2)
s3 = size(in,3)
! Calculate the starting index and ending index of output
if (s1==decomp%xsz(1)) then ! X-pencil input
if (global) then
xs = decomp%xst(1)
xe = decomp%xen(1)
ys = decomp%xst(2) - level
ye = decomp%xen(2) + level
zs = decomp%xst(3) - level
ze = decomp%xen(3) + level
else
xs = 1
xe = s1
ys = 1 - level
ye = s2 + level
zs = 1 - level
ze = s3 + level
end if
else if (s2==decomp%ysz(2)) then ! Y-pencil input
if (global) then
xs = decomp%yst(1) - level
xe = decomp%yen(1) + level
ys = decomp%yst(2)
ye = decomp%yen(2)
zs = decomp%yst(3) - level
ze = decomp%yen(3) + level
else
xs = 1 - level
xe = s1 + level
ys = 1
ye = s2
zs = 1 - level
ze = s3 + level
end if
else if (s3==decomp%zsz(3)) then ! Z-pencil input
if (global) then
xs = decomp%zst(1) - level
xe = decomp%zen(1) + level
ys = decomp%zst(2) - level
ye = decomp%zen(2) + level
zs = decomp%zst(3)
ze = decomp%zen(3)
else
xs = 1 - level
xe = s1 + level
ys = 1 - level
ye = s2 + level
zs = 1
ze = s3
end if
else
! invalid input
call decomp_2d_abort(10, &
'Invalid data passed to update_halo')
end if
allocate(out(xs:xe, ys:ye, zs:ze))
! out = -1.0_mytype ! fill the halo for debugging
! copy input data to output
if (global) then
! using global coordinate
! note the input array passed in always has index starting from 1
! need to work out the corresponding global index
if (s1==decomp%xsz(1)) then
do k=decomp%xst(3),decomp%xen(3)
do j=decomp%xst(2),decomp%xen(2)
do i=1,s1 ! x all local
out(i,j,k) = in(i,j-decomp%xst(2)+1,k-decomp%xst(3)+1)
end do
end do
end do
else if (s2==decomp%ysz(2)) then
do k=decomp%yst(3),decomp%yen(3)
do j=1,s2 ! y all local
do i=decomp%yst(1),decomp%yen(1)
out(i,j,k) = in(i-decomp%yst(1)+1,j,k-decomp%yst(3)+1)
end do
end do
end do
else if (s3==decomp%zsz(3)) then
do k=1,s3 ! z all local
do j=decomp%zst(2),decomp%zen(2)
do i=decomp%zst(1),decomp%zen(1)
out(i,j,k) = in(i-decomp%zst(1)+1,j-decomp%zst(2)+1,k)
end do
end do
end do
end if
else
! not using global coordinate
do k=1,s3
do j=1,s2
do i=1,s1
out(i,j,k) = in(i,j,k)
end do
end do
end do
end if
! If needed, define MPI derived data type to pack halo data,
! then call MPI send/receive to exchange halo data
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! X-pencil
if (s1==decomp%xsz(1)) then
#ifdef HALO_DEBUG
if (nrank==4) then
write(*,*) 'X-pencil input'
write(*,*) '=============='
write(*,*) 'Data on a y-z plane is shown'
write(*,*) 'Before halo exchange'
do j=ye,ys,-1
write(*,'(10F4.0)') (out(1,j,k),k=zs,ze)
end do
end if
#endif
! *** east/west ***
! all data in local memory already, no halo exchange
! *** north/south ***
tag_s = coord(1)
if (coord(1)==dims(1)-1 .AND. periodic_y) then
tag_n = 0
else
tag_n = coord(1) + 1
end if
icount = s3 + 2*level
ilength = level * s1
ijump = s1*(s2+2*level)
call MPI_TYPE_VECTOR(icount, ilength, ijump, &
data_type, halo12, ierror)
call MPI_TYPE_COMMIT(halo12, ierror)
! receive from south
call MPI_IRECV(out(xs,ys,zs), 1, halo12, &
neighbour(1,4), tag_s, DECOMP_2D_COMM_CART_X, &
requests(1), ierror)
! receive from north
call MPI_IRECV(out(xs,ye-level+1,zs), 1, halo12, &
neighbour(1,3), tag_n, DECOMP_2D_COMM_CART_X, &
requests(2), ierror)
! send to south
call MPI_ISSEND(out(xs,ys+level,zs), 1, halo12, &
neighbour(1,4), tag_s, DECOMP_2D_COMM_CART_X, &
requests(3), ierror)
! send to north
call MPI_ISSEND(out(xs,ye-level-level+1,zs), 1, halo12, &
neighbour(1,3), tag_n, DECOMP_2D_COMM_CART_X, &
requests(4), ierror)
call MPI_WAITALL(4, requests, status, ierror)
call MPI_TYPE_FREE(halo12, ierror)
#ifdef HALO_DEBUG
if (nrank==4) then
write(*,*) 'After exchange in Y'
do j=ye,ys,-1
write(*,'(10F4.0)') (out(1,j,k),k=zs,ze)
end do
end if
#endif
! *** top/bottom ***
! no need to define derived data type as data on xy-planes
! all contiguous in memory, which can be sent/received using
! MPI directly
tag_b = coord(2)
if (coord(2)==dims(2)-1 .AND. periodic_z) then
tag_t = 0
else
tag_t = coord(2) + 1
end if
icount = (s1 * (s2+2*level)) * level
! receive from bottom
call MPI_IRECV(out(xs,ys,zs), icount, data_type, &
neighbour(1,6), tag_b, DECOMP_2D_COMM_CART_X, &
requests(1), ierror)
! receive from top
call MPI_IRECV(out(xs,ys,ze-level+1), icount, data_type, &
neighbour(1,5), tag_t, DECOMP_2D_COMM_CART_X, &
requests(2), ierror)
! send to bottom
call MPI_ISSEND(out(xs,ys,zs+level), icount, data_type, &
neighbour(1,6), tag_b, DECOMP_2D_COMM_CART_X, &
requests(3), ierror)
! send to top
call MPI_ISSEND(out(xs,ys,ze-level-level+1), icount, data_type, &
neighbour(1,5), tag_t, DECOMP_2D_COMM_CART_X, &
requests(4), ierror)
call MPI_WAITALL(4, requests, status, ierror)
#ifdef HALO_DEBUG
if (nrank==4) then
write(*,*) 'After exchange in Z'
do j=ye,ys,-1
write(*,'(10F4.0)') (out(1,j,k),k=zs,ze)
end do
end if
#endif
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Y-pencil
else if (s2==decomp%ysz(2)) then
#ifdef HALO_DEBUG
if (nrank==4) then
write(*,*) 'Y-pencil input'
write(*,*) '=============='
write(*,*) 'Data on a x-z plane is shown'
write(*,*) 'Before halo exchange'
do i=xe,xs,-1
write(*,'(10F4.0)') (out(i,1,k),k=zs,ze)
end do
end if
#endif
! *** east/west ***
tag_w = coord(1)
if (coord(1)==dims(1)-1 .AND. periodic_x) then
tag_e = 0
else
tag_e = coord(1) + 1
end if
icount = s2*(s3+2*level)
ilength = level
ijump = s1+2*level
call MPI_TYPE_VECTOR(icount, ilength, ijump, &
data_type, halo21, ierror)
call MPI_TYPE_COMMIT(halo21, ierror)
! receive from west
call MPI_IRECV(out(xs,ys,zs), 1, halo21, &
neighbour(2,2), tag_w, DECOMP_2D_COMM_CART_Y, &
requests(1), ierror)
! receive from east
call MPI_IRECV(out(xe-level+1,ys,zs), 1, halo21, &
neighbour(2,1), tag_e, DECOMP_2D_COMM_CART_Y, &
requests(2), ierror)
! send to west
call MPI_ISSEND(out(xs+level,ys,zs), 1, halo21, &
neighbour(2,2), tag_w, DECOMP_2D_COMM_CART_Y, &
requests(3), ierror)
! send to east
call MPI_ISSEND(out(xe-level-level+1,ys,zs), 1, halo21, &
neighbour(2,1), tag_e, DECOMP_2D_COMM_CART_Y, &
requests(4), ierror)
call MPI_WAITALL(4, requests, status, ierror)
call MPI_TYPE_FREE(halo21, ierror)
#ifdef HALO_DEBUG
if (nrank==4) then
write(*,*) 'After exchange in X'
do i=xe,xs,-1
write(*,'(10F4.0)') (out(i,1,k),k=zs,ze)
end do
end if
#endif
! *** north/south ***
! all data in local memory already, no halo exchange
! *** top/bottom ***
! no need to define derived data type as data on xy-planes
! all contiguous in memory, which can be sent/received using
! MPI directly
tag_b = coord(2)
if (coord(2)==dims(2)-1 .AND. periodic_z) then
tag_t = 0
else
tag_t = coord(2) + 1
end if
icount = (s2 * (s1+2*level)) * level
! receive from bottom
call MPI_IRECV(out(xs,ys,zs), icount, data_type, &
neighbour(2,6), tag_b, DECOMP_2D_COMM_CART_Y, &
requests(1), ierror)
! receive from top
call MPI_IRECV(out(xs,ys,ze-level+1), icount, data_type, &
neighbour(2,5), tag_t, DECOMP_2D_COMM_CART_Y, &
requests(2), ierror)
! send to bottom
call MPI_ISSEND(out(xs,ys,zs+level), icount, data_type, &
neighbour(2,6), tag_b, DECOMP_2D_COMM_CART_Y, &
requests(3), ierror)
! send to top
call MPI_ISSEND(out(xs,ys,ze-level-level+1), icount, data_type, &
neighbour(2,5), tag_t, DECOMP_2D_COMM_CART_Y, &
requests(4), ierror)
call MPI_WAITALL(4, requests, status, ierror)
#ifdef HALO_DEBUG
if (nrank==4) then
write(*,*) 'After exchange in Z'
do i=xe,xs,-1
write(*,'(10F4.0)') (out(i,1,k),k=zs,ze)
end do
end if
#endif
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Z-pencil
else if (s3==decomp%zsz(3)) then
#ifdef HALO_DEBUG
if (nrank==4) then
write(*,*) 'Z-pencil input'
write(*,*) '=============='
write(*,*) 'Data on a x-y plane is shown'
write(*,*) 'Before halo exchange'
do i=xe,xs,-1
write(*,'(10F4.0)') (out(i,j,1),j=ys,ye)
end do
end if
#endif
! *** east/west ***
tag_w = coord(1)
if (coord(1)==dims(1)-1 .AND. periodic_x) then
tag_e = 0
else
tag_e = coord(1) + 1
end if
icount = (s2+2*level)*s3
ilength = level
ijump = s1+2*level
call MPI_TYPE_VECTOR(icount, ilength, ijump, &
data_type, halo31, ierror)
call MPI_TYPE_COMMIT(halo31, ierror)
! receive from west
call MPI_IRECV(out(xs,ys,zs), 1, halo31, &
neighbour(3,2), tag_w, DECOMP_2D_COMM_CART_Z, &
requests(1), ierror)
! receive from east
call MPI_IRECV(out(xe-level+1,ys,zs), 1, halo31, &
neighbour(3,1), tag_e, DECOMP_2D_COMM_CART_Z, &
requests(2), ierror)
! send to west
call MPI_ISSEND(out(xs+level,ys,zs), 1, halo31, &
neighbour(3,2), tag_w, DECOMP_2D_COMM_CART_Z, &
requests(3), ierror)
! send to east
call MPI_ISSEND(out(xe-level-level+1,ys,zs), 1, halo31, &
neighbour(3,1), tag_e, DECOMP_2D_COMM_CART_Z, &
requests(4), ierror)
call MPI_WAITALL(4, requests, status, ierror)
call MPI_TYPE_FREE(halo31, ierror)
#ifdef HALO_DEBUG
if (nrank==4) then
write(*,*) 'After exchange in X'
do i=xe,xs,-1
write(*,'(10F4.0)') (out(i,j,1),j=ys,ye)
end do
end if
#endif
! *** north/south ***
tag_s = coord(2)
if (coord(2)==dims(2)-1 .AND. periodic_y) then
tag_n = 0
else
tag_n = coord(2) + 1
end if
icount = s3
ilength = level * (s1+2*level)
ijump = (s1+2*level) * (s2+2*level)
call MPI_TYPE_VECTOR(icount, ilength, ijump, &
data_type, halo32, ierror)
call MPI_TYPE_COMMIT(halo32, ierror)
! receive from south
call MPI_IRECV(out(xs,ys,zs), 1, halo32, &
neighbour(3,4), tag_s, DECOMP_2D_COMM_CART_Z, &
requests(1), ierror)
! receive from north
call MPI_IRECV(out(xs,ye-level+1,zs), 1, halo32, &
neighbour(3,3), tag_n, DECOMP_2D_COMM_CART_Z, &
requests(2), ierror)
! send to south
call MPI_ISSEND(out(xs,ys+level,zs), 1, halo32, &
neighbour(3,4), tag_s, DECOMP_2D_COMM_CART_Z, &
requests(3), ierror)
! send to north
call MPI_ISSEND(out(xs,ye-level-level+1,zs), 1, halo32, &
neighbour(3,3), tag_n, DECOMP_2D_COMM_CART_Z, &
requests(4), ierror)
call MPI_WAITALL(4, requests, status, ierror)
call MPI_TYPE_FREE(halo32, ierror)
#ifdef HALO_DEBUG
if (nrank==4) then
write(*,*) 'After exchange in Y'
do i=xe,xs,-1
write(*,'(10F4.0)') (out(i,j,1),j=ys,ye)
end do
end if
#endif
! *** top/bottom ***
! all data in local memory already, no halo exchange
end if ! pencil