-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSolvePressureCorrection.F90
More file actions
392 lines (342 loc) · 14.6 KB
/
Copy pathSolvePressureCorrection.F90
File metadata and controls
392 lines (342 loc) · 14.6 KB
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
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! !
! FILE: SolvePressureCorrection.F90 !
! CONTAINS: subroutine SolvePressureCorrection, !
! CreateFFTTmpArrays, DestroyFFTTmpArrays !
! !
! PURPOSE: Compute the pressure correction by solving !
! a Poisson equation !
! !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#define BATCH_RND (32)
!#define NO_BATCH
subroutine SolvePressureCorrection(ry, cy, cz, dphc)
use, intrinsic :: iso_c_binding
use param
use fftw_params
use local_arrays, only: dph
#ifdef USE_CUDA
use local_arrays, only: dph_d
use ep_trans
use ep_solve
#endif
use decomp_2d
use decomp_2d_fft
use mpih
implicit none
real(fp_kind), dimension(ph%yst(1):ph%yen(1), ph%yst(2):ph%yen(2), ph%yst(3):ph%yen(3)) :: ry
complex(fp_kind), dimension(sp%yst(1):sp%yen(1), sp%yst(2):sp%yen(2), sp%yst(3):sp%yen(3)) :: cy
complex(fp_kind), dimension(sp%zst(1):sp%zen(1), sp%zst(2):sp%zen(2), sp%zst(3):sp%zen(3)) :: cz
#ifndef USE_CUDA
complex(fp_kind), dimension(sp%xst(1):sp%xen(1), sp%xst(2):sp%xen(2), sp%xst(3):sp%xen(3)) :: dphc
#else
complex(fp_kind), dimension(sp%wst(1):sp%wen(1), sp%wst(2):sp%wen(2), sp%wst(3):sp%wen(3)) :: dphc
attributes(device) :: ry, cy, cz, dphc
#endif
integer :: i,j,k,info
complex(fp_kind) :: acphT_b
complex(fp_kind) :: appph(nxm-2)
complex(fp_kind), dimension(nxm) :: acphT,apph,amph
integer :: phpiv(nxm)
integer :: nymh
#ifdef DEBUG
print *,"-----In SolvePressure",minval(dph),maxval(dph)
#endif
nymh=nym/2+1
#ifdef USE_CUDA
call transpose_x_to_y(dph_d,ry,ph)
#else
call transpose_x_to_y(dph,ry,ph)
#endif
#ifdef USE_CUDA
call trans_xy(ry, work1_r_d, SIZE(ry,1), SIZE(ry,2), SIZE(ry,3))
#ifdef NO_BATCH
do i=1,(ph%yen(1)-ph%yst(1)+1)*(ph%yen(3)-ph%yst(3)+1)
istat = cufftExecD2Z(cufft_plan_fwd_y,work1_r_d((i-1)*nym+1),work1_c_d((i-1)*nymh+1))
end do
#else
istat = cufftExecD2Z(cufft_plan_fwd_y,work1_r_d,work1_c_d)
#endif
call trans_yx(work1_c_d, cy, SIZE(cy,2), SIZE(cy,1), SIZE(cy,3))
#else
call dfftw_execute_dft_r2c(fwd_guruplan_y,ry,cy)
#endif
#ifdef USE_CUDA
call transpose_y_to_z(cy,cz,sp)
#else
call transpose_y_to_z(cy,cz,sp)
#endif
#ifdef USE_CUDA
call trans_yz(cz,work1_c_d,SIZE(cz,1),SIZE(cz,2),SIZE(cz,3))
#ifdef NO_BATCH
do i=1,((sp%zen(1)-sp%zst(1)+1)*(sp%zen(2)-sp%zst(2)+1))
istat = cufftExecZ2Z(cufft_plan_fwd_z,work1_c_d((i-1)*nzm+1),work1_c_d((i-1)*nzm+1),CUFFT_FORWARD)
end do
#else
istat = cufftExecZ2Z(cufft_plan_fwd_z,work1_c_d,work1_c_d,CUFFT_FORWARD)
#endif
call trans_zy(work1_c_d,cz,SIZE(cz,3),SIZE(cz,1),SIZE(cz,2))
#else
call dfftw_execute_dft(fwd_guruplan_z,cz,cz)
#endif
!EP Normalize. FFT does not do this
#ifdef USE_CUDA
!$cuf kernel do(3) <<<*,*>>>
do k=sp%zst(3),sp%zen(3)
do j=sp%zst(2),sp%zen(2)
do i=sp%zst(1),sp%zen(1)
cz(i,j,k) = cz(i,j,k)/(nzm*nym)
end do
end do
end do
#else
cz = cz / (nzm*nym)
#endif
#ifdef USE_CUDA
call transpose_z_to_x(cz,dphc,sp)
#else
!! call transpose_z_to_x(cz,dphc,sp)
call transpose_z_to_y(cz,cy,sp)
call transpose_y_to_x(cy,dphc,sp)
#endif
!RO Solve the tridiagonal matrix with complex coefficients
#ifdef USE_CUDA
!call epZgtsv_pressure_piv_dd( nxm, SIZE(dphc_d,2), SIZE(dphc_d,3), amphk_d, acphk_d, apphk_d, ak1_d, ak2_d, dphc_d, SIZE(dphc_d,1), scratch_d, sp%xst(2), sp%xst(3))
!call epZgtsv_pressure_piv( nxm, SIZE(dphc_d,2), SIZE(dphc_d,3), amphk, acphk, apphk, ak1, ak2, dphc_d, SIZE(dphc_d,1), scratch, sp%xst(2), sp%xst(3))
!call epZgtsv_pressure( nxm, SIZE(dphc_d,2), SIZE(dphc_d,3), amphk, acphk, apphk, ak1, ak2, dphc_d, SIZE(dphc_d,1), scratch)
call tepZgtsv_pressure( nxm, SIZE(dphc,2), SIZE(dphc,3), amphk_d, acphk_d, apphk_d, ak1_d, ak2_d, dphc, SIZE(dphc,1), sp%wst(2), sp%wst(3),work1_c_d)
#else
!$OMP PARALLEL DO COLLAPSE(2) &
!$OMP DEFAULT(none) &
!$OMP SHARED(sp,nxm) &
!$OMP SHARED(acphk,ak2,ak1,dphc,apphk,amphk) &
!$OMP PRIVATE(apph,amph,acphT,acphT_b) &
!$OMP PRIVATE(phpiv,info,appph)
do i=sp%xst(3),sp%xen(3)
do j=sp%xst(2),sp%xen(2)
do k = 1,nxm
acphT_b=real(1.0,fp_kind)/(acphk(k)-ak2(j)-ak1(i))
dphc(k,j,i)=dphc(k,j,i)*acphT_b
apph(k)=apphk(k)*acphT_b
amph(k)=amphk(k)*acphT_b
acphT(k)=real(1.0,fp_kind)
enddo
call zgttrf(nxm, amph(2), acphT, apph(1), appph, phpiv, info)
call zgttrs('N',nxm,1,amph(2),acphT,apph(1),appph,phpiv, &
dphc(1,j,i), nxm, info)
enddo
enddo
!$OMP END PARALLEL DO
#endif
#ifdef USE_CUDA
call transpose_x_to_z(dphc,cz,sp)
#else
! call transpose_x_to_z(dphc,cz,sp)
call transpose_x_to_y(dphc,cy,sp)
call transpose_y_to_z(cy,cz,sp)
#endif
#ifdef USE_CUDA
call trans_yz(cz,work1_c_d,SIZE(cz,1),SIZE(cz,2),SIZE(cz,3))
#ifdef NO_BATCH
do i=1,((sp%zen(1)-sp%zst(1)+1)*(sp%zen(2)-sp%zst(2)+1))
istat = cufftExecZ2Z(cufft_plan_bwd_z,work1_c_d((i-1)*nzm+1),work1_c_d((i-1)*nzm+1),CUFFT_INVERSE)
end do
#else
istat = cufftExecZ2Z(cufft_plan_bwd_z,work1_c_d,work1_c_d,CUFFT_INVERSE)
#endif
call trans_zy(work1_c_d,cz,SIZE(cz,3),SIZE(cz,1),SIZE(cz,2))
#else
call dfftw_execute_dft(bwd_guruplan_z,cz,cz)
#endif
#ifdef USE_CUDA
call transpose_z_to_y(cz,cy,sp)
#else
call transpose_z_to_y(cz,cy,sp)
#endif
#ifdef USE_CUDA
call trans_yx(cy, work1_c_d, SIZE(cy,1), SIZE(cy,2),SIZE(cy,3))
#ifdef NO_BATCH
do i=1,(sp%yen(1)-sp%yst(1)+1)*(sp%yen(3)-sp%yst(3)+1)
istat = cufftExecZ2D(cufft_plan_bwd_y,work1_c_d((i-1)*nymh+1),work1_r_d((i-1)*nym+1))
end do
#else
istat = cufftExecZ2D(cufft_plan_bwd_y,work1_c_d,work1_r_d)
#endif
call trans_xy(work1_r_d, ry, SIZE(ry,2), SIZE(ry,1), SIZE(ry,3))
#else
call dfftw_execute_dft_c2r(bwd_guruplan_y,cy,ry)
#endif
#ifdef USE_CUDA
call transpose_y_to_x(ry,dph_d,ph)
#else
call transpose_y_to_x(ry,dph,ph)
#endif
#ifdef DEBUG
print *,"In SolvePressure",minval(dph),maxval(dph)
#endif
return
end
!======================================================================
subroutine CreateFFTTmpArrays
use fftw_params
use decomp_2d
use decomp_2d_fft
use param, only: nx
implicit none
integer :: nd_exp, nd_ry1, nd_cy1, nd_cz1, nd_dphc1
allocate(ry1(ph%yst(1):ph%yen(1), &
& ph%yst(2):ph%yen(2), &
& ph%yst(3):ph%yen(3)))
! allocate(rz1(ph%zst(1):ph%zen(1), &
! & ph%zst(2):ph%zen(2), &
! & ph%zst(3):ph%zen(3)))
allocate(cy1(sp%yst(1):sp%yen(1), &
& sp%yst(2):sp%yen(2), &
& sp%yst(3):sp%yen(3)))
allocate(cz1(sp%zst(1):sp%zen(1), &
& sp%zst(2):sp%zen(2), &
& sp%zst(3):sp%zen(3)))
allocate(dphc1(sp%xst(1):sp%xen(1), &
& sp%xst(2):sp%xen(2), &
& sp%xst(3):sp%xen(3)))
#ifdef USE_CUDA
! JR Replacing old FFT temporary buffers. Leaving this in place for future use if necessary.
! allocate(ry1_d(ph%yst(1):ph%yen(1), &
!& ph%yst(2):ph%yen(2), &
!& ph%yst(3):ph%yen(3)))
! allocate(cy1_d(sp%yst(1):sp%yen(1), &
!& sp%yst(2):sp%yen(2), &
!& sp%yst(3):sp%yen(3)))
! allocate(cz1_d(sp%zst(1):sp%zen(1), &
!& sp%zst(2):sp%zen(2), &
!& sp%zst(3):sp%zen(3)))
! allocate(dphc1_d(sp%wst(1):sp%wen(1), &
!& sp%wst(2):sp%wen(2), &
!& sp%wst(3):sp%wen(3)))
! JR Allocating buffers for fft ops. Size to be reused for explicit terms.
nd_exp = nx * xsize(2) * xsize(3) ! required number of doubles for explicit terms
nd_ry1 = ph%ysz(1) * ph%ysz(2) * ph%ysz(3) ! required number of doubles for ry1
nd_cy1 = 2*sp%ysz(1) * sp%ysz(2) * sp%ysz(3) ! required number of doubles for ry1
nd_cz1 = 2*sp%zsz(1) * sp%zsz(2) * sp%zsz(3) ! required number of doubles for rz1
nd_dphc1 = 2*sp%wsz(1) * sp%wsz(2) * sp%wsz(3) ! required number of doubles for rz1
allocate(rbuff1_d(max(nd_exp, max(nd_cy1, nd_dphc1)))) !sized to max size of exp term, cy1, or dphc1
allocate(rbuff2_d(max(nd_exp, max(nd_cz1, nd_ry1)))) !sized to max size of exp term, cz1, or ry1
#endif
return
end
!======================================================================
subroutine DestroyFFTTmpArrays
use fftw_params
implicit none
if(allocated(dphc1)) deallocate(dphc1)
! if(allocated(rz1)) deallocate(rz1)
if(allocated(cz1)) deallocate(cz1)
if(allocated(ry1)) deallocate(ry1)
if(allocated(cy1)) deallocate(cy1)
return
end
!======================================================================
subroutine InitPressureSolverPlans
use param
use fftw_params
use decomp_2d_fft
use mpih
#ifdef USE_CUDA
integer(int_ptr_kind()) :: worksize,max_worksize
max_worksize = 0
#ifdef NO_BATCH
batch = 1
#else
batch = (sp%zen(1)-sp%zst(1)+1)*(sp%zen(2)-sp%zst(2)+1)
#endif
! istat = cufftPlan1D(cufft_plan_fwd_z,nzm,CUFFT_Z2Z,batch)
istat = cufftCreate( cufft_plan_fwd_z )
istat = cufftSetAutoAllocation( cufft_plan_fwd_z, 0 )
istat = cufftMakePlanMany(cufft_plan_fwd_z,1,nzm,0,1,nzm,0,1,nzm,CUFFT_Z2Z,batch,worksize)
max_worksize = max(worksize,max_worksize)
! istat = cufftPlan1D(cufft_plan_bwd_z,nzm,CUFFT_Z2Z,batch)
istat = cufftCreate( cufft_plan_bwd_z )
istat = cufftSetAutoAllocation( cufft_plan_bwd_z, 0 )
istat = cufftMakePlanMany(cufft_plan_bwd_z,1,nzm,0,1,nzm,0,1,nzm,CUFFT_Z2Z,batch,worksize)
max_worksize = max(worksize,max_worksize)
#ifdef NO_BATCH
batch = 1
#else
batch = (ph%yen(1)-ph%yst(1)+1)*(ph%yen(3)-ph%yst(3)+1)
batch = ceiling(real(batch)/BATCH_RND)*BATCH_RND
#endif
!istat = cufftPlan1D(cufft_plan_fwd_y,nym,CUFFT_D2Z,batch)
istat = cufftCreate( cufft_plan_fwd_y )
istat = cufftSetAutoAllocation( cufft_plan_fwd_y, 0 )
istat = cufftMakePlanMany(cufft_plan_fwd_y,1,nym,0,1,nym,0,1,nym/2+1,CUFFT_D2Z,batch,worksize)
max_worksize = max(worksize,max_worksize)
#ifdef NO_BATCH
batch = 1
#else
batch = (sp%yen(1)-sp%yst(1)+1)*(sp%yen(3)-sp%yst(3)+1)
batch = ceiling(real(batch)/BATCH_RND)*BATCH_RND
#endif
!istat = cufftPlan1D(cufft_plan_bwd_y,nym,CUFFT_Z2D,batch)
istat = cufftCreate( cufft_plan_bwd_y )
istat = cufftSetAutoAllocation( cufft_plan_bwd_y, 0 )
istat = cufftMakePlanMany(cufft_plan_bwd_y,1,nym,0,1,nym/2+1,0,1,nym,CUFFT_Z2D,batch,worksize)
max_worksize = max(worksize,max_worksize)
allocate(cufft_workspace(max_worksize/16))
istat = cufftSetWorkArea( cufft_plan_fwd_z, cufft_workspace )
istat = cufftSetWorkArea( cufft_plan_bwd_z, cufft_workspace )
istat = cufftSetWorkArea( cufft_plan_fwd_y, cufft_workspace )
istat = cufftSetWorkArea( cufft_plan_bwd_y, cufft_workspace )
#else
iodim(1)%n=nzm
iodim(1)%is=(sp%zen(1)-sp%zst(1)+1)*(sp%zen(2)-sp%zst(2)+1)
iodim(1)%os=(sp%zen(1)-sp%zst(1)+1)*(sp%zen(2)-sp%zst(2)+1)
iodim_howmany(1)%n=(sp%zen(1)-sp%zst(1)+1)
iodim_howmany(1)%is=1
iodim_howmany(1)%os=1
iodim_howmany(2)%n=(sp%zen(2)-sp%zst(2)+1)
iodim_howmany(2)%is=(sp%zen(1)-sp%zst(1)+1)
iodim_howmany(2)%os=(sp%zen(1)-sp%zst(1)+1)
fwd_guruplan_z=fftw_plan_guru_dft(1,iodim, &
& 2,iodim_howmany,cz1,cz1, &
& FFTW_FORWARD,FFTW_ESTIMATE)
iodim(1)%n=nzm
bwd_guruplan_z=fftw_plan_guru_dft(1,iodim, &
& 2,iodim_howmany,cz1,cz1, &
& FFTW_BACKWARD,FFTW_ESTIMATE)
if (.not.c_associated(bwd_guruplan_z)) then
if (ismaster) print*,'Failed to create guru plan. You should'
if (ismaster) print*,'link with FFTW3 before MKL'
if (ismaster) print*,'Please check linking order.'
call MPI_Abort(MPI_COMM_WORLD,1,info)
endif
iodim(1)%n=nym
iodim(1)%is=ph%yen(1)-ph%yst(1)+1
iodim(1)%os=sp%yen(1)-sp%yst(1)+1
iodim_howmany(1)%n=(ph%yen(1)-ph%yst(1)+1)
iodim_howmany(1)%is=1
iodim_howmany(1)%os=1
iodim_howmany(2)%n=(ph%yen(3)-ph%yst(3)+1)
iodim_howmany(2)%is=(ph%yen(1)-ph%yst(1)+1) &
& *(ph%yen(2)-ph%yst(2)+1)
iodim_howmany(2)%os=(sp%yen(1)-sp%yst(1)+1) &
& *(sp%yen(2)-sp%yst(2)+1)
fwd_guruplan_y=fftw_plan_guru_dft_r2c(1,iodim, &
& 2,iodim_howmany,ry1,cy1, &
& FFTW_ESTIMATE)
iodim(1)%n=nym
iodim(1)%is=sp%yen(1)-sp%yst(1)+1
iodim(1)%os=ph%yen(1)-ph%yst(1)+1
iodim_howmany(1)%n=(sp%yen(1)-sp%yst(1)+1)
iodim_howmany(1)%is=1
iodim_howmany(1)%os=1
iodim_howmany(2)%n=(sp%yen(3)-sp%yst(3)+1)
iodim_howmany(2)%is=(sp%yen(1)-sp%yst(1)+1) &
& *(sp%yen(2)-sp%yst(2)+1)
iodim_howmany(2)%os=(ph%yen(1)-ph%yst(1)+1) &
& *(ph%yen(2)-ph%yst(2)+1)
bwd_guruplan_y=fftw_plan_guru_dft_c2r(1,iodim, &
& 2,iodim_howmany,cy1,ry1, &
& FFTW_ESTIMATE)
#endif
return
end