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 : MODULE fftw3_lib
8 : USE ISO_C_BINDING, ONLY: C_ASSOCIATED, &
9 : C_CHAR, &
10 : C_DOUBLE, &
11 : C_DOUBLE_COMPLEX, &
12 : C_INT, &
13 : C_NULL_PTR, &
14 : C_PTR
15 : #if defined(__FFTW3)
16 : USE ISO_C_BINDING, ONLY: &
17 : C_FLOAT, &
18 : C_FLOAT_COMPLEX, &
19 : C_FUNPTR, &
20 : C_INT32_T, &
21 : C_INTPTR_T, &
22 : C_LOC, &
23 : C_NULL_CHAR, &
24 : C_SIZE_T, C_F_POINTER
25 : USE mathconstants, ONLY: z_zero
26 : #endif
27 : USE fft_kinds, ONLY: dp
28 : USE fft_plan, ONLY: fft_plan_type
29 :
30 : !$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num, omp_get_num_threads
31 :
32 : #include "../../base/base_uses.f90"
33 :
34 : IMPLICIT NONE
35 : PRIVATE
36 :
37 : PUBLIC :: fftw3_do_init, fftw3_do_cleanup, fftw3_get_lengths, fftw33d, fftw31dm
38 : PUBLIC :: fftw3_destroy_plan, fftw3_create_plan_1d, fftw3_create_plan_3d
39 : PUBLIC :: fftw_alloc, fftw_dealloc, fftw3_pair_major_3d
40 : PUBLIC :: fftw3_pair_major_plan_type, fftw3_pair_major_plan_destroy
41 :
42 : INTEGER, SAVE :: fftw_plan_type = -1
43 :
44 : ! The handle is owned by one caller and reused only with matching FFTW parameters.
45 : TYPE fftw3_pair_major_plan_type
46 : PRIVATE
47 : TYPE(C_PTR) :: handle = C_NULL_PTR
48 : INTEGER :: sign = 0, n(3) = 0, nbatch = 0, nthreads = 0
49 : END TYPE fftw3_pair_major_plan_type
50 :
51 : #if defined(__FFTW3)
52 : #include "fftw3.f03"
53 : #endif
54 :
55 : INTERFACE fftw_alloc
56 : MODULE PROCEDURE :: fftw_alloc_complex_1d
57 : MODULE PROCEDURE :: fftw_alloc_complex_2d
58 : MODULE PROCEDURE :: fftw_alloc_complex_3d
59 : END INTERFACE fftw_alloc
60 :
61 : INTERFACE fftw_dealloc
62 : MODULE PROCEDURE :: fftw_dealloc_complex_1d
63 : MODULE PROCEDURE :: fftw_dealloc_complex_2d
64 : MODULE PROCEDURE :: fftw_dealloc_complex_3d
65 : END INTERFACE fftw_dealloc
66 :
67 : CONTAINS
68 :
69 : #:set maxdim = 3
70 : #:for dim in range(1, maxdim+1)
71 : ! Concatenate the components of the dimensions passed to this function to use it if FFTW3 is not used
72 : #:set dim_extended = ", ".join(["n("+str(i)+")" for i in range(1, dim+1)])
73 252212 : SUBROUTINE fftw_alloc_complex_${dim}$d(array, n)
74 : COMPLEX(C_DOUBLE_COMPLEX), DIMENSION(:${", :"*(dim-1)}$), CONTIGUOUS, POINTER, INTENT(OUT) :: array
75 : INTEGER, DIMENSION(${dim}$), INTENT(IN) :: n
76 :
77 : #if defined(__FFTW3)
78 : TYPE(C_PTR) :: data_ptr
79 750465 : data_ptr = fftw_alloc_complex(INT(PRODUCT(n), KIND=C_SIZE_T))
80 750465 : CALL C_F_POINTER(data_ptr, array, n)
81 : #else
82 : ! Just allocate the array
83 : ALLOCATE (array(${dim_extended}$))
84 : #endif
85 :
86 : END SUBROUTINE fftw_alloc_complex_${dim}$d
87 252212 :
88 252185 : SUBROUTINE fftw_dealloc_complex_${dim}$d(array)
89 : COMPLEX(C_DOUBLE_COMPLEX), DIMENSION(:${", :"*(dim-1)}$), CONTIGUOUS, POINTER, INTENT(INOUT) :: array
90 :
91 : #if defined(__FFTW3)
92 252185 : CALL fftw_free(C_LOC(array))
93 252185 : NULLIFY (array)
94 : #else
95 : ! Just deallocate the array
96 : DEALLOCATE (array)
97 : #endif
98 :
99 252185 : END SUBROUTINE fftw_dealloc_complex_${dim}$d
100 : #:endfor
101 :
102 : #if defined(__FFTW3)
103 : ! **************************************************************************************************
104 : !> \brief A workaround that allows us to compile with -Werror=unused-parameter
105 : ! **************************************************************************************************
106 0 : SUBROUTINE dummy_routine_to_call_mark_used()
107 : MARK_USED(FFTW_R2HC)
108 : MARK_USED(FFTW_HC2R)
109 : MARK_USED(FFTW_DHT)
110 : MARK_USED(FFTW_REDFT00)
111 : MARK_USED(FFTW_REDFT01)
112 : MARK_USED(FFTW_REDFT10)
113 : MARK_USED(FFTW_REDFT11)
114 : MARK_USED(FFTW_RODFT00)
115 : MARK_USED(FFTW_RODFT01)
116 : MARK_USED(FFTW_RODFT10)
117 : MARK_USED(FFTW_RODFT11)
118 : MARK_USED(FFTW_FORWARD)
119 : MARK_USED(FFTW_BACKWARD)
120 : MARK_USED(FFTW_MEASURE)
121 : MARK_USED(FFTW_DESTROY_INPUT)
122 : MARK_USED(FFTW_UNALIGNED)
123 : MARK_USED(FFTW_CONSERVE_MEMORY)
124 : MARK_USED(FFTW_EXHAUSTIVE)
125 : MARK_USED(FFTW_PRESERVE_INPUT)
126 : MARK_USED(FFTW_PATIENT)
127 : MARK_USED(FFTW_ESTIMATE)
128 : MARK_USED(FFTW_WISDOM_ONLY)
129 : MARK_USED(FFTW_ESTIMATE_PATIENT)
130 : MARK_USED(FFTW_BELIEVE_PCOST)
131 : MARK_USED(FFTW_NO_DFT_R2HC)
132 : MARK_USED(FFTW_NO_NONTHREADED)
133 : MARK_USED(FFTW_NO_BUFFERING)
134 : MARK_USED(FFTW_NO_INDIRECT_OP)
135 : MARK_USED(FFTW_ALLOW_LARGE_GENERIC)
136 : MARK_USED(FFTW_NO_RANK_SPLITS)
137 : MARK_USED(FFTW_NO_VRANK_SPLITS)
138 : MARK_USED(FFTW_NO_VRECURSE)
139 : MARK_USED(FFTW_NO_SIMD)
140 : MARK_USED(FFTW_NO_SLOW)
141 : MARK_USED(FFTW_NO_FIXED_RADIX_LARGE_N)
142 : MARK_USED(FFTW_ALLOW_PRUNING)
143 0 : END SUBROUTINE dummy_routine_to_call_mark_used
144 : #endif
145 :
146 : ! **************************************************************************************************
147 : !> \brief ...
148 : !> \param wisdom_file ...
149 : !> \param ionode ...
150 : !> \param export_wisdom Export the wisdom to wisdom_file (default: no export). It should only be
151 : !> requested for a wisdom file specified explicitly in the input, since the
152 : !> default wisdom file (/etc/fftw/wisdom) is usually not writable.
153 : ! **************************************************************************************************
154 11690 : SUBROUTINE fftw3_do_cleanup(wisdom_file, ionode, export_wisdom)
155 :
156 : CHARACTER(LEN=*), INTENT(IN) :: wisdom_file
157 : LOGICAL :: ionode
158 : LOGICAL, INTENT(IN), OPTIONAL :: export_wisdom
159 :
160 : #if defined(__FFTW3)
161 11690 : CHARACTER(LEN=1, KIND=C_CHAR), DIMENSION(:), ALLOCATABLE :: wisdom_file_name_c
162 : INTEGER :: file_name_length, i
163 : INTEGER(KIND=C_INT) :: isuccess
164 : LOGICAL :: my_export_wisdom
165 :
166 11690 : my_export_wisdom = .FALSE.
167 11690 : IF (PRESENT(export_wisdom)) my_export_wisdom = export_wisdom
168 :
169 : ! Write out FFTW3 wisdom to file, if requested. Only the ionode updates the wisdom.
170 : ! FFTW opens the file itself and reports a failure.
171 11690 : IF (ionode .AND. my_export_wisdom) THEN
172 2 : file_name_length = LEN_TRIM(wisdom_file)
173 6 : ALLOCATE (wisdom_file_name_c(file_name_length + 1))
174 18 : DO i = 1, file_name_length
175 18 : wisdom_file_name_c(i) = wisdom_file(i:i)
176 : END DO
177 2 : wisdom_file_name_c(file_name_length + 1) = C_NULL_CHAR
178 2 : isuccess = fftw_export_wisdom_to_filename(wisdom_file_name_c)
179 2 : IF (isuccess == 0) THEN
180 : CALL cp_warn(__LOCATION__, "Error exporting wisdom to file "//TRIM(wisdom_file)//". "// &
181 0 : "Wisdom was not exported.")
182 : END IF
183 : END IF
184 :
185 11690 : CALL fftw_cleanup()
186 : #else
187 : MARK_USED(wisdom_file)
188 : MARK_USED(ionode)
189 : MARK_USED(export_wisdom)
190 : #endif
191 :
192 11690 : END SUBROUTINE fftw3_do_cleanup
193 :
194 : ! **************************************************************************************************
195 : !> \brief ...
196 : !> \param wisdom_file ...
197 : ! **************************************************************************************************
198 11902 : SUBROUTINE fftw3_do_init(wisdom_file, plan_style)
199 :
200 : CHARACTER(LEN=*), INTENT(IN) :: wisdom_file
201 : INTEGER, INTENT(IN) :: plan_style
202 :
203 : #if defined(__FFTW3)
204 11902 : CHARACTER(LEN=1, KIND=C_CHAR), DIMENSION(:), ALLOCATABLE :: wisdom_file_name_c
205 : INTEGER :: file_name_length, i
206 : INTEGER(KIND=C_INT) :: isuccess
207 : LOGICAL :: file_exists
208 :
209 11902 : isuccess = fftw_init_threads()
210 11902 : IF (isuccess == 0) THEN
211 0 : CPABORT("Error initializing FFTW with threads")
212 : END IF
213 :
214 : ! Read FFTW wisdom (if available). All nodes read the file here.
215 : ! FFTW opens the file itself and reports a failure.
216 11902 : INQUIRE (FILE=wisdom_file, EXIST=file_exists)
217 11902 : IF (file_exists) THEN
218 2 : file_name_length = LEN_TRIM(wisdom_file)
219 6 : ALLOCATE (wisdom_file_name_c(file_name_length + 1))
220 18 : DO i = 1, file_name_length
221 18 : wisdom_file_name_c(i) = wisdom_file(i:i)
222 : END DO
223 2 : wisdom_file_name_c(file_name_length + 1) = C_NULL_CHAR
224 2 : isuccess = fftw_import_wisdom_from_filename(wisdom_file_name_c)
225 2 : IF (isuccess == 0) THEN
226 : CALL cp_warn(__LOCATION__, "Error importing wisdom from file "//TRIM(wisdom_file)//". "// &
227 : "Maybe the file was created with a different configuration than CP2K is "// &
228 2 : "run with. CP2K continues without importing wisdom.")
229 : END IF
230 : END IF
231 :
232 23794 : SELECT CASE (plan_style)
233 : CASE (1)
234 11892 : fftw_plan_type = FFTW_ESTIMATE
235 : CASE (2)
236 2 : fftw_plan_type = FFTW_MEASURE
237 : CASE (3)
238 4 : fftw_plan_type = FFTW_PATIENT
239 : CASE (4)
240 4 : fftw_plan_type = FFTW_EXHAUSTIVE
241 : CASE DEFAULT
242 11902 : CPABORT("Invalid FFTW_PLAN_TYPE")
243 : END SELECT
244 : #else
245 : MARK_USED(wisdom_file)
246 : MARK_USED(plan_style)
247 : #endif
248 :
249 11902 : END SUBROUTINE fftw3_do_init
250 :
251 : ! **************************************************************************************************
252 : !> \brief ...
253 : !> \param DATA ...
254 : !> \param max_length ...
255 : !> \par History
256 : !> JGH 23-Jan-2006 : initial version
257 : !> Adapted for new interface
258 : !> IAB 09-Jan-2009 : Modified to cache plans in fft_plan_type
259 : !> (c) The Numerical Algorithms Group (NAG) Ltd, 2009 on behalf of the HECToR project
260 : !> IAB 09-Oct-2009 : Added OpenMP directives to 1D FFT, and planning routines
261 : !> (c) The Numerical Algorithms Group (NAG) Ltd, 2009 on behalf of the HECToR project
262 : !> IAB 11-Sep-2012 : OpenMP parallel 3D FFT (Ruyman Reyes, PRACE)
263 : !> \author JGH
264 : ! **************************************************************************************************
265 0 : SUBROUTINE fftw3_get_lengths(DATA, max_length)
266 :
267 : INTEGER, DIMENSION(*) :: DATA
268 : INTEGER, INTENT(INOUT) :: max_length
269 :
270 : INTEGER :: h, i, j, k, m, maxn, maxn_elevens, &
271 : maxn_fives, maxn_sevens, &
272 : maxn_thirteens, maxn_threes, &
273 : maxn_twos, ndata, nmax, number
274 0 : INTEGER, ALLOCATABLE, DIMENSION(:) :: dlocal, idx
275 :
276 : !------------------------------------------------------------------------------
277 : ! compute ndata
278 : !! FFTW can do arbitrary(?) lengths, maybe you want to limit them to some
279 : !! powers of small prime numbers though...
280 :
281 0 : maxn_twos = 15
282 0 : maxn_threes = 3
283 0 : maxn_fives = 2
284 0 : maxn_sevens = 1
285 0 : maxn_elevens = 1
286 0 : maxn_thirteens = 0
287 0 : maxn = 37748736
288 :
289 0 : ndata = 0
290 0 : DO h = 0, maxn_twos
291 0 : nmax = HUGE(0)/2**h
292 0 : DO i = 0, maxn_threes
293 0 : DO j = 0, maxn_fives
294 0 : DO k = 0, maxn_sevens
295 0 : DO m = 0, maxn_elevens
296 0 : number = (3**i)*(5**j)*(7**k)*(11**m)
297 :
298 0 : IF (number > nmax) CYCLE
299 :
300 0 : number = number*2**h
301 0 : IF (number >= maxn) CYCLE
302 :
303 0 : ndata = ndata + 1
304 : END DO
305 : END DO
306 : END DO
307 : END DO
308 : END DO
309 :
310 0 : ALLOCATE (dlocal(ndata), idx(ndata))
311 :
312 0 : ndata = 0
313 0 : dlocal(:) = 0
314 0 : DO h = 0, maxn_twos
315 0 : nmax = HUGE(0)/2**h
316 0 : DO i = 0, maxn_threes
317 0 : DO j = 0, maxn_fives
318 0 : DO k = 0, maxn_sevens
319 0 : DO m = 0, maxn_elevens
320 0 : number = (3**i)*(5**j)*(7**k)*(11**m)
321 :
322 0 : IF (number > nmax) CYCLE
323 :
324 0 : number = number*2**h
325 0 : IF (number >= maxn) CYCLE
326 :
327 0 : ndata = ndata + 1
328 0 : dlocal(ndata) = number
329 : END DO
330 : END DO
331 : END DO
332 : END DO
333 : END DO
334 :
335 0 : CALL sortint(dlocal, ndata, idx)
336 0 : ndata = MIN(ndata, max_length)
337 0 : DATA(1:ndata) = dlocal(1:ndata)
338 0 : max_length = ndata
339 :
340 0 : DEALLOCATE (dlocal, idx)
341 :
342 0 : END SUBROUTINE fftw3_get_lengths
343 :
344 : ! **************************************************************************************************
345 : !> \brief ...
346 : !> \param iarr ...
347 : !> \param n ...
348 : !> \param index ...
349 : ! **************************************************************************************************
350 0 : SUBROUTINE sortint(iarr, n, index)
351 :
352 : INTEGER, INTENT(IN) :: n
353 : INTEGER, INTENT(INOUT) :: iarr(1:n)
354 : INTEGER, INTENT(OUT) :: INDEX(1:n)
355 :
356 : INTEGER, PARAMETER :: m = 7, nstack = 50
357 :
358 : INTEGER :: a, i, ib, ir, istack(1:nstack), itemp, &
359 : j, jstack, k, l, temp
360 :
361 : !------------------------------------------------------------------------------
362 :
363 0 : DO i = 1, n
364 0 : INDEX(i) = i
365 : END DO
366 : jstack = 0
367 : l = 1
368 : ir = n
369 : DO WHILE (.TRUE.)
370 0 : IF (ir - l < m) THEN
371 0 : DO j = l + 1, ir
372 0 : a = iarr(j)
373 0 : ib = INDEX(j)
374 0 : DO i = j - 1, 0, -1
375 0 : IF (i == 0) EXIT
376 0 : IF (iarr(i) <= a) EXIT
377 0 : iarr(i + 1) = iarr(i)
378 0 : INDEX(i + 1) = INDEX(i)
379 : END DO
380 0 : iarr(i + 1) = a
381 0 : INDEX(i + 1) = ib
382 : END DO
383 0 : IF (jstack == 0) RETURN
384 0 : ir = istack(jstack)
385 0 : l = istack(jstack - 1)
386 0 : jstack = jstack - 2
387 : ELSE
388 0 : k = (l + ir)/2
389 0 : temp = iarr(k)
390 0 : iarr(k) = iarr(l + 1)
391 0 : iarr(l + 1) = temp
392 0 : itemp = INDEX(k)
393 0 : INDEX(k) = INDEX(l + 1)
394 0 : INDEX(l + 1) = itemp
395 0 : IF (iarr(l + 1) > iarr(ir)) THEN
396 0 : temp = iarr(l + 1)
397 0 : iarr(l + 1) = iarr(ir)
398 0 : iarr(ir) = temp
399 0 : itemp = INDEX(l + 1)
400 0 : INDEX(l + 1) = INDEX(ir)
401 0 : INDEX(ir) = itemp
402 : END IF
403 0 : IF (iarr(l) > iarr(ir)) THEN
404 0 : temp = iarr(l)
405 0 : iarr(l) = iarr(ir)
406 0 : iarr(ir) = temp
407 0 : itemp = INDEX(l)
408 0 : INDEX(l) = INDEX(ir)
409 0 : INDEX(ir) = itemp
410 : END IF
411 0 : IF (iarr(l + 1) > iarr(l)) THEN
412 0 : temp = iarr(l + 1)
413 0 : iarr(l + 1) = iarr(l)
414 0 : iarr(l) = temp
415 0 : itemp = INDEX(l + 1)
416 0 : INDEX(l + 1) = INDEX(l)
417 0 : INDEX(l) = itemp
418 : END IF
419 0 : i = l + 1
420 0 : j = ir
421 0 : a = iarr(l)
422 0 : ib = INDEX(l)
423 0 : DO WHILE (.TRUE.)
424 0 : i = i + 1
425 0 : DO WHILE (iarr(i) < a)
426 0 : i = i + 1
427 : END DO
428 0 : j = j - 1
429 0 : DO WHILE (iarr(j) > a)
430 0 : j = j - 1
431 : END DO
432 0 : IF (j < i) EXIT
433 0 : temp = iarr(i)
434 0 : iarr(i) = iarr(j)
435 0 : iarr(j) = temp
436 0 : itemp = INDEX(i)
437 0 : INDEX(i) = INDEX(j)
438 0 : INDEX(j) = itemp
439 : END DO
440 0 : iarr(l) = iarr(j)
441 0 : iarr(j) = a
442 0 : INDEX(l) = INDEX(j)
443 0 : INDEX(j) = ib
444 0 : jstack = jstack + 2
445 0 : IF (jstack > nstack) CPABORT("Nstack too small in sortint")
446 0 : IF (ir - i + 1 >= j - l) THEN
447 0 : istack(jstack) = ir
448 0 : istack(jstack - 1) = i
449 0 : ir = j - 1
450 : ELSE
451 0 : istack(jstack) = j - 1
452 0 : istack(jstack - 1) = l
453 0 : l = i
454 : END IF
455 : END IF
456 :
457 : END DO
458 :
459 : END SUBROUTINE sortint
460 :
461 : ! **************************************************************************************************
462 :
463 : ! **************************************************************************************************
464 : !> \brief ...
465 : !> \param plan ...
466 : !> \param fft_rank ...
467 : !> \param dim_n ...
468 : !> \param dim_istride ...
469 : !> \param dim_ostride ...
470 : !> \param hm_rank ...
471 : !> \param hm_n ...
472 : !> \param hm_istride ...
473 : !> \param hm_ostride ...
474 : !> \param zin ...
475 : !> \param zout ...
476 : !> \param fft_direction ...
477 : !> \param fftw_plan_type ...
478 : !> \param valid ...
479 : ! **************************************************************************************************
480 64492 : SUBROUTINE fftw3_create_guru_plan(plan, fft_rank, dim_n, &
481 : dim_istride, dim_ostride, hm_rank, &
482 : hm_n, hm_istride, hm_ostride, &
483 : zin, zout, fft_direction, fftw_plan_type, &
484 : valid)
485 :
486 : TYPE(C_PTR), INTENT(INOUT) :: plan
487 : COMPLEX(KIND=dp), DIMENSION(*), INTENT(INOUT) :: zin, zout
488 : INTEGER, INTENT(IN) :: dim_n(2), dim_istride(2), dim_ostride(2), &
489 : hm_n(2), hm_istride(2), hm_ostride(2), fft_rank, &
490 : fft_direction, fftw_plan_type, hm_rank
491 : LOGICAL, INTENT(OUT) :: valid
492 :
493 : #if defined(__FFTW3)
494 : TYPE(fftw_iodim) :: dim(2), hm(2)
495 : INTEGER :: i
496 :
497 193476 : DO i = 1, 2
498 128984 : DIM(i) = fftw_iodim(dim_n(i), dim_istride(i), dim_ostride(i))
499 193476 : hm(i) = fftw_iodim(hm_n(i), hm_istride(i), hm_ostride(i))
500 : END DO
501 :
502 : plan = fftw_plan_guru_dft(fft_rank, &
503 : dim, hm_rank, hm, &
504 : zin, zout, &
505 64492 : fft_direction, fftw_plan_type)
506 :
507 64492 : valid = C_ASSOCIATED(plan)
508 :
509 : #else
510 : MARK_USED(plan)
511 : MARK_USED(fft_rank)
512 : MARK_USED(dim_n)
513 : MARK_USED(dim_istride)
514 : MARK_USED(dim_ostride)
515 : MARK_USED(hm_rank)
516 : MARK_USED(hm_n)
517 : MARK_USED(hm_istride)
518 : MARK_USED(hm_ostride)
519 : MARK_USED(fft_direction)
520 : MARK_USED(fftw_plan_type)
521 : !MARK_USED does not work with assumed size arguments
522 : IF (.FALSE.) THEN; DO; IF (ABS(zin(1)) > ABS(zout(1))) EXIT; END DO; END IF
523 : valid = .FALSE.
524 :
525 : #endif
526 :
527 64492 : END SUBROUTINE fftw3_create_guru_plan
528 :
529 : ! **************************************************************************************************
530 :
531 : ! **************************************************************************************************
532 : !> \brief Attempt to create a plan with the guru interface for a 2d sub-space.
533 : !> If this fails, fall back to the FFTW3 threaded 3D transform instead
534 : !> of the hand-optimised version.
535 : !> \return ...
536 : ! **************************************************************************************************
537 64492 : FUNCTION fftw3_is_guru_supported() RESULT(guru_supported)
538 : LOGICAL :: guru_supported
539 : #if defined(__FFTW3)
540 : INTEGER :: dim_n(2), dim_istride(2), dim_ostride(2), &
541 : howmany_n(2), howmany_istride(2), howmany_ostride(2)
542 : TYPE(C_PTR) :: test_plan
543 : COMPLEX(KIND=dp), DIMENSION(1, 1, 1) :: zin
544 :
545 64492 : dim_n(1) = 1
546 64492 : dim_n(2) = 1
547 64492 : dim_istride(1) = 1
548 64492 : dim_istride(2) = 1
549 64492 : dim_ostride(1) = 1
550 64492 : dim_ostride(2) = 1
551 64492 : howmany_n(1) = 1
552 64492 : howmany_n(2) = 1
553 64492 : howmany_istride(1) = 1
554 64492 : howmany_istride(2) = 1
555 64492 : howmany_ostride(1) = 1
556 64492 : howmany_ostride(2) = 1
557 64492 : zin = z_zero
558 : CALL fftw3_create_guru_plan(test_plan, 1, &
559 : dim_n, dim_istride, dim_ostride, &
560 : 2, howmany_n, howmany_istride, howmany_ostride, &
561 : zin, zin, &
562 64492 : FFTW_FORWARD, FFTW_ESTIMATE, guru_supported)
563 64492 : IF (guru_supported) THEN
564 64492 : CALL fftw_destroy_plan(test_plan)
565 : END IF
566 :
567 : #else
568 : guru_supported = .FALSE.
569 : #endif
570 :
571 64492 : END FUNCTION fftw3_is_guru_supported
572 :
573 : ! **************************************************************************************************
574 :
575 : ! **************************************************************************************************
576 : !> \brief ...
577 : !> \param nrows ...
578 : !> \param nt ...
579 : !> \param rows_per_thread ...
580 : !> \param rows_per_thread_r ...
581 : !> \param th_planA ...
582 : !> \param th_planB ...
583 : ! **************************************************************************************************
584 0 : SUBROUTINE fftw3_compute_rows_per_th(nrows, nt, rows_per_thread, rows_per_thread_r, &
585 : th_planA, th_planB)
586 :
587 : INTEGER, INTENT(IN) :: nrows, nt
588 : INTEGER, INTENT(OUT) :: rows_per_thread, rows_per_thread_r, &
589 : th_planA, th_planB
590 :
591 0 : IF (MOD(nrows, nt) == 0) THEN
592 0 : rows_per_thread = nrows/nt
593 0 : rows_per_thread_r = 0
594 0 : th_planA = nt
595 0 : th_planB = 0
596 : ELSE
597 0 : rows_per_thread = nrows/nt + 1
598 0 : rows_per_thread_r = nrows/nt
599 0 : th_planA = MOD(nrows, nt)
600 0 : th_planB = nt - th_planA
601 : END IF
602 :
603 0 : END SUBROUTINE fftw3_compute_rows_per_th
604 :
605 : ! **************************************************************************************************
606 :
607 : ! **************************************************************************************************
608 : !> \brief ...
609 : !> \param plan ...
610 : !> \param plan_r ...
611 : !> \param dim_n ...
612 : !> \param dim_istride ...
613 : !> \param dim_ostride ...
614 : !> \param hm_n ...
615 : !> \param hm_istride ...
616 : !> \param hm_ostride ...
617 : !> \param input ...
618 : !> \param output ...
619 : !> \param fft_direction ...
620 : !> \param fftw_plan_type ...
621 : !> \param rows_per_th ...
622 : !> \param rows_per_th_r ...
623 : ! **************************************************************************************************
624 0 : SUBROUTINE fftw3_create_3d_plans(plan, plan_r, dim_n, dim_istride, dim_ostride, &
625 : hm_n, hm_istride, hm_ostride, &
626 : input, output, &
627 : fft_direction, fftw_plan_type, rows_per_th, &
628 : rows_per_th_r)
629 :
630 : TYPE(C_PTR), INTENT(INOUT) :: plan, plan_r
631 : INTEGER, INTENT(INOUT) :: dim_n(2), dim_istride(2), &
632 : dim_ostride(2), hm_n(2), &
633 : hm_istride(2), hm_ostride(2)
634 : COMPLEX(KIND=dp), DIMENSION(*), INTENT(INOUT) :: input, output
635 : INTEGER, INTENT(INOUT) :: fft_direction, fftw_plan_type
636 : INTEGER, INTENT(IN) :: rows_per_th, rows_per_th_r
637 :
638 : LOGICAL :: valid
639 :
640 : ! First plans will have an additional row
641 :
642 0 : hm_n(2) = rows_per_th
643 : CALL fftw3_create_guru_plan(plan, 1, &
644 : dim_n, dim_istride, dim_ostride, &
645 : 2, hm_n, hm_istride, hm_ostride, &
646 : input, output, &
647 0 : fft_direction, fftw_plan_type, valid)
648 :
649 0 : IF (.NOT. valid) THEN
650 0 : CPABORT("fftw3_create_plan")
651 : END IF
652 :
653 : !!!! Remainder
654 0 : hm_n(2) = rows_per_th_r
655 : CALL fftw3_create_guru_plan(plan_r, 1, &
656 : dim_n, dim_istride, dim_ostride, &
657 : 2, hm_n, hm_istride, hm_ostride, &
658 : input, output, &
659 0 : fft_direction, fftw_plan_type, valid)
660 0 : IF (.NOT. valid) THEN
661 0 : CPABORT("fftw3_create_plan (remaining)")
662 : END IF
663 :
664 0 : END SUBROUTINE fftw3_create_3d_plans
665 :
666 : ! **************************************************************************************************
667 :
668 : ! **************************************************************************************************
669 : !> \brief ...
670 : !> \param plan ...
671 : !> \param zin ...
672 : !> \param zout ...
673 : ! **************************************************************************************************
674 64492 : SUBROUTINE fftw3_create_plan_3d(plan, zin, zout)
675 :
676 : TYPE(fft_plan_type), INTENT(INOUT) :: plan
677 : COMPLEX(KIND=dp), DIMENSION(*), INTENT(INOUT) :: zin
678 : COMPLEX(KIND=dp), DIMENSION(*), INTENT(INOUT) :: zout
679 : #if defined(__FFTW3)
680 : INTEGER :: n1, n2, n3
681 : INTEGER :: nt
682 : INTEGER :: rows_per_th
683 : INTEGER :: rows_per_th_r
684 : INTEGER :: fft_direction
685 : INTEGER :: th_planA, th_planB
686 64492 : COMPLEX(KIND=dp), ALLOCATABLE :: tmp(:)
687 :
688 : ! GURU Interface
689 : INTEGER :: dim_n(2), dim_istride(2), dim_ostride(2), &
690 : howmany_n(2), howmany_istride(2), howmany_ostride(2)
691 :
692 64492 : IF (plan%fsign == +1) THEN
693 32246 : fft_direction = FFTW_FORWARD
694 : ELSE
695 32246 : fft_direction = FFTW_BACKWARD
696 : END IF
697 :
698 64492 : n1 = plan%n_3d(1)
699 64492 : n2 = plan%n_3d(2)
700 64492 : n3 = plan%n_3d(3)
701 :
702 64492 : nt = 1
703 64492 : !$OMP PARALLEL DEFAULT(NONE) SHARED(nt)
704 : !$OMP MASTER
705 : !$ nt = omp_get_num_threads()
706 : !$OMP END MASTER
707 : !$OMP END PARALLEL
708 :
709 : IF ((.NOT. fftw3_is_guru_supported()) .OR. &
710 64492 : (.NOT. fftw_plan_type == FFTW_ESTIMATE) .OR. &
711 : (n1 < 256 .AND. n2 < 256 .AND. n3 < 256 .AND. nt == 1)) THEN
712 : ! If the plan type is MEASURE, PATIENT and EXHAUSTIVE or
713 : ! the grid size is small (and we are single-threaded) then
714 : ! FFTW3 does a better job than handmade optimization
715 : ! so plan a single 3D FFT which will execute using all the threads
716 :
717 64492 : plan%separated_plans = .FALSE.
718 64492 : !$ CALL fftw_plan_with_nthreads(nt)
719 :
720 64492 : IF (plan%fft_in_place) THEN
721 32246 : plan%fftw_plan = fftw_plan_dft_3d(n3, n2, n1, zin, zin, fft_direction, fftw_plan_type)
722 : ELSE
723 32246 : plan%fftw_plan = fftw_plan_dft_3d(n3, n2, n1, zin, zout, fft_direction, fftw_plan_type)
724 : END IF
725 : ELSE
726 0 : ALLOCATE (tmp(n1*n2*n3))
727 : ! ************************* PLANS WITH TRANSPOSITIONS ****************************
728 : ! In the cases described above, we manually thread each stage of the 3D FFT.
729 : !
730 : ! The following plans replace the 3D FFT call by running 1D FFTW across all
731 : ! 3 directions of the array.
732 : !
733 : ! Output of FFTW is transposed to ensure that the next round of FFTW access
734 : ! contiguous information.
735 : !
736 : ! Assuming the input matrix is M(n3,n2,n1), FFTW/Transp are :
737 : ! M(n3,n2,n1) -> fftw(x) -> M(n3,n1,n2) -> fftw(y) -> M(n1,n2,n3) -> fftw(z) -> M(n1,n2,n3)
738 : ! Notice that last matrix is transposed in the Z axis. A DO-loop in the execute routine
739 : ! will perform the final transposition. Performance evaluation showed that using an external
740 : ! DO loop to do the final transposition performed better than directly transposing the output.
741 : ! However, this might vary depending on the compiler/platform, so a potential tuning spot
742 : ! is to perform the final transposition within the fftw library rather than using the external loop
743 : ! See comments below in Z-FFT for how to transpose the output to avoid the final DO loop.
744 : !
745 : ! Doc. for the Guru interface is in http://www.fftw.org/doc/Guru-Interface.html
746 : !
747 : ! OpenMP : Work is distributed on the Z plane.
748 : ! All transpositions are out-of-place to facilitate multi-threading
749 : !
750 : !!!! Plan for X : M(n3,n2,n1) -> fftw(x) -> M(n3,n1,n2)
751 : CALL fftw3_compute_rows_per_th(n3, nt, rows_per_th, rows_per_th_r, &
752 0 : th_planA, th_planB)
753 :
754 0 : dim_n(1) = n1
755 0 : dim_istride(1) = 1
756 0 : dim_ostride(1) = n2
757 0 : howmany_n(1) = n2
758 0 : howmany_n(2) = rows_per_th
759 0 : howmany_istride(1) = n1
760 0 : howmany_istride(2) = n1*n2
761 0 : howmany_ostride(1) = 1
762 0 : howmany_ostride(2) = n1*n2
763 : CALL fftw3_create_3d_plans(plan%fftw_plan_nx, plan%fftw_plan_nx_r, &
764 : dim_n, dim_istride, dim_ostride, howmany_n, &
765 : howmany_istride, howmany_ostride, &
766 : zin, tmp, &
767 : fft_direction, fftw_plan_type, rows_per_th, &
768 0 : rows_per_th_r)
769 :
770 : !!!! Plan for Y : M(n3,n1,n2) -> fftw(y) -> M(n1,n2,n3)
771 : CALL fftw3_compute_rows_per_th(n3, nt, rows_per_th, rows_per_th_r, &
772 0 : th_planA, th_planB)
773 0 : dim_n(1) = n2
774 : dim_istride(1) = 1
775 0 : dim_ostride(1) = n3
776 0 : howmany_n(1) = n1
777 0 : howmany_n(2) = rows_per_th
778 0 : howmany_istride(1) = n2
779 : howmany_istride(2) = n1*n2
780 : !!! transposed Z axis on output
781 0 : howmany_ostride(1) = n2*n3
782 0 : howmany_ostride(2) = 1
783 :
784 : CALL fftw3_create_3d_plans(plan%fftw_plan_ny, plan%fftw_plan_ny_r, &
785 : dim_n, dim_istride, dim_ostride, &
786 : howmany_n, howmany_istride, howmany_ostride, &
787 : tmp, zin, &
788 : fft_direction, fftw_plan_type, rows_per_th, &
789 0 : rows_per_th_r)
790 :
791 : !!!! Plan for Z : M(n1,n2,n3) -> fftw(z) -> M(n1,n2,n3)
792 : CALL fftw3_compute_rows_per_th(n1, nt, rows_per_th, rows_per_th_r, &
793 0 : th_planA, th_planB)
794 0 : dim_n(1) = n3
795 : dim_istride(1) = 1
796 0 : dim_ostride(1) = 1 ! To transpose: n2*n1
797 0 : howmany_n(1) = n2
798 0 : howmany_n(2) = rows_per_th
799 0 : howmany_istride(1) = n3
800 0 : howmany_istride(2) = n2*n3
801 0 : howmany_ostride(1) = n3 ! To transpose: n1
802 0 : howmany_ostride(2) = n2*n3 ! To transpose: 1
803 :
804 : CALL fftw3_create_3d_plans(plan%fftw_plan_nz, plan%fftw_plan_nz_r, &
805 : dim_n, dim_istride, dim_ostride, &
806 : howmany_n, howmany_istride, howmany_ostride, &
807 : zin, tmp, &
808 : fft_direction, fftw_plan_type, rows_per_th, &
809 0 : rows_per_th_r)
810 :
811 0 : plan%separated_plans = .TRUE.
812 :
813 0 : DEALLOCATE (tmp)
814 : END IF
815 :
816 : #else
817 : MARK_USED(plan)
818 : !MARK_USED does not work with assumed size arguments
819 : IF (.FALSE.) THEN; DO; IF (ABS(zin(1)) > ABS(zout(1))) EXIT; END DO; END IF
820 : #endif
821 :
822 64492 : END SUBROUTINE fftw3_create_plan_3d
823 :
824 : ! **************************************************************************************************
825 :
826 : ! **************************************************************************************************
827 : !> \brief ...
828 : !> \param plan ...
829 : !> \param plan_r ...
830 : !> \param split_dim ...
831 : !> \param nt ...
832 : !> \param tid ...
833 : !> \param input ...
834 : !> \param istride ...
835 : !> \param output ...
836 : !> \param ostride ...
837 : ! **************************************************************************************************
838 0 : SUBROUTINE fftw3_workshare_execute_dft(plan, plan_r, split_dim, nt, tid, &
839 : input, istride, output, ostride)
840 :
841 : INTEGER, INTENT(IN) :: split_dim, nt, tid
842 : INTEGER, INTENT(IN) :: istride, ostride
843 : COMPLEX(KIND=dp), DIMENSION(*), INTENT(INOUT) :: input, output
844 : TYPE(C_PTR) :: plan, plan_r
845 : #if defined(__FFTW3)
846 : INTEGER :: i_off, o_off
847 : INTEGER :: th_planA, th_planB
848 : INTEGER :: rows_per_thread, rows_per_thread_r
849 :
850 : CALL fftw3_compute_rows_per_th(split_dim, nt, rows_per_thread, &
851 : rows_per_thread_r, &
852 0 : th_planA, th_planB)
853 :
854 0 : IF (th_planB > 0) THEN
855 0 : IF (tid < th_planA) THEN
856 0 : i_off = (tid)*(istride*(rows_per_thread)) + 1
857 0 : o_off = (tid)*(ostride*(rows_per_thread)) + 1
858 0 : IF (rows_per_thread > 0) THEN
859 : CALL fftw_execute_dft(plan, input(i_off), &
860 0 : output(o_off))
861 : END IF
862 0 : ELSE IF ((tid - th_planA) < th_planB) THEN
863 :
864 : i_off = (th_planA)*istride*(rows_per_thread) + &
865 0 : (tid - th_planA)*istride*(rows_per_thread_r) + 1
866 : o_off = (th_planA)*ostride*(rows_per_thread) + &
867 0 : (tid - th_planA)*ostride*(rows_per_thread_r) + 1
868 :
869 : CALL fftw_execute_dft(plan_r, input(i_off), &
870 0 : output(o_off))
871 : END IF
872 :
873 : ELSE
874 0 : i_off = (tid)*(istride*(rows_per_thread)) + 1
875 0 : o_off = (tid)*(ostride*(rows_per_thread)) + 1
876 :
877 : CALL fftw_execute_dft(plan, input(i_off), &
878 0 : output(o_off))
879 :
880 : END IF
881 : #else
882 : MARK_USED(plan)
883 : MARK_USED(plan_r)
884 : MARK_USED(split_dim)
885 : MARK_USED(nt)
886 : MARK_USED(tid)
887 : MARK_USED(istride)
888 : MARK_USED(ostride)
889 : !MARK_USED does not work with assumed size arguments
890 : IF (.FALSE.) THEN; DO; IF (ABS(input(1)) > ABS(output(1))) EXIT; END DO; END IF
891 : #endif
892 :
893 0 : END SUBROUTINE fftw3_workshare_execute_dft
894 :
895 : ! **************************************************************************************************
896 :
897 : ! **************************************************************************************************
898 : !> \brief ...
899 : !> \param plan ...
900 : !> \param scale ...
901 : !> \param zin ...
902 : !> \param zout ...
903 : !> \param stat ...
904 : ! **************************************************************************************************
905 667952 : SUBROUTINE fftw33d(plan, scale, zin, zout, stat)
906 :
907 : TYPE(fft_plan_type), INTENT(IN) :: plan
908 : REAL(KIND=dp), INTENT(IN) :: scale
909 : COMPLEX(KIND=dp), DIMENSION(*), INTENT(INOUT), TARGET:: zin
910 : COMPLEX(KIND=dp), DIMENSION(*), INTENT(INOUT), TARGET:: zout
911 : INTEGER, INTENT(OUT) :: stat
912 : #if defined(__FFTW3)
913 667952 : COMPLEX(KIND=dp), POINTER :: xout(:)
914 667952 : COMPLEX(KIND=dp), ALLOCATABLE :: tmp1(:)
915 : INTEGER :: n1, n2, n3
916 : INTEGER :: tid, nt
917 : INTEGER :: i, j, k
918 :
919 667952 : n1 = plan%n_3d(1)
920 667952 : n2 = plan%n_3d(2)
921 667952 : n3 = plan%n_3d(3)
922 :
923 667952 : stat = 1
924 :
925 : ! We use a POINTER to the output array to avoid duplicating code
926 667952 : IF (plan%fft_in_place) THEN
927 667948 : xout => zin(:n1*n2*n3)
928 : ELSE
929 4 : xout => zout(:n1*n2*n3)
930 : END IF
931 :
932 : ! Either compute the full 3D FFT using a multithreaded plan
933 667952 : IF (.NOT. plan%separated_plans) THEN
934 667952 : CALL fftw_execute_dft(plan%fftw_plan, zin, xout)
935 : ELSE
936 : ! Or use the 3 stage FFT scheme described in fftw3_create_plan_3d
937 0 : ALLOCATE (tmp1(n1*n2*n3)) ! Temporary vector used for transpositions
938 0 : !$OMP PARALLEL DEFAULT(NONE) PRIVATE(tid,nt,i,j,k) SHARED(zin,tmp1,n1,n2,n3,plan,xout)
939 : tid = 0
940 : nt = 1
941 :
942 : !$ tid = omp_get_thread_num()
943 : !$ nt = omp_get_num_threads()
944 : CALL fftw3_workshare_execute_dft(plan%fftw_plan_nx, plan%fftw_plan_nx_r, &
945 : n3, nt, tid, &
946 : zin, n1*n2, tmp1, n1*n2)
947 :
948 : !$OMP BARRIER
949 : CALL fftw3_workshare_execute_dft(plan%fftw_plan_ny, plan%fftw_plan_ny_r, &
950 : n3, nt, tid, &
951 : tmp1, n1*n2, xout, 1)
952 : !$OMP BARRIER
953 : CALL fftw3_workshare_execute_dft(plan%fftw_plan_nz, plan%fftw_plan_nz_r, &
954 : n1, nt, tid, &
955 : xout, n2*n3, tmp1, n2*n3)
956 : !$OMP BARRIER
957 :
958 : !$OMP DO COLLAPSE(3)
959 : DO i = 1, n1
960 : DO j = 1, n2
961 : DO k = 1, n3
962 : xout((i - 1) + (j - 1)*n1 + (k - 1)*n1*n2 + 1) = &
963 : tmp1((k - 1) + (j - 1)*n3 + (i - 1)*n3*n2 + 1)
964 : END DO
965 : END DO
966 : END DO
967 : !$OMP END DO
968 :
969 : !$OMP END PARALLEL
970 : END IF
971 :
972 667952 : IF (scale /= 1.0_dp) THEN
973 312754 : CALL zdscal(n1*n2*n3, scale, xout, 1)
974 : END IF
975 :
976 : #else
977 : MARK_USED(plan)
978 : MARK_USED(scale)
979 : !MARK_USED does not work with assumed size arguments
980 : IF (.FALSE.) THEN; DO; IF (ABS(zin(1)) > ABS(zout(1))) EXIT; END DO; END IF
981 : stat = 0
982 :
983 : #endif
984 :
985 667952 : END SUBROUTINE fftw33d
986 :
987 : ! **************************************************************************************************
988 :
989 : ! **************************************************************************************************
990 : !> \brief ...
991 : !> \param plan ...
992 : !> \param zin ...
993 : !> \param zout ...
994 : ! **************************************************************************************************
995 486392 : SUBROUTINE fftw3_create_plan_1d(plan, zin, zout)
996 : TYPE(fft_plan_type), INTENT(INOUT) :: plan
997 : COMPLEX(KIND=dp), DIMENSION(*), INTENT(IN) :: zin
998 : COMPLEX(KIND=dp), DIMENSION(*), INTENT(IN) :: zout
999 : #if defined(__FFTW3)
1000 : INTEGER :: istride, idist, ostride, odist, num_threads, num_rows
1001 :
1002 486392 : num_threads = 1
1003 486392 : plan%separated_plans = .FALSE.
1004 : !$OMP PARALLEL DEFAULT(NONE), &
1005 486392 : !$OMP SHARED(NUM_THREADS)
1006 : !$OMP MASTER
1007 : !$ num_threads = omp_get_num_threads()
1008 : !$OMP END MASTER
1009 : !$OMP END PARALLEL
1010 :
1011 486392 : num_rows = plan%m/num_threads
1012 486392 : !$ plan%num_threads_needed = num_threads
1013 :
1014 : ! Check for number of rows less than num_threads
1015 486392 : !$ IF (plan%m < num_threads) THEN
1016 0 : !$ num_rows = 1
1017 0 : !$ plan%num_threads_needed = plan%m
1018 : !$ END IF
1019 :
1020 : ! Check for total number of rows not divisible by num_threads
1021 486392 : !$ IF (num_rows*plan%num_threads_needed /= plan%m) THEN
1022 0 : !$ plan%need_alt_plan = .TRUE.
1023 : !$ END IF
1024 :
1025 486392 : !$ plan%num_rows = num_rows
1026 486392 : istride = 1
1027 486392 : idist = plan%ldx_in
1028 486392 : ostride = 1
1029 486392 : odist = plan%ldx_out
1030 486392 : IF (plan%trans_in) THEN
1031 377906 : istride = plan%ldx_in
1032 377906 : idist = 1
1033 : END IF
1034 486392 : IF (plan%trans_out) THEN
1035 377906 : ostride = plan%ldx_out
1036 377906 : odist = 1
1037 : END IF
1038 :
1039 486392 : IF (plan%fsign == +1) THEN
1040 : CALL dfftw_plan_many_dft(plan%fftw_plan, 1, plan%n, num_rows, zin, 0, istride, idist, &
1041 241481 : zout, 0, ostride, odist, FFTW_FORWARD, fftw_plan_type)
1042 : ELSE
1043 : CALL dfftw_plan_many_dft(plan%fftw_plan, 1, plan%n, num_rows, zin, 0, istride, idist, &
1044 244911 : zout, 0, ostride, odist, FFTW_BACKWARD, fftw_plan_type)
1045 : END IF
1046 :
1047 486392 : !$ IF (plan%need_alt_plan) THEN
1048 0 : !$ plan%alt_num_rows = plan%m - (plan%num_threads_needed - 1)*num_rows
1049 0 : !$ IF (plan%fsign == +1) THEN
1050 : !$ CALL dfftw_plan_many_dft(plan%alt_fftw_plan, 1, plan%n, plan%alt_num_rows, zin, 0, istride, idist, &
1051 0 : !$ zout, 0, ostride, odist, FFTW_FORWARD, fftw_plan_type)
1052 : !$ ELSE
1053 : !$ CALL dfftw_plan_many_dft(plan%alt_fftw_plan, 1, plan%n, plan%alt_num_rows, zin, 0, istride, idist, &
1054 0 : !$ zout, 0, ostride, odist, FFTW_BACKWARD, fftw_plan_type)
1055 : !$ END IF
1056 : !$ END IF
1057 :
1058 : #else
1059 : MARK_USED(plan)
1060 : !MARK_USED does not work with assumed size arguments
1061 : IF (.FALSE.) THEN; DO; IF (ABS(zin(1)) > ABS(zout(1))) EXIT; END DO; END IF
1062 : #endif
1063 :
1064 486392 : END SUBROUTINE fftw3_create_plan_1d
1065 :
1066 : ! **************************************************************************************************
1067 : !> \brief Destroy a caller-owned pair-major plan, including plans for short final batches.
1068 : ! **************************************************************************************************
1069 2284 : SUBROUTINE fftw3_pair_major_plan_destroy(cached_plan)
1070 : TYPE(fftw3_pair_major_plan_type), INTENT(INOUT) :: cached_plan
1071 :
1072 : #if defined(__FFTW3)
1073 2284 : IF (C_ASSOCIATED(cached_plan%handle)) CALL fftw_destroy_plan(cached_plan%handle)
1074 : #endif
1075 2284 : cached_plan%handle = C_NULL_PTR
1076 2284 : cached_plan%sign = 0
1077 9136 : cached_plan%n = 0
1078 2284 : cached_plan%nbatch = 0
1079 2284 : cached_plan%nthreads = 0
1080 2284 : END SUBROUTINE fftw3_pair_major_plan_destroy
1081 :
1082 : ! **************************************************************************************************
1083 : !> \brief Complete all three FFT axes on pair-major (P,Z,Y,X) data using FFTW3.
1084 : !> Input and output use the same layout. n is in z,y,x storage order.
1085 : !> FFTW_ESTIMATE does not overwrite the input during planning.
1086 : !> Cached plans may execute with new FFTW-aligned arrays of the same layout.
1087 : ! **************************************************************************************************
1088 2639 : SUBROUTINE fftw3_pair_major_3d(sign, n, nbatch, zin, zout, stat, cached_plan)
1089 : INTEGER, INTENT(IN) :: sign, n(3), nbatch
1090 : COMPLEX(KIND=dp), DIMENSION(*), INTENT(INOUT) :: zin, zout
1091 : INTEGER, INTENT(OUT) :: stat
1092 : TYPE(fftw3_pair_major_plan_type), INTENT(INOUT), &
1093 : OPTIONAL :: cached_plan
1094 : #if defined(__FFTW3)
1095 : TYPE(C_PTR) :: plan
1096 : INTEGER :: fft_sign, nthreads, shape(3)
1097 :
1098 : ! The FFTW C binding expects x,y,z dimensions for z-fastest storage.
1099 10556 : shape = n([3, 2, 1])
1100 2639 : fft_sign = FFTW_BACKWARD
1101 2639 : IF (sign == 1) fft_sign = FFTW_FORWARD
1102 :
1103 2639 : nthreads = 1
1104 2639 : !$ nthreads = MIN(omp_get_max_threads(), nbatch)
1105 2639 : stat = 1
1106 2639 : IF (PRESENT(cached_plan)) THEN
1107 2639 : IF (C_ASSOCIATED(cached_plan%handle)) THEN
1108 : IF (cached_plan%sign /= sign .OR. ANY(cached_plan%n /= n) .OR. &
1109 6172 : cached_plan%nbatch /= nbatch .OR. cached_plan%nthreads /= nthreads) THEN
1110 28 : CALL fftw3_pair_major_plan_destroy(cached_plan)
1111 : END IF
1112 : END IF
1113 2639 : IF (C_ASSOCIATED(cached_plan%handle)) THEN
1114 1515 : CALL fftw_execute_dft(cached_plan%handle, zin, zout)
1115 1515 : stat = 0
1116 1515 : RETURN
1117 : END IF
1118 : END IF
1119 :
1120 1124 : CALL fftw_plan_with_nthreads(nthreads)
1121 : plan = fftw_plan_many_dft(3, shape, nbatch, zin, shape, nbatch, 1, &
1122 1124 : zout, shape, nbatch, 1, fft_sign, FFTW_ESTIMATE)
1123 1124 : CALL fftw_plan_with_nthreads(1)
1124 1124 : IF (.NOT. C_ASSOCIATED(plan)) RETURN
1125 1124 : IF (PRESENT(cached_plan)) THEN
1126 1124 : cached_plan%handle = plan
1127 1124 : cached_plan%sign = sign
1128 4496 : cached_plan%n = n
1129 1124 : cached_plan%nbatch = nbatch
1130 1124 : cached_plan%nthreads = nthreads
1131 : END IF
1132 1124 : CALL fftw_execute_dft(plan, zin, zout)
1133 1124 : IF (.NOT. PRESENT(cached_plan)) CALL fftw_destroy_plan(plan)
1134 1124 : stat = 0
1135 : #else
1136 : MARK_USED(sign)
1137 : MARK_USED(n)
1138 : MARK_USED(nbatch)
1139 : IF (.FALSE.) THEN; DO; IF (ABS(zin(1)) > ABS(zout(1))) EXIT; END DO; END IF
1140 : IF (PRESENT(cached_plan)) CALL fftw3_pair_major_plan_destroy(cached_plan)
1141 : stat = 1
1142 : #endif
1143 : END SUBROUTINE fftw3_pair_major_3d
1144 :
1145 : ! **************************************************************************************************
1146 : !> \brief ...
1147 : !> \param plan ...
1148 : ! **************************************************************************************************
1149 550830 : SUBROUTINE fftw3_destroy_plan(plan)
1150 :
1151 : TYPE(fft_plan_type), INTENT(INOUT) :: plan
1152 :
1153 : #if defined(__FFTW3)
1154 550830 : !$ IF (plan%need_alt_plan) THEN
1155 0 : !$ CALL fftw_destroy_plan(plan%alt_fftw_plan)
1156 : !$ END IF
1157 :
1158 550830 : IF (.NOT. plan%separated_plans) THEN
1159 550830 : CALL fftw_destroy_plan(plan%fftw_plan)
1160 : ELSE
1161 : ! If it is a separated plan then we have to destroy
1162 : ! each dim plan individually
1163 0 : CALL fftw_destroy_plan(plan%fftw_plan_nx)
1164 0 : CALL fftw_destroy_plan(plan%fftw_plan_ny)
1165 0 : CALL fftw_destroy_plan(plan%fftw_plan_nz)
1166 0 : CALL fftw_destroy_plan(plan%fftw_plan_nx_r)
1167 0 : CALL fftw_destroy_plan(plan%fftw_plan_ny_r)
1168 0 : CALL fftw_destroy_plan(plan%fftw_plan_nz_r)
1169 : END IF
1170 :
1171 : #else
1172 : MARK_USED(plan)
1173 : #endif
1174 :
1175 550830 : END SUBROUTINE fftw3_destroy_plan
1176 :
1177 : ! **************************************************************************************************
1178 : !> \brief ...
1179 : !> \param plan ...
1180 : !> \param zin ...
1181 : !> \param zout ...
1182 : !> \param scale ...
1183 : !> \param stat ...
1184 : ! **************************************************************************************************
1185 21854064 : SUBROUTINE fftw31dm(plan, zin, zout, scale, stat)
1186 : TYPE(fft_plan_type), INTENT(IN) :: plan
1187 : COMPLEX(KIND=dp), DIMENSION(*), INTENT(INOUT), &
1188 : TARGET :: zin
1189 : COMPLEX(KIND=dp), DIMENSION(*), INTENT(INOUT), &
1190 : TARGET :: zout
1191 : REAL(KIND=dp), INTENT(IN) :: scale
1192 : INTEGER, INTENT(OUT) :: stat
1193 :
1194 : INTEGER :: in_offset, my_id, num_rows, out_offset, &
1195 : scal_offset
1196 : TYPE(C_PTR) :: fftw_plan
1197 : !------------------------------------------------------------------------------
1198 :
1199 21854064 : my_id = 0
1200 21854064 : num_rows = plan%m
1201 :
1202 : #if defined(__FFTW3)
1203 21854064 : IF (plan%m <= 1) THEN
1204 140659 : stat = 1
1205 140659 : CALL fftw_execute_dft(plan%fftw_plan, zin(1), zout(1))
1206 140659 : IF (scale /= 1.0_dp) CALL zdscal(plan%n*plan%m, scale, zout, 1)
1207 140659 : RETURN
1208 : END IF
1209 : #endif
1210 :
1211 : !$OMP PARALLEL DEFAULT(NONE), &
1212 : !$OMP PRIVATE(my_id,num_rows,in_offset,out_offset,scal_offset,fftw_plan), &
1213 : !$OMP SHARED(zin,zout), &
1214 21713405 : !$OMP SHARED(plan,scale,stat)
1215 : !$ my_id = omp_get_thread_num()
1216 :
1217 : !$ if (my_id < plan%num_threads_needed) then
1218 :
1219 : fftw_plan = plan%fftw_plan
1220 :
1221 : in_offset = 1
1222 : out_offset = 1
1223 : scal_offset = 1
1224 :
1225 : !$ in_offset = 1 + plan%num_rows*my_id*plan%n
1226 : !$ out_offset = 1 + plan%num_rows*my_id*plan%n
1227 : !$ IF (plan%trans_in) THEN
1228 : !$ in_offset = 1 + plan%num_rows*my_id
1229 : !$ END IF
1230 : !$ IF (plan%trans_out) THEN
1231 : !$ out_offset = 1 + plan%num_rows*my_id
1232 : !$ END IF
1233 : !$ scal_offset = 1 + plan%n*plan%num_rows*my_id
1234 : !$ IF (plan%need_alt_plan .AND. my_id == plan%num_threads_needed - 1) THEN
1235 : !$ num_rows = plan%alt_num_rows
1236 : !$ fftw_plan = plan%alt_fftw_plan
1237 : !$ ELSE
1238 : !$ num_rows = plan%num_rows
1239 : !$ END IF
1240 :
1241 : #if defined(__FFTW3)
1242 : !$OMP MASTER
1243 : stat = 1
1244 : !$OMP END MASTER
1245 : CALL fftw_execute_dft(fftw_plan, zin(in_offset), zout(out_offset))
1246 : !$ end if
1247 : ! all threads need to meet at this barrier
1248 : !$OMP BARRIER
1249 : !$ if (my_id < plan%num_threads_needed) then
1250 : IF (scale /= 1.0_dp) CALL zdscal(plan%n*num_rows, scale, zout(scal_offset:scal_offset), 1)
1251 : !$ end if
1252 :
1253 : #else
1254 : MARK_USED(plan)
1255 : MARK_USED(scale)
1256 : !MARK_USED does not work with assumed size arguments
1257 : IF (.FALSE.) THEN; DO; IF (ABS(zin(1)) > ABS(zout(1))) EXIT; END DO; END IF
1258 : stat = 0
1259 :
1260 : !$ else
1261 : !$ end if
1262 :
1263 : #endif
1264 :
1265 : !$OMP END PARALLEL
1266 :
1267 : END SUBROUTINE fftw31dm
1268 :
1269 0 : END MODULE fftw3_lib
|