-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathm_fftw.fpp
More file actions
317 lines (271 loc) · 12 KB
/
Copy pathm_fftw.fpp
File metadata and controls
317 lines (271 loc) · 12 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
!>
!! @file
!! @brief Contains module m_fftw
#:include 'macros.fpp'
!> @brief Forward and inverse FFT wrappers (FFTW/cuFFT/hipFFT) for azimuthal Fourier filtering in cylindrical geometries
module m_fftw
use, intrinsic :: iso_c_binding
use m_derived_types
use m_global_parameters
use m_mpi_proxy
#if defined(MFC_GPU) && defined(__PGI)
use cufft
#elif defined(MFC_GPU)
use hipfort
use hipfort_check
use hipfort_hipfft
#endif
implicit none
private; public :: s_initialize_fftw_module, s_apply_fourier_filter, s_finalize_fftw_module
#if !defined(MFC_GPU)
include 'fftw3.f03'
#endif
type(c_ptr) :: fwd_plan, bwd_plan
type(c_ptr) :: fftw_real_data, fftw_cmplx_data, fftw_fltr_cmplx_data
integer :: real_size, cmplx_size, x_size, batch_size, Nfq, i2
real(c_double), pointer :: data_real(:) !< Real data
complex(c_double_complex), pointer :: data_cmplx(:) !< Complex data in Fourier space
complex(c_double_complex), pointer :: data_fltr_cmplx(:) !< Filtered complex data in Fourier space
#if defined(MFC_GPU)
$:GPU_DECLARE(create='[real_size, cmplx_size, x_size, batch_size, Nfq, i2]')
real(dp), allocatable, target :: data_real_gpu(:)
complex(dp), allocatable, target :: data_cmplx_gpu(:)
complex(dp), allocatable, target :: data_fltr_cmplx_gpu(:)
$:GPU_DECLARE(create='[data_real_gpu, data_cmplx_gpu, data_fltr_cmplx_gpu]')
!> @cond
#if defined(__PGI)
integer :: fwd_plan_gpu, bwd_plan_gpu
#else
!> @endcond
type(c_ptr) :: fwd_plan_gpu, bwd_plan_gpu
!> @cond
#endif
!> @endcond
integer, allocatable :: gpu_fft_size(:), iembed(:), oembed(:)
integer :: istride, ostride, rank
#endif
contains
!> Initialize the FFTW module
impure subroutine s_initialize_fftw_module
integer :: ierr !< Generic flag used to identify and report GPU errors
! Size of input array going into DFT
real_size = p + 1
! Size of output array coming out of DFT
cmplx_size = (p + 1)/2 + 1
x_size = m + 1
batch_size = x_size*sys_size
#if defined(MFC_GPU)
rank = 1; istride = 1; ostride = 1
allocate (gpu_fft_size(1:rank), iembed(1:rank), oembed(1:rank))
gpu_fft_size(1) = real_size
iembed(1) = real_size
oembed(1) = cmplx_size
$:GPU_ENTER_DATA(copyin='[real_size, cmplx_size, x_size, sys_size, batch_size, Nfq]')
$:GPU_UPDATE(device='[real_size, cmplx_size, x_size, sys_size, batch_size]')
#else
! Allocate input and output DFT data sizes
fftw_real_data = fftw_alloc_real(int(real_size, c_size_t))
fftw_cmplx_data = fftw_alloc_complex(int(cmplx_size, c_size_t))
fftw_fltr_cmplx_data = fftw_alloc_complex(int(cmplx_size, c_size_t))
! Associate input and output data pointers with allocated memory
call c_f_pointer(fftw_real_data, data_real, [real_size])
call c_f_pointer(fftw_cmplx_data, data_cmplx, [cmplx_size])
call c_f_pointer(fftw_fltr_cmplx_data, data_fltr_cmplx, [cmplx_size])
! Generate plans for forward and backward DFTs
fwd_plan = fftw_plan_dft_r2c_1d(real_size, data_real, data_cmplx, FFTW_ESTIMATE)
bwd_plan = fftw_plan_dft_c2r_1d(real_size, data_fltr_cmplx, data_real, FFTW_ESTIMATE)
#endif
#if defined(MFC_GPU)
@:ALLOCATE(data_real_gpu(1:real_size*x_size*sys_size))
@:ALLOCATE(data_cmplx_gpu(1:cmplx_size*x_size*sys_size))
@:ALLOCATE(data_fltr_cmplx_gpu(1:cmplx_size*x_size*sys_size))
#if defined(__PGI)
ierr = cufftPlanMany(fwd_plan_gpu, rank, gpu_fft_size, iembed, istride, real_size, oembed, ostride, cmplx_size, &
& CUFFT_D2Z, batch_size)
ierr = cufftPlanMany(bwd_plan_gpu, rank, gpu_fft_size, iembed, istride, cmplx_size, oembed, ostride, real_size, &
& CUFFT_Z2D, batch_size)
#else
ierr = hipfftPlanMany(fwd_plan_gpu, rank, gpu_fft_size, iembed, istride, real_size, oembed, ostride, cmplx_size, &
& HIPFFT_D2Z, batch_size)
ierr = hipfftPlanMany(bwd_plan_gpu, rank, gpu_fft_size, iembed, istride, cmplx_size, oembed, ostride, real_size, &
& HIPFFT_Z2D, batch_size)
#endif
#endif
end subroutine s_initialize_fftw_module
!> Apply a Fourier low-pass filter in the azimuthal direction to remove high-frequency content
impure subroutine s_apply_fourier_filter(q_cons_vf)
type(scalar_field), dimension(sys_size), intent(inout) :: q_cons_vf
integer :: i, j, k, l !< Generic loop iterators
integer :: ierr !< Generic flag used to identify and report GPU errors
! Restrict filter to processors that have cells adjacent to axis
if (bc_y%beg >= 0) return
#if defined(MFC_GPU)
$:GPU_PARALLEL_LOOP(collapse=3)
do k = 1, sys_size
do j = 0, m
do l = 1, cmplx_size
data_fltr_cmplx_gpu(l + j*cmplx_size + (k - 1)*cmplx_size*x_size) = (0_dp, 0_dp)
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
$:GPU_PARALLEL_LOOP(collapse=3)
do k = 1, sys_size
do j = 0, m
do l = 0, p
data_real_gpu(l + j*real_size + 1 + (k - 1)*real_size*x_size) = q_cons_vf(k)%sf(j, 0, l)
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
#:call GPU_HOST_DATA(use_device_addr='[data_real_gpu, data_cmplx_gpu, data_fltr_cmplx_gpu]')
#if defined(__PGI)
ierr = cufftExecD2Z(fwd_plan_gpu, data_real_gpu, data_cmplx_gpu)
#else
ierr = hipfftExecD2Z(fwd_plan_gpu, data_real_gpu, data_cmplx_gpu)
call hipCheck(hipDeviceSynchronize())
#endif
#:endcall GPU_HOST_DATA
Nfq = 3
$:GPU_UPDATE(device='[Nfq]')
$:GPU_PARALLEL_LOOP(collapse=3)
do k = 1, sys_size
do j = 0, m
do l = 1, Nfq
data_fltr_cmplx_gpu(l + j*cmplx_size + (k - 1)*cmplx_size*x_size) = data_cmplx_gpu(l + j*cmplx_size + (k - 1) &
& *cmplx_size*x_size)
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
#:call GPU_HOST_DATA(use_device_addr='[data_real_gpu, data_cmplx_gpu, data_fltr_cmplx_gpu]')
#if defined(__PGI)
ierr = cufftExecZ2D(bwd_plan_gpu, data_fltr_cmplx_gpu, data_real_gpu)
#else
ierr = hipfftExecZ2D(bwd_plan_gpu, data_fltr_cmplx_gpu, data_real_gpu)
call hipCheck(hipDeviceSynchronize())
#endif
#:endcall GPU_HOST_DATA
$:GPU_PARALLEL_LOOP(collapse=3)
do k = 1, sys_size
do j = 0, m
do l = 0, p
data_real_gpu(l + j*real_size + 1 + (k - 1)*real_size*x_size) = data_real_gpu(l + j*real_size + 1 + (k - 1) &
& *real_size*x_size)/real(real_size, dp)
q_cons_vf(k)%sf(j, 0, l) = data_real_gpu(l + j*real_size + 1 + (k - 1)*real_size*x_size)
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
do i = 1, fourier_rings
i2 = i
$:GPU_UPDATE(device='[i2]')
$:GPU_PARALLEL_LOOP(collapse=3)
do k = 1, sys_size
do j = 0, m
do l = 1, cmplx_size
data_fltr_cmplx_gpu(l + j*cmplx_size + (k - 1)*cmplx_size*x_size) = (0_dp, 0_dp)
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
$:GPU_PARALLEL_LOOP(collapse=3)
do k = 1, sys_size
do j = 0, m
do l = 0, p
data_real_gpu(l + j*real_size + 1 + (k - 1)*real_size*x_size) = q_cons_vf(k)%sf(j, i2, l)
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
#:call GPU_HOST_DATA(use_device_addr='[data_real_gpu, data_cmplx_gpu, data_fltr_cmplx_gpu]')
#if defined(__PGI)
ierr = cufftExecD2Z(fwd_plan_gpu, data_real_gpu, data_cmplx_gpu)
#else
ierr = hipfftExecD2Z(fwd_plan_gpu, data_real_gpu, data_cmplx_gpu)
call hipCheck(hipDeviceSynchronize())
#endif
#:endcall GPU_HOST_DATA
Nfq = min(floor(2_dp*real(i, dp)*pi), cmplx_size)
$:GPU_UPDATE(device='[Nfq]')
$:GPU_PARALLEL_LOOP(collapse=3)
do k = 1, sys_size
do j = 0, m
do l = 1, Nfq
data_fltr_cmplx_gpu(l + j*cmplx_size + (k - 1)*cmplx_size*x_size) = data_cmplx_gpu(l + j*cmplx_size + (k &
& - 1)*cmplx_size*x_size)
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
#:call GPU_HOST_DATA(use_device_addr='[data_real_gpu, data_cmplx_gpu, data_fltr_cmplx_gpu]')
#if defined(__PGI)
ierr = cufftExecZ2D(bwd_plan_gpu, data_fltr_cmplx_gpu, data_real_gpu)
#else
ierr = hipfftExecZ2D(bwd_plan_gpu, data_fltr_cmplx_gpu, data_real_gpu)
call hipCheck(hipDeviceSynchronize())
#endif
#:endcall GPU_HOST_DATA
$:GPU_PARALLEL_LOOP(collapse=3)
do k = 1, sys_size
do j = 0, m
do l = 0, p
data_real_gpu(l + j*real_size + 1 + (k - 1)*real_size*x_size) = data_real_gpu(l + j*real_size + 1 + (k &
& - 1)*real_size*x_size)/real(real_size, dp)
q_cons_vf(k)%sf(j, i2, l) = data_real_gpu(l + j*real_size + 1 + (k - 1)*real_size*x_size)
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
end do
#else
Nfq = 3
do j = 0, m
do k = 1, sys_size
data_fltr_cmplx(:) = (0_dp, 0_dp)
data_real(1:p + 1) = q_cons_vf(k)%sf(j, 0,0:p)
call fftw_execute_dft_r2c(fwd_plan, data_real, data_cmplx)
data_fltr_cmplx(1:Nfq) = data_cmplx(1:Nfq)
call fftw_execute_dft_c2r(bwd_plan, data_fltr_cmplx, data_real)
data_real(:) = data_real(:)/real(real_size, dp)
q_cons_vf(k)%sf(j, 0,0:p) = data_real(1:p + 1)
end do
end do
! Apply Fourier filter to additional rings
do i = 1, fourier_rings
Nfq = min(floor(2_dp*real(i, dp)*pi), cmplx_size)
do j = 0, m
do k = 1, sys_size
data_fltr_cmplx(:) = (0_dp, 0_dp)
data_real(1:p + 1) = q_cons_vf(k)%sf(j, i,0:p)
call fftw_execute_dft_r2c(fwd_plan, data_real, data_cmplx)
data_fltr_cmplx(1:Nfq) = data_cmplx(1:Nfq)
call fftw_execute_dft_c2r(bwd_plan, data_fltr_cmplx, data_real)
data_real(:) = data_real(:)/real(real_size, dp)
q_cons_vf(k)%sf(j, i,0:p) = data_real(1:p + 1)
end do
end do
end do
#endif
end subroutine s_apply_fourier_filter
!> Finalize the FFTW module
impure subroutine s_finalize_fftw_module
#if defined(MFC_GPU)
integer :: ierr !< Generic flag used to identify and report GPU errors
@:DEALLOCATE(data_real_gpu, data_fltr_cmplx_gpu, data_cmplx_gpu)
#if defined(__PGI)
ierr = cufftDestroy(fwd_plan_gpu)
ierr = cufftDestroy(bwd_plan_gpu)
#else
ierr = hipfftDestroy(fwd_plan_gpu)
ierr = hipfftDestroy(bwd_plan_gpu)
#endif
#else
call fftw_free(fftw_real_data)
call fftw_free(fftw_cmplx_data)
call fftw_free(fftw_fltr_cmplx_data)
call fftw_destroy_plan(fwd_plan)
call fftw_destroy_plan(bwd_plan)
#endif
end subroutine s_finalize_fftw_module
end module m_fftw