Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 : ! **************************************************************************************************
9 : !> \note
10 : !> This module contains routines necessary to operate on plane waves on GPUs
11 : ! > independently of the GPU platform.
12 : !> \par History
13 : !> BGL (06-Mar-2008) : Created
14 : !> AG (18-May-2012) : Refacturing:
15 : !> - added explicit interfaces to C routines
16 : !> - enable double precision complex transformations
17 : !> AG (11-Sept-2012) : Modifications:
18 : !> - use pointers if precision mapping is not required
19 : !> - use OMP for mapping
20 : !> MT (Jan 2022) : Modifications
21 : !> - use a generic interface for fft calls to GPUs
22 : !> - Support both Nvidia and AMD GPUs. Other GPUs manufacturers
23 : !> can be added easily.
24 : !> \author Benjamin G. Levine
25 : ! **************************************************************************************************
26 : MODULE fft_gpu
27 : USE ISO_C_BINDING, ONLY: C_BOOL,&
28 : C_DOUBLE,&
29 : C_INT,&
30 : C_LOC,&
31 : C_PTR
32 : USE fft_tools, ONLY: &
33 : cube_transpose_1, cube_transpose_2, fft_scratch_sizes, fft_scratch_type, get_fft_scratch, &
34 : release_fft_scratch, x_to_yz, xz_to_yz, yz_to_x, yz_to_xz
35 : USE kinds, ONLY: dp
36 : USE mathconstants, ONLY: z_zero
37 : USE message_passing, ONLY: mp_cart_type
38 : USE pw_grid_types, ONLY: FULLSPACE
39 : USE pw_types, ONLY: pw_c1d_gs_type,&
40 : pw_r3d_rs_type
41 : #include "../base/base_uses.f90"
42 :
43 : IMPLICIT NONE
44 :
45 : PRIVATE
46 :
47 : PUBLIC :: fft_gpu_r3dc1d_3d
48 : PUBLIC :: fft_gpu_c1dr3d_3d
49 : PUBLIC :: fft_gpu_r3dc1d_3d_ps
50 : PUBLIC :: fft_gpu_c1dr3d_3d_ps
51 :
52 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'fft_gpu'
53 : LOGICAL, PARAMETER, PRIVATE :: debug_this_module = .FALSE.
54 :
55 : CONTAINS
56 :
57 : ! **************************************************************************************************
58 : !> \brief perform an fft followed by a gather on the gpu
59 : !> \param pw1 ...
60 : !> \param pw2 ...
61 : !> \author Benjamin G Levine
62 : ! **************************************************************************************************
63 0 : SUBROUTINE fft_gpu_r3dc1d_3d(pw1, pw2)
64 : TYPE(pw_r3d_rs_type), INTENT(IN) :: pw1
65 : TYPE(pw_c1d_gs_type), INTENT(INOUT) :: pw2
66 :
67 : CHARACTER(len=*), PARAMETER :: routineN = 'fft_gpu_r3dc1d_3d'
68 :
69 : COMPLEX(KIND=dp), POINTER :: ptr_pwout
70 : INTEGER :: handle, l1, l2, l3, ngpts
71 0 : INTEGER, DIMENSION(:), POINTER :: npts
72 : INTEGER, POINTER :: ptr_ghatmap
73 : REAL(KIND=dp) :: scale
74 : REAL(KIND=dp), POINTER :: ptr_pwin
75 : INTERFACE
76 : SUBROUTINE fft_gpu_cfffg_c(din, zout, ghatmap, npts, ngpts, scale) BIND(C, name="fft_gpu_cfffg")
77 : IMPORT
78 : TYPE(C_PTR), INTENT(IN), VALUE :: din
79 : TYPE(C_PTR), VALUE :: zout
80 : TYPE(C_PTR), INTENT(IN), VALUE :: ghatmap
81 : INTEGER(KIND=C_INT), DIMENSION(*), INTENT(IN):: npts
82 : INTEGER(KIND=C_INT), INTENT(IN), VALUE :: ngpts
83 : REAL(KIND=C_DOUBLE), INTENT(IN), VALUE :: scale
84 :
85 : END SUBROUTINE fft_gpu_cfffg_c
86 : END INTERFACE
87 :
88 0 : CALL timeset(routineN, handle)
89 :
90 0 : scale = 1.0_dp/REAL(pw1%pw_grid%ngpts, KIND=dp)
91 :
92 0 : ngpts = SIZE(pw2%pw_grid%gsq)
93 : l1 = LBOUND(pw1%array, 1)
94 : l2 = LBOUND(pw1%array, 2)
95 : l3 = LBOUND(pw1%array, 3)
96 0 : npts => pw1%pw_grid%npts
97 :
98 : ! pointers to data arrays
99 0 : ptr_pwin => pw1%array(l1, l2, l3)
100 0 : ptr_pwout => pw2%array(1)
101 :
102 : ! pointer to map array
103 0 : ptr_ghatmap => pw2%pw_grid%g_hatmap(1, 1)
104 :
105 : ! invoke the combined transformation
106 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
107 : CALL fft_gpu_cfffg_c(c_loc(ptr_pwin), c_loc(ptr_pwout), c_loc(ptr_ghatmap), npts, ngpts, scale)
108 : #else
109 0 : CPABORT("Compiled without pw offloading.")
110 : #endif
111 :
112 0 : CALL timestop(handle)
113 0 : END SUBROUTINE fft_gpu_r3dc1d_3d
114 :
115 : ! **************************************************************************************************
116 : !> \brief perform an scatter followed by a fft on the gpu
117 : !> \param pw1 ...
118 : !> \param pw2 ...
119 : !> \author Benjamin G Levine
120 : ! **************************************************************************************************
121 0 : SUBROUTINE fft_gpu_c1dr3d_3d(pw1, pw2)
122 : TYPE(pw_c1d_gs_type), INTENT(IN) :: pw1
123 : TYPE(pw_r3d_rs_type), INTENT(INOUT) :: pw2
124 :
125 : CHARACTER(len=*), PARAMETER :: routineN = 'fft_gpu_c1dr3d_3d'
126 :
127 : COMPLEX(KIND=dp), POINTER :: ptr_pwin
128 : INTEGER :: handle, l1, l2, l3, ngpts, nmaps
129 0 : INTEGER, DIMENSION(:), POINTER :: npts
130 : INTEGER, POINTER :: ptr_ghatmap
131 : REAL(KIND=dp) :: scale
132 : REAL(KIND=dp), POINTER :: ptr_pwout
133 : INTERFACE
134 : SUBROUTINE fft_gpu_sfffc_c(zin, dout, ghatmap, npts, ngpts, nmaps, scale) BIND(C, name="fft_gpu_sfffc")
135 : IMPORT
136 : TYPE(C_PTR), INTENT(IN), VALUE :: zin
137 : TYPE(C_PTR), VALUE :: dout
138 : TYPE(C_PTR), INTENT(IN), VALUE :: ghatmap
139 : INTEGER(KIND=C_INT), DIMENSION(*), INTENT(IN):: npts
140 : INTEGER(KIND=C_INT), INTENT(IN), VALUE :: ngpts, nmaps
141 : REAL(KIND=C_DOUBLE), INTENT(IN), VALUE :: scale
142 : END SUBROUTINE fft_gpu_sfffc_c
143 : END INTERFACE
144 :
145 0 : CALL timeset(routineN, handle)
146 :
147 0 : scale = 1.0_dp
148 :
149 0 : ngpts = SIZE(pw1%pw_grid%gsq)
150 : l1 = LBOUND(pw2%array, 1)
151 : l2 = LBOUND(pw2%array, 2)
152 : l3 = LBOUND(pw2%array, 3)
153 0 : npts => pw1%pw_grid%npts
154 :
155 : ! pointers to data arrays
156 0 : ptr_pwin => pw1%array(1)
157 0 : ptr_pwout => pw2%array(l1, l2, l3)
158 :
159 : ! pointer to map array
160 0 : nmaps = SIZE(pw1%pw_grid%g_hatmap, 2)
161 0 : ptr_ghatmap => pw1%pw_grid%g_hatmap(1, 1)
162 :
163 : ! invoke the combined transformation
164 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
165 : CALL fft_gpu_sfffc_c(c_loc(ptr_pwin), c_loc(ptr_pwout), c_loc(ptr_ghatmap), npts, ngpts, nmaps, scale)
166 : #else
167 0 : CPABORT("Compiled without pw offloading")
168 : #endif
169 :
170 0 : CALL timestop(handle)
171 0 : END SUBROUTINE fft_gpu_c1dr3d_3d
172 :
173 : ! **************************************************************************************************
174 : !> \brief perform an parallel fft followed by a gather on the gpu
175 : !> \param pw1 ...
176 : !> \param pw2 ...
177 : !> \author Andreas Gloess
178 : ! **************************************************************************************************
179 0 : SUBROUTINE fft_gpu_r3dc1d_3d_ps(pw1, pw2)
180 : TYPE(pw_r3d_rs_type), INTENT(IN) :: pw1
181 : TYPE(pw_c1d_gs_type), INTENT(INOUT) :: pw2
182 :
183 : CHARACTER(len=*), PARAMETER :: routineN = 'fft_gpu_r3dc1d_3d_ps'
184 :
185 0 : COMPLEX(KIND=dp), DIMENSION(:, :), POINTER :: grays, pbuf, qbuf, rbuf, sbuf
186 0 : COMPLEX(KIND=dp), DIMENSION(:, :, :), POINTER :: tbuf
187 : INTEGER :: g_pos, handle, lg, lmax, mg, mmax, mx2, &
188 : mz2, n1, n2, ngpts, nmax, numtask, rp
189 0 : INTEGER, ALLOCATABLE, DIMENSION(:) :: p2p
190 : INTEGER, DIMENSION(2) :: r_dim, r_pos
191 0 : INTEGER, DIMENSION(:), POINTER :: n, nloc, nyzray
192 0 : INTEGER, DIMENSION(:, :, :, :), POINTER :: bo
193 : REAL(KIND=dp) :: scale
194 0 : TYPE(fft_scratch_sizes) :: fft_scratch_size
195 : TYPE(fft_scratch_type), POINTER :: fft_scratch
196 0 : TYPE(mp_cart_type) :: rs_group
197 :
198 0 : CALL timeset(routineN, handle)
199 :
200 0 : scale = 1.0_dp/REAL(pw1%pw_grid%ngpts, KIND=dp)
201 :
202 : ! dimensions
203 0 : n => pw1%pw_grid%npts
204 0 : nloc => pw1%pw_grid%npts_local
205 0 : grays => pw1%pw_grid%grays
206 0 : ngpts = nloc(1)*nloc(2)*nloc(3)
207 :
208 : !..transform
209 0 : IF (pw1%pw_grid%para%ray_distribution) THEN
210 0 : rs_group = pw1%pw_grid%para%group
211 0 : nyzray => pw1%pw_grid%para%nyzray
212 0 : bo => pw1%pw_grid%para%bo
213 :
214 0 : g_pos = rs_group%mepos
215 0 : numtask = rs_group%num_pe
216 0 : r_dim = rs_group%num_pe_cart
217 0 : r_pos = rs_group%mepos_cart
218 :
219 0 : lg = SIZE(grays, 1)
220 0 : mg = SIZE(grays, 2)
221 0 : mmax = MAX(mg, 1)
222 0 : lmax = MAX(lg, (ngpts/mmax + 1))
223 :
224 0 : ALLOCATE (p2p(0:numtask - 1))
225 :
226 0 : CALL rs_group%rank_compare(rs_group, p2p)
227 :
228 0 : rp = p2p(g_pos)
229 0 : mx2 = bo(2, 1, rp, 2) - bo(1, 1, rp, 2) + 1
230 0 : mz2 = bo(2, 3, rp, 2) - bo(1, 3, rp, 2) + 1
231 0 : n1 = MAXVAL(bo(2, 1, :, 1) - bo(1, 1, :, 1) + 1)
232 0 : n2 = MAXVAL(bo(2, 2, :, 1) - bo(1, 2, :, 1) + 1)
233 0 : nmax = MAX((2*n2)/numtask, 2)*mx2*mz2
234 0 : nmax = MAX(nmax, n1*MAXVAL(nyzray))
235 :
236 0 : fft_scratch_size%nx = nloc(1)
237 0 : fft_scratch_size%ny = nloc(2)
238 0 : fft_scratch_size%nz = nloc(3)
239 0 : fft_scratch_size%lmax = lmax
240 0 : fft_scratch_size%mmax = mmax
241 0 : fft_scratch_size%mx1 = bo(2, 1, rp, 1) - bo(1, 1, rp, 1) + 1
242 0 : fft_scratch_size%mx2 = mx2
243 0 : fft_scratch_size%my1 = bo(2, 2, rp, 1) - bo(1, 2, rp, 1) + 1
244 0 : fft_scratch_size%mz2 = mz2
245 0 : fft_scratch_size%lg = lg
246 0 : fft_scratch_size%mg = mg
247 0 : fft_scratch_size%nbx = MAXVAL(bo(2, 1, :, 2))
248 0 : fft_scratch_size%nbz = MAXVAL(bo(2, 3, :, 2))
249 0 : fft_scratch_size%mcz1 = MAXVAL(bo(2, 3, :, 1) - bo(1, 3, :, 1) + 1)
250 0 : fft_scratch_size%mcx2 = MAXVAL(bo(2, 1, :, 2) - bo(1, 1, :, 2) + 1)
251 0 : fft_scratch_size%mcz2 = MAXVAL(bo(2, 3, :, 2) - bo(1, 3, :, 2) + 1)
252 0 : fft_scratch_size%nmax = nmax
253 0 : fft_scratch_size%nmray = MAXVAL(nyzray)
254 0 : fft_scratch_size%nyzray = nyzray(g_pos)
255 0 : fft_scratch_size%rs_group = rs_group
256 0 : fft_scratch_size%g_pos = g_pos
257 0 : fft_scratch_size%r_pos = r_pos
258 0 : fft_scratch_size%r_dim = r_dim
259 0 : fft_scratch_size%numtask = numtask
260 :
261 0 : IF (r_dim(2) > 1) THEN
262 : !
263 : ! real space is distributed over x and y coordinate
264 : ! we have two stages of communication
265 : !
266 0 : IF (r_dim(1) == 1) THEN
267 0 : CPABORT("This processor distribution is not supported.")
268 : END IF
269 :
270 0 : CALL get_fft_scratch(fft_scratch, tf_type=300, n=n, fft_sizes=fft_scratch_size)
271 :
272 : ! assign buffers
273 0 : qbuf => fft_scratch%p2buf
274 0 : rbuf => fft_scratch%p3buf
275 0 : pbuf => fft_scratch%p4buf
276 0 : sbuf => fft_scratch%p5buf
277 :
278 : ! FFT along z
279 0 : CALL fft_gpu_cf(pw1, qbuf)
280 :
281 : ! Exchange data ( transpose of matrix )
282 0 : CALL cube_transpose_2(qbuf, bo(:, :, :, 1), bo(:, :, :, 2), rbuf, fft_scratch)
283 :
284 : ! FFT along y
285 : ! use the inbuild fft-lib
286 : ! CALL fft_1d(fft_scratch%fft_plan(2), rbuf, pbuf, 1.0_dp, stat)
287 : ! or cufft (works faster, but is only faster if plans are stored)
288 0 : CALL fft_gpu_1d_local(rbuf, pbuf, +1, n(2), mx2*mz2)
289 :
290 : ! Exchange data ( transpose of matrix ) and sort
291 : CALL xz_to_yz(pbuf, rs_group, r_dim, g_pos, p2p, pw1%pw_grid%para%yzp, nyzray, &
292 0 : bo(:, :, :, 2), sbuf, fft_scratch)
293 :
294 : ! FFT along x
295 0 : CALL fft_gpu_fg(sbuf, pw2, scale)
296 :
297 0 : CALL release_fft_scratch(fft_scratch)
298 :
299 : ELSE
300 : !
301 : ! real space is only distributed over x coordinate
302 : ! we have one stage of communication, after the transform of
303 : ! direction x
304 : !
305 :
306 0 : CALL get_fft_scratch(fft_scratch, tf_type=200, n=n, fft_sizes=fft_scratch_size)
307 :
308 : ! assign buffers
309 0 : tbuf => fft_scratch%tbuf
310 0 : sbuf => fft_scratch%r1buf
311 :
312 : ! FFT along y and z
313 0 : CALL fft_gpu_cff(pw1, tbuf)
314 :
315 : ! Exchange data ( transpose of matrix ) and sort
316 : CALL yz_to_x(tbuf, rs_group, g_pos, p2p, pw1%pw_grid%para%yzp, nyzray, &
317 0 : bo(:, :, :, 2), sbuf, fft_scratch)
318 :
319 : ! FFT along x
320 0 : CALL fft_gpu_fg(sbuf, pw2, scale)
321 :
322 0 : CALL release_fft_scratch(fft_scratch)
323 :
324 : END IF
325 :
326 0 : DEALLOCATE (p2p)
327 :
328 : !--------------------------------------------------------------------------
329 : ELSE
330 0 : CPABORT("Not implemented (no ray_distr.) in: fft_gpu_r3dc1d_3d_ps.")
331 : END IF
332 :
333 0 : CALL timestop(handle)
334 0 : END SUBROUTINE fft_gpu_r3dc1d_3d_ps
335 :
336 : ! **************************************************************************************************
337 : !> \brief perform an parallel scatter followed by a fft on the gpu
338 : !> \param pw1 ...
339 : !> \param pw2 ...
340 : !> \author Andreas Gloess
341 : ! **************************************************************************************************
342 0 : SUBROUTINE fft_gpu_c1dr3d_3d_ps(pw1, pw2)
343 : TYPE(pw_c1d_gs_type), INTENT(IN) :: pw1
344 : TYPE(pw_r3d_rs_type), INTENT(INOUT) :: pw2
345 :
346 : CHARACTER(len=*), PARAMETER :: routineN = 'fft_gpu_c1dr3d_3d_ps'
347 :
348 0 : COMPLEX(KIND=dp), DIMENSION(:, :), POINTER :: grays, pbuf, qbuf, rbuf, sbuf
349 0 : COMPLEX(KIND=dp), DIMENSION(:, :, :), POINTER :: tbuf
350 : INTEGER :: g_pos, handle, lg, lmax, mg, mmax, mx2, &
351 : mz2, n1, n2, ngpts, nmax, numtask, rp
352 0 : INTEGER, ALLOCATABLE, DIMENSION(:) :: p2p
353 : INTEGER, DIMENSION(2) :: r_dim, r_pos
354 0 : INTEGER, DIMENSION(:), POINTER :: n, nloc, nyzray
355 0 : INTEGER, DIMENSION(:, :, :, :), POINTER :: bo
356 : REAL(KIND=dp) :: scale
357 0 : TYPE(fft_scratch_sizes) :: fft_scratch_size
358 : TYPE(fft_scratch_type), POINTER :: fft_scratch
359 0 : TYPE(mp_cart_type) :: rs_group
360 :
361 0 : CALL timeset(routineN, handle)
362 :
363 0 : scale = 1.0_dp
364 :
365 : ! dimensions
366 0 : n => pw1%pw_grid%npts
367 0 : nloc => pw1%pw_grid%npts_local
368 0 : grays => pw1%pw_grid%grays
369 0 : ngpts = nloc(1)*nloc(2)*nloc(3)
370 :
371 : !..transform
372 0 : IF (pw1%pw_grid%para%ray_distribution) THEN
373 0 : rs_group = pw1%pw_grid%para%group
374 0 : nyzray => pw1%pw_grid%para%nyzray
375 0 : bo => pw1%pw_grid%para%bo
376 :
377 0 : g_pos = rs_group%mepos
378 0 : numtask = rs_group%num_pe
379 0 : r_dim = rs_group%num_pe_cart
380 0 : r_pos = rs_group%mepos_cart
381 :
382 0 : lg = SIZE(grays, 1)
383 0 : mg = SIZE(grays, 2)
384 0 : mmax = MAX(mg, 1)
385 0 : lmax = MAX(lg, (ngpts/mmax + 1))
386 :
387 0 : ALLOCATE (p2p(0:numtask - 1))
388 :
389 0 : CALL rs_group%rank_compare(rs_group, p2p)
390 :
391 0 : rp = p2p(g_pos)
392 0 : mx2 = bo(2, 1, rp, 2) - bo(1, 1, rp, 2) + 1
393 0 : mz2 = bo(2, 3, rp, 2) - bo(1, 3, rp, 2) + 1
394 0 : n1 = MAXVAL(bo(2, 1, :, 1) - bo(1, 1, :, 1) + 1)
395 0 : n2 = MAXVAL(bo(2, 2, :, 1) - bo(1, 2, :, 1) + 1)
396 0 : nmax = MAX((2*n2)/numtask, 2)*mx2*mz2
397 0 : nmax = MAX(nmax, n1*MAXVAL(nyzray))
398 :
399 0 : fft_scratch_size%nx = nloc(1)
400 0 : fft_scratch_size%ny = nloc(2)
401 0 : fft_scratch_size%nz = nloc(3)
402 0 : fft_scratch_size%lmax = lmax
403 0 : fft_scratch_size%mmax = mmax
404 0 : fft_scratch_size%mx1 = bo(2, 1, rp, 1) - bo(1, 1, rp, 1) + 1
405 0 : fft_scratch_size%mx2 = mx2
406 0 : fft_scratch_size%my1 = bo(2, 2, rp, 1) - bo(1, 2, rp, 1) + 1
407 0 : fft_scratch_size%mz2 = mz2
408 0 : fft_scratch_size%lg = lg
409 0 : fft_scratch_size%mg = mg
410 0 : fft_scratch_size%nbx = MAXVAL(bo(2, 1, :, 2))
411 0 : fft_scratch_size%nbz = MAXVAL(bo(2, 3, :, 2))
412 0 : fft_scratch_size%mcz1 = MAXVAL(bo(2, 3, :, 1) - bo(1, 3, :, 1) + 1)
413 0 : fft_scratch_size%mcx2 = MAXVAL(bo(2, 1, :, 2) - bo(1, 1, :, 2) + 1)
414 0 : fft_scratch_size%mcz2 = MAXVAL(bo(2, 3, :, 2) - bo(1, 3, :, 2) + 1)
415 0 : fft_scratch_size%nmax = nmax
416 0 : fft_scratch_size%nmray = MAXVAL(nyzray)
417 0 : fft_scratch_size%nyzray = nyzray(g_pos)
418 0 : fft_scratch_size%rs_group = rs_group
419 0 : fft_scratch_size%g_pos = g_pos
420 0 : fft_scratch_size%r_pos = r_pos
421 0 : fft_scratch_size%r_dim = r_dim
422 0 : fft_scratch_size%numtask = numtask
423 :
424 0 : IF (r_dim(2) > 1) THEN
425 : !
426 : ! real space is distributed over x and y coordinate
427 : ! we have two stages of communication
428 : !
429 0 : IF (r_dim(1) == 1) THEN
430 0 : CPABORT("This processor distribution is not supported.")
431 : END IF
432 :
433 0 : CALL get_fft_scratch(fft_scratch, tf_type=300, n=n, fft_sizes=fft_scratch_size)
434 :
435 : ! assign buffers
436 0 : pbuf => fft_scratch%p7buf
437 0 : qbuf => fft_scratch%p4buf
438 0 : rbuf => fft_scratch%p3buf
439 0 : sbuf => fft_scratch%p2buf
440 :
441 : ! FFT along x
442 0 : CALL fft_gpu_sf(pw1, pbuf, scale)
443 :
444 : ! Exchange data ( transpose of matrix ) and sort
445 0 : IF (pw1%pw_grid%grid_span /= FULLSPACE) qbuf = z_zero
446 : CALL yz_to_xz(pbuf, rs_group, r_dim, g_pos, p2p, pw1%pw_grid%para%yzp, nyzray, &
447 0 : bo(:, :, :, 2), qbuf, fft_scratch)
448 :
449 : ! FFT along y
450 : ! use the inbuild fft-lib
451 : ! CALL fft_1d(fft_scratch%fft_plan(5), qbuf, rbuf, 1.0_dp, stat)
452 : ! or cufft (works faster, but is only faster if plans are stored)
453 0 : CALL fft_gpu_1d_local(qbuf, rbuf, -1, n(2), mx2*mz2)
454 :
455 : ! Exchange data ( transpose of matrix )
456 0 : IF (pw1%pw_grid%grid_span /= FULLSPACE) sbuf = z_zero
457 :
458 0 : CALL cube_transpose_1(rbuf, bo(:, :, :, 2), bo(:, :, :, 1), sbuf, fft_scratch)
459 :
460 : ! FFT along z
461 0 : CALL fft_gpu_fc(sbuf, pw2)
462 :
463 0 : CALL release_fft_scratch(fft_scratch)
464 :
465 : ELSE
466 : !
467 : ! real space is only distributed over x coordinate
468 : ! we have one stage of communication, after the transform of
469 : ! direction x
470 : !
471 :
472 0 : CALL get_fft_scratch(fft_scratch, tf_type=200, n=n, fft_sizes=fft_scratch_size)
473 :
474 : ! assign buffers
475 0 : sbuf => fft_scratch%r1buf
476 0 : tbuf => fft_scratch%tbuf
477 :
478 : ! FFT along x
479 0 : CALL fft_gpu_sf(pw1, sbuf, scale)
480 :
481 : ! Exchange data ( transpose of matrix ) and sort
482 0 : IF (pw1%pw_grid%grid_span /= FULLSPACE) tbuf = z_zero
483 : CALL x_to_yz(sbuf, rs_group, g_pos, p2p, pw1%pw_grid%para%yzp, nyzray, &
484 0 : bo(:, :, :, 2), tbuf, fft_scratch)
485 :
486 : ! FFT along y and z
487 0 : CALL fft_gpu_ffc(tbuf, pw2)
488 :
489 0 : CALL release_fft_scratch(fft_scratch)
490 :
491 : END IF
492 :
493 0 : DEALLOCATE (p2p)
494 :
495 : !--------------------------------------------------------------------------
496 : ELSE
497 0 : CPABORT("Not implemented (no ray_distr.) in: fft_gpu_c1dr3d_3d_ps.")
498 : END IF
499 :
500 0 : CALL timestop(handle)
501 0 : END SUBROUTINE fft_gpu_c1dr3d_3d_ps
502 :
503 : ! **************************************************************************************************
504 : !> \brief perform a parallel real_to_complex copy followed by a 2D-FFT on the gpu
505 : !> \param pw1 ...
506 : !> \param pwbuf ...
507 : !> \author Andreas Gloess
508 : ! **************************************************************************************************
509 0 : SUBROUTINE fft_gpu_cff(pw1, pwbuf)
510 : TYPE(pw_r3d_rs_type), INTENT(IN) :: pw1
511 : COMPLEX(KIND=dp), DIMENSION(:, :, :), &
512 : INTENT(INOUT), TARGET :: pwbuf
513 :
514 : CHARACTER(len=*), PARAMETER :: routineN = 'fft_gpu_cff'
515 :
516 : COMPLEX(KIND=dp), POINTER :: ptr_pwout
517 : INTEGER :: handle, l1, l2, l3
518 0 : INTEGER, DIMENSION(:), POINTER :: npts
519 : REAL(KIND=dp), POINTER :: ptr_pwin
520 : INTERFACE
521 : SUBROUTINE fft_gpu_cff_c(din, zout, npts) BIND(C, name="fft_gpu_cff")
522 : IMPORT
523 : TYPE(C_PTR), INTENT(IN), VALUE :: din
524 : TYPE(C_PTR), VALUE :: zout
525 : INTEGER(KIND=C_INT), DIMENSION(*), INTENT(IN):: npts
526 : END SUBROUTINE fft_gpu_cff_c
527 : END INTERFACE
528 :
529 0 : CALL timeset(routineN, handle)
530 :
531 : ! dimensions
532 0 : npts => pw1%pw_grid%npts_local
533 : l1 = LBOUND(pw1%array, 1)
534 : l2 = LBOUND(pw1%array, 2)
535 : l3 = LBOUND(pw1%array, 3)
536 :
537 : ! pointers to data arrays
538 0 : ptr_pwin => pw1%array(l1, l2, l3)
539 0 : ptr_pwout => pwbuf(1, 1, 1)
540 :
541 : ! invoke the combined transformation
542 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
543 : CALL fft_gpu_cff_c(c_loc(ptr_pwin), c_loc(ptr_pwout), npts)
544 : #else
545 0 : CPABORT("Compiled without pw offloading")
546 : #endif
547 :
548 0 : CALL timestop(handle)
549 0 : END SUBROUTINE fft_gpu_cff
550 :
551 : ! **************************************************************************************************
552 : !> \brief perform a parallel 2D-FFT followed by a complex_to_real copy on the gpu
553 : !> \param pwbuf ...
554 : !> \param pw2 ...
555 : !> \author Andreas Gloess
556 : ! **************************************************************************************************
557 0 : SUBROUTINE fft_gpu_ffc(pwbuf, pw2)
558 : COMPLEX(KIND=dp), DIMENSION(:, :, :), INTENT(IN), &
559 : TARGET :: pwbuf
560 : TYPE(pw_r3d_rs_type), INTENT(IN) :: pw2
561 :
562 : CHARACTER(len=*), PARAMETER :: routineN = 'fft_gpu_ffc'
563 :
564 : COMPLEX(KIND=dp), POINTER :: ptr_pwin
565 : INTEGER :: handle, l1, l2, l3
566 0 : INTEGER, DIMENSION(:), POINTER :: npts
567 : REAL(KIND=dp), POINTER :: ptr_pwout
568 : INTERFACE
569 : SUBROUTINE fft_gpu_ffc_c(zin, dout, npts) BIND(C, name="fft_gpu_ffc")
570 : IMPORT
571 : TYPE(C_PTR), INTENT(IN), VALUE :: zin
572 : TYPE(C_PTR), VALUE :: dout
573 : INTEGER(KIND=C_INT), DIMENSION(*), INTENT(IN):: npts
574 : END SUBROUTINE fft_gpu_ffc_c
575 : END INTERFACE
576 :
577 0 : CALL timeset(routineN, handle)
578 :
579 : ! dimensions
580 0 : npts => pw2%pw_grid%npts_local
581 : l1 = LBOUND(pw2%array, 1)
582 : l2 = LBOUND(pw2%array, 2)
583 : l3 = LBOUND(pw2%array, 3)
584 :
585 : ! pointers to data arrays
586 0 : ptr_pwin => pwbuf(1, 1, 1)
587 0 : ptr_pwout => pw2%array(l1, l2, l3)
588 :
589 : ! invoke the combined transformation
590 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
591 : CALL fft_gpu_ffc_c(c_loc(ptr_pwin), c_loc(ptr_pwout), npts)
592 : #else
593 0 : CPABORT("Compiled without pw offloading")
594 : #endif
595 :
596 0 : CALL timestop(handle)
597 0 : END SUBROUTINE fft_gpu_ffc
598 :
599 : ! **************************************************************************************************
600 : !> \brief perform a parallel real_to_complex copy followed by a 1D-FFT on the gpu
601 : !> \param pw1 ...
602 : !> \param pwbuf ...
603 : !> \author Andreas Gloess
604 : ! **************************************************************************************************
605 0 : SUBROUTINE fft_gpu_cf(pw1, pwbuf)
606 : TYPE(pw_r3d_rs_type), INTENT(IN) :: pw1
607 : COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(INOUT), &
608 : TARGET :: pwbuf
609 :
610 : CHARACTER(len=*), PARAMETER :: routineN = 'fft_gpu_cf'
611 :
612 : COMPLEX(KIND=dp), POINTER :: ptr_pwout
613 : INTEGER :: handle, l1, l2, l3
614 0 : INTEGER, DIMENSION(:), POINTER :: npts
615 : REAL(KIND=dp), POINTER :: ptr_pwin
616 : INTERFACE
617 : SUBROUTINE fft_gpu_cf_c(din, zout, npts) BIND(C, name="fft_gpu_cf")
618 : IMPORT
619 : TYPE(C_PTR), INTENT(IN), VALUE :: din
620 : TYPE(C_PTR), VALUE :: zout
621 : INTEGER(KIND=C_INT), DIMENSION(*), INTENT(IN):: npts
622 : END SUBROUTINE fft_gpu_cf_c
623 : END INTERFACE
624 :
625 0 : CALL timeset(routineN, handle)
626 :
627 : ! dimensions
628 0 : npts => pw1%pw_grid%npts_local
629 : l1 = LBOUND(pw1%array, 1)
630 : l2 = LBOUND(pw1%array, 2)
631 : l3 = LBOUND(pw1%array, 3)
632 :
633 : ! pointers to data arrays
634 0 : ptr_pwin => pw1%array(l1, l2, l3)
635 0 : ptr_pwout => pwbuf(1, 1)
636 :
637 : ! invoke the combined transformation
638 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
639 : CALL fft_gpu_cf_c(c_loc(ptr_pwin), c_loc(ptr_pwout), npts)
640 : #else
641 0 : CPABORT("Compiled without pw offloading")
642 : #endif
643 0 : CALL timestop(handle)
644 0 : END SUBROUTINE fft_gpu_cf
645 :
646 : ! **************************************************************************************************
647 : !> \brief perform a parallel 1D-FFT followed by a complex_to_real copy on the gpu
648 : !> \param pwbuf ...
649 : !> \param pw2 ...
650 : !> \author Andreas Gloess
651 : ! **************************************************************************************************
652 0 : SUBROUTINE fft_gpu_fc(pwbuf, pw2)
653 : COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(IN), &
654 : TARGET :: pwbuf
655 : TYPE(pw_r3d_rs_type), INTENT(IN) :: pw2
656 :
657 : CHARACTER(len=*), PARAMETER :: routineN = 'fft_gpu_fc'
658 :
659 : COMPLEX(KIND=dp), POINTER :: ptr_pwin
660 : INTEGER :: handle, l1, l2, l3
661 0 : INTEGER, DIMENSION(:), POINTER :: npts
662 : REAL(KIND=dp), POINTER :: ptr_pwout
663 : INTERFACE
664 : SUBROUTINE fft_gpu_fc_c(zin, dout, npts) BIND(C, name="fft_gpu_fc")
665 : IMPORT
666 : TYPE(C_PTR), INTENT(IN), VALUE :: zin
667 : TYPE(C_PTR), VALUE :: dout
668 : INTEGER(KIND=C_INT), DIMENSION(*), INTENT(IN):: npts
669 : END SUBROUTINE fft_gpu_fc_c
670 : END INTERFACE
671 :
672 0 : CALL timeset(routineN, handle)
673 :
674 0 : npts => pw2%pw_grid%npts_local
675 : l1 = LBOUND(pw2%array, 1)
676 : l2 = LBOUND(pw2%array, 2)
677 : l3 = LBOUND(pw2%array, 3)
678 :
679 : ! pointers to data arrays
680 0 : ptr_pwin => pwbuf(1, 1)
681 0 : ptr_pwout => pw2%array(l1, l2, l3)
682 :
683 : ! invoke the combined transformation
684 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
685 : CALL fft_gpu_fc_c(c_loc(ptr_pwin), c_loc(ptr_pwout), npts)
686 : #else
687 0 : CPABORT("Compiled without pw offloading")
688 : #endif
689 :
690 0 : CALL timestop(handle)
691 0 : END SUBROUTINE fft_gpu_fc
692 :
693 : ! **************************************************************************************************
694 : !> \brief perform a parallel 1D-FFT on the gpu
695 : !> \param pwbuf1 ...
696 : !> \param pwbuf2 ...
697 : !> \param dir ...
698 : !> \param n ...
699 : !> \param m ...
700 : !> \author Andreas Gloess
701 : ! **************************************************************************************************
702 0 : SUBROUTINE fft_gpu_1d_local(pwbuf1, pwbuf2, dir, n, m)
703 : COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(IN), &
704 : TARGET :: pwbuf1
705 : COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(INOUT), &
706 : TARGET :: pwbuf2
707 : INTEGER, INTENT(IN) :: dir, n, m
708 :
709 : CHARACTER(len=*), PARAMETER :: routineN = 'fft_gpu_1d_local'
710 :
711 : COMPLEX(KIND=dp), POINTER :: ptr_pwin, ptr_pwout
712 : INTEGER :: handle, ldx_in, ldx_out
713 : LOGICAL :: transpose_in, transpose_out
714 : INTERFACE
715 : SUBROUTINE fft_gpu_f_c(zin, zout, dir, n, m, transpose_in, transpose_out, ldx_in, ldx_out) &
716 : BIND(C, name="fft_gpu_1d_local")
717 : IMPORT
718 : TYPE(C_PTR), INTENT(IN), VALUE :: zin
719 : TYPE(C_PTR), VALUE :: zout
720 : LOGICAL(KIND=C_BOOL), INTENT(IN), VALUE :: dir, transpose_in, transpose_out
721 : INTEGER(KIND=C_INT), INTENT(IN), VALUE :: n, m, ldx_in, ldx_out
722 : END SUBROUTINE fft_gpu_f_c
723 : END INTERFACE
724 :
725 0 : CALL timeset(routineN, handle)
726 :
727 0 : IF (n*m /= 0) THEN
728 : ! pointers to data arrays
729 0 : ptr_pwin => pwbuf1(1, 1)
730 0 : ptr_pwout => pwbuf2(1, 1)
731 :
732 : IF (dir > 0) THEN
733 : transpose_in = .TRUE.
734 : transpose_out = .FALSE.
735 : ldx_in = m
736 : ldx_out = n
737 : ELSE
738 : transpose_in = .FALSE.
739 : transpose_out = .TRUE.
740 : ldx_in = n
741 : ldx_out = m
742 : END IF
743 :
744 : ! invoke the combined transformation
745 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
746 : CALL fft_gpu_f_c(c_loc(ptr_pwin), c_loc(ptr_pwout), LOGICAL(dir > 0, C_BOOL), &
747 : INT(n, C_INT), INT(m, C_INT), LOGICAL(transpose_in, C_BOOL), &
748 : LOGICAL(transpose_out, C_BOOL), INT(ldx_in, C_INT), INT(ldx_out, C_INT))
749 : #else
750 : MARK_USED(dir)
751 0 : CPABORT("Compiled without pw offloading")
752 : #endif
753 : END IF
754 :
755 0 : CALL timestop(handle)
756 0 : END SUBROUTINE fft_gpu_1d_local
757 : ! **************************************************************************************************
758 : !> \brief perform a parallel 1D-FFT followed by a gather on the gpu
759 : !> \param pwbuf ...
760 : !> \param pw2 ...
761 : !> \param scale ...
762 : !> \author Andreas Gloess
763 : ! **************************************************************************************************
764 0 : SUBROUTINE fft_gpu_fg(pwbuf, pw2, scale)
765 : COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(IN), &
766 : TARGET :: pwbuf
767 : TYPE(pw_c1d_gs_type), INTENT(IN) :: pw2
768 : REAL(KIND=dp), INTENT(IN) :: scale
769 :
770 : CHARACTER(len=*), PARAMETER :: routineN = 'fft_gpu_fg'
771 :
772 : COMPLEX(KIND=dp), POINTER :: ptr_pwin, ptr_pwout
773 : INTEGER :: handle, mg, mmax, ngpts
774 0 : INTEGER, DIMENSION(:), POINTER :: npts
775 : INTEGER, POINTER :: ptr_ghatmap
776 : INTERFACE
777 : SUBROUTINE fft_gpu_fg_c(zin, zout, ghatmap, npts, mmax, ngpts, scale) BIND(C, name="fft_gpu_fg")
778 : IMPORT
779 : TYPE(C_PTR), INTENT(IN), VALUE :: zin
780 : TYPE(C_PTR), VALUE :: zout
781 : TYPE(C_PTR), INTENT(IN), VALUE :: ghatmap
782 : INTEGER(KIND=C_INT), DIMENSION(*), INTENT(IN):: npts
783 : INTEGER(KIND=C_INT), INTENT(IN), VALUE :: mmax, ngpts
784 : REAL(KIND=C_DOUBLE), INTENT(IN), VALUE :: scale
785 :
786 : END SUBROUTINE fft_gpu_fg_c
787 : END INTERFACE
788 :
789 0 : CALL timeset(routineN, handle)
790 :
791 0 : ngpts = SIZE(pw2%pw_grid%gsq)
792 0 : npts => pw2%pw_grid%npts
793 :
794 0 : IF ((npts(1) /= 0) .AND. (ngpts /= 0)) THEN
795 0 : mg = SIZE(pw2%pw_grid%grays, 2)
796 0 : mmax = MAX(mg, 1)
797 :
798 : ! pointers to data arrays
799 0 : ptr_pwin => pwbuf(1, 1)
800 0 : ptr_pwout => pw2%array(1)
801 :
802 : ! pointer to map array
803 0 : ptr_ghatmap => pw2%pw_grid%g_hatmap(1, 1)
804 :
805 : ! invoke the combined transformation
806 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
807 : CALL fft_gpu_fg_c(c_loc(ptr_pwin), c_loc(ptr_pwout), c_loc(ptr_ghatmap), npts, mmax, ngpts, scale)
808 : #else
809 : MARK_USED(scale)
810 0 : CPABORT("Compiled without pw offloading")
811 : #endif
812 : END IF
813 :
814 0 : CALL timestop(handle)
815 0 : END SUBROUTINE fft_gpu_fg
816 :
817 : ! **************************************************************************************************
818 : !> \brief perform a parallel scatter followed by a 1D-FFT on the gpu
819 : !> \param pw1 ...
820 : !> \param pwbuf ...
821 : !> \param scale ...
822 : !> \author Andreas Gloess
823 : ! **************************************************************************************************
824 0 : SUBROUTINE fft_gpu_sf(pw1, pwbuf, scale)
825 : TYPE(pw_c1d_gs_type), INTENT(IN) :: pw1
826 : COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(INOUT), &
827 : TARGET :: pwbuf
828 : REAL(KIND=dp), INTENT(IN) :: scale
829 :
830 : CHARACTER(len=*), PARAMETER :: routineN = 'fft_gpu_sf'
831 :
832 : COMPLEX(KIND=dp), POINTER :: ptr_pwin, ptr_pwout
833 : INTEGER :: handle, mg, mmax, ngpts, nmaps
834 0 : INTEGER, DIMENSION(:), POINTER :: npts
835 : INTEGER, POINTER :: ptr_ghatmap
836 : INTERFACE
837 : SUBROUTINE fft_gpu_sf_c(zin, zout, ghatmap, npts, mmax, ngpts, nmaps, scale) BIND(C, name="fft_gpu_sf")
838 : IMPORT
839 : TYPE(C_PTR), INTENT(IN), VALUE :: zin
840 : TYPE(C_PTR), VALUE :: zout
841 : TYPE(C_PTR), INTENT(IN), VALUE :: ghatmap
842 : INTEGER(KIND=C_INT), DIMENSION(*), INTENT(IN):: npts
843 : INTEGER(KIND=C_INT), INTENT(IN), VALUE :: mmax, ngpts, nmaps
844 : REAL(KIND=C_DOUBLE), INTENT(IN), VALUE :: scale
845 :
846 : END SUBROUTINE fft_gpu_sf_c
847 : END INTERFACE
848 :
849 0 : CALL timeset(routineN, handle)
850 :
851 0 : ngpts = SIZE(pw1%pw_grid%gsq)
852 0 : npts => pw1%pw_grid%npts
853 :
854 0 : IF ((npts(1) /= 0) .AND. (ngpts /= 0)) THEN
855 0 : mg = SIZE(pw1%pw_grid%grays, 2)
856 0 : mmax = MAX(mg, 1)
857 :
858 : ! pointers to data arrays
859 0 : ptr_pwin => pw1%array(1)
860 0 : ptr_pwout => pwbuf(1, 1)
861 :
862 : ! pointer to map array
863 0 : nmaps = SIZE(pw1%pw_grid%g_hatmap, 2)
864 0 : ptr_ghatmap => pw1%pw_grid%g_hatmap(1, 1)
865 :
866 : ! invoke the combined transformation
867 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
868 : CALL fft_gpu_sf_c(c_loc(ptr_pwin), c_loc(ptr_pwout), c_loc(ptr_ghatmap), npts, mmax, ngpts, nmaps, scale)
869 : #else
870 : MARK_USED(scale)
871 0 : CPABORT("Compiled without pw offloading")
872 : #endif
873 : END IF
874 :
875 0 : CALL timestop(handle)
876 0 : END SUBROUTINE fft_gpu_sf
877 :
878 : END MODULE fft_gpu
|