LCOV - code coverage report
Current view: top level - src/fft - fft_tools.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 50.0 % 1525 762
Test Date: 2026-11-24 01:22:08 Functions: 61.9 % 42 26

            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              : !> \par History
      10              : !>      JGH (30-Nov-2000): ESSL FFT Library added
      11              : !>      JGH (05-Jan-2001): Added SGI library FFT
      12              : !>      JGH (14-Jan-2001): Added parallel 3d FFT
      13              : !>      JGH (10-Feb-2006): New interface type
      14              : !>      JGH (31-Mar-2008): Remove local allocates and reshapes (performance)
      15              : !>                         Possible problems can be related with setting arrays
      16              : !>                         not to zero
      17              : !>                         Some interfaces could be further simplified by avoiding
      18              : !>                         an initial copy. However, this assumes contiguous arrays
      19              : !>      IAB (15-Oct-2008): Moved mp_cart_sub calls out of cube_tranpose_* and into
      20              : !>                         fft_scratch type, reducing number of calls dramatically
      21              : !>      IAB (05-Dec-2008): Moved all other non-essential MPI calls into scratch type
      22              : !>      IAB (09-Jan-2009): Added fft_plan_type to store FFT data, including cached FFTW plans
      23              : !>      IAB (13-Feb-2009): Extended plan caching to serial 3D FFT (fft3d_s)
      24              : !>      IAB (09-Oct-2009): Added OpenMP directives to parallel 3D FFT
      25              : !>                         (c) The Numerical Algorithms Group (NAG) Ltd, 2008-2009 on behalf of the HECToR project
      26              : !>      HFP (17-Oct-2024): Thread-safety insurance (CPASSERT), and OMP ATOMIC (tick_fft_pool)
      27              : !> \author JGH
      28              : ! **************************************************************************************************
      29              : MODULE fft_tools
      30              :    USE ISO_C_BINDING,                   ONLY: C_F_POINTER,&
      31              :                                               C_LOC,&
      32              :                                               C_PTR,&
      33              :                                               C_SIZE_T
      34              :    USE cp_log_handling,                 ONLY: cp_logger_get_default_io_unit
      35              :    USE fft_api,                         ONLY: &
      36              :         fft_1d_local, fft_2d_local, fft_3d_local, fft_3d_local_inplace, fft_allocate, &
      37              :         fft_deallocate, fft_library_finalize, fft_library_init, fft_register_1d_local, &
      38              :         fft_register_2d_local, fft_register_3d_local, fft_register_3d_local_inplace
      39              :    USE kinds,                           ONLY: dp,&
      40              :                                               dp_size,&
      41              :                                               sp
      42              :    USE mathconstants,                   ONLY: z_zero
      43              :    USE message_passing,                 ONLY: mp_cart_type,&
      44              :                                               mp_comm_null,&
      45              :                                               mp_comm_type,&
      46              :                                               mp_request_type,&
      47              :                                               mp_waitall
      48              :    USE offload_api,                     ONLY: offload_free_pinned_mem,&
      49              :                                               offload_malloc_pinned_mem
      50              : 
      51              : !$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num, omp_in_parallel
      52              : 
      53              : #include "../base/base_uses.f90"
      54              : 
      55              :    IMPLICIT NONE
      56              : 
      57              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'fft_tools'
      58              : 
      59              :    TYPE fft_plan_type
      60              :       LOGICAL                             :: fsign = .TRUE.
      61              :       LOGICAL                             :: trans_in = .FALSE., trans_out = .FALSE., fft_in_place = .FALSE., &
      62              :                                              valid = .FALSE., separated_plans = .FALSE.
      63              :       INTEGER                             :: n = -1, m = -1, ldx_in = -1, ldy_in = -1, ldx_out = -1, ldy_out = -1
      64              :       INTEGER, DIMENSION(3)               :: n_3d = -1
      65              :    END TYPE fft_plan_type
      66              : 
      67              :    ! Types for the pool of scratch data needed in FFT routines
      68              :    ! keep the subroutine "is_equal" up-to-date
      69              :    ! needs a default initialization
      70              :    TYPE fft_scratch_sizes
      71              :       INTEGER                              :: nx = 0, ny = 0, nz = 0
      72              :       INTEGER                              :: lmax = 0, mmax = 0, nmax = 0
      73              :       INTEGER                              :: mx1 = 0, mx2 = 0, mx3 = 0
      74              :       INTEGER                              :: my1 = 0, my2 = 0, my3 = 0
      75              :       INTEGER                              :: mz1 = 0, mz2 = 0, mz3 = 0
      76              :       INTEGER                              :: mcz1 = 0, mcz2 = 0, mcy3 = 0, mcx2 = 0
      77              :       INTEGER                              :: lg = 0, mg = 0
      78              :       INTEGER                              :: nbx = 0, nbz = 0
      79              :       INTEGER                              :: nmray = 0, nyzray = 0
      80              :       TYPE(mp_cart_type)                   :: rs_group = mp_cart_type()
      81              :       INTEGER, DIMENSION(2)                :: g_pos = 0, r_pos = 0, r_dim = 0
      82              :       INTEGER                              :: numtask = 0
      83              :    END TYPE fft_scratch_sizes
      84              : 
      85              :    TYPE fft_scratch_type
      86              :       INTEGER                              :: fft_scratch_id = 0
      87              :       INTEGER                              :: tf_type = -1
      88              :       LOGICAL                              :: in_use = .TRUE.
      89              :       TYPE(mp_comm_type)                   :: group = mp_comm_type()
      90              :       INTEGER, DIMENSION(3)                :: nfft = -1
      91              :       ! to be used in cube_transpose_* routines
      92              :       TYPE(mp_cart_type), DIMENSION(2)     :: cart_sub_comm = mp_cart_type()
      93              :       INTEGER, DIMENSION(2)                :: dim = -1, pos = -1
      94              :       ! to be used in fft3d_s
      95              :       COMPLEX(KIND=dp), DIMENSION(:, :, :), POINTER, CONTIGUOUS &
      96              :          :: ziptr => NULL(), zoptr => NULL()
      97              :       ! to be used in fft3d_ps : block distribution
      98              :       COMPLEX(KIND=dp), DIMENSION(:, :), CONTIGUOUS, POINTER &
      99              :          :: p1buf => NULL(), p2buf => NULL(), p3buf => NULL(), p4buf => NULL(), &
     100              :             p5buf => NULL(), p6buf => NULL(), p7buf => NULL()
     101              :       ! to be used in fft3d_ps : plane distribution
     102              :       COMPLEX(KIND=dp), DIMENSION(:, :), POINTER, CONTIGUOUS &
     103              :          :: r1buf => NULL(), r2buf => NULL()
     104              :       COMPLEX(KIND=dp), DIMENSION(:, :, :), POINTER, CONTIGUOUS &
     105              :          :: tbuf => NULL()
     106              :       ! to be used in fft3d_pb
     107              :       COMPLEX(KIND=dp), DIMENSION(:, :), POINTER, CONTIGUOUS &
     108              :          :: a1buf => NULL(), a2buf => NULL(), a3buf => NULL(), &
     109              :             a4buf => NULL(), a5buf => NULL(), a6buf => NULL()
     110              :       ! to be used in communication routines
     111              :       INTEGER, DIMENSION(:), CONTIGUOUS, POINTER    :: scount => NULL(), rcount => NULL(), sdispl => NULL(), rdispl => NULL()
     112              :       INTEGER, DIMENSION(:, :), CONTIGUOUS, POINTER :: pgcube => NULL()
     113              :       INTEGER, DIMENSION(:), CONTIGUOUS, POINTER    :: xzcount => NULL(), yzcount => NULL(), xzdispl => NULL(), yzdispl => NULL()
     114              :       INTEGER                                       :: in = 0, mip = -1
     115              :       REAL(KIND=dp)                                 :: rsratio = 1.0_dp
     116              :       COMPLEX(KIND=dp), DIMENSION(:), POINTER, CONTIGUOUS &
     117              :          :: xzbuf => NULL(), yzbuf => NULL()
     118              :       COMPLEX(KIND=sp), DIMENSION(:), POINTER, CONTIGUOUS &
     119              :          :: xzbuf_sgl => NULL(), yzbuf_sgl => NULL()
     120              :       COMPLEX(KIND=dp), DIMENSION(:, :), POINTER, CONTIGUOUS &
     121              :          :: rbuf1 => NULL(), rbuf2 => NULL(), rbuf3 => NULL(), rbuf4 => NULL(), &
     122              :             rbuf5 => NULL(), rbuf6 => NULL(), rr => NULL()
     123              :       COMPLEX(KIND=sp), DIMENSION(:, :), POINTER, CONTIGUOUS &
     124              :          :: ss => NULL(), tt => NULL()
     125              :       INTEGER, DIMENSION(:, :), POINTER, CONTIGUOUS :: pgrid => NULL()
     126              :       INTEGER, DIMENSION(:), POINTER, CONTIGUOUS    :: xcor => NULL(), zcor => NULL(), pzcoord => NULL()
     127              :       TYPE(fft_scratch_sizes)                       :: sizes = fft_scratch_sizes()
     128              :       TYPE(fft_plan_type), DIMENSION(6)             :: fft_plan = fft_plan_type()
     129              :       INTEGER                                       :: last_tick = -1
     130              :    END TYPE fft_scratch_type
     131              : 
     132              :    TYPE fft_scratch_pool_type
     133              :       TYPE(fft_scratch_type), POINTER       :: fft_scratch => NULL()
     134              :       TYPE(fft_scratch_pool_type), POINTER  :: fft_scratch_next => NULL()
     135              :    END TYPE fft_scratch_pool_type
     136              : 
     137              :    INTEGER, SAVE                              :: init_fft_pool = 0
     138              :    ! the clock for fft pool. Allows to identify the least recently used scratch
     139              :    INTEGER, SAVE                              :: tick_fft_pool = 0
     140              :    ! limit the number of scratch pools to fft_pool_scratch_limit.
     141              :    INTEGER, SAVE                              :: fft_pool_scratch_limit = 15
     142              :    TYPE(fft_scratch_pool_type), POINTER, SAVE :: fft_scratch_first
     143              :    ! END of types for the pool of scratch data needed in FFT routines
     144              : 
     145              :    PRIVATE
     146              :    PUBLIC :: init_fft, fft3d, finalize_fft, fft_1d, fft_3d, fft_create_plan_1d, fft_create_plan_3d
     147              :    PUBLIC :: fft_allocate, fft_deallocate
     148              :    PUBLIC :: init_fft_scratch_pool
     149              :    PUBLIC :: fft_radix_operations
     150              :    PUBLIC :: FWFFT, BWFFT
     151              :    PUBLIC :: FFT_RADIX_CLOSEST, FFT_RADIX_NEXT
     152              :    PUBLIC :: FFT_RADIX_NEXT_ODD
     153              :    PUBLIC :: fft_plan_type, fft_destroy_plan
     154              : 
     155              :    LOGICAL, PARAMETER :: FWFFT = .TRUE., BWFFT = .FALSE.
     156              :    INTEGER, PARAMETER :: FFT_RADIX_CLOSEST = 493, FFT_RADIX_NEXT = 494
     157              :    INTEGER, PARAMETER :: FFT_RADIX_ALLOWED = 495, FFT_RADIX_DISALLOWED = 496
     158              :    INTEGER, PARAMETER :: FFT_RADIX_NEXT_ODD = 497
     159              : 
     160              :    REAL(KIND=dp), PARAMETER :: ratio_sparse_alltoall = 0.5_dp
     161              : 
     162              :    ! these saved variables are FFT globals
     163              :    LOGICAL, SAVE :: alltoall_sgl = .FALSE.
     164              : 
     165              :    ! these are only needed for fft_gpu (-D__OFFLOAD)
     166              :    PUBLIC :: get_fft_scratch, release_fft_scratch
     167              :    PUBLIC :: cube_transpose_1, cube_transpose_2
     168              :    PUBLIC :: yz_to_x, x_to_yz, xz_to_yz, yz_to_xz
     169              :    PUBLIC :: fft_scratch_sizes, fft_scratch_type
     170              : 
     171              :    INTERFACE fft3d
     172              :       MODULE PROCEDURE fft3d_s, fft3d_ps, fft3d_pb
     173              :    END INTERFACE
     174              : 
     175              : ! **************************************************************************************************
     176              : 
     177              : CONTAINS
     178              : 
     179              : ! **************************************************************************************************
     180              : !> \brief ...
     181              : !> \param fftlib ...
     182              : !> \param alltoall ...
     183              : !> \param pool_limit ...
     184              : !> \param wisdom_file ...
     185              : !> \param plan_style ...
     186              : !> \param status ...
     187              : !> \param use_guru_interface ...
     188              : !> \author JGH
     189              : ! **************************************************************************************************
     190        23874 :    SUBROUTINE init_fft(fftlib, alltoall, pool_limit, wisdom_file, &
     191              :                        plan_style, status, use_guru_interface)
     192              : 
     193              :       INTEGER, INTENT(IN)                                :: fftlib
     194              :       LOGICAL, INTENT(IN)                                :: alltoall
     195              :       INTEGER, INTENT(IN)                                :: pool_limit
     196              :       CHARACTER(LEN=*), INTENT(IN)                       :: wisdom_file
     197              :       INTEGER, INTENT(IN)                                :: plan_style
     198              :       INTEGER, INTENT(OUT)                               :: status
     199              :       LOGICAL, INTENT(IN), OPTIONAL                      :: use_guru_interface
     200              : 
     201              :       LOGICAL                                            :: use_guru
     202              : 
     203        11937 :       use_guru = .TRUE.
     204        11937 :       IF (PRESENT(use_guru_interface)) use_guru = use_guru_interface
     205              : 
     206        11937 :       alltoall_sgl = alltoall
     207        11937 :       fft_pool_scratch_limit = pool_limit
     208              : 
     209        11937 :       CALL release_fft_scratch_pool()
     210              : 
     211        11937 :       CALL fft_library_init(fftlib, plan_style, use_guru, wisdom_file, status)
     212              : 
     213              :       ! setup the FFT scratch pool, if one is associated, clear first
     214        11937 :       CALL init_fft_scratch_pool()
     215              : 
     216        11937 :    END SUBROUTINE init_fft
     217              : 
     218              : ! **************************************************************************************************
     219              : !> \brief does whatever is needed to finalize the current fft setup
     220              : !> \param wisdom_file ...
     221              : !> \par History
     222              : !>      10.2007 created [Joost VandeVondele]
     223              : ! **************************************************************************************************
     224        11723 :    SUBROUTINE finalize_fft(wisdom_file)
     225              :       CHARACTER(LEN=*), INTENT(IN)                       :: wisdom_file
     226              : 
     227              : ! release the FFT scratch pool
     228              : 
     229        11723 :       CALL release_fft_scratch_pool()
     230              : 
     231              :       ! finalize fft libs
     232              : 
     233        11723 :       CALL fft_library_finalize(wisdom_file)
     234              : 
     235        11723 :    END SUBROUTINE finalize_fft
     236              : 
     237              : ! **************************************************************************************************
     238              : !> \brief ...
     239              : !> \param DATA ...
     240              : !> \param max_length ...
     241              : !> \par History
     242              : !>      JGH 23-Jan-2006 : initial version
     243              : !>      Adapted for new interface
     244              : !>      IAB 09-Jan-2009 : Modified to cache plans in fft_plan_type
     245              : !>                        (c) The Numerical Algorithms Group (NAG) Ltd, 2009 on behalf of the HECToR project
     246              : !>      IAB 09-Oct-2009 : Added OpenMP directives to 1D FFT, and planning routines
     247              : !>                        (c) The Numerical Algorithms Group (NAG) Ltd, 2009 on behalf of the HECToR project
     248              : !>      IAB 11-Sep-2012 : OpenMP parallel 3D FFT (Ruyman Reyes, PRACE)
     249              : !> \author JGH
     250              : ! **************************************************************************************************
     251            0 :    SUBROUTINE fft_get_lengths(DATA, max_length)
     252              : 
     253              :       INTEGER, DIMENSION(:), INTENT(INOUT)               :: DATA
     254              :       INTEGER, INTENT(INOUT)                             :: max_length
     255              : 
     256              :       INTEGER                                            :: h, i, j, k, m, maxn, maxn_elevens, &
     257              :                                                             maxn_fives, maxn_sevens, &
     258              :                                                             maxn_thirteens, maxn_threes, &
     259              :                                                             maxn_twos, ndata, nmax, number
     260            0 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: dlocal, idx
     261              : 
     262              : !------------------------------------------------------------------------------
     263              : ! compute ndata
     264              : !! FFTW can do arbitrary(?) lengths, maybe you want to limit them to some
     265              : !!    powers of small prime numbers though...
     266              : 
     267            0 :       maxn_twos = 15
     268            0 :       maxn_threes = 3
     269            0 :       maxn_fives = 2
     270            0 :       maxn_sevens = 1
     271            0 :       maxn_elevens = 1
     272            0 :       maxn_thirteens = 0
     273            0 :       maxn = 37748736
     274              : 
     275            0 :       ndata = 0
     276            0 :       DO h = 0, maxn_twos
     277            0 :          nmax = HUGE(0)/2**h
     278            0 :          DO i = 0, maxn_threes
     279            0 :             DO j = 0, maxn_fives
     280            0 :                DO k = 0, maxn_sevens
     281            0 :                   DO m = 0, maxn_elevens
     282            0 :                      number = (3**i)*(5**j)*(7**k)*(11**m)
     283              : 
     284            0 :                      IF (number > nmax) CYCLE
     285              : 
     286            0 :                      number = number*2**h
     287            0 :                      IF (number >= maxn) CYCLE
     288              : 
     289            0 :                      ndata = ndata + 1
     290              :                   END DO
     291              :                END DO
     292              :             END DO
     293              :          END DO
     294              :       END DO
     295              : 
     296            0 :       ALLOCATE (dlocal(ndata), idx(ndata))
     297              : 
     298            0 :       ndata = 0
     299            0 :       dlocal(:) = 0
     300            0 :       DO h = 0, maxn_twos
     301            0 :          nmax = HUGE(0)/2**h
     302            0 :          DO i = 0, maxn_threes
     303            0 :             DO j = 0, maxn_fives
     304            0 :                DO k = 0, maxn_sevens
     305            0 :                   DO m = 0, maxn_elevens
     306            0 :                      number = (3**i)*(5**j)*(7**k)*(11**m)
     307              : 
     308            0 :                      IF (number > nmax) CYCLE
     309              : 
     310            0 :                      number = number*2**h
     311            0 :                      IF (number >= maxn) CYCLE
     312              : 
     313            0 :                      ndata = ndata + 1
     314            0 :                      dlocal(ndata) = number
     315              :                   END DO
     316              :                END DO
     317              :             END DO
     318              :          END DO
     319              :       END DO
     320              : 
     321            0 :       CALL sortint(dlocal, ndata, idx)
     322            0 :       ndata = MIN(ndata, max_length)
     323            0 :       DATA(1:ndata) = dlocal(1:ndata)
     324            0 :       max_length = ndata
     325              : 
     326            0 :       DEALLOCATE (dlocal, idx)
     327              : 
     328            0 :    END SUBROUTINE fft_get_lengths
     329              : 
     330              : ! **************************************************************************************************
     331              : !> \brief ...
     332              : !> \param iarr ...
     333              : !> \param n ...
     334              : !> \param index ...
     335              : ! **************************************************************************************************
     336            0 :    SUBROUTINE sortint(iarr, n, index)
     337              : 
     338              :       INTEGER, INTENT(IN)                                :: n
     339              :       INTEGER, INTENT(INOUT)                             :: iarr(1:n)
     340              :       INTEGER, INTENT(OUT)                               :: INDEX(1:n)
     341              : 
     342              :       INTEGER, PARAMETER                                 :: m = 7, nstack = 50
     343              : 
     344              :       INTEGER                                            :: a, i, ib, ir, istack(1:nstack), itemp, &
     345              :                                                             j, jstack, k, l, temp
     346              : 
     347              : !------------------------------------------------------------------------------
     348              : 
     349            0 :       DO i = 1, n
     350            0 :          INDEX(i) = i
     351              :       END DO
     352              :       jstack = 0
     353              :       l = 1
     354              :       ir = n
     355              :       DO WHILE (.TRUE.)
     356            0 :       IF (ir - l < m) THEN
     357            0 :          DO j = l + 1, ir
     358            0 :             a = iarr(j)
     359            0 :             ib = INDEX(j)
     360            0 :             DO i = j - 1, 0, -1
     361            0 :                IF (i == 0) EXIT
     362            0 :                IF (iarr(i) <= a) EXIT
     363            0 :                iarr(i + 1) = iarr(i)
     364            0 :                INDEX(i + 1) = INDEX(i)
     365              :             END DO
     366            0 :             iarr(i + 1) = a
     367            0 :             INDEX(i + 1) = ib
     368              :          END DO
     369            0 :          IF (jstack == 0) RETURN
     370            0 :          ir = istack(jstack)
     371            0 :          l = istack(jstack - 1)
     372            0 :          jstack = jstack - 2
     373              :       ELSE
     374            0 :          k = (l + ir)/2
     375            0 :          temp = iarr(k)
     376            0 :          iarr(k) = iarr(l + 1)
     377            0 :          iarr(l + 1) = temp
     378            0 :          itemp = INDEX(k)
     379            0 :          INDEX(k) = INDEX(l + 1)
     380            0 :          INDEX(l + 1) = itemp
     381            0 :          IF (iarr(l + 1) > iarr(ir)) THEN
     382            0 :             temp = iarr(l + 1)
     383            0 :             iarr(l + 1) = iarr(ir)
     384            0 :             iarr(ir) = temp
     385            0 :             itemp = INDEX(l + 1)
     386            0 :             INDEX(l + 1) = INDEX(ir)
     387            0 :             INDEX(ir) = itemp
     388              :          END IF
     389            0 :          IF (iarr(l) > iarr(ir)) THEN
     390            0 :             temp = iarr(l)
     391            0 :             iarr(l) = iarr(ir)
     392            0 :             iarr(ir) = temp
     393            0 :             itemp = INDEX(l)
     394            0 :             INDEX(l) = INDEX(ir)
     395            0 :             INDEX(ir) = itemp
     396              :          END IF
     397            0 :          IF (iarr(l + 1) > iarr(l)) THEN
     398            0 :             temp = iarr(l + 1)
     399            0 :             iarr(l + 1) = iarr(l)
     400            0 :             iarr(l) = temp
     401            0 :             itemp = INDEX(l + 1)
     402            0 :             INDEX(l + 1) = INDEX(l)
     403            0 :             INDEX(l) = itemp
     404              :          END IF
     405            0 :          i = l + 1
     406            0 :          j = ir
     407            0 :          a = iarr(l)
     408            0 :          ib = INDEX(l)
     409            0 :          DO WHILE (.TRUE.)
     410            0 :             i = i + 1
     411            0 :             DO WHILE (iarr(i) < a)
     412            0 :                i = i + 1
     413              :             END DO
     414            0 :             j = j - 1
     415            0 :             DO WHILE (iarr(j) > a)
     416            0 :                j = j - 1
     417              :             END DO
     418            0 :             IF (j < i) EXIT
     419            0 :             temp = iarr(i)
     420            0 :             iarr(i) = iarr(j)
     421            0 :             iarr(j) = temp
     422            0 :             itemp = INDEX(i)
     423            0 :             INDEX(i) = INDEX(j)
     424            0 :             INDEX(j) = itemp
     425              :          END DO
     426            0 :          iarr(l) = iarr(j)
     427            0 :          iarr(j) = a
     428            0 :          INDEX(l) = INDEX(j)
     429            0 :          INDEX(j) = ib
     430            0 :          jstack = jstack + 2
     431            0 :          IF (jstack > nstack) CPABORT("Nstack too small in sortint")
     432            0 :          IF (ir - i + 1 >= j - l) THEN
     433            0 :             istack(jstack) = ir
     434            0 :             istack(jstack - 1) = i
     435            0 :             ir = j - 1
     436              :          ELSE
     437            0 :             istack(jstack) = j - 1
     438            0 :             istack(jstack - 1) = l
     439            0 :             l = i
     440              :          END IF
     441              :       END IF
     442              : 
     443              :       END DO
     444              : 
     445              :    END SUBROUTINE sortint
     446              : 
     447              : ! **************************************************************************************************
     448              : !> \brief Determine the allowed lengths of FFT's   '''
     449              : !> \param radix_in ...
     450              : !> \param radix_out ...
     451              : !> \param operation ...
     452              : !> \par History
     453              : !>      new library structure (JGH)
     454              : !> \author Ari Seitsonen
     455              : ! **************************************************************************************************
     456       275304 :    SUBROUTINE fft_radix_operations(radix_in, radix_out, operation)
     457              : 
     458              :       INTEGER, INTENT(IN)                                :: radix_in
     459              :       INTEGER, INTENT(OUT)                               :: radix_out
     460              :       INTEGER, INTENT(IN)                                :: operation
     461              : 
     462              :       INTEGER, PARAMETER                                 :: number_of_small_radices = 81
     463              :       INTEGER, DIMENSION(number_of_small_radices), PARAMETER :: small_radices = [2, 4, 6, 8, 9, 12,&
     464              :          15, 16, 18, 20, 24, 25, 27, 30, 32, 36, 40, 45, 48, 54, 60, 64, 72, 75, 80, 81, 90, 96, &
     465              :          100, 108, 120, 125, 128, 135, 144, 150, 160, 162, 180, 192, 200, 216, 225, 240, 243, 256, &
     466              :          270, 288, 300, 320, 324, 360, 375, 384, 400, 405, 432, 450, 480, 486, 500, 512, 540, 576, &
     467              :          600, 625, 640, 648, 675, 720, 729, 750, 768, 800, 810, 864, 900, 960, 972, 1000, 1024]
     468              : 
     469              :       INTEGER                                            :: i, iloc, ldata, trial_number
     470              :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: DATA
     471              : 
     472       275304 :       ldata = 1024
     473       275304 :       ALLOCATE (DATA(ldata))
     474    282186600 :       DATA = -1
     475              : 
     476     22574928 :       DATA(1:number_of_small_radices) = small_radices(1:number_of_small_radices)
     477              : 
     478              :       ! First run: try smaller radices
     479              :       ! Second run: Fetch extended list of radices and try these
     480       275304 :       DO trial_number = 1, 2
     481       275304 :          IF (trial_number == 2) CALL fft_get_lengths(DATA, ldata)
     482       275304 :          iloc = 0
     483      3609263 :          DO i = 1, ldata
     484      3609263 :             IF (DATA(i) == radix_in) THEN
     485              :                iloc = i
     486              :                EXIT
     487              :             ELSE
     488      3460484 :                IF (OPERATION == FFT_RADIX_ALLOWED) THEN
     489              :                   CYCLE
     490      3460484 :                ELSE IF (DATA(i) > radix_in) THEN
     491              :                   iloc = i
     492              :                   EXIT
     493              :                END IF
     494              :             END IF
     495              :          END DO
     496              : 
     497       275304 :          IF (iloc == 0) THEN
     498            0 :             IF (trial_number == 1) THEN
     499              :                ! We haven't found any suitable size in the list of smaller radices
     500              :                ! because the requested radix is too large (large cell or large cutoff)
     501              :                ! So, try again with the larger radices
     502              :                CYCLE
     503              :             ELSE
     504            0 :                IF (OPERATION == FFT_RADIX_CLOSEST) THEN
     505            0 :                   radix_out = MAXVAL(DATA)
     506              :                ELSE
     507              :                   CALL cp_abort(__LOCATION__, &
     508            0 :                                 "No suitable FFT lengths found! Either enable FFTW or reduce the grid size!")
     509              :                END IF
     510            0 :                RETURN
     511              :             END IF
     512              : 
     513              :          END IF
     514              : 
     515       275304 :          IF (OPERATION == FFT_RADIX_ALLOWED) THEN
     516            0 :             IF (DATA(iloc) == radix_in) THEN
     517            0 :                radix_out = FFT_RADIX_ALLOWED
     518              :             ELSE
     519            0 :                radix_out = FFT_RADIX_DISALLOWED
     520              :             END IF
     521              : 
     522       275304 :          ELSE IF (OPERATION == FFT_RADIX_CLOSEST) THEN
     523          276 :             IF (DATA(iloc) == radix_in) THEN
     524          114 :                radix_out = DATA(iloc)
     525              :             ELSE
     526          162 :                IF (ABS(DATA(iloc - 1) - radix_in) <= &
     527              :                    ABS(DATA(iloc) - radix_in)) THEN
     528          156 :                   radix_out = DATA(iloc - 1)
     529              :                ELSE
     530            6 :                   radix_out = DATA(iloc)
     531              :                END IF
     532              :             END IF
     533              : 
     534       275028 :          ELSE IF (OPERATION == FFT_RADIX_NEXT) THEN
     535       273042 :             radix_out = DATA(iloc)
     536              : 
     537         1986 :          ELSE IF (OPERATION == FFT_RADIX_NEXT_ODD) THEN
     538         3434 :             DO i = iloc, ldata
     539         3434 :                IF (MOD(DATA(i), 2) == 1) THEN
     540         1986 :                   radix_out = DATA(i)
     541         1986 :                   EXIT
     542              :                END IF
     543              :             END DO
     544         1986 :             IF (MOD(radix_out, 2) == 0) THEN
     545              :                ! No larger odd radix found
     546            0 :                IF (trial_number == 1) THEN
     547              :                   CYCLE
     548              :                ELSE
     549            0 :                   radix_out = radix_in + 1 - MOD(radix_in, 2)
     550              :                END IF
     551              :             END IF
     552              : 
     553              :          END IF
     554            0 :          EXIT
     555              :       END DO
     556              : 
     557       275304 :       DEALLOCATE (DATA)
     558              : 
     559            0 :    END SUBROUTINE fft_radix_operations
     560              : 
     561              : ! **************************************************************************************************
     562              : !> \brief ...
     563              : !> \param plan ...
     564              : !> \param fsign ...
     565              : !> \param trans_in ...
     566              : !> \param trans_out ...
     567              : !> \param ldx_in ...
     568              : !> \param ldx_out ...
     569              : !> \param n ...
     570              : !> \param m ...
     571              : !> \param zin ...
     572              : !> \param zout ...
     573              : ! **************************************************************************************************
     574       343568 :    SUBROUTINE fft_create_plan_1d(plan, fsign, trans_in, trans_out, ldx_in, ldx_out, n, m, zin, zout)
     575              :       TYPE(fft_plan_type), INTENT(INOUT)                 :: plan
     576              :       LOGICAL, INTENT(IN)                                :: fsign, trans_in, trans_out
     577              :       INTEGER, INTENT(IN)                                :: ldx_in, ldx_out, n, m
     578              :       COMPLEX(KIND=dp), DIMENSION(*), INTENT(INOUT), &
     579              :          TARGET                                          :: zin, zout
     580              : 
     581       343568 :       plan%fsign = fsign
     582       343568 :       plan%trans_in = trans_in
     583       343568 :       plan%trans_out = trans_out
     584       343568 :       IF (plan%trans_in) THEN
     585       307596 :          plan%ldx_in = ldx_in
     586       307596 :          plan%ldy_in = n
     587              :       ELSE
     588        35972 :          plan%ldx_in = ldx_in
     589        35972 :          plan%ldy_in = m
     590              :       END IF
     591       343568 :       IF (plan%trans_out) THEN
     592       307596 :          plan%ldx_out = ldx_out
     593       307596 :          plan%ldy_out = n
     594              :       ELSE
     595        35972 :          plan%ldx_out = ldx_out
     596        35972 :          plan%ldy_out = m
     597              :       END IF
     598              : 
     599       343568 :       plan%n = n
     600       343568 :       plan%m = m
     601              : 
     602       343568 :       IF (n*m /= 0) THEN
     603       343568 :          CALL fft_register_1d_local(fsign, n, m, trans_in, trans_out, ldx_in, ldx_out, zin, zout)
     604       343568 :          plan%valid = .TRUE.
     605              :       ELSE
     606            0 :          plan%valid = .FALSE.
     607              :       END IF
     608              : 
     609       343568 :    END SUBROUTINE fft_create_plan_1d
     610              : 
     611              : ! **************************************************************************************************
     612              : !> \brief ...
     613              : !> \param plan ...
     614              : !> \param fsign ...
     615              : !> \param trans_in ...
     616              : !> \param trans_out ...
     617              : !> \param n ...
     618              : !> \param m ...
     619              : !> \param zin ...
     620              : !> \param zout ...
     621              : ! **************************************************************************************************
     622        71944 :    SUBROUTINE fft_create_plan_2d(plan, fsign, trans_in, trans_out, n, m, zin, zout)
     623              :       TYPE(fft_plan_type), INTENT(INOUT)                 :: plan
     624              :       LOGICAL, INTENT(IN)                                :: fsign, trans_in, trans_out
     625              :       INTEGER, INTENT(IN)                                :: n(2), m
     626              :       COMPLEX(KIND=dp), DIMENSION(*), INTENT(INOUT), &
     627              :          TARGET                                          :: zin, zout
     628              : 
     629        71944 :       plan%fsign = fsign
     630        71944 :       plan%trans_in = trans_in
     631        71944 :       plan%trans_out = trans_out
     632        71944 :       IF (plan%trans_in) THEN
     633        35972 :          plan%ldx_in = m
     634        35972 :          plan%ldy_in = n(1)*n(2)
     635              :       ELSE
     636        35972 :          plan%ldx_in = n(1)*n(2)
     637        35972 :          plan%ldy_in = m
     638              :       END IF
     639        71944 :       IF (plan%trans_out) THEN
     640        35972 :          plan%ldx_out = m
     641        35972 :          plan%ldy_out = n(1)*n(2)
     642              :       ELSE
     643        35972 :          plan%ldx_out = n(1)*n(2)
     644        35972 :          plan%ldy_out = m
     645              :       END IF
     646              : 
     647       215832 :       plan%n_3d(1:2) = n
     648        71944 :       plan%n_3d(3) = 0
     649        71944 :       plan%m = m
     650              : 
     651        71944 :       IF (n(1) > 0 .AND. n(2) > 0 .AND. m > 0) THEN
     652        71944 :          CALL fft_register_2d_local(fsign, n, m, trans_in, trans_out, zin, zout)
     653        71944 :          plan%valid = .TRUE.
     654              :       ELSE
     655            0 :          plan%valid = .FALSE.
     656              :       END IF
     657              : 
     658        71944 :    END SUBROUTINE fft_create_plan_2d
     659              : 
     660              : ! **************************************************************************************************
     661              : !> \brief ...
     662              : !> \param plan ...
     663              : !> \param fsign ...
     664              : !> \param n ...
     665              : !> \param zin ...
     666              : !> \param zout ...
     667              : ! **************************************************************************************************
     668        68080 :    SUBROUTINE fft_create_plan_3d(plan, fsign, n, zin, zout)
     669              : 
     670              :       TYPE(fft_plan_type), INTENT(INOUT)                 :: plan
     671              :       LOGICAL, INTENT(IN)                                :: fsign
     672              :       INTEGER, DIMENSION(3), INTENT(IN)                  :: n
     673              :       COMPLEX(KIND=dp), DIMENSION(PRODUCT(n)), &
     674              :          INTENT(INOUT), TARGET                           :: zin, zout
     675              : 
     676         8510 :       plan%fsign = fsign
     677         8510 :       plan%fft_in_place = .FALSE.
     678        34040 :       plan%n_3d = n
     679              : 
     680         8510 :       CALL fft_register_3d_local(fsign, n, 1, .FALSE., .FALSE., zin, zout)
     681         8510 :       plan%valid = .TRUE.
     682              : 
     683         8510 :    END SUBROUTINE fft_create_plan_3d
     684              : 
     685              : ! **************************************************************************************************
     686              : !> \brief ...
     687              : !> \param plan ...
     688              : !> \param fsign ...
     689              : !> \param n ...
     690              : !> \param zinout ...
     691              : ! **************************************************************************************************
     692        34040 :    SUBROUTINE fft_create_plan_3d_inplace(plan, fsign, n, zinout)
     693              : 
     694              :       TYPE(fft_plan_type), INTENT(INOUT)                 :: plan
     695              :       LOGICAL, INTENT(IN)                                :: fsign
     696              :       INTEGER, DIMENSION(3), INTENT(IN)                  :: n
     697              :       COMPLEX(KIND=dp), DIMENSION(PRODUCT(n)), &
     698              :          INTENT(INOUT), TARGET                           :: zinout
     699              : 
     700         8510 :       plan%fsign = fsign
     701         8510 :       plan%fft_in_place = .TRUE.
     702        34040 :       plan%n_3d = n
     703              : 
     704         8510 :       CALL fft_register_3d_local_inplace(fsign, n, 1, .FALSE., zinout)
     705         8510 :       plan%valid = .TRUE.
     706              : 
     707         8510 :    END SUBROUTINE fft_create_plan_3d_inplace
     708              : 
     709              : ! **************************************************************************************************
     710              : !> \brief ...
     711              : !> \param plan ...
     712              : !> \param scale ...
     713              : !> \param zin ...
     714              : !> \param zout ...
     715              : ! **************************************************************************************************
     716            4 :    SUBROUTINE fft_3d(plan, scale, zin, zout)
     717              :       TYPE(fft_plan_type), INTENT(IN)                    :: plan
     718              :       REAL(KIND=dp), INTENT(IN)                          :: scale
     719              :       COMPLEX(KIND=dp), DIMENSION(*), INTENT(INOUT), &
     720              :          TARGET                                          :: zin, zout
     721              : 
     722            4 :       CPASSERT(.NOT. plan%fft_in_place)
     723              : 
     724           16 :       IF (ALL(plan%n_3d > 0)) THEN
     725            4 :          CALL fft_3d_local(plan%fsign, plan%n_3d, 1, .FALSE., .FALSE., zin, zout)
     726              :       END IF
     727        55324 :       zout(1:PRODUCT(plan%n_3d)) = zout(1:PRODUCT(plan%n_3d))*scale
     728              : 
     729            4 :    END SUBROUTINE fft_3d
     730              : 
     731              : ! **************************************************************************************************
     732              : !> \brief ...
     733              : !> \param plan ...
     734              : !> \param scale ...
     735              : !> \param zinout ...
     736              : ! **************************************************************************************************
     737       658060 :    SUBROUTINE fft_3d_inplace(plan, scale, zinout)
     738              :       TYPE(fft_plan_type), INTENT(IN)                    :: plan
     739              :       REAL(KIND=dp), INTENT(IN)                          :: scale
     740              :       COMPLEX(KIND=dp), DIMENSION(*), INTENT(INOUT), &
     741              :          TARGET                                          :: zinout
     742              : 
     743       658060 :       CPASSERT(plan%fft_in_place)
     744              : 
     745      2632240 :       IF (ALL(plan%n_3d > 0)) THEN
     746       658060 :          CALL fft_3d_local_inplace(plan%fsign, plan%n_3d, 1, .FALSE., zinout)
     747              :       END IF
     748   6074600011 :       zinout(1:PRODUCT(plan%n_3d)) = zinout(1:PRODUCT(plan%n_3d))*scale
     749              : 
     750       658060 :    END SUBROUTINE fft_3d_inplace
     751              : 
     752              : ! **************************************************************************************************
     753              : !> \brief ...
     754              : !> \param plan ...
     755              : ! **************************************************************************************************
     756       667360 :    SUBROUTINE fft_destroy_plan(plan)
     757              :       TYPE(fft_plan_type), INTENT(INOUT)                 :: plan
     758              : 
     759       667360 :       plan%valid = .FALSE.
     760              : 
     761       667360 :    END SUBROUTINE fft_destroy_plan
     762              : 
     763              : ! **************************************************************************************************
     764              : !> \brief ...
     765              : !> \param plan ...
     766              : !> \param zin ...
     767              : !> \param zout ...
     768              : !> \param scale ...
     769              : ! **************************************************************************************************
     770     14401967 :    SUBROUTINE fft_1d(plan, zin, zout, scale)
     771              :       TYPE(fft_plan_type), INTENT(IN)                    :: plan
     772              :       COMPLEX(KIND=dp), DIMENSION(*), INTENT(INOUT), &
     773              :          TARGET                                          :: zin, zout
     774              :       REAL(KIND=dp), INTENT(IN)                          :: scale
     775              : 
     776     14401967 :       IF (plan%n*plan%m > 0) THEN
     777     14401967 :          CALL fft_1d_local(plan%fsign, plan%n, plan%m, plan%trans_in, plan%trans_out, plan%ldx_in, plan%ldx_out, zin, zout)
     778  55836155719 :          IF (scale /= 1.0_dp) zout(1:plan%n*plan%m) = zout(1:plan%n*plan%m)*scale
     779              :       END IF
     780              : 
     781     14401967 :    END SUBROUTINE fft_1d
     782              : 
     783              : ! **************************************************************************************************
     784              : !> \brief ...
     785              : !> \param plan ...
     786              : !> \param zin ...
     787              : !> \param zout ...
     788              : !> \param scale ...
     789              : ! **************************************************************************************************
     790      3746278 :    SUBROUTINE fft_2d(plan, zin, zout, scale)
     791              :       TYPE(fft_plan_type), INTENT(IN)                    :: plan
     792              :       COMPLEX(KIND=dp), DIMENSION(*), INTENT(INOUT), &
     793              :          TARGET                                          :: zin, zout
     794              :       REAL(KIND=dp), INTENT(IN)                          :: scale
     795              : 
     796      3746278 :       IF (plan%n_3d(1) > 0 .AND. plan%n_3d(2) > 0 .AND. plan%m > 0) THEN
     797      3746278 :          CALL fft_2d_local(plan%fsign, plan%n_3d(1:2), plan%m, plan%trans_in, plan%trans_out, zin, zout)
     798      3746278 :          IF (scale /= 1.0_dp) zout(1:plan%n_3d(1)*plan%n_3d(2)*plan%m) = &
     799       396546 :             zout(1:plan%n_3d(1)*plan%n_3d(2)*plan%m)*scale
     800              :       END IF
     801              : 
     802      3746278 :    END SUBROUTINE fft_2d
     803              : 
     804              : ! **************************************************************************************************
     805              : !> \brief Calls the 3D-FFT function from the initialized library
     806              : !> \param fsign ...
     807              : !> \param n ...
     808              : !> \param zin ...
     809              : !> \param zout ...
     810              : !> \param debug ...
     811              : !> \par History
     812              : !>      none
     813              : !> \author JGH
     814              : ! **************************************************************************************************
     815       658064 :    SUBROUTINE fft3d_s(fsign, n, zin, zout, debug)
     816              : 
     817              :       LOGICAL, INTENT(IN)                                :: fsign
     818              :       INTEGER, DIMENSION(:), INTENT(INOUT)               :: n
     819              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :, :), &
     820              :          INTENT(INOUT), TARGET                           :: zin
     821              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :, :), &
     822              :          INTENT(INOUT), OPTIONAL, TARGET                 :: zout
     823              :       LOGICAL, INTENT(IN), OPTIONAL                      :: debug
     824              : 
     825              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'fft3d_s'
     826              : 
     827              :       INTEGER                                            :: handle, ld(3), lo(3), output_unit
     828              :       LOGICAL                                            :: fft_in_place, test
     829              :       REAL(KIND=dp)                                      :: in_sum, norm, out_sum
     830              :       TYPE(fft_scratch_type), POINTER                    :: fft_scratch
     831              : 
     832       658064 :       CALL timeset(routineN, handle)
     833       658064 :       output_unit = cp_logger_get_default_io_unit()
     834              : 
     835       658064 :       IF (fsign) THEN
     836      1207480 :          norm = 1.0_dp/REAL(PRODUCT(n), KIND=dp)
     837              :       ELSE
     838       356194 :          norm = 1.0_dp
     839              :       END IF
     840              : 
     841       658064 :       IF (PRESENT(debug)) THEN
     842       619676 :          test = debug
     843              :       ELSE
     844              :          test = .FALSE.
     845              :       END IF
     846              : 
     847       658064 :       IF (PRESENT(zout)) THEN
     848              :          fft_in_place = .FALSE.
     849              :       ELSE
     850       658060 :          fft_in_place = .TRUE.
     851              :       END IF
     852              : 
     853       658064 :       IF (test) THEN
     854            0 :          in_sum = SUM(ABS(zin))
     855              :       END IF
     856              : 
     857       658064 :       ld(1) = SIZE(zin, 1)
     858       658064 :       ld(2) = SIZE(zin, 2)
     859       658064 :       ld(3) = SIZE(zin, 3)
     860              : 
     861       658064 :       IF (n(1) /= ld(1) .OR. n(2) /= ld(2) .OR. n(3) /= ld(3)) THEN
     862            0 :          CPABORT("Size and dimension (zin) have to be the same.")
     863              :       END IF
     864              : 
     865       658064 :       CALL get_fft_scratch(fft_scratch, tf_type=400, n=n)
     866              : 
     867       658064 :       IF (fft_in_place) THEN
     868       658060 :          IF (fsign) THEN
     869       301868 :             CALL fft_3d_inplace(fft_scratch%fft_plan(1), norm, zin)
     870              :          ELSE
     871       356192 :             CALL fft_3d_inplace(fft_scratch%fft_plan(2), norm, zin)
     872              :          END IF
     873              :       ELSE
     874            4 :          IF (fsign) THEN
     875            2 :             CALL fft_3d(fft_scratch%fft_plan(3), norm, zin, zout)
     876              :          ELSE
     877            2 :             CALL fft_3d(fft_scratch%fft_plan(4), norm, zin, zout)
     878              :          END IF
     879              :       END IF
     880              : 
     881       658064 :       CALL release_fft_scratch(fft_scratch)
     882              : 
     883       658064 :       IF (PRESENT(zout)) THEN
     884            4 :          lo(1) = SIZE(zout, 1)
     885            4 :          lo(2) = SIZE(zout, 2)
     886            4 :          lo(3) = SIZE(zout, 3)
     887            4 :          IF (n(1) /= lo(1) .OR. n(2) /= lo(2) .OR. n(3) /= lo(3)) THEN
     888            0 :             CPABORT("Size and dimension (zout) have to be the same.")
     889              :          END IF
     890              :       END IF
     891              : 
     892       658064 :       IF (test .AND. output_unit > 0) THEN
     893            0 :          IF (PRESENT(zout)) THEN
     894            0 :             out_sum = SUM(ABS(zout))
     895            0 :             WRITE (output_unit, '(A)') "  Out of place 3D FFT (local)  : fft3d_s"
     896            0 :             WRITE (output_unit, '(A,T60,3I7)') "     Transform lengths ", n
     897            0 :             WRITE (output_unit, '(A,T60,3I7)') "     Input array dimensions ", ld
     898            0 :             WRITE (output_unit, '(A,T60,3I7)') "     Output array dimensions ", lo
     899            0 :             WRITE (output_unit, '(A,T61,E20.14)') "     Sum of input data ", in_sum
     900            0 :             WRITE (output_unit, '(A,T61,E20.14)') "     Sum of output data ", out_sum
     901              :          ELSE
     902            0 :             out_sum = SUM(ABS(zin))
     903            0 :             WRITE (output_unit, '(A)') "  In place 3D FFT (local)  : fft3d_s"
     904            0 :             WRITE (output_unit, '(A,T60,3I7)') "     Transform lengths ", n
     905            0 :             WRITE (output_unit, '(A,T60,3I7)') "     Input/output array dimensions ", ld
     906            0 :             WRITE (output_unit, '(A,T61,E20.14)') "     Sum of input data ", in_sum
     907            0 :             WRITE (output_unit, '(A,T61,E20.14)') "     Sum of output data ", out_sum
     908              :          END IF
     909              :       END IF
     910              : 
     911       658064 :       CALL timestop(handle)
     912              : 
     913       658064 :    END SUBROUTINE fft3d_s
     914              : 
     915              : ! **************************************************************************************************
     916              : !> \brief ...
     917              : !> \param fsign ...
     918              : !> \param n ...
     919              : !> \param cin ...
     920              : !> \param gin ...
     921              : !> \param rs_group ...
     922              : !> \param yzp ...
     923              : !> \param nyzray ...
     924              : !> \param bo ...
     925              : !> \param debug ...
     926              : ! **************************************************************************************************
     927      3746070 :    SUBROUTINE fft3d_ps(fsign, n, cin, gin, rs_group, yzp, nyzray, &
     928      3746070 :                        bo, debug)
     929              : 
     930              :       LOGICAL, INTENT(IN)                                :: fsign
     931              :       INTEGER, DIMENSION(:), INTENT(IN)                  :: n
     932              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :, :), &
     933              :          INTENT(INOUT)                                   :: cin
     934              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
     935              :          INTENT(INOUT)                                   :: gin
     936              :       TYPE(mp_cart_type), INTENT(IN)                     :: rs_group
     937              :       INTEGER, CONTIGUOUS, DIMENSION(:, :, 0:), &
     938              :          INTENT(IN)                                      :: yzp
     939              :       INTEGER, CONTIGUOUS, DIMENSION(0:), INTENT(IN)     :: nyzray
     940              :       INTEGER, CONTIGUOUS, DIMENSION(:, :, 0:, :), &
     941              :          INTENT(IN)                                      :: bo
     942              :       LOGICAL, INTENT(IN), OPTIONAL                      :: debug
     943              : 
     944              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'fft3d_ps'
     945              : 
     946              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
     947      3746070 :          POINTER                                         :: pbuf, qbuf, rbuf, sbuf
     948              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :, :), &
     949      3746070 :          POINTER                                         :: tbuf
     950              :       INTEGER :: g_pos, handle, lg, lmax, mcx2, mcz1, mcz2, mg, mmax, mx1, mx2, my1, mz2, n1, n2, &
     951              :          nmax, numtask, nx, ny, nz, output_unit, r_dim(2), r_pos(2), rp
     952      3746070 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: p2p
     953              :       LOGICAL                                            :: test
     954              :       REAL(KIND=dp)                                      :: norm, sum_data
     955     26222490 :       TYPE(fft_scratch_sizes)                            :: fft_scratch_size
     956              :       TYPE(fft_scratch_type), POINTER                    :: fft_scratch
     957              : 
     958      3746070 :       CALL timeset(routineN, handle)
     959      3746070 :       output_unit = cp_logger_get_default_io_unit()
     960              : 
     961      3746070 :       IF (PRESENT(debug)) THEN
     962      3746070 :          test = debug
     963              :       ELSE
     964              :          test = .FALSE.
     965              :       END IF
     966              : 
     967      3746070 :       g_pos = rs_group%mepos
     968      3746070 :       numtask = rs_group%num_pe
     969     11238210 :       r_dim = rs_group%num_pe_cart
     970     11238210 :       r_pos = rs_group%mepos_cart
     971              : 
     972      3746070 :       IF (fsign) THEN
     973      7484592 :          norm = 1.0_dp/REAL(PRODUCT(n), KIND=dp)
     974              :       ELSE
     975      1874922 :          norm = 1.0_dp
     976              :       END IF
     977              : 
     978      3746070 :       lg = SIZE(gin, 1)
     979      3746070 :       mg = SIZE(gin, 2)
     980              : 
     981      3746070 :       nx = SIZE(cin, 1)
     982      3746070 :       ny = SIZE(cin, 2)
     983      3746070 :       nz = SIZE(cin, 3)
     984              : 
     985      3746070 :       IF (mg == 0) THEN
     986              :          mmax = 1
     987              :       ELSE
     988      3746070 :          mmax = mg
     989              :       END IF
     990      3746070 :       lmax = MAX(lg, (nx*ny*nz)/mmax + 1)
     991              : 
     992     11238210 :       ALLOCATE (p2p(0:numtask - 1))
     993              : 
     994      3746070 :       CALL rs_group%rank_compare(rs_group, p2p)
     995              : 
     996      3746070 :       rp = p2p(g_pos)
     997      3746070 :       mx1 = bo(2, 1, rp, 1) - bo(1, 1, rp, 1) + 1
     998      3746070 :       my1 = bo(2, 2, rp, 1) - bo(1, 2, rp, 1) + 1
     999      3746070 :       mx2 = bo(2, 1, rp, 2) - bo(1, 1, rp, 2) + 1
    1000      3746070 :       mz2 = bo(2, 3, rp, 2) - bo(1, 3, rp, 2) + 1
    1001              : 
    1002     11238210 :       n1 = MAXVAL(bo(2, 1, :, 1) - bo(1, 1, :, 1) + 1)
    1003     11238210 :       n2 = MAXVAL(bo(2, 2, :, 1) - bo(1, 2, :, 1) + 1)
    1004      3746070 :       nmax = MAX((2*n2)/numtask, 2)*mx2*mz2
    1005     11238210 :       nmax = MAX(nmax, n1*MAXVAL(nyzray))
    1006     11238210 :       n1 = MAXVAL(bo(2, 1, :, 2))
    1007     11238210 :       n2 = MAXVAL(bo(2, 3, :, 2))
    1008              : 
    1009      3746070 :       fft_scratch_size%nx = nx
    1010      3746070 :       fft_scratch_size%ny = ny
    1011      3746070 :       fft_scratch_size%nz = nz
    1012      3746070 :       fft_scratch_size%lmax = lmax
    1013      3746070 :       fft_scratch_size%mmax = mmax
    1014      3746070 :       fft_scratch_size%mx1 = mx1
    1015      3746070 :       fft_scratch_size%mx2 = mx2
    1016      3746070 :       fft_scratch_size%my1 = my1
    1017      3746070 :       fft_scratch_size%mz2 = mz2
    1018      3746070 :       fft_scratch_size%lg = lg
    1019      3746070 :       fft_scratch_size%mg = mg
    1020      3746070 :       fft_scratch_size%nbx = n1
    1021      3746070 :       fft_scratch_size%nbz = n2
    1022     11238210 :       mcz1 = MAXVAL(bo(2, 3, :, 1) - bo(1, 3, :, 1) + 1)
    1023     11238210 :       mcx2 = MAXVAL(bo(2, 1, :, 2) - bo(1, 1, :, 2) + 1)
    1024     11238210 :       mcz2 = MAXVAL(bo(2, 3, :, 2) - bo(1, 3, :, 2) + 1)
    1025      3746070 :       fft_scratch_size%mcz1 = mcz1
    1026      3746070 :       fft_scratch_size%mcx2 = mcx2
    1027      3746070 :       fft_scratch_size%mcz2 = mcz2
    1028      3746070 :       fft_scratch_size%nmax = nmax
    1029     11238210 :       fft_scratch_size%nmray = MAXVAL(nyzray)
    1030      3746070 :       fft_scratch_size%nyzray = nyzray(g_pos)
    1031      3746070 :       fft_scratch_size%rs_group = rs_group
    1032     11238210 :       fft_scratch_size%g_pos = g_pos
    1033     11238210 :       fft_scratch_size%r_pos = r_pos
    1034     11238210 :       fft_scratch_size%r_dim = r_dim
    1035      3746070 :       fft_scratch_size%numtask = numtask
    1036              : 
    1037      3746070 :       IF (test) THEN
    1038            8 :          IF (g_pos == 0 .AND. output_unit > 0) THEN
    1039            4 :             WRITE (output_unit, '(A)') "  Parallel 3D FFT : fft3d_ps"
    1040            4 :             WRITE (output_unit, '(A,T60,3I7)') "     Transform lengths ", n
    1041            4 :             WRITE (output_unit, '(A,T67,2I7)') "     Array dimensions (gin) ", lg, mg
    1042            4 :             WRITE (output_unit, '(A,T60,3I7)') "     Array dimensions (cin) ", nx, ny, nz
    1043              :          END IF
    1044              :       END IF
    1045              : 
    1046      3746070 :       IF (r_dim(2) > 1) THEN
    1047              : 
    1048              :          !
    1049              :          ! real space is distributed over x and y coordinate
    1050              :          ! we have two stages of communication
    1051              :          !
    1052              : 
    1053            0 :          IF (r_dim(1) == 1) THEN
    1054            0 :             CPABORT("This processor distribution is not supported.")
    1055              :          END IF
    1056            0 :          CALL get_fft_scratch(fft_scratch, tf_type=300, n=n, fft_sizes=fft_scratch_size)
    1057              : 
    1058            0 :          IF (fsign) THEN
    1059              :             ! cin -> gin
    1060              : 
    1061            0 :             IF (test) THEN
    1062            0 :                sum_data = ABS(SUM(cin))
    1063            0 :                CALL rs_group%sum(sum_data)
    1064            0 :                IF (g_pos == 0 .AND. output_unit > 0) THEN
    1065            0 :                   WRITE (output_unit, '(A)') "  Two step communication algorithm "
    1066            0 :                   WRITE (output_unit, '(A,T60,3I7)') "     Transform Z ", n(3), mx1*my1
    1067            0 :                   WRITE (output_unit, '(A,T60,3I7)') "     Transform Y ", n(2), mx2*mz2
    1068            0 :                   WRITE (output_unit, '(A,T67,2I7)') "     Transform X ", n(1), nyzray(g_pos)
    1069            0 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(1) ", sum_data
    1070              :                END IF
    1071              :             END IF
    1072              : 
    1073            0 :             pbuf => fft_scratch%p1buf
    1074            0 :             qbuf => fft_scratch%p2buf
    1075              : 
    1076              :             ! FFT along z
    1077            0 :             CALL fft_1d(fft_scratch%fft_plan(1), cin, qbuf, norm)
    1078              : 
    1079            0 :             rbuf => fft_scratch%p3buf
    1080              : 
    1081            0 :             IF (test) THEN
    1082            0 :                sum_data = ABS(SUM(qbuf))
    1083            0 :                CALL rs_group%sum(sum_data)
    1084            0 :                IF (g_pos == 0 .AND. output_unit > 0) THEN
    1085            0 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(2) T", sum_data
    1086              :                END IF
    1087              :             END IF
    1088              : 
    1089              :             ! Exchange data ( transpose of matrix )
    1090            0 :             CALL cube_transpose_2(qbuf, bo(:, :, :, 1), bo(:, :, :, 2), rbuf, fft_scratch)
    1091              : 
    1092            0 :             IF (test) THEN
    1093            0 :                sum_data = ABS(SUM(rbuf))
    1094            0 :                CALL rs_group%sum(sum_data)
    1095            0 :                IF (g_pos == 0 .AND. output_unit > 0) THEN
    1096            0 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(3) T", sum_data
    1097              :                END IF
    1098              :             END IF
    1099              : 
    1100            0 :             pbuf => fft_scratch%p4buf
    1101              : 
    1102              :             ! FFT along y
    1103            0 :             CALL fft_1d(fft_scratch%fft_plan(2), rbuf, pbuf, 1.0_dp)
    1104              : 
    1105            0 :             qbuf => fft_scratch%p5buf
    1106              : 
    1107            0 :             IF (test) THEN
    1108            0 :                sum_data = ABS(SUM(pbuf))
    1109            0 :                CALL rs_group%sum(sum_data)
    1110            0 :                IF (g_pos == 0 .AND. output_unit > 0) THEN
    1111            0 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(4) TS", sum_data
    1112              :                END IF
    1113              :             END IF
    1114              : 
    1115              :             ! Exchange data ( transpose of matrix ) and sort
    1116              :             CALL xz_to_yz(pbuf, rs_group, r_dim, g_pos, p2p, yzp, nyzray, &
    1117            0 :                           bo(:, :, :, 2), qbuf, fft_scratch)
    1118              : 
    1119            0 :             IF (test) THEN
    1120            0 :                sum_data = ABS(SUM(qbuf))
    1121            0 :                CALL rs_group%sum(sum_data)
    1122            0 :                IF (g_pos == 0 .AND. output_unit > 0) THEN
    1123            0 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(5) TS", sum_data
    1124              :                END IF
    1125              :             END IF
    1126              : 
    1127              :             ! FFT along x
    1128            0 :             CALL fft_1d(fft_scratch%fft_plan(3), qbuf, gin, 1.0_dp)
    1129              : 
    1130            0 :             IF (test) THEN
    1131            0 :                sum_data = ABS(SUM(gin))
    1132            0 :                CALL rs_group%sum(sum_data)
    1133            0 :                IF (g_pos == 0 .AND. output_unit > 0) THEN
    1134            0 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(6) ", sum_data
    1135              :                END IF
    1136              :             END IF
    1137              : 
    1138              :          ELSE
    1139              :             ! gin -> cin
    1140              : 
    1141            0 :             IF (test) THEN
    1142            0 :                sum_data = ABS(SUM(gin))
    1143            0 :                CALL rs_group%sum(sum_data)
    1144            0 :                IF (g_pos == 0 .AND. output_unit > 0) THEN
    1145            0 :                   WRITE (output_unit, '(A)') "  Two step communication algorithm "
    1146            0 :                   WRITE (output_unit, '(A,T67,2I7)') "     Transform X ", n(1), nyzray(g_pos)
    1147            0 :                   WRITE (output_unit, '(A,T60,3I7)') "     Transform Y ", n(2), mx2*mz2
    1148            0 :                   WRITE (output_unit, '(A,T60,3I7)') "     Transform Z ", n(3), mx1*my1
    1149            0 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(1) ", sum_data
    1150              :                END IF
    1151              :             END IF
    1152              : 
    1153            0 :             pbuf => fft_scratch%p7buf
    1154              : 
    1155              :             ! FFT along x
    1156            0 :             CALL fft_1d(fft_scratch%fft_plan(4), gin, pbuf, norm)
    1157              : 
    1158            0 :             qbuf => fft_scratch%p4buf
    1159              : 
    1160            0 :             IF (test) THEN
    1161            0 :                sum_data = ABS(SUM(pbuf))
    1162            0 :                CALL rs_group%sum(sum_data)
    1163            0 :                IF (g_pos == 0 .AND. output_unit > 0) THEN
    1164            0 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(2) TS", sum_data
    1165              :                END IF
    1166              :             END IF
    1167              : 
    1168              :             ! Exchange data ( transpose of matrix ) and sort
    1169              :             CALL yz_to_xz(pbuf, rs_group, r_dim, g_pos, p2p, yzp, nyzray, &
    1170            0 :                           bo(:, :, :, 2), qbuf, fft_scratch)
    1171              : 
    1172            0 :             IF (test) THEN
    1173            0 :                sum_data = ABS(SUM(qbuf))
    1174            0 :                CALL rs_group%sum(sum_data)
    1175            0 :                IF (g_pos == 0 .AND. output_unit > 0) THEN
    1176            0 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(3) TS", sum_data
    1177              :                END IF
    1178              :             END IF
    1179              : 
    1180            0 :             rbuf => fft_scratch%p3buf
    1181              : 
    1182              :             ! FFT along y
    1183            0 :             CALL fft_1d(fft_scratch%fft_plan(5), qbuf, rbuf, 1.0_dp)
    1184              : 
    1185            0 :             pbuf => fft_scratch%p2buf
    1186              : 
    1187            0 :             IF (test) THEN
    1188            0 :                sum_data = ABS(SUM(rbuf))
    1189            0 :                CALL rs_group%sum(sum_data)
    1190            0 :                IF (g_pos == 0 .AND. output_unit > 0) THEN
    1191            0 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(4) T", sum_data
    1192              :                END IF
    1193              :             END IF
    1194              : 
    1195              :             ! Exchange data ( transpose of matrix )
    1196            0 :             CALL cube_transpose_1(rbuf, bo(:, :, :, 2), bo(:, :, :, 1), pbuf, fft_scratch)
    1197              : 
    1198            0 :             IF (test) THEN
    1199            0 :                sum_data = ABS(SUM(pbuf))
    1200            0 :                CALL rs_group%sum(sum_data)
    1201            0 :                IF (g_pos == 0 .AND. output_unit > 0) THEN
    1202            0 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(5) T", sum_data
    1203              :                END IF
    1204              :             END IF
    1205              : 
    1206            0 :             qbuf => fft_scratch%p1buf
    1207              : 
    1208              :             ! FFT along z
    1209            0 :             CALL fft_1d(fft_scratch%fft_plan(6), pbuf, cin, 1.0_dp)
    1210              : 
    1211            0 :             IF (test) THEN
    1212            0 :                sum_data = ABS(SUM(cin))
    1213            0 :                CALL rs_group%sum(sum_data)
    1214            0 :                IF (g_pos == 0 .AND. output_unit > 0) THEN
    1215            0 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(6) ", sum_data
    1216              :                END IF
    1217              :             END IF
    1218              :          END IF
    1219              : 
    1220            0 :          CALL release_fft_scratch(fft_scratch)
    1221              : 
    1222              :       ELSE
    1223              : 
    1224              :          !
    1225              :          ! real space is only distributed over x coordinate
    1226              :          ! we have one stage of communication, after the transform of
    1227              :          ! direction x
    1228              :          !
    1229              : 
    1230      3746070 :          CALL get_fft_scratch(fft_scratch, tf_type=200, n=n, fft_sizes=fft_scratch_size)
    1231              : 
    1232      3746070 :          sbuf => fft_scratch%r1buf
    1233      3746070 :          tbuf => fft_scratch%tbuf
    1234              : 
    1235      3746070 :          sbuf = z_zero
    1236      3746070 :          tbuf = z_zero
    1237              : 
    1238      3746070 :          IF (fsign) THEN
    1239              :             ! cin -> gin
    1240              : 
    1241      1871148 :             IF (test) THEN
    1242         9284 :                sum_data = ABS(SUM(cin))
    1243            4 :                CALL rs_group%sum(sum_data)
    1244            4 :                IF (g_pos == 0 .AND. output_unit > 0) THEN
    1245            2 :                   WRITE (output_unit, '(A)') "     One step communication algorithm "
    1246            2 :                   WRITE (output_unit, '(A,T60,3I7)') "     Transform YZ ", n(2), n(3), nx
    1247            2 :                   WRITE (output_unit, '(A,T67,2I7)') "     Transform X ", n(1), nyzray(g_pos)
    1248            2 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(1) ", sum_data
    1249              :                END IF
    1250              :             END IF
    1251              : 
    1252              :             ! FFT along y and z
    1253      1871148 :             CALL fft_2d(fft_scratch%fft_plan(1), cin, tbuf, 1._dp)
    1254              : 
    1255      1871148 :             IF (test) THEN
    1256         8740 :                sum_data = ABS(SUM(tbuf))
    1257            4 :                CALL rs_group%sum(sum_data)
    1258            4 :                IF (g_pos == 0 .AND. output_unit > 0) THEN
    1259            2 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(2) TS", sum_data
    1260              :                END IF
    1261              :             END IF
    1262              : 
    1263              :             ! Exchange data ( transpose of matrix ) and sort
    1264              :             CALL yz_to_x(tbuf, rs_group, g_pos, p2p, yzp, nyzray, &
    1265      1871148 :                          bo(:, :, :, 2), sbuf, fft_scratch)
    1266              : 
    1267      1871148 :             IF (test) THEN
    1268         8776 :                sum_data = ABS(SUM(sbuf))
    1269            4 :                CALL rs_group%sum(sum_data)
    1270            4 :                IF (g_pos == 0 .AND. output_unit > 0) THEN
    1271            2 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(3) TS", sum_data
    1272              :                END IF
    1273              :             END IF
    1274              :             ! FFT along x
    1275      1871148 :             CALL fft_1d(fft_scratch%fft_plan(3), sbuf, gin, norm)
    1276              : 
    1277      1871148 :             IF (test) THEN
    1278         8708 :                sum_data = ABS(SUM(gin))
    1279            4 :                CALL rs_group%sum(sum_data)
    1280            4 :                IF (g_pos == 0 .AND. output_unit > 0) THEN
    1281            2 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(4) ", sum_data
    1282              :                END IF
    1283              :             END IF
    1284              : 
    1285              :          ELSE
    1286              :             ! gin -> cin
    1287              : 
    1288      1874922 :             IF (test) THEN
    1289         8708 :                sum_data = ABS(SUM(gin))
    1290            4 :                CALL rs_group%sum(sum_data)
    1291            4 :                IF (g_pos == 0 .AND. output_unit > 0) THEN
    1292            2 :                   WRITE (output_unit, '(A)') "  One step communication algorithm "
    1293            2 :                   WRITE (output_unit, '(A,T67,2I7)') "     Transform X ", n(1), nyzray(g_pos)
    1294            2 :                   WRITE (output_unit, '(A,T60,3I7)') "     Transform YZ ", n(2), n(3), nx
    1295            2 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(1) ", sum_data
    1296              :                END IF
    1297              :             END IF
    1298              : 
    1299              :             ! FFT along x
    1300      1874922 :             CALL fft_1d(fft_scratch%fft_plan(4), gin, sbuf, norm)
    1301              : 
    1302      1874922 :             IF (test) THEN
    1303         8776 :                sum_data = ABS(SUM(sbuf))
    1304            4 :                CALL rs_group%sum(sum_data)
    1305            4 :                IF (g_pos == 0 .AND. output_unit > 0) THEN
    1306            2 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(2) TS", sum_data
    1307              :                END IF
    1308              :             END IF
    1309              : 
    1310              :             ! Exchange data ( transpose of matrix ) and sort
    1311              :             CALL x_to_yz(sbuf, rs_group, g_pos, p2p, yzp, nyzray, &
    1312      1874922 :                          bo(:, :, :, 2), tbuf, fft_scratch)
    1313              : 
    1314      1874922 :             IF (test) THEN
    1315         8740 :                sum_data = ABS(SUM(tbuf))
    1316            4 :                CALL rs_group%sum(sum_data)
    1317            4 :                IF (g_pos == 0 .AND. output_unit > 0) THEN
    1318            2 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(3) TS", sum_data
    1319              :                END IF
    1320              :             END IF
    1321              : 
    1322              :             ! FFT along y and z
    1323      1874922 :             CALL fft_2d(fft_scratch%fft_plan(5), tbuf, cin, 1._dp)
    1324              : 
    1325      1874922 :             IF (test) THEN
    1326         9284 :                sum_data = ABS(SUM(cin))
    1327            4 :                CALL rs_group%sum(sum_data)
    1328            4 :                IF (g_pos == 0 .AND. output_unit > 0) THEN
    1329            2 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(4) ", sum_data
    1330              :                END IF
    1331              :             END IF
    1332              :          END IF
    1333              : 
    1334      3746070 :          CALL release_fft_scratch(fft_scratch)
    1335              : 
    1336              :       END IF
    1337              : 
    1338      3746070 :       DEALLOCATE (p2p)
    1339              : 
    1340      3746070 :       CALL timestop(handle)
    1341              : 
    1342      7492140 :    END SUBROUTINE fft3d_ps
    1343              : 
    1344              : ! **************************************************************************************************
    1345              : !> \brief ...
    1346              : !> \param fsign ...
    1347              : !> \param n ...
    1348              : !> \param zin ...
    1349              : !> \param gin ...
    1350              : !> \param group ...
    1351              : !> \param bo ...
    1352              : !> \param debug ...
    1353              : ! **************************************************************************************************
    1354          208 :    SUBROUTINE fft3d_pb(fsign, n, zin, gin, group, bo, debug)
    1355              : 
    1356              :       LOGICAL, INTENT(IN)                                :: fsign
    1357              :       INTEGER, DIMENSION(3), INTENT(IN)                  :: n
    1358              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :, :), &
    1359              :          INTENT(INOUT)                                   :: zin
    1360              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
    1361              :          INTENT(INOUT)                                   :: gin
    1362              :       TYPE(mp_cart_type), INTENT(IN)                     :: group
    1363              :       INTEGER, CONTIGUOUS, DIMENSION(:, :, 0:, :), &
    1364              :          INTENT(IN)                                      :: bo
    1365              :       LOGICAL, INTENT(IN), OPTIONAL                      :: debug
    1366              : 
    1367              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'fft3d_pb'
    1368              : 
    1369              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
    1370          208 :          POINTER                                         :: abuf, bbuf
    1371              :       INTEGER                                            :: handle, lg(2), lz(3), mcx2, mcy3, mcz1, &
    1372              :                                                             mcz2, mx1, mx2, mx3, my1, my2, my3, &
    1373              :                                                             my_pos, mz1, mz2, mz3, output_unit
    1374              :       INTEGER, DIMENSION(2)                              :: dim
    1375              :       LOGICAL                                            :: test
    1376              :       REAL(KIND=dp)                                      :: norm, sum_data
    1377         1456 :       TYPE(fft_scratch_sizes)                            :: fft_scratch_size
    1378              :       TYPE(fft_scratch_type), POINTER                    :: fft_scratch
    1379              : 
    1380              : !------------------------------------------------------------------------------
    1381              : ! "Real Space"  1) xyZ      or      1) xYZ
    1382              : !               2) xYz      or         not used
    1383              : ! "G Space"     3) Xyz      or      3) XYz
    1384              : !
    1385              : ! There is one communicator (2-dimensional) for all distributions
    1386              : ! np = n1 * n2, where np is the total number of processors
    1387              : ! If n2 = 1, we have the second case and only one transpose step is needed
    1388              : !
    1389              : ! Assignment of dimensions to axis for different steps
    1390              : ! First case: 1) n1=x; n2=y
    1391              : !             2) n1=x; n2=z
    1392              : !             3) n1=y; n2=z
    1393              : ! Second case 1) n1=x
    1394              : !             3) n1=z
    1395              : !
    1396              : ! The more general case with two communicators for the initial and final
    1397              : ! distribution is not covered.
    1398              : !------------------------------------------------------------------------------
    1399              : 
    1400          208 :       CALL timeset(routineN, handle)
    1401          208 :       output_unit = cp_logger_get_default_io_unit()
    1402              : 
    1403          624 :       dim = group%num_pe_cart
    1404          208 :       my_pos = group%mepos
    1405              : 
    1406          208 :       IF (PRESENT(debug)) THEN
    1407          208 :          test = debug
    1408              :       ELSE
    1409              :          test = .FALSE.
    1410              :       END IF
    1411              : 
    1412          208 :       IF (fsign) THEN
    1413          416 :          norm = 1.0_dp/REAL(PRODUCT(n), KIND=dp)
    1414              :       ELSE
    1415          104 :          norm = 1.0_dp
    1416              :       END IF
    1417              : 
    1418          208 :       IF (test) THEN
    1419            8 :          lg(1) = SIZE(gin, 1)
    1420            8 :          lg(2) = SIZE(gin, 2)
    1421            8 :          lz(1) = SIZE(zin, 1)
    1422            8 :          lz(2) = SIZE(zin, 2)
    1423            8 :          lz(3) = SIZE(zin, 3)
    1424            8 :          IF (my_pos == 0 .AND. output_unit > 0) THEN
    1425            4 :             WRITE (output_unit, '(A)') "  Parallel 3D FFT : fft3d_pb"
    1426            4 :             WRITE (output_unit, '(A,T60,3I7)') "     Transform lengths ", n
    1427            4 :             WRITE (output_unit, '(A,T67,2I7)') "     Array dimensions (gin) ", lg
    1428            4 :             WRITE (output_unit, '(A,T60,3I7)') "     Array dimensions (cin) ", lz
    1429              :          END IF
    1430              :       END IF
    1431              : 
    1432          208 :       mx1 = bo(2, 1, my_pos, 1) - bo(1, 1, my_pos, 1) + 1
    1433          208 :       my1 = bo(2, 2, my_pos, 1) - bo(1, 2, my_pos, 1) + 1
    1434          208 :       mz1 = bo(2, 3, my_pos, 1) - bo(1, 3, my_pos, 1) + 1
    1435          208 :       mx2 = bo(2, 1, my_pos, 2) - bo(1, 1, my_pos, 2) + 1
    1436          208 :       my2 = bo(2, 2, my_pos, 2) - bo(1, 2, my_pos, 2) + 1
    1437          208 :       mz2 = bo(2, 3, my_pos, 2) - bo(1, 3, my_pos, 2) + 1
    1438          208 :       mx3 = bo(2, 1, my_pos, 3) - bo(1, 1, my_pos, 3) + 1
    1439          208 :       my3 = bo(2, 2, my_pos, 3) - bo(1, 2, my_pos, 3) + 1
    1440          208 :       mz3 = bo(2, 3, my_pos, 3) - bo(1, 3, my_pos, 3) + 1
    1441          208 :       fft_scratch_size%mx1 = mx1
    1442          208 :       fft_scratch_size%mx2 = mx2
    1443          208 :       fft_scratch_size%mx3 = mx3
    1444          208 :       fft_scratch_size%my1 = my1
    1445          208 :       fft_scratch_size%my2 = my2
    1446          208 :       fft_scratch_size%my3 = my3
    1447          208 :       fft_scratch_size%mz1 = mz1
    1448          208 :       fft_scratch_size%mz2 = mz2
    1449          208 :       fft_scratch_size%mz3 = mz3
    1450          624 :       mcz1 = MAXVAL(bo(2, 3, :, 1) - bo(1, 3, :, 1) + 1)
    1451          624 :       mcx2 = MAXVAL(bo(2, 1, :, 2) - bo(1, 1, :, 2) + 1)
    1452          624 :       mcz2 = MAXVAL(bo(2, 3, :, 2) - bo(1, 3, :, 2) + 1)
    1453          624 :       mcy3 = MAXVAL(bo(2, 2, :, 3) - bo(1, 2, :, 3) + 1)
    1454          208 :       fft_scratch_size%mcz1 = mcz1
    1455          208 :       fft_scratch_size%mcx2 = mcx2
    1456          208 :       fft_scratch_size%mcz2 = mcz2
    1457          208 :       fft_scratch_size%mcy3 = mcy3
    1458          208 :       fft_scratch_size%rs_group = group
    1459          624 :       fft_scratch_size%g_pos = my_pos
    1460          208 :       fft_scratch_size%numtask = DIM(1)*DIM(2)
    1461              : 
    1462          208 :       IF (DIM(1) > 1 .AND. DIM(2) > 1) THEN
    1463              : 
    1464              :          !
    1465              :          ! First case; two stages of communication
    1466              :          !
    1467              : 
    1468            0 :          CALL get_fft_scratch(fft_scratch, tf_type=100, n=n, fft_sizes=fft_scratch_size)
    1469              : 
    1470            0 :          IF (fsign) THEN
    1471              :             ! Stage 1 -> 3
    1472              : 
    1473            0 :             bbuf => fft_scratch%a2buf
    1474              : 
    1475            0 :             IF (test) THEN
    1476            0 :                sum_data = ABS(SUM(zin))
    1477            0 :                CALL group%sum(sum_data)
    1478            0 :                IF (my_pos == 0 .AND. output_unit > 0) THEN
    1479            0 :                   WRITE (output_unit, '(A)') "  Two step communication algorithm "
    1480            0 :                   WRITE (output_unit, '(A,T67,2I7)') "     Transform Z ", n(3), mx1*my1
    1481            0 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(1) ", sum_data
    1482              :                END IF
    1483              :             END IF
    1484              : 
    1485              :             ! FFT along z
    1486            0 :             CALL fft_1d(fft_scratch%fft_plan(1), zin, bbuf, norm)
    1487              : 
    1488            0 :             abuf => fft_scratch%a3buf
    1489              : 
    1490            0 :             IF (test) THEN
    1491            0 :                sum_data = ABS(SUM(bbuf))
    1492            0 :                CALL group%sum(sum_data)
    1493            0 :                IF (my_pos == 0 .AND. output_unit > 0) THEN
    1494            0 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(2) T", sum_data
    1495              :                END IF
    1496              :             END IF
    1497              : 
    1498            0 :             CALL cube_transpose_2(bbuf, bo(:, :, :, 1), bo(:, :, :, 2), abuf, fft_scratch)
    1499              : 
    1500            0 :             bbuf => fft_scratch%a4buf
    1501              : 
    1502            0 :             IF (test) THEN
    1503            0 :                sum_data = ABS(SUM(abuf))
    1504            0 :                CALL group%sum(sum_data)
    1505            0 :                IF (my_pos == 0 .AND. output_unit > 0) THEN
    1506            0 :                   WRITE (output_unit, '(A,T67,2I7)') "     Transform Y ", n(2), mx2*mz2
    1507            0 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(3) ", sum_data
    1508              :                END IF
    1509              :             END IF
    1510              : 
    1511              :             ! FFT along y
    1512            0 :             CALL fft_1d(fft_scratch%fft_plan(2), abuf, bbuf, 1.0_dp)
    1513              : 
    1514            0 :             abuf => fft_scratch%a5buf
    1515              : 
    1516            0 :             IF (test) THEN
    1517            0 :                sum_data = ABS(SUM(bbuf))
    1518            0 :                CALL group%sum(sum_data)
    1519            0 :                IF (my_pos == 0 .AND. output_unit > 0) THEN
    1520            0 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(4) T", sum_data
    1521              :                END IF
    1522              :             END IF
    1523              : 
    1524            0 :             CALL cube_transpose_4(bbuf, bo(:, :, :, 2), bo(:, :, :, 3), abuf, fft_scratch)
    1525              : 
    1526            0 :             IF (test) THEN
    1527            0 :                sum_data = ABS(SUM(abuf))
    1528            0 :                CALL group%sum(sum_data)
    1529            0 :                IF (my_pos == 0 .AND. output_unit > 0) THEN
    1530            0 :                   WRITE (output_unit, '(A,T67,2I7)') "     Transform X ", n(1), my3*mz3
    1531            0 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(5) ", sum_data
    1532              :                END IF
    1533              :             END IF
    1534              : 
    1535              :             ! FFT along x
    1536            0 :             CALL fft_1d(fft_scratch%fft_plan(3), abuf, gin, 1.0_dp)
    1537              : 
    1538            0 :             IF (test) THEN
    1539            0 :                sum_data = ABS(SUM(gin))
    1540            0 :                CALL group%sum(sum_data)
    1541            0 :                IF (my_pos == 0 .AND. output_unit > 0) THEN
    1542            0 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(6) ", sum_data
    1543              :                END IF
    1544              :             END IF
    1545              : 
    1546              :          ELSE
    1547              :             ! Stage 3 -> 1
    1548              : 
    1549            0 :             bbuf => fft_scratch%a5buf
    1550              : 
    1551            0 :             IF (test) THEN
    1552            0 :                sum_data = ABS(SUM(gin))
    1553            0 :                CALL group%sum(sum_data)
    1554            0 :                IF (my_pos == 0 .AND. output_unit > 0) THEN
    1555            0 :                   WRITE (output_unit, '(A)') "  Two step communication algorithm "
    1556            0 :                   WRITE (output_unit, '(A,T67,2I7)') "     Transform X ", n(1), my3*mz3
    1557            0 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(1) ", sum_data
    1558              :                END IF
    1559              :             END IF
    1560              : 
    1561              :             ! FFT along x
    1562            0 :             CALL fft_1d(fft_scratch%fft_plan(4), gin, bbuf, 1.0_dp)
    1563              : 
    1564            0 :             abuf => fft_scratch%a4buf
    1565              : 
    1566            0 :             IF (test) THEN
    1567            0 :                sum_data = ABS(SUM(bbuf))
    1568            0 :                CALL group%sum(sum_data)
    1569            0 :                IF (my_pos == 0 .AND. output_unit > 0) THEN
    1570            0 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(2) T", sum_data
    1571              :                END IF
    1572              :             END IF
    1573              : 
    1574            0 :             CALL cube_transpose_3(bbuf, bo(:, :, :, 3), bo(:, :, :, 2), abuf, fft_scratch)
    1575              : 
    1576            0 :             bbuf => fft_scratch%a3buf
    1577              : 
    1578            0 :             IF (test) THEN
    1579            0 :                sum_data = ABS(SUM(abuf))
    1580            0 :                CALL group%sum(sum_data)
    1581            0 :                IF (my_pos == 0 .AND. output_unit > 0) THEN
    1582            0 :                   WRITE (output_unit, '(A,T67,2I7)') "     Transform Y ", n(2), mx2*mz2
    1583            0 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(3) ", sum_data
    1584              :                END IF
    1585              :             END IF
    1586              : 
    1587              :             ! FFT along y
    1588            0 :             CALL fft_1d(fft_scratch%fft_plan(5), abuf, bbuf, 1.0_dp)
    1589              : 
    1590            0 :             abuf => fft_scratch%a2buf
    1591              : 
    1592            0 :             IF (test) THEN
    1593            0 :                sum_data = ABS(SUM(bbuf))
    1594            0 :                CALL group%sum(sum_data)
    1595            0 :                IF (my_pos == 0 .AND. output_unit > 0) THEN
    1596            0 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(4) T", sum_data
    1597              :                END IF
    1598              :             END IF
    1599              : 
    1600            0 :             CALL cube_transpose_1(bbuf, bo(:, :, :, 2), bo(:, :, :, 1), abuf, fft_scratch)
    1601              : 
    1602            0 :             IF (test) THEN
    1603            0 :                sum_data = ABS(SUM(abuf))
    1604            0 :                CALL group%sum(sum_data)
    1605            0 :                IF (my_pos == 0 .AND. output_unit > 0) THEN
    1606            0 :                   WRITE (output_unit, '(A,T67,2I7)') "     Transform Z ", n(3), mx1*my1
    1607            0 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(5) ", sum_data
    1608              :                END IF
    1609              :             END IF
    1610              : 
    1611              :             ! FFT along z
    1612            0 :             CALL fft_1d(fft_scratch%fft_plan(6), abuf, zin, norm)
    1613              : 
    1614            0 :             IF (test) THEN
    1615            0 :                sum_data = ABS(SUM(zin))
    1616            0 :                CALL group%sum(sum_data)
    1617            0 :                IF (my_pos == 0 .AND. output_unit > 0) THEN
    1618            0 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(6) ", sum_data
    1619              :                END IF
    1620              :             END IF
    1621              :          END IF
    1622              : 
    1623            0 :          CALL release_fft_scratch(fft_scratch)
    1624              : 
    1625          208 :       ELSE IF (DIM(2) == 1) THEN
    1626              : 
    1627              :          !
    1628              :          ! Second case; one stage of communication
    1629              :          !
    1630              : 
    1631          208 :          CALL get_fft_scratch(fft_scratch, tf_type=101, n=n, fft_sizes=fft_scratch_size)
    1632              : 
    1633          208 :          IF (fsign) THEN
    1634              :             ! Stage 1 -> 3
    1635              : 
    1636          104 :             IF (test) THEN
    1637         9284 :                sum_data = ABS(SUM(zin))
    1638            4 :                CALL group%sum(sum_data)
    1639            4 :                IF (my_pos == 0 .AND. output_unit > 0) THEN
    1640            2 :                   WRITE (output_unit, '(A)') "  one step communication algorithm "
    1641            2 :                   WRITE (output_unit, '(A,T67,2I7)') "     Transform Z ", n(3), mx1*my1
    1642            2 :                   WRITE (output_unit, '(A,T67,2I7)') "     Transform Y ", n(2), mx1*mz1
    1643            2 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(1) ", sum_data
    1644              :                END IF
    1645              :             END IF
    1646              : 
    1647          104 :             bbuf => fft_scratch%a4buf
    1648              :             ! FFT along z and y
    1649          104 :             CALL fft_2d(fft_scratch%fft_plan(1), zin, bbuf, norm)
    1650              : 
    1651          104 :             abuf => fft_scratch%a5buf
    1652              : 
    1653          104 :             IF (test) THEN
    1654         8708 :                sum_data = ABS(SUM(bbuf))
    1655            4 :                CALL group%sum(sum_data)
    1656            4 :                IF (my_pos == 0 .AND. output_unit > 0) THEN
    1657            2 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(2) T", sum_data
    1658              :                END IF
    1659              :             END IF
    1660              : 
    1661          104 :             CALL cube_transpose_6(bbuf, group, bo(:, :, :, 1), bo(:, :, :, 3), abuf, fft_scratch)
    1662              : 
    1663          104 :             IF (test) THEN
    1664         8260 :                sum_data = ABS(SUM(abuf))
    1665            4 :                CALL group%sum(sum_data)
    1666            4 :                IF (my_pos == 0 .AND. output_unit > 0) THEN
    1667            2 :                   WRITE (output_unit, '(A,T67,2I7)') "     Transform X ", n(1), my3*mz3
    1668            2 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(3) ", sum_data
    1669              :                END IF
    1670              :             END IF
    1671              : 
    1672              :             ! FFT along x
    1673          104 :             CALL fft_1d(fft_scratch%fft_plan(3), abuf, gin, 1.0_dp)
    1674              : 
    1675          104 :             IF (test) THEN
    1676         8708 :                sum_data = ABS(SUM(gin))
    1677            4 :                CALL group%sum(sum_data)
    1678            4 :                IF (my_pos == 0 .AND. output_unit > 0) THEN
    1679            2 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(4) ", sum_data
    1680              :                END IF
    1681              :             END IF
    1682              : 
    1683              :          ELSE
    1684              :             ! Stage 3 -> 1
    1685              : 
    1686          104 :             IF (test) THEN
    1687         8708 :                sum_data = ABS(SUM(gin))
    1688            4 :                CALL group%sum(sum_data)
    1689            4 :                IF (my_pos == 0 .AND. output_unit > 0) THEN
    1690            2 :                   WRITE (output_unit, '(A)') "  one step communication algorithm "
    1691            2 :                   WRITE (output_unit, '(A,T67,2I7)') "     Transform X ", n(1), my3*mz3
    1692            2 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(1) ", sum_data
    1693              :                END IF
    1694              :             END IF
    1695              : 
    1696          104 :             bbuf => fft_scratch%a5buf
    1697              : 
    1698              :             ! FFT along x
    1699          104 :             CALL fft_1d(fft_scratch%fft_plan(4), gin, bbuf, 1.0_dp)
    1700              : 
    1701          104 :             abuf => fft_scratch%a4buf
    1702              : 
    1703          104 :             IF (test) THEN
    1704         8260 :                sum_data = ABS(SUM(bbuf))
    1705            4 :                CALL group%sum(sum_data)
    1706            4 :                IF (my_pos == 0 .AND. output_unit > 0) THEN
    1707            2 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(2) T", sum_data
    1708              :                END IF
    1709              :             END IF
    1710              : 
    1711          104 :             CALL cube_transpose_5(bbuf, group, bo(:, :, :, 3), bo(:, :, :, 1), abuf, fft_scratch)
    1712              : 
    1713          104 :             IF (test) THEN
    1714         8708 :                sum_data = ABS(SUM(abuf))
    1715            4 :                CALL group%sum(sum_data)
    1716            4 :                IF (my_pos == 0 .AND. output_unit > 0) THEN
    1717            2 :                   WRITE (output_unit, '(A,T67,2I7)') "     Transform Y ", n(2), mx1*mz1
    1718            2 :                   WRITE (output_unit, '(A,T67,2I7)') "     Transform Z ", n(3), mx1*my1
    1719            2 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(3) ", sum_data
    1720              :                END IF
    1721              :             END IF
    1722              : 
    1723              :             ! FFT along y and z
    1724          104 :             CALL fft_2d(fft_scratch%fft_plan(5), abuf, zin, norm)
    1725              : 
    1726          104 :             IF (test) THEN
    1727         9284 :                sum_data = ABS(SUM(zin))
    1728            4 :                CALL group%sum(sum_data)
    1729            4 :                IF (my_pos == 0 .AND. output_unit > 0) THEN
    1730            2 :                   WRITE (output_unit, '(A,T61,E20.14)') "     Sum of data(4) ", sum_data
    1731              :                END IF
    1732              :             END IF
    1733              :          END IF
    1734              : 
    1735          208 :          CALL release_fft_scratch(fft_scratch)
    1736              : 
    1737              :       ELSE
    1738              : 
    1739            0 :          CPABORT("Partition not implemented.")
    1740              : 
    1741              :       END IF
    1742              : 
    1743          208 :       CALL timestop(handle)
    1744              : 
    1745          416 :    END SUBROUTINE fft3d_pb
    1746              : 
    1747              : ! **************************************************************************************************
    1748              : !> \brief ...
    1749              : !> \param sb ...
    1750              : !> \param group ...
    1751              : !> \param my_pos ...
    1752              : !> \param p2p ...
    1753              : !> \param yzp ...
    1754              : !> \param nray ...
    1755              : !> \param bo ...
    1756              : !> \param tb ...
    1757              : !> \param fft_scratch ...
    1758              : !> \par History
    1759              : !>      15. Feb. 2006 : single precision all_to_all
    1760              : !> \author JGH (14-Jan-2001)
    1761              : ! **************************************************************************************************
    1762      1874922 :    SUBROUTINE x_to_yz(sb, group, my_pos, p2p, yzp, nray, bo, tb, fft_scratch)
    1763              : 
    1764              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
    1765              :          INTENT(IN)                                      :: sb
    1766              : 
    1767              :       CLASS(mp_comm_type), INTENT(IN)                    :: group
    1768              :       INTEGER, INTENT(IN)                                :: my_pos
    1769              :       INTEGER, CONTIGUOUS, DIMENSION(0:), INTENT(IN)     :: p2p
    1770              :       INTEGER, CONTIGUOUS, DIMENSION(:, :, 0:), &
    1771              :          INTENT(IN)                                      :: yzp
    1772              :       INTEGER, CONTIGUOUS, DIMENSION(0:), INTENT(IN)     :: nray
    1773              :       INTEGER, CONTIGUOUS, DIMENSION(:, :, 0:), &
    1774              :          INTENT(IN)                                      :: bo
    1775              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :, :), &
    1776              :          INTENT(INOUT)                                   :: tb
    1777              :       TYPE(fft_scratch_type), INTENT(IN)                 :: fft_scratch
    1778              : 
    1779              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'x_to_yz'
    1780              : 
    1781              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
    1782      1874922 :          POINTER                                         :: rr
    1783              :       COMPLEX(KIND=sp), CONTIGUOUS, DIMENSION(:, :), &
    1784      1874922 :          POINTER                                         :: ss, tt
    1785              :       INTEGER                                            :: handle, ip, ir, ix, ixx, iy, iz, mpr, &
    1786              :                                                             nm, np, nr, nx
    1787      1874922 :       INTEGER, CONTIGUOUS, DIMENSION(:), POINTER         :: rcount, rdispl, scount, sdispl
    1788              : 
    1789      1874922 :       CALL timeset(routineN, handle)
    1790              : 
    1791      1874922 :       np = SIZE(p2p)
    1792      1874922 :       scount => fft_scratch%scount
    1793      1874922 :       rcount => fft_scratch%rcount
    1794      1874922 :       sdispl => fft_scratch%sdispl
    1795      1874922 :       rdispl => fft_scratch%rdispl
    1796              : 
    1797      1874922 :       IF (alltoall_sgl) THEN
    1798          230 :          ss => fft_scratch%ss
    1799          230 :          tt => fft_scratch%tt
    1800      3441099 :          ss(:, :) = CMPLX(sb(:, :), KIND=sp)
    1801          230 :          tt(:, :) = 0._sp
    1802              :       ELSE
    1803      1874692 :          rr => fft_scratch%rr
    1804              :       END IF
    1805              : 
    1806      1874922 :       mpr = p2p(my_pos)
    1807      5624766 :       nm = MAXVAL(nray(0:np - 1))
    1808      1874922 :       nr = nray(my_pos)
    1809              : !$OMP PARALLEL DO DEFAULT(NONE), &
    1810              : !$OMP             PRIVATE(ix,nx), &
    1811      1874922 : !$OMP             SHARED(np,p2p,bo,nr,scount,sdispl)
    1812              :       DO ip = 0, np - 1
    1813              :          ix = p2p(ip)
    1814              :          nx = bo(2, 1, ix) - bo(1, 1, ix) + 1
    1815              :          scount(ip) = nr*nx
    1816              :          sdispl(ip) = nr*(bo(1, 1, ix) - 1)
    1817              :       END DO
    1818              : !$OMP END PARALLEL DO
    1819      1874922 :       nx = bo(2, 1, mpr) - bo(1, 1, mpr) + 1
    1820              : !$OMP PARALLEL DO DEFAULT(NONE), &
    1821              : !$OMP             PRIVATE(nr), &
    1822      1874922 : !$OMP             SHARED(np,nray,nx,rcount,rdispl,nm)
    1823              :       DO ip = 0, np - 1
    1824              :          nr = nray(ip)
    1825              :          rcount(ip) = nr*nx
    1826              :          rdispl(ip) = nm*nx*ip
    1827              :       END DO
    1828              : !$OMP END PARALLEL DO
    1829      1874922 :       IF (alltoall_sgl) THEN
    1830          230 :          CALL group%alltoall(ss, scount, sdispl, tt, rcount, rdispl)
    1831              :       ELSE
    1832      1874692 :          CALL group%alltoall(sb, scount, sdispl, rr, rcount, rdispl)
    1833              :       END IF
    1834              : 
    1835      1874922 :       nx = bo(2, 1, mpr) - bo(1, 1, mpr) + 1
    1836              : !$OMP PARALLEL DO DEFAULT(NONE) COLLAPSE(2) &
    1837              : !$OMP             PRIVATE(ixx,ir,iy,iz,ix) &
    1838      1874922 : !$OMP             SHARED(np,nray,nx,alltoall_sgl,yzp,tt,rr,tb)
    1839              :       DO ip = 0, np - 1
    1840              :          DO ix = 1, nx
    1841              :             ixx = nray(ip)*(ix - 1)
    1842              :             IF (alltoall_sgl) THEN
    1843              :                DO ir = 1, nray(ip)
    1844              :                   iy = yzp(1, ir, ip)
    1845              :                   iz = yzp(2, ir, ip)
    1846              :                   tb(iy, iz, ix) = tt(ir + ixx, ip)
    1847              :                END DO
    1848              :             ELSE
    1849              :                DO ir = 1, nray(ip)
    1850              :                   iy = yzp(1, ir, ip)
    1851              :                   iz = yzp(2, ir, ip)
    1852              :                   tb(iy, iz, ix) = rr(ir + ixx, ip)
    1853              :                END DO
    1854              :             END IF
    1855              :          END DO
    1856              :       END DO
    1857              : !$OMP END PARALLEL DO
    1858              : 
    1859      1874922 :       CALL timestop(handle)
    1860              : 
    1861      1874922 :    END SUBROUTINE x_to_yz
    1862              : 
    1863              : ! **************************************************************************************************
    1864              : !> \brief ...
    1865              : !> \param tb ...
    1866              : !> \param group ...
    1867              : !> \param my_pos ...
    1868              : !> \param p2p ...
    1869              : !> \param yzp ...
    1870              : !> \param nray ...
    1871              : !> \param bo ...
    1872              : !> \param sb ...
    1873              : !> \param fft_scratch ...
    1874              : !> \par History
    1875              : !>      15. Feb. 2006 : single precision all_to_all
    1876              : !> \author JGH (14-Jan-2001)
    1877              : ! **************************************************************************************************
    1878      1871148 :    SUBROUTINE yz_to_x(tb, group, my_pos, p2p, yzp, nray, bo, sb, fft_scratch)
    1879              : 
    1880              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :, :), &
    1881              :          INTENT(IN)                                      :: tb
    1882              : 
    1883              :       CLASS(mp_comm_type), INTENT(IN)                    :: group
    1884              :       INTEGER, INTENT(IN)                                :: my_pos
    1885              :       INTEGER, CONTIGUOUS, DIMENSION(0:), INTENT(IN)     :: p2p
    1886              :       INTEGER, CONTIGUOUS, DIMENSION(:, :, 0:), &
    1887              :          INTENT(IN)                                      :: yzp
    1888              :       INTEGER, CONTIGUOUS, DIMENSION(0:), INTENT(IN)     :: nray
    1889              :       INTEGER, DIMENSION(:, :, 0:), INTENT(IN)           :: bo
    1890              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
    1891              :          INTENT(INOUT)                                   :: sb
    1892              :       TYPE(fft_scratch_type), INTENT(IN)                 :: fft_scratch
    1893              : 
    1894              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'yz_to_x'
    1895              : 
    1896              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
    1897      1871148 :          POINTER                                         :: rr
    1898              :       COMPLEX(KIND=sp), CONTIGUOUS, DIMENSION(:, :), &
    1899      1871148 :          POINTER                                         :: ss, tt
    1900              :       INTEGER                                            :: handle, ip, ir, ix, ixx, iy, iz, mpr, &
    1901              :                                                             nm, np, nr, nx
    1902      1871148 :       INTEGER, CONTIGUOUS, DIMENSION(:), POINTER         :: rcount, rdispl, scount, sdispl
    1903              : 
    1904      1871148 :       CALL timeset(routineN, handle)
    1905              : 
    1906      1871148 :       np = SIZE(p2p)
    1907      1871148 :       mpr = p2p(my_pos)
    1908      1871148 :       scount => fft_scratch%scount
    1909      1871148 :       rcount => fft_scratch%rcount
    1910      1871148 :       sdispl => fft_scratch%sdispl
    1911      1871148 :       rdispl => fft_scratch%rdispl
    1912              : 
    1913      1871148 :       IF (alltoall_sgl) THEN
    1914          238 :          ss => fft_scratch%ss
    1915          238 :          tt => fft_scratch%tt
    1916          238 :          ss = 0._sp
    1917          238 :          tt = 0._sp
    1918              :       ELSE
    1919      1870910 :          rr => fft_scratch%rr
    1920              :       END IF
    1921              : 
    1922      1871148 :       nx = bo(2, 1, mpr) - bo(1, 1, mpr) + 1
    1923              : !$OMP PARALLEL DO DEFAULT(NONE) COLLAPSE(2) &
    1924              : !$OMP             PRIVATE(ip, ixx, ir, iy, iz, ix) &
    1925      1871148 : !$OMP             SHARED(np,nray,nx,alltoall_sgl,yzp,tb,tt,rr)
    1926              :       DO ip = 0, np - 1
    1927              :          DO ix = 1, nx
    1928              :             ixx = nray(ip)*(ix - 1)
    1929              :             IF (alltoall_sgl) THEN
    1930              :                DO ir = 1, nray(ip)
    1931              :                   iy = yzp(1, ir, ip)
    1932              :                   iz = yzp(2, ir, ip)
    1933              :                   tt(ir + ixx, ip) = CMPLX(tb(iy, iz, ix), KIND=sp)
    1934              :                END DO
    1935              :             ELSE
    1936              :                DO ir = 1, nray(ip)
    1937              :                   iy = yzp(1, ir, ip)
    1938              :                   iz = yzp(2, ir, ip)
    1939              :                   rr(ir + ixx, ip) = tb(iy, iz, ix)
    1940              :                END DO
    1941              :             END IF
    1942              :          END DO
    1943              :       END DO
    1944              : !$OMP END PARALLEL DO
    1945      5613444 :       nm = MAXVAL(nray(0:np - 1))
    1946      1871148 :       nr = nray(my_pos)
    1947              : !$OMP PARALLEL DO DEFAULT(NONE), &
    1948              : !$OMP             PRIVATE(ix,nx), &
    1949      1871148 : !$OMP             SHARED(np,p2p,bo,rcount,rdispl,nr)
    1950              :       DO ip = 0, np - 1
    1951              :          ix = p2p(ip)
    1952              :          nx = bo(2, 1, ix) - bo(1, 1, ix) + 1
    1953              :          rcount(ip) = nr*nx
    1954              :          rdispl(ip) = nr*(bo(1, 1, ix) - 1)
    1955              :       END DO
    1956              : !$OMP END PARALLEL DO
    1957      1871148 :       nx = bo(2, 1, mpr) - bo(1, 1, mpr) + 1
    1958              : !$OMP PARALLEL DO DEFAULT(NONE), &
    1959              : !$OMP             PRIVATE(nr), &
    1960      1871148 : !$OMP             SHARED(np,nray,scount,sdispl,nx,nm)
    1961              :       DO ip = 0, np - 1
    1962              :          nr = nray(ip)
    1963              :          scount(ip) = nr*nx
    1964              :          sdispl(ip) = nm*nx*ip
    1965              :       END DO
    1966              : !$OMP END PARALLEL DO
    1967              : 
    1968      1871148 :       IF (alltoall_sgl) THEN
    1969          238 :          CALL group%alltoall(tt, scount, sdispl, ss, rcount, rdispl)
    1970      3703835 :          sb = ss
    1971              :       ELSE
    1972      1870910 :          CALL group%alltoall(rr, scount, sdispl, sb, rcount, rdispl)
    1973              :       END IF
    1974              : 
    1975      1871148 :       CALL timestop(handle)
    1976              : 
    1977      1871148 :    END SUBROUTINE yz_to_x
    1978              : 
    1979              : ! **************************************************************************************************
    1980              : !> \brief ...
    1981              : !> \param sb ...
    1982              : !> \param group ...
    1983              : !> \param dims ...
    1984              : !> \param my_pos ...
    1985              : !> \param p2p ...
    1986              : !> \param yzp ...
    1987              : !> \param nray ...
    1988              : !> \param bo ...
    1989              : !> \param tb ...
    1990              : !> \param fft_scratch ...
    1991              : !> \par History
    1992              : !>      15. Feb. 2006 : single precision all_to_all
    1993              : !> \author JGH (18-Jan-2001)
    1994              : ! **************************************************************************************************
    1995            0 :    SUBROUTINE yz_to_xz(sb, group, dims, my_pos, p2p, yzp, nray, bo, tb, fft_scratch)
    1996              : 
    1997              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
    1998              :          INTENT(IN)                                      :: sb
    1999              : 
    2000              :       CLASS(mp_comm_type), INTENT(IN)                    :: group
    2001              :       INTEGER, DIMENSION(2), INTENT(IN)                  :: dims
    2002              :       INTEGER, INTENT(IN)                                :: my_pos
    2003              :       INTEGER, CONTIGUOUS, DIMENSION(0:), INTENT(IN)                 :: p2p
    2004              :       INTEGER, CONTIGUOUS, DIMENSION(:, :, 0:), INTENT(IN)           :: yzp
    2005              :       INTEGER, CONTIGUOUS, DIMENSION(0:), INTENT(IN)                 :: nray
    2006              :       INTEGER, CONTIGUOUS, DIMENSION(:, :, 0:), INTENT(IN)           :: bo
    2007              :       COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(INOUT), CONTIGUOUS   :: tb
    2008              :       TYPE(fft_scratch_type), INTENT(INOUT)              :: fft_scratch
    2009              : 
    2010              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'yz_to_xz'
    2011              : 
    2012            0 :       COMPLEX(KIND=dp), DIMENSION(:), POINTER, CONTIGUOUS            :: xzbuf, yzbuf
    2013            0 :       COMPLEX(KIND=sp), DIMENSION(:), POINTER, CONTIGUOUS            :: xzbuf_sgl, yzbuf_sgl
    2014              :       INTEGER                                            :: handle, icrs, ip, ipl, ipr, ir, ix, iz, &
    2015              :                                                             jj, jx, jy, jz, myx, myz, np, npx, &
    2016              :                                                             npz, nx, nz, rs_pos
    2017            0 :       INTEGER, DIMENSION(:), POINTER, CONTIGUOUS                     :: pzcoord, rcount, rdispl, scount, sdispl, &
    2018            0 :                                                                         xcor, zcor
    2019            0 :       INTEGER, DIMENSION(:, :), CONTIGUOUS, POINTER                  :: pgrid
    2020              : 
    2021            0 :       CALL timeset(routineN, handle)
    2022              : 
    2023            0 :       np = SIZE(p2p)
    2024              : 
    2025            0 :       rs_pos = p2p(my_pos)
    2026              : 
    2027            0 :       IF (alltoall_sgl) THEN
    2028            0 :          yzbuf_sgl => fft_scratch%yzbuf_sgl
    2029            0 :          xzbuf_sgl => fft_scratch%xzbuf_sgl
    2030              :       ELSE
    2031            0 :          yzbuf => fft_scratch%yzbuf
    2032            0 :          xzbuf => fft_scratch%xzbuf
    2033              :       END IF
    2034            0 :       npx = dims(1)
    2035            0 :       npz = dims(2)
    2036            0 :       pgrid => fft_scratch%pgrid
    2037            0 :       xcor => fft_scratch%xcor
    2038            0 :       zcor => fft_scratch%zcor
    2039            0 :       pzcoord => fft_scratch%pzcoord
    2040            0 :       scount => fft_scratch%scount
    2041            0 :       rcount => fft_scratch%rcount
    2042            0 :       sdispl => fft_scratch%sdispl
    2043            0 :       rdispl => fft_scratch%rdispl
    2044              : 
    2045            0 :       nx = SIZE(sb, 2)
    2046              : 
    2047              : ! If the send and recv counts are not already cached, then
    2048              : ! calculate and store them
    2049            0 :       IF (fft_scratch%in == 0) THEN
    2050              : 
    2051            0 :          scount = 0
    2052              : 
    2053            0 :          DO ix = 0, npx - 1
    2054            0 :             ip = pgrid(ix, 0)
    2055            0 :             xcor(bo(1, 1, ip):bo(2, 1, ip)) = ix
    2056              :          END DO
    2057            0 :          DO iz = 0, npz - 1
    2058            0 :             ip = pgrid(0, iz)
    2059            0 :             zcor(bo(1, 3, ip):bo(2, 3, ip)) = iz
    2060              :          END DO
    2061            0 :          DO jx = 1, nx
    2062            0 :             IF (alltoall_sgl) THEN
    2063            0 :                DO ir = 1, nray(my_pos)
    2064            0 :                   jy = yzp(1, ir, my_pos)
    2065            0 :                   jz = yzp(2, ir, my_pos)
    2066            0 :                   ip = pgrid(xcor(jx), zcor(jz))
    2067            0 :                   scount(ip) = scount(ip) + 1
    2068              :                END DO
    2069              :             ELSE
    2070            0 :                DO ir = 1, nray(my_pos)
    2071            0 :                   jy = yzp(1, ir, my_pos)
    2072            0 :                   jz = yzp(2, ir, my_pos)
    2073            0 :                   ip = pgrid(xcor(jx), zcor(jz))
    2074            0 :                   scount(ip) = scount(ip) + 1
    2075              :                END DO
    2076              :             END IF
    2077              :          END DO
    2078              : 
    2079            0 :          CALL group%alltoall(scount, rcount, 1)
    2080            0 :          fft_scratch%yzcount = scount
    2081            0 :          fft_scratch%xzcount = rcount
    2082              : 
    2083              :          ! Work out the correct displacements in the buffers
    2084            0 :          sdispl(0) = 0
    2085            0 :          rdispl(0) = 0
    2086            0 :          DO ip = 1, np - 1
    2087            0 :             sdispl(ip) = sdispl(ip - 1) + scount(ip - 1)
    2088            0 :             rdispl(ip) = rdispl(ip - 1) + rcount(ip - 1)
    2089              :          END DO
    2090              : 
    2091            0 :          fft_scratch%yzdispl = sdispl
    2092            0 :          fft_scratch%xzdispl = rdispl
    2093              : 
    2094            0 :          icrs = 0
    2095            0 :          DO ip = 0, np - 1
    2096            0 :             IF (scount(ip) /= 0) icrs = icrs + 1
    2097            0 :             IF (rcount(ip) /= 0) icrs = icrs + 1
    2098              :          END DO
    2099            0 :          CALL group%sum(icrs)
    2100            0 :          fft_scratch%rsratio = REAL(icrs, KIND=dp)/(REAL(2*np, KIND=dp)*REAL(np, KIND=dp))
    2101              : 
    2102            0 :          fft_scratch%in = 1
    2103              :       ELSE
    2104            0 :          scount = fft_scratch%yzcount
    2105            0 :          rcount = fft_scratch%xzcount
    2106            0 :          sdispl = fft_scratch%yzdispl
    2107            0 :          rdispl = fft_scratch%xzdispl
    2108              :       END IF
    2109              : 
    2110              : ! Do the actual packing
    2111              : !$OMP PARALLEL DO DEFAULT(NONE), &
    2112              : !$OMP             PRIVATE(ipl,jj,nx,ir,jx,jy,jz),&
    2113              : !$OMP             SHARED(np,p2p,pzcoord,bo,nray,yzp,zcor),&
    2114              : !$OMP             SHARED(yzbuf,sb,scount,sdispl,my_pos),&
    2115            0 : !$OMP             SHARED(yzbuf_sgl,alltoall_sgl)
    2116              :       DO ip = 0, np - 1
    2117              :          IF (scount(ip) == 0) CYCLE
    2118              :          ipl = p2p(ip)
    2119              :          jj = 0
    2120              :          nx = bo(2, 1, ipl) - bo(1, 1, ipl) + 1
    2121              :          DO ir = 1, nray(my_pos)
    2122              :             jz = yzp(2, ir, my_pos)
    2123              :             IF (zcor(jz) == pzcoord(ipl)) THEN
    2124              :                jj = jj + 1
    2125              :                jy = yzp(1, ir, my_pos)
    2126              :                IF (alltoall_sgl) THEN
    2127              :                   DO jx = 0, nx - 1
    2128              :                      yzbuf_sgl(sdispl(ip) + jj + jx*scount(ip)/nx) = CMPLX(sb(ir, jx + bo(1, 1, ipl)), KIND=sp)
    2129              :                   END DO
    2130              :                ELSE
    2131              :                   DO jx = 0, nx - 1
    2132              :                      yzbuf(sdispl(ip) + jj + jx*scount(ip)/nx) = sb(ir, jx + bo(1, 1, ipl))
    2133              :                   END DO
    2134              :                END IF
    2135              :             END IF
    2136              :          END DO
    2137              :       END DO
    2138              : !$OMP END PARALLEL DO
    2139              : 
    2140            0 :       IF (alltoall_sgl) THEN
    2141            0 :          CALL group%alltoall(yzbuf_sgl, scount, sdispl, xzbuf_sgl, rcount, rdispl)
    2142              :       ELSE
    2143            0 :          IF (fft_scratch%rsratio < ratio_sparse_alltoall) THEN
    2144            0 :             CALL sparse_alltoall(yzbuf, scount, sdispl, xzbuf, rcount, rdispl, group)
    2145              :          ELSE
    2146            0 :             CALL group%alltoall(yzbuf, scount, sdispl, xzbuf, rcount, rdispl)
    2147              :          END IF
    2148              :       END IF
    2149              : 
    2150            0 :       myx = fft_scratch%sizes%r_pos(1)
    2151            0 :       myz = fft_scratch%sizes%r_pos(2)
    2152            0 :       nz = bo(2, 3, rs_pos) - bo(1, 3, rs_pos) + 1
    2153              : 
    2154              : !$OMP PARALLEL DO DEFAULT(NONE), &
    2155              : !$OMP             PRIVATE(ipr,jj,ir,jx,jy,jz),&
    2156              : !$OMP             SHARED(tb,np,p2p,bo,rs_pos,nray),&
    2157              : !$OMP             SHARED(yzp,alltoall_sgl,zcor,myz),&
    2158            0 : !$OMP             SHARED(xzbuf,xzbuf_sgl,nz,rdispl)
    2159              :       DO ip = 0, np - 1
    2160              :          ipr = p2p(ip)
    2161              :          jj = 0
    2162              :          DO jx = 0, bo(2, 1, rs_pos) - bo(1, 1, rs_pos)
    2163              :             DO ir = 1, nray(ip)
    2164              :                jz = yzp(2, ir, ip)
    2165              :                IF (alltoall_sgl) THEN
    2166              :                   IF (zcor(jz) == myz) THEN
    2167              :                      jj = jj + 1
    2168              :                      jy = yzp(1, ir, ip)
    2169              :                      jz = jz - bo(1, 3, rs_pos) + 1
    2170              :                      tb(jy, jz + jx*nz) = xzbuf_sgl(jj + rdispl(ipr))
    2171              :                   END IF
    2172              :                ELSE
    2173              :                   IF (zcor(jz) == myz) THEN
    2174              :                      jj = jj + 1
    2175              :                      jy = yzp(1, ir, ip)
    2176              :                      jz = jz - bo(1, 3, rs_pos) + 1
    2177              :                      tb(jy, jz + jx*nz) = xzbuf(jj + rdispl(ipr))
    2178              :                   END IF
    2179              :                END IF
    2180              :             END DO
    2181              :          END DO
    2182              :       END DO
    2183              : !$OMP END PARALLEL DO
    2184              : 
    2185            0 :       CALL timestop(handle)
    2186              : 
    2187            0 :    END SUBROUTINE yz_to_xz
    2188              : 
    2189              : ! **************************************************************************************************
    2190              : !> \brief ...
    2191              : !> \param sb ...
    2192              : !> \param group ...
    2193              : !> \param dims ...
    2194              : !> \param my_pos ...
    2195              : !> \param p2p ...
    2196              : !> \param yzp ...
    2197              : !> \param nray ...
    2198              : !> \param bo ...
    2199              : !> \param tb ...
    2200              : !> \param fft_scratch ...
    2201              : !> \par History
    2202              : !>      15. Feb. 2006 : single precision all_to_all
    2203              : !> \author JGH (19-Jan-2001)
    2204              : ! **************************************************************************************************
    2205            0 :    SUBROUTINE xz_to_yz(sb, group, dims, my_pos, p2p, yzp, nray, bo, tb, fft_scratch)
    2206              : 
    2207              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
    2208              :          INTENT(IN)                                      :: sb
    2209              : 
    2210              :       CLASS(mp_comm_type), INTENT(IN)                    :: group
    2211              :       INTEGER, DIMENSION(2), INTENT(IN)                  :: dims
    2212              :       INTEGER, INTENT(IN)                                :: my_pos
    2213              :       INTEGER, CONTIGUOUS, DIMENSION(0:), INTENT(IN)                 :: p2p
    2214              :       INTEGER, CONTIGUOUS, DIMENSION(:, :, 0:), INTENT(IN)           :: yzp
    2215              :       INTEGER, CONTIGUOUS, DIMENSION(0:), INTENT(IN)                 :: nray
    2216              :       INTEGER, CONTIGUOUS, DIMENSION(:, :, 0:), INTENT(IN)           :: bo
    2217              :       COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(INOUT), CONTIGUOUS   :: tb
    2218              :       TYPE(fft_scratch_type), INTENT(INOUT)              :: fft_scratch
    2219              : 
    2220              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'xz_to_yz'
    2221              : 
    2222            0 :       COMPLEX(KIND=dp), DIMENSION(:), POINTER, CONTIGUOUS            :: xzbuf, yzbuf
    2223            0 :       COMPLEX(KIND=sp), DIMENSION(:), POINTER, CONTIGUOUS            :: xzbuf_sgl, yzbuf_sgl
    2224              :       INTEGER                                            :: handle, icrs, ip, ipl, ir, ix, ixx, iz, &
    2225              :                                                             jj, jx, jy, jz, mp, myx, myz, np, npx, &
    2226              :                                                             npz, nx, nz
    2227            0 :       INTEGER, DIMENSION(:), POINTER, CONTIGUOUS                     :: pzcoord, rcount, rdispl, scount, sdispl, &
    2228            0 :                                                                         xcor, zcor
    2229            0 :       INTEGER, DIMENSION(:, :), CONTIGUOUS, POINTER                  :: pgrid
    2230              : 
    2231            0 :       CALL timeset(routineN, handle)
    2232              : 
    2233            0 :       np = SIZE(p2p)
    2234              : 
    2235            0 :       IF (alltoall_sgl) THEN
    2236            0 :          yzbuf_sgl => fft_scratch%yzbuf_sgl
    2237            0 :          xzbuf_sgl => fft_scratch%xzbuf_sgl
    2238              :       ELSE
    2239            0 :          yzbuf => fft_scratch%yzbuf
    2240            0 :          xzbuf => fft_scratch%xzbuf
    2241              :       END IF
    2242            0 :       npx = dims(1)
    2243            0 :       npz = dims(2)
    2244            0 :       pgrid => fft_scratch%pgrid
    2245            0 :       xcor => fft_scratch%xcor
    2246            0 :       zcor => fft_scratch%zcor
    2247            0 :       pzcoord => fft_scratch%pzcoord
    2248            0 :       scount => fft_scratch%scount
    2249            0 :       rcount => fft_scratch%rcount
    2250            0 :       sdispl => fft_scratch%sdispl
    2251            0 :       rdispl => fft_scratch%rdispl
    2252              : 
    2253              : ! If the send and recv counts are not already cached, then
    2254              : ! calculate and store them
    2255            0 :       IF (fft_scratch%in == 0) THEN
    2256              : 
    2257            0 :          rcount = 0
    2258            0 :          nx = MAXVAL(bo(2, 1, :))
    2259              : 
    2260            0 :          DO ix = 0, npx - 1
    2261            0 :             ip = pgrid(ix, 0)
    2262            0 :             xcor(bo(1, 1, ip):bo(2, 1, ip)) = ix
    2263              :          END DO
    2264            0 :          DO iz = 0, npz - 1
    2265            0 :             ip = pgrid(0, iz)
    2266            0 :             zcor(bo(1, 3, ip):bo(2, 3, ip)) = iz
    2267              :          END DO
    2268            0 :          DO jx = 1, nx
    2269            0 :             DO ir = 1, nray(my_pos)
    2270            0 :                jy = yzp(1, ir, my_pos)
    2271            0 :                jz = yzp(2, ir, my_pos)
    2272            0 :                ip = pgrid(xcor(jx), zcor(jz))
    2273            0 :                rcount(ip) = rcount(ip) + 1
    2274              :             END DO
    2275              :          END DO
    2276              : 
    2277            0 :          CALL group%alltoall(rcount, scount, 1)
    2278            0 :          fft_scratch%xzcount = scount
    2279            0 :          fft_scratch%yzcount = rcount
    2280              : 
    2281              :          ! Work out the correct displacements in the buffers
    2282            0 :          sdispl(0) = 0
    2283            0 :          rdispl(0) = 0
    2284            0 :          DO ip = 1, np - 1
    2285            0 :             sdispl(ip) = sdispl(ip - 1) + scount(ip - 1)
    2286            0 :             rdispl(ip) = rdispl(ip - 1) + rcount(ip - 1)
    2287              :          END DO
    2288              : 
    2289            0 :          fft_scratch%xzdispl = sdispl
    2290            0 :          fft_scratch%yzdispl = rdispl
    2291              : 
    2292            0 :          icrs = 0
    2293            0 :          DO ip = 0, np - 1
    2294            0 :             IF (scount(ip) /= 0) icrs = icrs + 1
    2295            0 :             IF (rcount(ip) /= 0) icrs = icrs + 1
    2296              :          END DO
    2297            0 :          CALL group%sum(icrs)
    2298            0 :          fft_scratch%rsratio = REAL(icrs, KIND=dp)/(REAL(2*np, KIND=dp)*REAL(np, KIND=dp))
    2299              : 
    2300            0 :          fft_scratch%in = 1
    2301              :       ELSE
    2302            0 :          scount = fft_scratch%xzcount
    2303            0 :          rcount = fft_scratch%yzcount
    2304            0 :          sdispl = fft_scratch%xzdispl
    2305            0 :          rdispl = fft_scratch%yzdispl
    2306              :       END IF
    2307              : 
    2308              : ! Now do the actual packing
    2309            0 :       myx = fft_scratch%sizes%r_pos(1)
    2310            0 :       myz = fft_scratch%sizes%r_pos(2)
    2311            0 :       mp = p2p(my_pos)
    2312            0 :       nz = bo(2, 3, mp) - bo(1, 3, mp) + 1
    2313            0 :       nx = bo(2, 1, mp) - bo(1, 1, mp) + 1
    2314              : 
    2315              : !$OMP PARALLEL DO DEFAULT(NONE), &
    2316              : !$OMP             PRIVATE(jj,ipl,ir,jx,jy,jz,ixx),&
    2317              : !$OMP             SHARED(np,p2p,nray,yzp,zcor,myz,bo,mp),&
    2318              : !$OMP             SHARED(alltoall_sgl,nx,scount,sdispl),&
    2319            0 : !$OMP             SHARED(xzbuf,xzbuf_sgl,sb,nz)
    2320              :       DO ip = 0, np - 1
    2321              :          jj = 0
    2322              :          ipl = p2p(ip)
    2323              :          DO ir = 1, nray(ip)
    2324              :             jz = yzp(2, ir, ip)
    2325              :             IF (zcor(jz) == myz) THEN
    2326              :                jj = jj + 1
    2327              :                jy = yzp(1, ir, ip)
    2328              :                jz = yzp(2, ir, ip) - bo(1, 3, mp) + 1
    2329              :                IF (alltoall_sgl) THEN
    2330              :                   DO jx = 0, nx - 1
    2331              :                      ixx = jj + jx*scount(ipl)/nx
    2332              :                      xzbuf_sgl(ixx + sdispl(ipl)) = CMPLX(sb(jy, jz + jx*nz), KIND=sp)
    2333              :                   END DO
    2334              :                ELSE
    2335              :                   DO jx = 0, nx - 1
    2336              :                      ixx = jj + jx*scount(ipl)/nx
    2337              :                      xzbuf(ixx + sdispl(ipl)) = sb(jy, jz + jx*nz)
    2338              :                   END DO
    2339              :                END IF
    2340              :             END IF
    2341              :          END DO
    2342              :       END DO
    2343              : !$OMP END PARALLEL DO
    2344              : 
    2345            0 :       IF (alltoall_sgl) THEN
    2346            0 :          CALL group%alltoall(xzbuf_sgl, scount, sdispl, yzbuf_sgl, rcount, rdispl)
    2347              :       ELSE
    2348            0 :          IF (fft_scratch%rsratio < ratio_sparse_alltoall) THEN
    2349            0 :             CALL sparse_alltoall(xzbuf, scount, sdispl, yzbuf, rcount, rdispl, group)
    2350              :          ELSE
    2351            0 :             CALL group%alltoall(xzbuf, scount, sdispl, yzbuf, rcount, rdispl)
    2352              :          END IF
    2353              :       END IF
    2354              : 
    2355              : !$OMP PARALLEL DO DEFAULT(NONE), &
    2356              : !$OMP             PRIVATE(ipl,jj,nx,ir,jx,jy,jz),&
    2357              : !$OMP             SHARED(p2p,pzcoord,bo,nray,my_pos,yzp),&
    2358              : !$OMP             SHARED(rcount,rdispl,tb,yzbuf,zcor),&
    2359            0 : !$OMP             SHARED(yzbuf_sgl,alltoall_sgl,np)
    2360              :       DO ip = 0, np - 1
    2361              :          IF (rcount(ip) == 0) CYCLE
    2362              :          ipl = p2p(ip)
    2363              :          jj = 0
    2364              :          nx = bo(2, 1, ipl) - bo(1, 1, ipl) + 1
    2365              :          DO ir = 1, nray(my_pos)
    2366              :             jz = yzp(2, ir, my_pos)
    2367              :             IF (zcor(jz) == pzcoord(ipl)) THEN
    2368              :                jj = jj + 1
    2369              :                jy = yzp(1, ir, my_pos)
    2370              :                IF (alltoall_sgl) THEN
    2371              :                   DO jx = 0, nx - 1
    2372              :                      tb(ir, jx + bo(1, 1, ipl)) = yzbuf_sgl(rdispl(ip) + jj + jx*rcount(ip)/nx)
    2373              :                   END DO
    2374              :                ELSE
    2375              :                   DO jx = 0, nx - 1
    2376              :                      tb(ir, jx + bo(1, 1, ipl)) = yzbuf(rdispl(ip) + jj + jx*rcount(ip)/nx)
    2377              :                   END DO
    2378              :                END IF
    2379              :             END IF
    2380              :          END DO
    2381              :       END DO
    2382              : !$OMP END PARALLEL DO
    2383              : 
    2384            0 :       CALL timestop(handle)
    2385              : 
    2386            0 :    END SUBROUTINE xz_to_yz
    2387              : 
    2388              : ! **************************************************************************************************
    2389              : !> \brief ...
    2390              : !> \param cin ...
    2391              : !> \param boin ...
    2392              : !> \param boout ...
    2393              : !> \param sout ...
    2394              : !> \param fft_scratch ...
    2395              : !> \par History
    2396              : !>      none
    2397              : !> \author JGH (20-Jan-2001)
    2398              : ! **************************************************************************************************
    2399            0 :    SUBROUTINE cube_transpose_1(cin, boin, boout, sout, fft_scratch)
    2400              : 
    2401              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
    2402              :          INTENT(IN)                                      :: cin
    2403              :       INTEGER, CONTIGUOUS, DIMENSION(:, :, 0:), &
    2404              :          INTENT(IN)                                      :: boin, boout
    2405              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
    2406              :          INTENT(OUT)                                     :: sout
    2407              :       TYPE(fft_scratch_type), INTENT(IN)                 :: fft_scratch
    2408              : 
    2409              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'cube_transpose_1'
    2410              : 
    2411              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
    2412            0 :          POINTER                                         :: rbuf
    2413              :       INTEGER                                            :: handle, ip, ipl, ir, is, ixy, iz, mip, &
    2414              :                                                             mz, np, nx, ny, nz
    2415            0 :       INTEGER, CONTIGUOUS, DIMENSION(:), POINTER         :: rcount, rdispl, scount, sdispl
    2416            0 :       INTEGER, CONTIGUOUS, DIMENSION(:, :), POINTER      :: pgrid
    2417              :       INTEGER, DIMENSION(2)                              :: dim, pos
    2418              : 
    2419            0 :       CALL timeset(routineN, handle)
    2420              : 
    2421            0 :       mip = fft_scratch%mip
    2422            0 :       dim = fft_scratch%dim
    2423            0 :       pos = fft_scratch%pos
    2424            0 :       scount => fft_scratch%scount
    2425            0 :       rcount => fft_scratch%rcount
    2426            0 :       sdispl => fft_scratch%sdispl
    2427            0 :       rdispl => fft_scratch%rdispl
    2428            0 :       pgrid => fft_scratch%pgcube
    2429            0 :       np = DIM(2)
    2430              : 
    2431            0 :       nx = boin(2, 1, mip) - boin(1, 1, mip) + 1
    2432            0 :       nz = boin(2, 3, mip) - boin(1, 3, mip) + 1
    2433              : 
    2434              : !$OMP PARALLEL DO DEFAULT(NONE), &
    2435              : !$OMP             PRIVATE(ipl,ny), &
    2436            0 : !$OMP             SHARED(np,pgrid,boout,scount,sdispl,nx,nz)
    2437              :       DO ip = 0, np - 1
    2438              :          ipl = pgrid(ip, 2)
    2439              :          ny = boout(2, 2, ipl) - boout(1, 2, ipl) + 1
    2440              :          scount(ip) = nx*nz*ny
    2441              :          sdispl(ip) = nx*nz*(boout(1, 2, ipl) - 1)
    2442              :       END DO
    2443              : !$OMP END PARALLEL DO
    2444            0 :       ny = boout(2, 2, mip) - boout(1, 2, mip) + 1
    2445            0 :       mz = MAXVAL(boin(2, 3, :) - boin(1, 3, :) + 1)
    2446              : !$OMP PARALLEL DO DEFAULT(NONE), &
    2447              : !$OMP             PRIVATE(ipl,nz), &
    2448            0 : !$OMP             SHARED(np,pgrid,boin,nx,ny,rcount,rdispl,mz)
    2449              :       DO ip = 0, np - 1
    2450              :          ipl = pgrid(ip, 2)
    2451              :          nz = boin(2, 3, ipl) - boin(1, 3, ipl) + 1
    2452              :          rcount(ip) = nx*nz*ny
    2453              :          rdispl(ip) = nx*ny*mz*ip
    2454              :       END DO
    2455              : !$OMP END PARALLEL DO
    2456              : 
    2457            0 :       rbuf => fft_scratch%rbuf1
    2458              : 
    2459            0 :       CALL fft_scratch%cart_sub_comm(2)%alltoall(cin, scount, sdispl, rbuf, rcount, rdispl)
    2460              : 
    2461              : !$OMP PARALLEL DO DEFAULT(NONE) COLLAPSE(2) &
    2462              : !$OMP             PRIVATE(ip,ipl,nz,iz,is,ir) &
    2463            0 : !$OMP             SHARED(nx,ny,np,pgrid,boin,sout,rbuf)
    2464              :       DO ixy = 1, nx*ny
    2465              :          DO ip = 0, np - 1
    2466              :             ipl = pgrid(ip, 2)
    2467              :             nz = boin(2, 3, ipl) - boin(1, 3, ipl) + 1
    2468              :             DO iz = 1, nz
    2469              :                is = boin(1, 3, ipl) + iz - 1
    2470              :                ir = iz + nz*(ixy - 1)
    2471              :                sout(is, ixy) = rbuf(ir, ip)
    2472              :             END DO
    2473              :          END DO
    2474              :       END DO
    2475              : !$OMP END PARALLEL DO
    2476              : 
    2477            0 :       CALL timestop(handle)
    2478              : 
    2479            0 :    END SUBROUTINE cube_transpose_1
    2480              : 
    2481              : ! **************************************************************************************************
    2482              : !> \brief ...
    2483              : !> \param cin ...
    2484              : !> \param boin ...
    2485              : !> \param boout ...
    2486              : !> \param sout ...
    2487              : !> \param fft_scratch ...
    2488              : ! **************************************************************************************************
    2489            0 :    SUBROUTINE cube_transpose_2(cin, boin, boout, sout, fft_scratch)
    2490              : 
    2491              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
    2492              :          INTENT(IN)                                      :: cin
    2493              :       INTEGER, CONTIGUOUS, DIMENSION(:, :, 0:), &
    2494              :          INTENT(IN)                                      :: boin, boout
    2495              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
    2496              :          INTENT(OUT)                                     :: sout
    2497              :       TYPE(fft_scratch_type), INTENT(IN)                 :: fft_scratch
    2498              : 
    2499              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'cube_transpose_2'
    2500              : 
    2501              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
    2502            0 :          POINTER                                         :: rbuf
    2503              :       INTEGER                                            :: handle, ip, ipl, ir, ixy, iz, mip, mz, &
    2504              :                                                             np, nx, ny, nz
    2505            0 :       INTEGER, CONTIGUOUS, DIMENSION(:), POINTER         :: rcount, rdispl, scount, sdispl
    2506            0 :       INTEGER, CONTIGUOUS, DIMENSION(:, :), POINTER      :: pgrid
    2507              :       INTEGER, DIMENSION(2)                              :: dim, pos
    2508              :       TYPE(mp_comm_type)                                 :: sub_group
    2509              : 
    2510            0 :       CALL timeset(routineN, handle)
    2511              : 
    2512            0 :       sub_group = fft_scratch%cart_sub_comm(2)
    2513            0 :       mip = fft_scratch%mip
    2514            0 :       dim = fft_scratch%dim
    2515            0 :       pos = fft_scratch%pos
    2516            0 :       scount => fft_scratch%scount
    2517            0 :       rcount => fft_scratch%rcount
    2518            0 :       sdispl => fft_scratch%sdispl
    2519            0 :       rdispl => fft_scratch%rdispl
    2520            0 :       pgrid => fft_scratch%pgcube
    2521            0 :       np = DIM(2)
    2522              : 
    2523            0 :       nx = boin(2, 1, mip) - boin(1, 1, mip) + 1
    2524            0 :       ny = boin(2, 2, mip) - boin(1, 2, mip) + 1
    2525            0 :       mz = MAXVAL(boout(2, 3, :) - boout(1, 3, :) + 1)
    2526              : 
    2527            0 :       rbuf => fft_scratch%rbuf2
    2528              : 
    2529              : !$OMP PARALLEL DEFAULT(NONE), &
    2530              : !$OMP          PRIVATE(ip,ipl,nz,iz,ir), &
    2531            0 : !$OMP          SHARED(nx,ny,np,pgrid,boout,rbuf,cin,scount,sdispl,mz)
    2532              : !$OMP DO COLLAPSE(2)
    2533              :       DO ixy = 1, nx*ny
    2534              :          DO ip = 0, np - 1
    2535              :             ipl = pgrid(ip, 2)
    2536              :             nz = boout(2, 3, ipl) - boout(1, 3, ipl) + 1
    2537              :             DO iz = boout(1, 3, ipl), boout(2, 3, ipl)
    2538              :                ir = iz - boout(1, 3, ipl) + 1 + (ixy - 1)*nz
    2539              :                rbuf(ir, ip) = cin(iz, ixy)
    2540              :             END DO
    2541              :          END DO
    2542              :       END DO
    2543              : !$OMP END DO
    2544              : !$OMP DO
    2545              :       DO ip = 0, np - 1
    2546              :          ipl = pgrid(ip, 2)
    2547              :          nz = boout(2, 3, ipl) - boout(1, 3, ipl) + 1
    2548              :          scount(ip) = nx*ny*nz
    2549              :          sdispl(ip) = nx*ny*mz*ip
    2550              :       END DO
    2551              : !$OMP END DO
    2552              : !$OMP END PARALLEL
    2553            0 :       nz = boout(2, 3, mip) - boout(1, 3, mip) + 1
    2554              : !$OMP PARALLEL DO DEFAULT(NONE), &
    2555              : !$OMP             PRIVATE(ipl,ny), &
    2556            0 : !$OMP             SHARED(np,pgrid,boin,nx,nz,rcount,rdispl)
    2557              :       DO ip = 0, np - 1
    2558              :          ipl = pgrid(ip, 2)
    2559              :          ny = boin(2, 2, ipl) - boin(1, 2, ipl) + 1
    2560              :          rcount(ip) = nx*ny*nz
    2561              :          rdispl(ip) = nx*nz*(boin(1, 2, ipl) - 1)
    2562              :       END DO
    2563              : !$OMP END PARALLEL DO
    2564              : 
    2565            0 :       CALL sub_group%alltoall(rbuf, scount, sdispl, sout, rcount, rdispl)
    2566              : 
    2567            0 :       CALL timestop(handle)
    2568              : 
    2569            0 :    END SUBROUTINE cube_transpose_2
    2570              : 
    2571              : ! **************************************************************************************************
    2572              : !> \brief ...
    2573              : !> \param cin ...
    2574              : !> \param boin ...
    2575              : !> \param boout ...
    2576              : !> \param sout ...
    2577              : !> \param fft_scratch ...
    2578              : ! **************************************************************************************************
    2579            0 :    SUBROUTINE cube_transpose_3(cin, boin, boout, sout, fft_scratch)
    2580              : 
    2581              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
    2582              :          INTENT(IN)                                      :: cin
    2583              :       INTEGER, CONTIGUOUS, DIMENSION(:, :, 0:), &
    2584              :          INTENT(IN)                                      :: boin, boout
    2585              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
    2586              :          INTENT(OUT)                                     :: sout
    2587              :       TYPE(fft_scratch_type), INTENT(IN)                 :: fft_scratch
    2588              : 
    2589              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'cube_transpose_3'
    2590              : 
    2591              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
    2592            0 :          POINTER                                         :: rbuf
    2593              :       INTEGER                                            :: handle, ip, ipl, ir, is, ixz, iy, lb, &
    2594              :                                                             mip, my, my_id, np, num_threads, nx, &
    2595              :                                                             ny, nz, ub
    2596            0 :       INTEGER, CONTIGUOUS, DIMENSION(:), POINTER         :: rcount, rdispl, scount, sdispl
    2597            0 :       INTEGER, CONTIGUOUS, DIMENSION(:, :), POINTER      :: pgrid
    2598              :       INTEGER, DIMENSION(2)                              :: dim, pos
    2599              :       TYPE(mp_comm_type)                                 :: sub_group
    2600              : 
    2601            0 :       CALL timeset(routineN, handle)
    2602              : 
    2603            0 :       sub_group = fft_scratch%cart_sub_comm(1)
    2604            0 :       mip = fft_scratch%mip
    2605            0 :       dim = fft_scratch%dim
    2606            0 :       pos = fft_scratch%pos
    2607            0 :       np = DIM(1)
    2608            0 :       scount => fft_scratch%scount
    2609            0 :       rcount => fft_scratch%rcount
    2610            0 :       sdispl => fft_scratch%sdispl
    2611            0 :       rdispl => fft_scratch%rdispl
    2612            0 :       pgrid => fft_scratch%pgcube
    2613              : 
    2614            0 :       ny = boin(2, 2, mip) - boin(1, 2, mip) + 1
    2615            0 :       nz = boin(2, 3, mip) - boin(1, 3, mip) + 1
    2616              : !$OMP PARALLEL DO DEFAULT(NONE), &
    2617              : !$OMP             PRIVATE(ipl, nx), &
    2618            0 : !$OMP             SHARED(np,pgrid,boout,ny,nz,scount,sdispl)
    2619              :       DO ip = 0, np - 1
    2620              :          ipl = pgrid(ip, 1)
    2621              :          nx = boout(2, 1, ipl) - boout(1, 1, ipl) + 1
    2622              :          scount(ip) = nx*nz*ny
    2623              :          sdispl(ip) = ny*nz*(boout(1, 1, ipl) - 1)
    2624              :       END DO
    2625              : !$OMP END PARALLEL DO
    2626            0 :       nx = boout(2, 1, mip) - boout(1, 1, mip) + 1
    2627            0 :       my = MAXVAL(boin(2, 2, :) - boin(1, 2, :) + 1)
    2628              : !$OMP PARALLEL DO DEFAULT(NONE), &
    2629              : !$OMP             PRIVATE(ipl, ny), &
    2630            0 : !$OMP             SHARED(np,pgrid,boin,nx,nz,my,rcount,rdispl)
    2631              :       DO ip = 0, np - 1
    2632              :          ipl = pgrid(ip, 1)
    2633              :          ny = boin(2, 2, ipl) - boin(1, 2, ipl) + 1
    2634              :          rcount(ip) = nx*nz*ny
    2635              :          rdispl(ip) = nx*my*nz*ip
    2636              :       END DO
    2637              : !$OMP END PARALLEL DO
    2638              : 
    2639            0 :       rbuf => fft_scratch%rbuf3
    2640            0 :       num_threads = 1
    2641            0 :       my_id = 0
    2642              : !$OMP PARALLEL DEFAULT(NONE), &
    2643              : !$OMP          PRIVATE(NUM_THREADS, my_id, lb, ub) &
    2644            0 : !$OMP          SHARED(rbuf)
    2645              : !$    num_threads = MIN(omp_get_max_threads(), SIZE(rbuf, 2))
    2646              : !$    my_id = omp_get_thread_num()
    2647              :       IF (my_id < num_threads) THEN
    2648              :          lb = (SIZE(rbuf, 2)*my_id)/num_threads
    2649              :          ub = (SIZE(rbuf, 2)*(my_id + 1))/num_threads - 1
    2650              :          rbuf(:, lb:ub) = 0.0_dp
    2651              :       END IF
    2652              : !$OMP END PARALLEL
    2653              : 
    2654            0 :       CALL sub_group%alltoall(cin, scount, sdispl, rbuf, rcount, rdispl)
    2655              : 
    2656              : !$OMP PARALLEL DO DEFAULT(NONE) COLLAPSE(2) &
    2657              : !$OMP             PRIVATE(ip,ipl,ny,iy,is,ir) &
    2658            0 : !$OMP             SHARED(nx,nz,np,pgrid,boin,rbuf,sout)
    2659              :       DO ixz = 1, nx*nz
    2660              :          DO ip = 0, np - 1
    2661              :             ipl = pgrid(ip, 1)
    2662              :             ny = boin(2, 2, ipl) - boin(1, 2, ipl) + 1
    2663              :             DO iy = 1, ny
    2664              :                is = boin(1, 2, ipl) + iy - 1
    2665              :                ir = iy + ny*(ixz - 1)
    2666              :                sout(is, ixz) = rbuf(ir, ip)
    2667              :             END DO
    2668              :          END DO
    2669              :       END DO
    2670              : !$OMP END PARALLEL DO
    2671              : 
    2672            0 :       CALL timestop(handle)
    2673              : 
    2674            0 :    END SUBROUTINE cube_transpose_3
    2675              : 
    2676              : ! **************************************************************************************************
    2677              : !> \brief ...
    2678              : !> \param cin ...
    2679              : !> \param boin ...
    2680              : !> \param boout ...
    2681              : !> \param sout ...
    2682              : !> \param fft_scratch ...
    2683              : ! **************************************************************************************************
    2684            0 :    SUBROUTINE cube_transpose_4(cin, boin, boout, sout, fft_scratch)
    2685              : 
    2686              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
    2687              :          INTENT(IN)                                      :: cin
    2688              :       INTEGER, CONTIGUOUS, DIMENSION(:, :, 0:), &
    2689              :          INTENT(IN)                                      :: boin, boout
    2690              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
    2691              :          INTENT(OUT)                                     :: sout
    2692              :       TYPE(fft_scratch_type), INTENT(IN)                 :: fft_scratch
    2693              : 
    2694              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'cube_transpose_4'
    2695              : 
    2696              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
    2697            0 :          POINTER                                         :: rbuf
    2698              :       INTEGER                                            :: handle, ip, ipl, ir, iy, izx, lb, mip, &
    2699              :                                                             my, my_id, np, num_threads, nx, ny, &
    2700              :                                                             nz, ub
    2701            0 :       INTEGER, CONTIGUOUS, DIMENSION(:), POINTER         :: rcount, rdispl, scount, sdispl
    2702            0 :       INTEGER, CONTIGUOUS, DIMENSION(:, :), POINTER      :: pgrid
    2703              :       INTEGER, DIMENSION(2)                              :: dim, pos
    2704              :       TYPE(mp_comm_type)                                 :: sub_group
    2705              : 
    2706            0 :       CALL timeset(routineN, handle)
    2707              : 
    2708            0 :       sub_group = fft_scratch%cart_sub_comm(1)
    2709            0 :       mip = fft_scratch%mip
    2710            0 :       dim = fft_scratch%dim
    2711            0 :       pos = fft_scratch%pos
    2712            0 :       np = DIM(1)
    2713            0 :       scount => fft_scratch%scount
    2714            0 :       rcount => fft_scratch%rcount
    2715            0 :       sdispl => fft_scratch%sdispl
    2716            0 :       rdispl => fft_scratch%rdispl
    2717            0 :       pgrid => fft_scratch%pgcube
    2718              : 
    2719            0 :       nx = boin(2, 1, mip) - boin(1, 1, mip) + 1
    2720            0 :       nz = boin(2, 3, mip) - boin(1, 3, mip) + 1
    2721            0 :       my = MAXVAL(boout(2, 2, :) - boout(1, 2, :) + 1)
    2722              : 
    2723            0 :       rbuf => fft_scratch%rbuf4
    2724            0 :       num_threads = 1
    2725            0 :       my_id = 0
    2726              : !$OMP PARALLEL DEFAULT(NONE), &
    2727              : !$OMP          PRIVATE(NUM_THREADS,my_id,lb,ub,ip,ipl,ny,iy,ir), &
    2728            0 : !$OMP          SHARED(rbuf,nz,nx,np,pgrid,boout,cin,my,scount,sdispl)
    2729              : !$    num_threads = MIN(omp_get_max_threads(), SIZE(rbuf, 2))
    2730              : !$    my_id = omp_get_thread_num()
    2731              :       IF (my_id < num_threads) THEN
    2732              :          lb = (SIZE(rbuf, 2)*my_id)/num_threads
    2733              :          ub = (SIZE(rbuf, 2)*(my_id + 1))/num_threads - 1
    2734              :          rbuf(:, lb:ub) = 0.0_dp
    2735              :       END IF
    2736              : !$OMP BARRIER
    2737              : 
    2738              : !$OMP DO COLLAPSE(2)
    2739              :       DO izx = 1, nz*nx
    2740              :          DO ip = 0, np - 1
    2741              :             ipl = pgrid(ip, 1)
    2742              :             ny = boout(2, 2, ipl) - boout(1, 2, ipl) + 1
    2743              :             DO iy = boout(1, 2, ipl), boout(2, 2, ipl)
    2744              :                ir = iy - boout(1, 2, ipl) + 1 + (izx - 1)*ny
    2745              :                rbuf(ir, ip) = cin(iy, izx)
    2746              :             END DO
    2747              :          END DO
    2748              :       END DO
    2749              : !$OMP END DO
    2750              : !$OMP DO
    2751              :       DO ip = 0, np - 1
    2752              :          ipl = pgrid(ip, 1)
    2753              :          ny = boout(2, 2, ipl) - boout(1, 2, ipl) + 1
    2754              :          scount(ip) = nx*ny*nz
    2755              :          sdispl(ip) = nx*nz*my*ip
    2756              :       END DO
    2757              : !$OMP END DO
    2758              : !$OMP END PARALLEL
    2759            0 :       ny = boout(2, 2, mip) - boout(1, 2, mip) + 1
    2760              : !$OMP PARALLEL DO DEFAULT(NONE), &
    2761              : !$OMP             PRIVATE(ipl,nx), &
    2762            0 : !$OMP             SHARED(np,pgrid,boin,rcount,rdispl,ny,nz)
    2763              :       DO ip = 0, np - 1
    2764              :          ipl = pgrid(ip, 1)
    2765              :          nx = boin(2, 1, ipl) - boin(1, 1, ipl) + 1
    2766              :          rcount(ip) = nx*ny*nz
    2767              :          rdispl(ip) = ny*nz*(boin(1, 1, ipl) - 1)
    2768              :       END DO
    2769              : !$OMP END PARALLEL DO
    2770              : 
    2771            0 :       CALL sub_group%alltoall(rbuf, scount, sdispl, sout, rcount, rdispl)
    2772              : 
    2773            0 :       CALL timestop(handle)
    2774              : 
    2775            0 :    END SUBROUTINE cube_transpose_4
    2776              : 
    2777              : ! **************************************************************************************************
    2778              : !> \brief ...
    2779              : !> \param cin ...
    2780              : !> \param group ...
    2781              : !> \param boin ...
    2782              : !> \param boout ...
    2783              : !> \param sout ...
    2784              : !> \param fft_scratch ...
    2785              : ! **************************************************************************************************
    2786          104 :    SUBROUTINE cube_transpose_5(cin, group, boin, boout, sout, fft_scratch)
    2787              : 
    2788              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
    2789              :          INTENT(IN)                                      :: cin
    2790              : 
    2791              :       CLASS(mp_comm_type), INTENT(IN)                    :: group
    2792              :       INTEGER, CONTIGUOUS, DIMENSION(:, :, 0:), INTENT(IN)           :: boin, boout
    2793              :       COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(OUT), CONTIGUOUS     :: sout
    2794              :       TYPE(fft_scratch_type), INTENT(IN)                 :: fft_scratch
    2795              : 
    2796              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'cube_transpose_5'
    2797              : 
    2798          104 :       COMPLEX(KIND=dp), DIMENSION(:, :), POINTER, CONTIGUOUS         :: rbuf
    2799              :       INTEGER                                            :: handle, ip, ir, is, ixz, iy, lb, mip, &
    2800              :                                                             my, my_id, np, num_threads, nx, ny, &
    2801              :                                                             nz, ub
    2802          104 :       INTEGER, DIMENSION(:), POINTER, CONTIGUOUS                     :: rcount, rdispl, scount, sdispl
    2803              : 
    2804          104 :       CALL timeset(routineN, handle)
    2805              : 
    2806          104 :       np = fft_scratch%sizes%numtask
    2807          104 :       mip = fft_scratch%mip
    2808          104 :       scount => fft_scratch%scount
    2809          104 :       rcount => fft_scratch%rcount
    2810          104 :       sdispl => fft_scratch%sdispl
    2811          104 :       rdispl => fft_scratch%rdispl
    2812              : 
    2813          104 :       ny = boin(2, 2, mip) - boin(1, 2, mip) + 1
    2814          104 :       nz = boin(2, 3, mip) - boin(1, 3, mip) + 1
    2815              : !$OMP PARALLEL DO DEFAULT(NONE), &
    2816              : !$OMP             PRIVATE(nx), &
    2817          104 : !$OMP             SHARED(np,boout,ny,nz,scount,sdispl)
    2818              :       DO ip = 0, np - 1
    2819              :          nx = boout(2, 1, ip) - boout(1, 1, ip) + 1
    2820              :          scount(ip) = nx*nz*ny
    2821              :          sdispl(ip) = ny*nz*(boout(1, 1, ip) - 1)
    2822              :       END DO
    2823              : !$OMP END PARALLEL DO
    2824          104 :       nx = boout(2, 1, mip) - boout(1, 1, mip) + 1
    2825          312 :       my = MAXVAL(boin(2, 2, :) - boin(1, 2, :) + 1)
    2826              : !$OMP PARALLEL DO DEFAULT(NONE), &
    2827              : !$OMP             PRIVATE(ny), &
    2828          104 : !$OMP             SHARED(np,boin,nx,nz,rcount,rdispl,my)
    2829              :       DO ip = 0, np - 1
    2830              :          ny = boin(2, 2, ip) - boin(1, 2, ip) + 1
    2831              :          rcount(ip) = nx*nz*ny
    2832              :          rdispl(ip) = nx*my*nz*ip
    2833              :       END DO
    2834              : !$OMP END PARALLEL DO
    2835              : 
    2836          104 :       rbuf => fft_scratch%rbuf5
    2837          104 :       num_threads = 1
    2838          104 :       my_id = 0
    2839              : !$OMP PARALLEL DEFAULT(NONE), &
    2840              : !$OMP          PRIVATE(NUM_THREADS, my_id, lb, ub), &
    2841          104 : !$OMP          SHARED(rbuf)
    2842              : !$    num_threads = MIN(omp_get_max_threads(), SIZE(rbuf, 2))
    2843              : !$    my_id = omp_get_thread_num()
    2844              :       IF (my_id < num_threads) THEN
    2845              :          lb = (SIZE(rbuf, 2)*my_id)/num_threads
    2846              :          ub = (SIZE(rbuf, 2)*(my_id + 1))/num_threads - 1
    2847              :          rbuf(:, lb:ub) = 0.0_dp
    2848              :       END IF
    2849              : !$OMP END PARALLEL
    2850              : 
    2851          104 :       CALL group%alltoall(cin, scount, sdispl, rbuf, rcount, rdispl)
    2852              : 
    2853              : !$OMP PARALLEL DO DEFAULT(NONE) COLLAPSE(2) &
    2854              : !$OMP             PRIVATE(ip,ny,iy,is,ir) &
    2855          104 : !$OMP             SHARED(nx,nz,np,boin,sout,rbuf)
    2856              :       DO ixz = 1, nx*nz
    2857              :          DO ip = 0, np - 1
    2858              :             ny = boin(2, 2, ip) - boin(1, 2, ip) + 1
    2859              :             DO iy = 1, ny
    2860              :                is = boin(1, 2, ip) + iy - 1
    2861              :                ir = iy + ny*(ixz - 1)
    2862              :                sout(is, ixz) = rbuf(ir, ip)
    2863              :             END DO
    2864              :          END DO
    2865              :       END DO
    2866              : !$OMP END PARALLEL DO
    2867              : 
    2868          104 :       CALL timestop(handle)
    2869              : 
    2870          104 :    END SUBROUTINE cube_transpose_5
    2871              : 
    2872              : ! **************************************************************************************************
    2873              : !> \brief ...
    2874              : !> \param cin ...
    2875              : !> \param group ...
    2876              : !> \param boin ...
    2877              : !> \param boout ...
    2878              : !> \param sout ...
    2879              : !> \param fft_scratch ...
    2880              : ! **************************************************************************************************
    2881          104 :    SUBROUTINE cube_transpose_6(cin, group, boin, boout, sout, fft_scratch)
    2882              : 
    2883              :       COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
    2884              :          INTENT(IN)                                      :: cin
    2885              : 
    2886              :       CLASS(mp_comm_type), INTENT(IN)                    :: group
    2887              :       INTEGER, CONTIGUOUS, DIMENSION(:, :, 0:), INTENT(IN)           :: boin, boout
    2888              :       COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(OUT), CONTIGUOUS     :: sout
    2889              :       TYPE(fft_scratch_type), INTENT(IN)                 :: fft_scratch
    2890              : 
    2891              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'cube_transpose_6'
    2892              : 
    2893          104 :       COMPLEX(KIND=dp), DIMENSION(:, :), POINTER, CONTIGUOUS         :: rbuf
    2894              :       INTEGER                                            :: handle, ip, ir, iy, izx, lb, mip, my, &
    2895              :                                                             my_id, np, num_threads, nx, ny, nz, ub
    2896          104 :       INTEGER, DIMENSION(:), POINTER, CONTIGUOUS                     :: rcount, rdispl, scount, sdispl
    2897              : 
    2898          104 :       CALL timeset(routineN, handle)
    2899              : 
    2900          104 :       np = fft_scratch%sizes%numtask
    2901          104 :       mip = fft_scratch%mip
    2902          104 :       scount => fft_scratch%scount
    2903          104 :       rcount => fft_scratch%rcount
    2904          104 :       sdispl => fft_scratch%sdispl
    2905          104 :       rdispl => fft_scratch%rdispl
    2906              : 
    2907          104 :       nx = boin(2, 1, mip) - boin(1, 1, mip) + 1
    2908          104 :       nz = boin(2, 3, mip) - boin(1, 3, mip) + 1
    2909          312 :       my = MAXVAL(boout(2, 2, :) - boout(1, 2, :) + 1)
    2910              : 
    2911          104 :       rbuf => fft_scratch%rbuf5
    2912          104 :       num_threads = 1
    2913          104 :       my_id = 0
    2914              : !$OMP PARALLEL DEFAULT(NONE), &
    2915              : !$OMP          PRIVATE(NUM_THREADS,my_id,lb,ub,ip,ny,iy,ir), &
    2916          104 : !$OMP          SHARED(rbuf,nx,nz,np,boout,cin,my,scount,sdispl)
    2917              : !$    num_threads = MIN(omp_get_max_threads(), SIZE(rbuf, 2))
    2918              : !$    my_id = omp_get_thread_num()
    2919              :       IF (my_id < num_threads) THEN
    2920              :          lb = (SIZE(rbuf, 2)*my_id)/num_threads
    2921              :          ub = (SIZE(rbuf, 2)*(my_id + 1))/num_threads - 1
    2922              :          rbuf(:, lb:ub) = 0.0_dp
    2923              :       END IF
    2924              : !$OMP BARRIER
    2925              : 
    2926              : !$OMP DO COLLAPSE(2)
    2927              :       DO izx = 1, nz*nx
    2928              :          DO ip = 0, np - 1
    2929              :             ny = boout(2, 2, ip) - boout(1, 2, ip) + 1
    2930              :             DO iy = boout(1, 2, ip), boout(2, 2, ip)
    2931              :                ir = iy - boout(1, 2, ip) + 1 + (izx - 1)*ny
    2932              :                rbuf(ir, ip) = cin(iy, izx)
    2933              :             END DO
    2934              :          END DO
    2935              :       END DO
    2936              : !$OMP END DO
    2937              : !$OMP DO
    2938              :       DO ip = 0, np - 1
    2939              :          ny = boout(2, 2, ip) - boout(1, 2, ip) + 1
    2940              :          scount(ip) = nx*ny*nz
    2941              :          sdispl(ip) = nx*nz*my*ip
    2942              :       END DO
    2943              : !$OMP END DO
    2944              : !$OMP END PARALLEL
    2945          104 :       ny = boout(2, 2, mip) - boout(1, 2, mip) + 1
    2946              : !$OMP PARALLEL DO DEFAULT(NONE), &
    2947              : !$OMP             PRIVATE(nx), &
    2948          104 : !$OMP             SHARED(np,boin,rcount,rdispl,nz,ny)
    2949              :       DO ip = 0, np - 1
    2950              :          nx = boin(2, 1, ip) - boin(1, 1, ip) + 1
    2951              :          rcount(ip) = nx*ny*nz
    2952              :          rdispl(ip) = ny*nz*(boin(1, 1, ip) - 1)
    2953              :       END DO
    2954              : !$OMP END PARALLEL DO
    2955              : 
    2956          104 :       CALL group%alltoall(rbuf, scount, sdispl, sout, rcount, rdispl)
    2957              : 
    2958          104 :       CALL timestop(handle)
    2959              : 
    2960          104 :    END SUBROUTINE cube_transpose_6
    2961              : 
    2962              : ! **************************************************************************************************
    2963              : !> \brief ...
    2964              : ! **************************************************************************************************
    2965        25729 :    SUBROUTINE init_fft_scratch_pool()
    2966              : 
    2967        25729 :       CALL release_fft_scratch_pool()
    2968              : 
    2969              :       ! Allocate first scratch and mark it as used
    2970        25729 :       ALLOCATE (fft_scratch_first)
    2971       746141 :       ALLOCATE (fft_scratch_first%fft_scratch)
    2972              :       ! this is a very special scratch, it seems, we always keep it 'most - recent' so we will never delete it
    2973        25729 :       fft_scratch_first%fft_scratch%last_tick = HUGE(fft_scratch_first%fft_scratch%last_tick)
    2974              : 
    2975        25729 :       init_fft_pool = init_fft_pool + 1
    2976              : 
    2977        25729 :    END SUBROUTINE init_fft_scratch_pool
    2978              : 
    2979              : ! **************************************************************************************************
    2980              : !> \brief ...
    2981              : !> \param fft_scratch ...
    2982              : ! **************************************************************************************************
    2983        65956 :    SUBROUTINE deallocate_fft_scratch_type(fft_scratch)
    2984              :       TYPE(fft_scratch_type), INTENT(INOUT)    :: fft_scratch
    2985              : 
    2986              : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
    2987              :       INTEGER                   :: ierr
    2988              :       COMPLEX(KIND=dp), POINTER :: dummy_ptr_z
    2989              : #endif
    2990              : 
    2991              :       ! deallocate structures
    2992        65956 :       IF (ASSOCIATED(fft_scratch%ziptr)) THEN
    2993         4255 :          CALL fft_deallocate(fft_scratch%ziptr)
    2994              :       END IF
    2995        65956 :       IF (ASSOCIATED(fft_scratch%zoptr)) THEN
    2996         4255 :          CALL fft_deallocate(fft_scratch%zoptr)
    2997              :       END IF
    2998        65956 :       IF (ASSOCIATED(fft_scratch%p1buf)) THEN
    2999            0 :          CALL fft_deallocate(fft_scratch%p1buf)
    3000              :       END IF
    3001        65956 :       IF (ASSOCIATED(fft_scratch%p2buf)) THEN
    3002              : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
    3003              :          dummy_ptr_z => fft_scratch%p2buf(1, 1)
    3004              :          ierr = offload_free_pinned_mem(c_loc(dummy_ptr_z))
    3005              : #else
    3006            0 :          CALL fft_deallocate(fft_scratch%p2buf)
    3007              : #endif
    3008              :       END IF
    3009        65956 :       IF (ASSOCIATED(fft_scratch%p3buf)) THEN
    3010              : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
    3011              :          dummy_ptr_z => fft_scratch%p3buf(1, 1)
    3012              :          ierr = offload_free_pinned_mem(c_loc(dummy_ptr_z))
    3013              : #else
    3014            0 :          CALL fft_deallocate(fft_scratch%p3buf)
    3015              : #endif
    3016              :       END IF
    3017        65956 :       IF (ASSOCIATED(fft_scratch%p4buf)) THEN
    3018              : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
    3019              :          dummy_ptr_z => fft_scratch%p4buf(1, 1)
    3020              :          ierr = offload_free_pinned_mem(c_loc(dummy_ptr_z))
    3021              : #else
    3022            0 :          CALL fft_deallocate(fft_scratch%p4buf)
    3023              : #endif
    3024              :       END IF
    3025        65956 :       IF (ASSOCIATED(fft_scratch%p5buf)) THEN
    3026              : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
    3027              :          dummy_ptr_z => fft_scratch%p5buf(1, 1)
    3028              :          ierr = offload_free_pinned_mem(c_loc(dummy_ptr_z))
    3029              : #else
    3030            0 :          CALL fft_deallocate(fft_scratch%p5buf)
    3031              : #endif
    3032              :       END IF
    3033        65956 :       IF (ASSOCIATED(fft_scratch%p6buf)) THEN
    3034            0 :          CALL fft_deallocate(fft_scratch%p6buf)
    3035              :       END IF
    3036        65956 :       IF (ASSOCIATED(fft_scratch%p7buf)) THEN
    3037              : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
    3038              :          dummy_ptr_z => fft_scratch%p7buf(1, 1)
    3039              :          ierr = offload_free_pinned_mem(c_loc(dummy_ptr_z))
    3040              : #else
    3041            0 :          CALL fft_deallocate(fft_scratch%p7buf)
    3042              : #endif
    3043              :       END IF
    3044        65956 :       IF (ASSOCIATED(fft_scratch%r1buf)) THEN
    3045              : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
    3046              :          dummy_ptr_z => fft_scratch%r1buf(1, 1)
    3047              :          ierr = offload_free_pinned_mem(c_loc(dummy_ptr_z))
    3048              : #else
    3049        35964 :          CALL fft_deallocate(fft_scratch%r1buf)
    3050              : #endif
    3051              :       END IF
    3052        65956 :       IF (ASSOCIATED(fft_scratch%r2buf)) THEN
    3053        35964 :          CALL fft_deallocate(fft_scratch%r2buf)
    3054              :       END IF
    3055        65956 :       IF (ASSOCIATED(fft_scratch%tbuf)) THEN
    3056              : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
    3057              :          dummy_ptr_z => fft_scratch%tbuf(1, 1, 1)
    3058              :          ierr = offload_free_pinned_mem(c_loc(dummy_ptr_z))
    3059              : #else
    3060        35964 :          CALL fft_deallocate(fft_scratch%tbuf)
    3061              : #endif
    3062              :       END IF
    3063        65956 :       IF (ASSOCIATED(fft_scratch%a1buf)) THEN
    3064            8 :          CALL fft_deallocate(fft_scratch%a1buf)
    3065              :       END IF
    3066        65956 :       IF (ASSOCIATED(fft_scratch%a2buf)) THEN
    3067            8 :          CALL fft_deallocate(fft_scratch%a2buf)
    3068              :       END IF
    3069        65956 :       IF (ASSOCIATED(fft_scratch%a3buf)) THEN
    3070            0 :          CALL fft_deallocate(fft_scratch%a3buf)
    3071              :       END IF
    3072        65956 :       IF (ASSOCIATED(fft_scratch%a4buf)) THEN
    3073            8 :          CALL fft_deallocate(fft_scratch%a4buf)
    3074              :       END IF
    3075        65956 :       IF (ASSOCIATED(fft_scratch%a5buf)) THEN
    3076            8 :          CALL fft_deallocate(fft_scratch%a5buf)
    3077              :       END IF
    3078        65956 :       IF (ASSOCIATED(fft_scratch%a6buf)) THEN
    3079            8 :          CALL fft_deallocate(fft_scratch%a6buf)
    3080              :       END IF
    3081        65956 :       IF (ASSOCIATED(fft_scratch%scount)) THEN
    3082            0 :          DEALLOCATE (fft_scratch%scount, fft_scratch%rcount, &
    3083        35972 :                      fft_scratch%sdispl, fft_scratch%rdispl)
    3084              :       END IF
    3085        65956 :       IF (ASSOCIATED(fft_scratch%rr)) THEN
    3086        35956 :          DEALLOCATE (fft_scratch%rr)
    3087              :       END IF
    3088        65956 :       IF (ASSOCIATED(fft_scratch%xzbuf)) THEN
    3089            0 :          DEALLOCATE (fft_scratch%xzbuf)
    3090              :       END IF
    3091        65956 :       IF (ASSOCIATED(fft_scratch%yzbuf)) THEN
    3092            0 :          DEALLOCATE (fft_scratch%yzbuf)
    3093              :       END IF
    3094        65956 :       IF (ASSOCIATED(fft_scratch%xzbuf_sgl)) THEN
    3095            0 :          DEALLOCATE (fft_scratch%xzbuf_sgl)
    3096              :       END IF
    3097        65956 :       IF (ASSOCIATED(fft_scratch%yzbuf_sgl)) THEN
    3098            0 :          DEALLOCATE (fft_scratch%yzbuf_sgl)
    3099              :       END IF
    3100        65956 :       IF (ASSOCIATED(fft_scratch%ss)) THEN
    3101            8 :          DEALLOCATE (fft_scratch%ss)
    3102              :       END IF
    3103        65956 :       IF (ASSOCIATED(fft_scratch%tt)) THEN
    3104            8 :          DEALLOCATE (fft_scratch%tt)
    3105              :       END IF
    3106        65956 :       IF (ASSOCIATED(fft_scratch%pgrid)) THEN
    3107            0 :          DEALLOCATE (fft_scratch%pgrid)
    3108              :       END IF
    3109        65956 :       IF (ASSOCIATED(fft_scratch%pgcube)) THEN
    3110        35972 :          DEALLOCATE (fft_scratch%pgcube)
    3111              :       END IF
    3112        65956 :       IF (ASSOCIATED(fft_scratch%xcor)) THEN
    3113            0 :          DEALLOCATE (fft_scratch%xcor, fft_scratch%zcor)
    3114              :       END IF
    3115        65956 :       IF (ASSOCIATED(fft_scratch%pzcoord)) THEN
    3116            0 :          DEALLOCATE (fft_scratch%pzcoord)
    3117              :       END IF
    3118        65956 :       IF (ASSOCIATED(fft_scratch%xzcount)) THEN
    3119            0 :          DEALLOCATE (fft_scratch%xzcount, fft_scratch%yzcount)
    3120            0 :          DEALLOCATE (fft_scratch%xzdispl, fft_scratch%yzdispl)
    3121            0 :          fft_scratch%in = 0
    3122            0 :          fft_scratch%rsratio = 1._dp
    3123              :       END IF
    3124        65956 :       IF (ASSOCIATED(fft_scratch%rbuf1)) THEN
    3125            0 :          DEALLOCATE (fft_scratch%rbuf1)
    3126              :       END IF
    3127        65956 :       IF (ASSOCIATED(fft_scratch%rbuf2)) THEN
    3128            0 :          DEALLOCATE (fft_scratch%rbuf2)
    3129              :       END IF
    3130        65956 :       IF (ASSOCIATED(fft_scratch%rbuf3)) THEN
    3131            0 :          DEALLOCATE (fft_scratch%rbuf3)
    3132              :       END IF
    3133        65956 :       IF (ASSOCIATED(fft_scratch%rbuf4)) THEN
    3134            0 :          DEALLOCATE (fft_scratch%rbuf4)
    3135              :       END IF
    3136        65956 :       IF (ASSOCIATED(fft_scratch%rbuf5)) THEN
    3137            8 :          DEALLOCATE (fft_scratch%rbuf5)
    3138              :       END IF
    3139        65956 :       IF (ASSOCIATED(fft_scratch%rbuf6)) THEN
    3140            8 :          DEALLOCATE (fft_scratch%rbuf6)
    3141              :       END IF
    3142              : 
    3143        65956 :       IF (fft_scratch%cart_sub_comm(1) /= mp_comm_null) THEN
    3144            0 :          CALL fft_scratch%cart_sub_comm(1)%free()
    3145              :       END IF
    3146        65956 :       IF (fft_scratch%cart_sub_comm(2) /= mp_comm_null) THEN
    3147            0 :          CALL fft_scratch%cart_sub_comm(2)%free()
    3148              :       END IF
    3149              : 
    3150        65956 :       CALL fft_destroy_plan(fft_scratch%fft_plan(1))
    3151        65956 :       CALL fft_destroy_plan(fft_scratch%fft_plan(2))
    3152        65956 :       CALL fft_destroy_plan(fft_scratch%fft_plan(3))
    3153        65956 :       CALL fft_destroy_plan(fft_scratch%fft_plan(4))
    3154        65956 :       CALL fft_destroy_plan(fft_scratch%fft_plan(5))
    3155        65956 :       CALL fft_destroy_plan(fft_scratch%fft_plan(6))
    3156              : 
    3157        65956 :    END SUBROUTINE deallocate_fft_scratch_type
    3158              : 
    3159              : ! **************************************************************************************************
    3160              : !> \brief ...
    3161              : ! **************************************************************************************************
    3162        49389 :    SUBROUTINE release_fft_scratch_pool()
    3163              :       TYPE(fft_scratch_pool_type), POINTER               :: fft_scratch, fft_scratch_current
    3164              : 
    3165        49389 : !$    CPASSERT(.NOT. omp_in_parallel() .OR. 0 == omp_get_thread_num())
    3166              : 
    3167        49389 :       IF (init_fft_pool == 0) NULLIFY (fft_scratch_first)
    3168              : 
    3169        49389 :       fft_scratch => fft_scratch_first
    3170        65825 :       DO
    3171       115214 :          IF (ASSOCIATED(fft_scratch)) THEN
    3172        65825 :             fft_scratch_current => fft_scratch
    3173        65825 :             fft_scratch => fft_scratch_current%fft_scratch_next
    3174        65825 :             NULLIFY (fft_scratch_current%fft_scratch_next)
    3175              : 
    3176        65825 :             CALL deallocate_fft_scratch_type(fft_scratch_current%fft_scratch)
    3177              : 
    3178       263300 :             DEALLOCATE (fft_scratch_current%fft_scratch)
    3179        65825 :             DEALLOCATE (fft_scratch_current)
    3180              :          ELSE
    3181              :             EXIT
    3182              :          END IF
    3183              :       END DO
    3184              : 
    3185        49389 :       init_fft_pool = 0
    3186              : 
    3187        49389 :    END SUBROUTINE release_fft_scratch_pool
    3188              : 
    3189              : ! **************************************************************************************************
    3190              : !> \brief ...
    3191              : ! **************************************************************************************************
    3192      4404342 :    SUBROUTINE resize_fft_scratch_pool()
    3193              : 
    3194              :       INTEGER                                            :: last_tick, nscratch
    3195              :       TYPE(fft_scratch_pool_type), POINTER               :: fft_scratch_current, fft_scratch_old
    3196              : 
    3197      4404342 :       nscratch = 0
    3198              : 
    3199      4404342 :       last_tick = HUGE(last_tick)
    3200      4404342 :       NULLIFY (fft_scratch_old)
    3201              : 
    3202              :       ! start at the global pool, count, and find a deletion candidate
    3203      4404342 :       fft_scratch_current => fft_scratch_first
    3204     23531122 :       DO
    3205     27935464 :          IF (ASSOCIATED(fft_scratch_current)) THEN
    3206     23531122 :             nscratch = nscratch + 1
    3207              :             ! is this a candidate for deletion (i.e. least recently used, and not in use)
    3208     23531122 :             IF (.NOT. fft_scratch_current%fft_scratch%in_use) THEN
    3209     19126780 :                IF (fft_scratch_current%fft_scratch%last_tick < last_tick) THEN
    3210      9177580 :                   last_tick = fft_scratch_current%fft_scratch%last_tick
    3211      9177580 :                   fft_scratch_old => fft_scratch_current
    3212              :                END IF
    3213              :             END IF
    3214     23531122 :             fft_scratch_current => fft_scratch_current%fft_scratch_next
    3215              :          ELSE
    3216              :             EXIT
    3217              :          END IF
    3218              :       END DO
    3219              : 
    3220              :       ! we should delete a scratch
    3221      4404342 :       IF (nscratch > fft_pool_scratch_limit) THEN
    3222              :          ! note that we never deallocate the first (special) element of the list
    3223          131 :          IF (ASSOCIATED(fft_scratch_old)) THEN
    3224              :             fft_scratch_current => fft_scratch_first
    3225              :             DO
    3226         2227 :                IF (ASSOCIATED(fft_scratch_current)) THEN
    3227              :                   ! should we delete the next in the list?
    3228         2096 :                   IF (ASSOCIATED(fft_scratch_current%fft_scratch_next, fft_scratch_old)) THEN
    3229              :                      ! fix the linked list
    3230          131 :                      fft_scratch_current%fft_scratch_next => fft_scratch_old%fft_scratch_next
    3231              : 
    3232              :                      ! deallocate the element
    3233          131 :                      CALL deallocate_fft_scratch_type(fft_scratch_old%fft_scratch)
    3234          524 :                      DEALLOCATE (fft_scratch_old%fft_scratch)
    3235          131 :                      DEALLOCATE (fft_scratch_old)
    3236              : 
    3237              :                   ELSE
    3238              :                      fft_scratch_current => fft_scratch_current%fft_scratch_next
    3239              :                   END IF
    3240              :                ELSE
    3241              :                   EXIT
    3242              :                END IF
    3243              :             END DO
    3244              : 
    3245              :          ELSE
    3246            0 :             CPWARN("The number of the scratches exceeded the limit, but none could be deallocated")
    3247              :          END IF
    3248              :       END IF
    3249              : 
    3250      4404342 :    END SUBROUTINE resize_fft_scratch_pool
    3251              : 
    3252              : ! **************************************************************************************************
    3253              : !> \brief ...
    3254              : !> \param fft_scratch ...
    3255              : !> \param tf_type ...
    3256              : !> \param n ...
    3257              : !> \param fft_sizes ...
    3258              : ! **************************************************************************************************
    3259      4404342 :    SUBROUTINE get_fft_scratch(fft_scratch, tf_type, n, fft_sizes)
    3260              :       TYPE(fft_scratch_type), POINTER          :: fft_scratch
    3261              :       INTEGER, INTENT(IN)                      :: tf_type
    3262              :       INTEGER, DIMENSION(:), INTENT(IN)        :: n
    3263              :       TYPE(fft_scratch_sizes), INTENT(IN), &
    3264              :          OPTIONAL                              :: fft_sizes
    3265              : 
    3266              :       CHARACTER(len=*), PARAMETER :: routineN = 'get_fft_scratch'
    3267              : 
    3268              :       INTEGER :: coord(2), DIM(2), handle, i, ix, iz, lg, lmax, m1, m2, &
    3269              :                  mcx2, mcy3, mcz1, mcz2, mg, mmax, mx1, mx2, my1, my3, mz1, mz2, mz3, &
    3270              :                  nbx, nbz, nm, nmax, nmray, np, nx, ny, nyzray, nz, pos(2)
    3271              :       INTEGER, DIMENSION(3)                    :: pcoord
    3272              :       LOGICAL                                  :: equal
    3273              :       LOGICAL, DIMENSION(2)                    :: dims
    3274              :       TYPE(fft_scratch_pool_type), POINTER     :: fft_scratch_current, &
    3275              :                                                   fft_scratch_last, &
    3276              :                                                   fft_scratch_new
    3277              : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
    3278              :       INTEGER                   :: ierr
    3279              :       INTEGER(KIND=C_SIZE_T)    :: length
    3280              :       TYPE(C_PTR)               :: cptr_r1buf, cptr_tbuf, &
    3281              :                                    cptr_p2buf, cptr_p3buf, cptr_p4buf, cptr_p5buf, cptr_p7buf
    3282              : #endif
    3283      4404342 :       CALL timeset(routineN, handle)
    3284              : 
    3285              :       ! this is the place to check that the scratch_pool does not grow without limits
    3286              :       ! before we add a new scratch check the size of the pool and release some of the list if needed
    3287      4404342 :       CALL resize_fft_scratch_pool()
    3288              : 
    3289              :       ! get the required scratch
    3290      4404342 : !$OMP ATOMIC
    3291              :       tick_fft_pool = tick_fft_pool + 1
    3292      4404342 :       fft_scratch_current => fft_scratch_first
    3293              :       DO
    3294     13523951 :          IF (ASSOCIATED(fft_scratch_current)) THEN
    3295     13483724 :             IF (fft_scratch_current%fft_scratch%in_use) THEN
    3296      4404342 :                fft_scratch_last => fft_scratch_current
    3297      4404342 :                fft_scratch_current => fft_scratch_current%fft_scratch_next
    3298      4404342 :                CYCLE
    3299              :             END IF
    3300      9079382 :             IF (tf_type /= fft_scratch_current%fft_scratch%tf_type) THEN
    3301       352994 :                fft_scratch_last => fft_scratch_current
    3302       352994 :                fft_scratch_current => fft_scratch_current%fft_scratch_next
    3303       352994 :                CYCLE
    3304              :             END IF
    3305     22135990 :             IF (.NOT. ALL(n == fft_scratch_current%fft_scratch%nfft)) THEN
    3306      4257246 :                fft_scratch_last => fft_scratch_current
    3307      4257246 :                fft_scratch_current => fft_scratch_current%fft_scratch_next
    3308      4257246 :                CYCLE
    3309              :             END IF
    3310      4469142 :             IF (PRESENT(fft_sizes)) THEN
    3311      3815333 :                IF (fft_sizes%rs_group /= fft_scratch_current%fft_scratch%group) THEN
    3312       104707 :                   fft_scratch_last => fft_scratch_current
    3313       104707 :                   fft_scratch_current => fft_scratch_current%fft_scratch_next
    3314       104707 :                   CYCLE
    3315              :                END IF
    3316      3710626 :                CALL is_equal(fft_sizes, fft_scratch_current%fft_scratch%sizes, equal)
    3317      3710626 :                IF (.NOT. equal) THEN
    3318          320 :                   fft_scratch_last => fft_scratch_current
    3319          320 :                   fft_scratch_current => fft_scratch_current%fft_scratch_next
    3320          320 :                   CYCLE
    3321              :                END IF
    3322              :             END IF
    3323              :             ! Success
    3324      4364115 :             fft_scratch => fft_scratch_current%fft_scratch
    3325      4364115 :             fft_scratch_current%fft_scratch%in_use = .TRUE.
    3326      4364115 :             EXIT
    3327              :          ELSE
    3328              :             ! We cannot find the scratch type in this pool
    3329              :             ! Generate a new scratch set
    3330        40227 :             ALLOCATE (fft_scratch_new)
    3331      1045902 :             ALLOCATE (fft_scratch_new%fft_scratch)
    3332              : 
    3333        40227 :             IF (tf_type /= 400) THEN
    3334        35972 :                fft_scratch_new%fft_scratch%sizes = fft_sizes
    3335        35972 :                np = fft_sizes%numtask
    3336              :                ALLOCATE (fft_scratch_new%fft_scratch%scount(0:np - 1), fft_scratch_new%fft_scratch%rcount(0:np - 1), &
    3337              :                          fft_scratch_new%fft_scratch%sdispl(0:np - 1), fft_scratch_new%fft_scratch%rdispl(0:np - 1), &
    3338       323748 :                          fft_scratch_new%fft_scratch%pgcube(0:np - 1, 2))
    3339              :             END IF
    3340              : 
    3341            0 :             SELECT CASE (tf_type)
    3342              :             CASE DEFAULT
    3343            0 :                CPABORT("Invalid scratch type.")
    3344              :             CASE (100) ! fft3d_pb: full cube distribution
    3345            0 :                CPASSERT(PRESENT(fft_sizes))
    3346            0 :                mx1 = fft_sizes%mx1
    3347            0 :                my1 = fft_sizes%my1
    3348            0 :                mx2 = fft_sizes%mx2
    3349            0 :                mz2 = fft_sizes%mz2
    3350            0 :                my3 = fft_sizes%my3
    3351            0 :                mz3 = fft_sizes%mz3
    3352            0 :                CALL fft_allocate(fft_scratch_new%fft_scratch%a1buf, [mx1*my1, n(3)])
    3353            0 :                CALL fft_allocate(fft_scratch_new%fft_scratch%a2buf, [n(3), mx1*my1])
    3354            0 :                CALL fft_allocate(fft_scratch_new%fft_scratch%a3buf, [mx2*mz2, n(2)])
    3355            0 :                CALL fft_allocate(fft_scratch_new%fft_scratch%a4buf, [n(2), mx2*mz2])
    3356            0 :                CALL fft_allocate(fft_scratch_new%fft_scratch%a5buf, [my3*mz3, n(1)])
    3357            0 :                CALL fft_allocate(fft_scratch_new%fft_scratch%a6buf, [n(1), my3*mz3])
    3358            0 :                fft_scratch_new%fft_scratch%group = fft_sizes%rs_group
    3359              : 
    3360            0 :                dim = fft_sizes%rs_group%num_pe_cart
    3361            0 :                pos = fft_sizes%rs_group%mepos_cart
    3362            0 :                fft_scratch_new%fft_scratch%mip = fft_sizes%rs_group%mepos
    3363            0 :                fft_scratch_new%fft_scratch%dim = dim
    3364            0 :                fft_scratch_new%fft_scratch%pos = pos
    3365            0 :                mcz1 = fft_sizes%mcz1
    3366            0 :                mcx2 = fft_sizes%mcx2
    3367            0 :                mcz2 = fft_sizes%mcz2
    3368            0 :                mcy3 = fft_sizes%mcy3
    3369            0 :                ALLOCATE (fft_scratch_new%fft_scratch%rbuf1(mx2*my1*mcz2, 0:DIM(2) - 1))
    3370            0 :                ALLOCATE (fft_scratch_new%fft_scratch%rbuf2(mx1*my1*mcz2, 0:DIM(2) - 1))
    3371            0 :                ALLOCATE (fft_scratch_new%fft_scratch%rbuf3(mx2*mz3*mcy3, 0:DIM(1) - 1))
    3372            0 :                ALLOCATE (fft_scratch_new%fft_scratch%rbuf4(mx2*mz2*mcy3, 0:DIM(1) - 1))
    3373              : 
    3374            0 :                dims = [.TRUE., .FALSE.]
    3375            0 :                CALL fft_scratch_new%fft_scratch%cart_sub_comm(1)%from_sub(fft_sizes%rs_group, dims)
    3376            0 :                dims = [.FALSE., .TRUE.]
    3377            0 :                CALL fft_scratch_new%fft_scratch%cart_sub_comm(2)%from_sub(fft_sizes%rs_group, dims)
    3378              : 
    3379              :                !initialise pgcube
    3380            0 :                DO i = 0, DIM(1) - 1
    3381            0 :                   coord = [i, pos(2)]
    3382            0 :                   CALL fft_sizes%rs_group%rank_cart(coord, fft_scratch_new%fft_scratch%pgcube(i, 1))
    3383              :                END DO
    3384            0 :                DO i = 0, DIM(2) - 1
    3385            0 :                   coord = [pos(1), i]
    3386            0 :                   CALL fft_sizes%rs_group%rank_cart(coord, fft_scratch_new%fft_scratch%pgcube(i, 2))
    3387              :                END DO
    3388              : 
    3389              :                !set up fft plans
    3390              :                CALL fft_create_plan_1d(fft_scratch_new%fft_scratch%fft_plan(1), FWFFT, .TRUE., .FALSE., &
    3391              :                                        mx1*my1, n(3), n(3), mx1*my1, &
    3392            0 :                                        fft_scratch_new%fft_scratch%a1buf, fft_scratch_new%fft_scratch%a2buf)
    3393              :                CALL fft_create_plan_1d(fft_scratch_new%fft_scratch%fft_plan(2), FWFFT, .TRUE., .FALSE., &
    3394              :                                        mx2*mz2, n(2), n(2), mx2*mz2, &
    3395            0 :                                        fft_scratch_new%fft_scratch%a3buf, fft_scratch_new%fft_scratch%a4buf)
    3396              :                CALL fft_create_plan_1d(fft_scratch_new%fft_scratch%fft_plan(3), FWFFT, .TRUE., .FALSE., &
    3397              :                                        my3*mz3, n(1), n(1), my3*mz3, &
    3398            0 :                                        fft_scratch_new%fft_scratch%a5buf, fft_scratch_new%fft_scratch%a6buf)
    3399              :                CALL fft_create_plan_1d(fft_scratch_new%fft_scratch%fft_plan(4), BWFFT, .FALSE., .TRUE., &
    3400              :                                        n(1), my3*mz3, n(1), my3*mz3, &
    3401            0 :                                        fft_scratch_new%fft_scratch%a6buf, fft_scratch_new%fft_scratch%a5buf)
    3402              :                CALL fft_create_plan_1d(fft_scratch_new%fft_scratch%fft_plan(5), BWFFT, .FALSE., .TRUE., &
    3403              :                                        n(2), mx2*mz2, n(2), mx2*mz2, &
    3404            0 :                                        fft_scratch_new%fft_scratch%a4buf, fft_scratch_new%fft_scratch%a3buf)
    3405              :                CALL fft_create_plan_1d(fft_scratch_new%fft_scratch%fft_plan(6), BWFFT, .FALSE., .TRUE., &
    3406              :                                        n(3), mx1*my1, n(3), mx1*my1, &
    3407            0 :                                        fft_scratch_new%fft_scratch%a2buf, fft_scratch_new%fft_scratch%a1buf)
    3408              : 
    3409              :             CASE (101) ! fft3d_pb: full cube distribution (dim 1)
    3410            8 :                CPASSERT(PRESENT(fft_sizes))
    3411            8 :                mx1 = fft_sizes%mx1
    3412            8 :                my1 = fft_sizes%my1
    3413            8 :                mz1 = fft_sizes%mz1
    3414            8 :                my3 = fft_sizes%my3
    3415            8 :                mz3 = fft_sizes%mz3
    3416           24 :                CALL fft_allocate(fft_scratch_new%fft_scratch%a1buf, [mx1*my1, n(3)])
    3417           24 :                CALL fft_allocate(fft_scratch_new%fft_scratch%a2buf, [n(3), mx1*my1])
    3418            8 :                fft_scratch_new%fft_scratch%group = fft_sizes%rs_group
    3419           24 :                CALL fft_allocate(fft_scratch_new%fft_scratch%a4buf, [n(2), mx1*mz1])
    3420           24 :                CALL fft_allocate(fft_scratch_new%fft_scratch%a5buf, [my3*mz3, n(1)])
    3421           24 :                CALL fft_allocate(fft_scratch_new%fft_scratch%a6buf, [n(1), my3*mz3])
    3422              : 
    3423           24 :                dim = fft_sizes%rs_group%num_pe_cart
    3424           24 :                pos = fft_sizes%rs_group%mepos_cart
    3425            8 :                fft_scratch_new%fft_scratch%mip = fft_sizes%rs_group%mepos
    3426           24 :                fft_scratch_new%fft_scratch%dim = dim
    3427           24 :                fft_scratch_new%fft_scratch%pos = pos
    3428            8 :                mcy3 = fft_sizes%mcy3
    3429           32 :                ALLOCATE (fft_scratch_new%fft_scratch%rbuf5(mx1*mz3*mcy3, 0:DIM(1) - 1))
    3430           32 :                ALLOCATE (fft_scratch_new%fft_scratch%rbuf6(mx1*mz1*mcy3, 0:DIM(1) - 1))
    3431              : 
    3432              :                !set up fft plans
    3433              :                CALL fft_create_plan_2d(fft_scratch_new%fft_scratch%fft_plan(1), FWFFT, .TRUE., .FALSE., &
    3434              :                                        [n(2), n(3)], mx1, &
    3435           24 :                                        fft_scratch_new%fft_scratch%a1buf, fft_scratch_new%fft_scratch%a4buf)
    3436              :                CALL fft_create_plan_1d(fft_scratch_new%fft_scratch%fft_plan(3), FWFFT, .TRUE., .FALSE., &
    3437              :                                        my3*mz3, n(1), n(1), my3*mz3, &
    3438            8 :                                        fft_scratch_new%fft_scratch%a5buf, fft_scratch_new%fft_scratch%a6buf)
    3439              :                CALL fft_create_plan_1d(fft_scratch_new%fft_scratch%fft_plan(4), BWFFT, .FALSE., .TRUE., &
    3440              :                                        n(1), my3*mz3, n(1), my3*mz3, &
    3441            8 :                                        fft_scratch_new%fft_scratch%a6buf, fft_scratch_new%fft_scratch%a5buf)
    3442              :                CALL fft_create_plan_2d(fft_scratch_new%fft_scratch%fft_plan(5), BWFFT, .FALSE., .TRUE., &
    3443           24 :                                        [n(2), n(3)], mx1, fft_scratch_new%fft_scratch%a4buf, fft_scratch_new%fft_scratch%a1buf)
    3444              : 
    3445              :             CASE (200) ! fft3d_ps: plane distribution
    3446        35964 :                CPASSERT(PRESENT(fft_sizes))
    3447        35964 :                nx = fft_sizes%nx
    3448        35964 :                ny = fft_sizes%ny
    3449        35964 :                nz = fft_sizes%nz
    3450        35964 :                mx2 = fft_sizes%mx2
    3451        35964 :                lmax = fft_sizes%lmax
    3452        35964 :                mmax = fft_sizes%mmax
    3453        35964 :                lg = fft_sizes%lg
    3454        35964 :                mg = fft_sizes%mg
    3455        35964 :                np = fft_sizes%numtask
    3456        35964 :                nmray = fft_sizes%nmray
    3457        35964 :                nyzray = fft_sizes%nyzray
    3458              : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
    3459              :                length = INT(2*dp_size*MAX(mmax, 1)*MAX(lmax, 1), KIND=C_SIZE_T)
    3460              :                ierr = offload_malloc_pinned_mem(cptr_r1buf, length)
    3461              :                CPASSERT(ierr == 0)
    3462              :                CALL c_f_pointer(cptr_r1buf, fft_scratch_new%fft_scratch%r1buf, [MAX(mmax, 1), MAX(lmax, 1)])
    3463              :                length = INT(2*dp_size*MAX(ny, 1)*MAX(nz, 1)*MAX(nx, 1), KIND=C_SIZE_T)
    3464              :                ierr = offload_malloc_pinned_mem(cptr_tbuf, length)
    3465              :                CPASSERT(ierr == 0)
    3466              :                CALL c_f_pointer(cptr_tbuf, fft_scratch_new%fft_scratch%tbuf, [MAX(ny, 1), MAX(nz, 1), MAX(nx, 1)])
    3467              : #else
    3468       107892 :                CALL fft_allocate(fft_scratch_new%fft_scratch%r1buf, [mmax, lmax])
    3469       143856 :                CALL fft_allocate(fft_scratch_new%fft_scratch%tbuf, [ny, nz, nx])
    3470              : #endif
    3471        35964 :                fft_scratch_new%fft_scratch%group = fft_sizes%rs_group
    3472       107892 :                CALL fft_allocate(fft_scratch_new%fft_scratch%r2buf, [lg, mg])
    3473        35964 :                nm = nmray*mx2
    3474        35964 :                IF (alltoall_sgl) THEN
    3475           32 :                   ALLOCATE (fft_scratch_new%fft_scratch%ss(mmax, lmax))
    3476           32 :                   ALLOCATE (fft_scratch_new%fft_scratch%tt(nm, 0:np - 1))
    3477              :                ELSE
    3478       143824 :                   ALLOCATE (fft_scratch_new%fft_scratch%rr(nm, 0:np - 1))
    3479              :                END IF
    3480              : 
    3481              :                !set up fft plans
    3482              :                CALL fft_create_plan_2d(fft_scratch_new%fft_scratch%fft_plan(1), FWFFT, .TRUE., .FALSE., &
    3483       107892 :                                        [ny, nz], nx, fft_scratch_new%fft_scratch%r1buf, fft_scratch_new%fft_scratch%tbuf)
    3484              :                CALL fft_create_plan_1d(fft_scratch_new%fft_scratch%fft_plan(3), FWFFT, .TRUE., .FALSE., &
    3485              :                                        nyzray, n(1), n(1), nyzray, &
    3486        35964 :                                        fft_scratch_new%fft_scratch%r1buf, fft_scratch_new%fft_scratch%r2buf)
    3487              :                CALL fft_create_plan_1d(fft_scratch_new%fft_scratch%fft_plan(4), BWFFT, .FALSE., .TRUE., &
    3488              :                                        n(1), nyzray, n(1), nyzray, &
    3489        35964 :                                        fft_scratch_new%fft_scratch%r2buf, fft_scratch_new%fft_scratch%r1buf)
    3490              :                CALL fft_create_plan_2d(fft_scratch_new%fft_scratch%fft_plan(5), BWFFT, .FALSE., .TRUE., &
    3491              :                                        [ny, nz], nx, &
    3492       107892 :                                        fft_scratch_new%fft_scratch%tbuf, fft_scratch_new%fft_scratch%r1buf)
    3493              : 
    3494              :             CASE (300) ! fft3d_ps: block distribution
    3495            0 :                CPASSERT(PRESENT(fft_sizes))
    3496            0 :                mx1 = fft_sizes%mx1
    3497            0 :                mx2 = fft_sizes%mx2
    3498            0 :                my1 = fft_sizes%my1
    3499            0 :                mz2 = fft_sizes%mz2
    3500            0 :                mcx2 = fft_sizes%mcx2
    3501            0 :                lg = fft_sizes%lg
    3502            0 :                mg = fft_sizes%mg
    3503            0 :                nmax = fft_sizes%nmax
    3504            0 :                nmray = fft_sizes%nmray
    3505            0 :                nyzray = fft_sizes%nyzray
    3506            0 :                m1 = fft_sizes%r_dim(1)
    3507            0 :                m2 = fft_sizes%r_dim(2)
    3508            0 :                nbx = fft_sizes%nbx
    3509            0 :                nbz = fft_sizes%nbz
    3510            0 :                CALL fft_allocate(fft_scratch_new%fft_scratch%p1buf, [mx1*my1, n(3)])
    3511            0 :                CALL fft_allocate(fft_scratch_new%fft_scratch%p6buf, [lg, mg])
    3512              : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
    3513              :                length = INT(2*dp_size*MAX(n(3), 1)*MAX(mx1*my1, 1), KIND=C_SIZE_T)
    3514              :                ierr = offload_malloc_pinned_mem(cptr_p2buf, length)
    3515              :                CPASSERT(ierr == 0)
    3516              :                CALL c_f_pointer(cptr_p2buf, fft_scratch_new%fft_scratch%p2buf, [MAX(n(3), 1), MAX(mx1*my1, 1)])
    3517              :                length = INT(2*dp_size*MAX(mx2*mz2, 1)*MAX(n(2), 1), KIND=C_SIZE_T)
    3518              :                ierr = offload_malloc_pinned_mem(cptr_p3buf, length)
    3519              :                CPASSERT(ierr == 0)
    3520              :                CALL c_f_pointer(cptr_p3buf, fft_scratch_new%fft_scratch%p3buf, [MAX(mx2*mz2, 1), MAX(n(2), 1)])
    3521              :                length = INT(2*dp_size*MAX(n(2), 1)*MAX(mx2*mz2, 1), KIND=C_SIZE_T)
    3522              :                ierr = offload_malloc_pinned_mem(cptr_p4buf, length)
    3523              :                CPASSERT(ierr == 0)
    3524              :                CALL c_f_pointer(cptr_p4buf, fft_scratch_new%fft_scratch%p4buf, [MAX(n(2), 1), MAX(mx2*mz2, 1)])
    3525              :                length = INT(2*dp_size*MAX(nyzray, 1)*MAX(n(1), 1), KIND=C_SIZE_T)
    3526              :                ierr = offload_malloc_pinned_mem(cptr_p5buf, length)
    3527              :                CPASSERT(ierr == 0)
    3528              :                CALL c_f_pointer(cptr_p5buf, fft_scratch_new%fft_scratch%p5buf, [MAX(nyzray, 1), MAX(n(1), 1)])
    3529              :                length = INT(2*dp_size*MAX(mg, 1)*MAX(lg, 1), KIND=C_SIZE_T)
    3530              :                ierr = offload_malloc_pinned_mem(cptr_p7buf, length)
    3531              :                CPASSERT(ierr == 0)
    3532              :                CALL c_f_pointer(cptr_p7buf, fft_scratch_new%fft_scratch%p7buf, [MAX(mg, 1), MAX(lg, 1)])
    3533              : #else
    3534            0 :                CALL fft_allocate(fft_scratch_new%fft_scratch%p2buf, [n(3), mx1*my1])
    3535            0 :                CALL fft_allocate(fft_scratch_new%fft_scratch%p3buf, [mx2*mz2, n(2)])
    3536            0 :                CALL fft_allocate(fft_scratch_new%fft_scratch%p4buf, [n(2), mx2*mz2])
    3537            0 :                CALL fft_allocate(fft_scratch_new%fft_scratch%p5buf, [nyzray, n(1)])
    3538            0 :                CALL fft_allocate(fft_scratch_new%fft_scratch%p7buf, [mg, lg])
    3539              : #endif
    3540            0 :                IF (alltoall_sgl) THEN
    3541            0 :                   ALLOCATE (fft_scratch_new%fft_scratch%yzbuf_sgl(mg*lg))
    3542            0 :                   ALLOCATE (fft_scratch_new%fft_scratch%xzbuf_sgl(n(2)*mx2*mz2))
    3543              :                ELSE
    3544            0 :                   ALLOCATE (fft_scratch_new%fft_scratch%yzbuf(mg*lg))
    3545            0 :                   ALLOCATE (fft_scratch_new%fft_scratch%xzbuf(n(2)*mx2*mz2))
    3546              :                END IF
    3547            0 :                ALLOCATE (fft_scratch_new%fft_scratch%pgrid(0:m1 - 1, 0:m2 - 1))
    3548            0 :                ALLOCATE (fft_scratch_new%fft_scratch%xcor(nbx))
    3549            0 :                ALLOCATE (fft_scratch_new%fft_scratch%zcor(nbz))
    3550            0 :                ALLOCATE (fft_scratch_new%fft_scratch%pzcoord(0:np - 1))
    3551              :                ALLOCATE (fft_scratch_new%fft_scratch%xzcount(0:np - 1), &
    3552            0 :                          fft_scratch_new%fft_scratch%yzcount(0:np - 1))
    3553              :                ALLOCATE (fft_scratch_new%fft_scratch%xzdispl(0:np - 1), &
    3554            0 :                          fft_scratch_new%fft_scratch%yzdispl(0:np - 1))
    3555            0 :                fft_scratch_new%fft_scratch%group = fft_sizes%rs_group
    3556              : 
    3557            0 :                dim = fft_sizes%rs_group%num_pe_cart
    3558            0 :                pos = fft_sizes%rs_group%mepos_cart
    3559            0 :                fft_scratch_new%fft_scratch%mip = fft_sizes%rs_group%mepos
    3560            0 :                fft_scratch_new%fft_scratch%dim = dim
    3561            0 :                fft_scratch_new%fft_scratch%pos = pos
    3562            0 :                mcz1 = fft_sizes%mcz1
    3563            0 :                mcz2 = fft_sizes%mcz2
    3564            0 :                ALLOCATE (fft_scratch_new%fft_scratch%rbuf1(mx2*my1*mcz2, 0:DIM(2) - 1))
    3565            0 :                ALLOCATE (fft_scratch_new%fft_scratch%rbuf2(mx1*my1*mcz2, 0:DIM(2) - 1))
    3566              : 
    3567            0 :                dims = [.FALSE., .TRUE.]
    3568            0 :                CALL fft_scratch_new%fft_scratch%cart_sub_comm(2)%from_sub(fft_sizes%rs_group, dims)
    3569              : 
    3570              :                !initialise pgcube
    3571            0 :                DO i = 0, DIM(2) - 1
    3572            0 :                   coord = [pos(1), i]
    3573            0 :                   CALL fft_sizes%rs_group%rank_cart(coord, fft_scratch_new%fft_scratch%pgcube(i, 2))
    3574              :                END DO
    3575              : 
    3576              :                !initialise pgrid
    3577            0 :                DO ix = 0, m1 - 1
    3578            0 :                   DO iz = 0, m2 - 1
    3579            0 :                      coord = [ix, iz]
    3580            0 :                      CALL fft_sizes%rs_group%rank_cart(coord, fft_scratch_new%fft_scratch%pgrid(ix, iz))
    3581              :                   END DO
    3582              :                END DO
    3583              : 
    3584              :                !initialise pzcoord
    3585            0 :                DO i = 0, np - 1
    3586            0 :                   CALL fft_sizes%rs_group%coords(i, pcoord)
    3587            0 :                   fft_scratch_new%fft_scratch%pzcoord(i) = pcoord(2)
    3588              :                END DO
    3589              : 
    3590              :                !set up fft plans
    3591              :                ! The scratch buffers must cover the geometry declared in each call
    3592              :                ! below, because the FFTW planner may write the buffers during
    3593              :                ! planning. The plans pair the buffers such that each of p1-p6 is
    3594              :                ! checked once; p7 shares the size of p6.
    3595            0 :                CPASSERT(SIZE(fft_scratch_new%fft_scratch%p1buf) >= mx1*my1*n(3))
    3596              :                CALL fft_create_plan_1d(fft_scratch_new%fft_scratch%fft_plan(1), FWFFT, .TRUE., .FALSE., &
    3597              :                                        mx1*my1, n(3), n(3), mx1*my1, &
    3598            0 :                                        fft_scratch_new%fft_scratch%p1buf, fft_scratch_new%fft_scratch%p2buf)
    3599            0 :                CPASSERT(SIZE(fft_scratch_new%fft_scratch%p3buf) >= mx2*mz2*n(2))
    3600              :                CALL fft_create_plan_1d(fft_scratch_new%fft_scratch%fft_plan(2), FWFFT, .TRUE., .FALSE., &
    3601              :                                        mx2*mz2, n(2), n(2), mx2*mz2, &
    3602            0 :                                        fft_scratch_new%fft_scratch%p3buf, fft_scratch_new%fft_scratch%p4buf)
    3603            0 :                CPASSERT(SIZE(fft_scratch_new%fft_scratch%p5buf) >= nyzray*n(1))
    3604              :                CALL fft_create_plan_1d(fft_scratch_new%fft_scratch%fft_plan(3), FWFFT, .TRUE., .FALSE., &
    3605              :                                        nyzray, n(1), n(1), nyzray, &
    3606            0 :                                        fft_scratch_new%fft_scratch%p5buf, fft_scratch_new%fft_scratch%p6buf)
    3607            0 :                CPASSERT(SIZE(fft_scratch_new%fft_scratch%p6buf) >= n(1)*nyzray)
    3608              :                CALL fft_create_plan_1d(fft_scratch_new%fft_scratch%fft_plan(4), BWFFT, .FALSE., .TRUE., &
    3609              :                                        n(1), nyzray, n(1), nyzray, &
    3610            0 :                                        fft_scratch_new%fft_scratch%p6buf, fft_scratch_new%fft_scratch%p7buf)
    3611            0 :                CPASSERT(SIZE(fft_scratch_new%fft_scratch%p4buf) >= n(2)*mx2*mz2)
    3612              :                CALL fft_create_plan_1d(fft_scratch_new%fft_scratch%fft_plan(5), BWFFT, .FALSE., .TRUE., &
    3613              :                                        n(2), mx2*mz2, n(2), mx2*mz2, &
    3614            0 :                                        fft_scratch_new%fft_scratch%p4buf, fft_scratch_new%fft_scratch%p3buf)
    3615            0 :                CPASSERT(SIZE(fft_scratch_new%fft_scratch%p2buf) >= n(3)*mx1*my1)
    3616              :                CALL fft_create_plan_1d(fft_scratch_new%fft_scratch%fft_plan(6), BWFFT, .FALSE., .TRUE., &
    3617              :                                        n(3), mx1*my1, n(3), mx1*my1, &
    3618            0 :                                        fft_scratch_new%fft_scratch%p2buf, fft_scratch_new%fft_scratch%p1buf)
    3619              : 
    3620              :             CASE (400) ! serial FFT
    3621         4255 :                np = 0
    3622         4255 :                CALL fft_allocate(fft_scratch_new%fft_scratch%ziptr, n)
    3623         4255 :                CALL fft_allocate(fft_scratch_new%fft_scratch%zoptr, n)
    3624              : 
    3625              :                !in place plans
    3626              :                CALL fft_create_plan_3d_inplace(fft_scratch_new%fft_scratch%fft_plan(1), FWFFT, n, &
    3627         4255 :                                                fft_scratch_new%fft_scratch%ziptr)
    3628              :                CALL fft_create_plan_3d_inplace(fft_scratch_new%fft_scratch%fft_plan(2), BWFFT, n, &
    3629         4255 :                                                fft_scratch_new%fft_scratch%ziptr)
    3630              :                ! out of place plans
    3631              :                CALL fft_create_plan_3d(fft_scratch_new%fft_scratch%fft_plan(3), FWFFT, n, &
    3632         4255 :                                        fft_scratch_new%fft_scratch%ziptr, fft_scratch_new%fft_scratch%zoptr)
    3633              :                CALL fft_create_plan_3d(fft_scratch_new%fft_scratch%fft_plan(4), BWFFT, n, &
    3634        44482 :                                        fft_scratch_new%fft_scratch%ziptr, fft_scratch_new%fft_scratch%zoptr)
    3635              : 
    3636              :             END SELECT
    3637              : 
    3638        40227 :             NULLIFY (fft_scratch_new%fft_scratch_next)
    3639              :             fft_scratch_new%fft_scratch%fft_scratch_id = &
    3640        40227 :                fft_scratch_last%fft_scratch%fft_scratch_id + 1
    3641        40227 :             fft_scratch_new%fft_scratch%in_use = .TRUE.
    3642       160908 :             fft_scratch_new%fft_scratch%nfft = n
    3643        40227 :             fft_scratch_last%fft_scratch_next => fft_scratch_new
    3644        40227 :             fft_scratch_new%fft_scratch%tf_type = tf_type
    3645        40227 :             fft_scratch => fft_scratch_new%fft_scratch
    3646        40227 :             EXIT
    3647              : 
    3648              :          END IF
    3649              :       END DO
    3650              : 
    3651      4404342 : !$OMP ATOMIC READ
    3652              :       fft_scratch%last_tick = tick_fft_pool
    3653              : 
    3654      4404342 :       CALL timestop(handle)
    3655              : 
    3656      4404342 :    END SUBROUTINE get_fft_scratch
    3657              : 
    3658              : ! **************************************************************************************************
    3659              : !> \brief ...
    3660              : !> \param fft_scratch ...
    3661              : ! **************************************************************************************************
    3662      4404342 :    SUBROUTINE release_fft_scratch(fft_scratch)
    3663              : 
    3664              :       TYPE(fft_scratch_type), POINTER                    :: fft_scratch
    3665              : 
    3666              :       INTEGER                                            :: scratch_id
    3667              :       TYPE(fft_scratch_pool_type), POINTER               :: fft_scratch_current
    3668              : 
    3669      4404342 :       scratch_id = fft_scratch%fft_scratch_id
    3670              : 
    3671      4404342 :       fft_scratch_current => fft_scratch_first
    3672      9119609 :       DO
    3673     13523951 :          IF (ASSOCIATED(fft_scratch_current)) THEN
    3674     13523951 :             IF (scratch_id == fft_scratch_current%fft_scratch%fft_scratch_id) THEN
    3675      4404342 :                fft_scratch%in_use = .FALSE.
    3676      4404342 :                NULLIFY (fft_scratch)
    3677      4404342 :                EXIT
    3678              :             END IF
    3679      9119609 :             fft_scratch_current => fft_scratch_current%fft_scratch_next
    3680              :          ELSE
    3681              :             ! We cannot find the scratch type in this pool
    3682            0 :             CPABORT("Invalid scratch type.")
    3683            0 :             EXIT
    3684              :          END IF
    3685              :       END DO
    3686              : 
    3687      4404342 :    END SUBROUTINE release_fft_scratch
    3688              : 
    3689              : ! **************************************************************************************************
    3690              : !> \brief ...
    3691              : !> \param rs ...
    3692              : !> \param scount ...
    3693              : !> \param sdispl ...
    3694              : !> \param rq ...
    3695              : !> \param rcount ...
    3696              : !> \param rdispl ...
    3697              : !> \param group ...
    3698              : ! **************************************************************************************************
    3699            0 :    SUBROUTINE sparse_alltoall(rs, scount, sdispl, rq, rcount, rdispl, group)
    3700              :       COMPLEX(KIND=dp), DIMENSION(:), POINTER            :: rs
    3701              :       INTEGER, DIMENSION(:), POINTER                     :: scount, sdispl
    3702              :       COMPLEX(KIND=dp), DIMENSION(:), POINTER            :: rq
    3703              :       INTEGER, DIMENSION(:), POINTER                     :: rcount, rdispl
    3704              : 
    3705              :       CLASS(mp_comm_type), INTENT(IN)                    :: group
    3706              : 
    3707            0 :       COMPLEX(KIND=dp), DIMENSION(:), POINTER            :: msgin, msgout
    3708              :       INTEGER                                            :: ip, n, nr, ns, pos
    3709            0 :       TYPE(mp_request_type), ALLOCATABLE, DIMENSION(:)   :: rreq, sreq
    3710              : 
    3711            0 :       CALL group%sync()
    3712            0 :       n = group%num_pe
    3713            0 :       pos = group%mepos
    3714            0 :       ALLOCATE (sreq(0:n - 1))
    3715            0 :       ALLOCATE (rreq(0:n - 1))
    3716            0 :       nr = 0
    3717            0 :       DO ip = 0, n - 1
    3718            0 :          IF (rcount(ip) == 0) CYCLE
    3719            0 :          IF (ip == pos) CYCLE
    3720            0 :          msgout => rq(rdispl(ip) + 1:rdispl(ip) + rcount(ip))
    3721            0 :          CALL group%irecv(msgout, ip, rreq(nr))
    3722            0 :          nr = nr + 1
    3723              :       END DO
    3724            0 :       ns = 0
    3725            0 :       DO ip = 0, n - 1
    3726            0 :          IF (scount(ip) == 0) CYCLE
    3727            0 :          IF (ip == pos) CYCLE
    3728            0 :          msgin => rs(sdispl(ip) + 1:sdispl(ip) + scount(ip))
    3729            0 :          CALL group%isend(msgin, ip, sreq(ns))
    3730            0 :          ns = ns + 1
    3731              :       END DO
    3732            0 :       IF (rcount(pos) /= 0) THEN
    3733            0 :          IF (rcount(pos) /= scount(pos)) CPABORT("Invalid count.")
    3734            0 :          rq(rdispl(pos) + 1:rdispl(pos) + rcount(pos)) = rs(sdispl(pos) + 1:sdispl(pos) + scount(pos))
    3735              :       END IF
    3736            0 :       CALL mp_waitall(sreq(0:ns - 1))
    3737            0 :       CALL mp_waitall(rreq(0:nr - 1))
    3738            0 :       DEALLOCATE (sreq)
    3739            0 :       DEALLOCATE (rreq)
    3740            0 :       CALL group%sync()
    3741              : 
    3742            0 :    END SUBROUTINE sparse_alltoall
    3743              : 
    3744              : ! **************************************************************************************************
    3745              : !> \brief  test data structures for equality. It is assumed that if they are
    3746              : !>         different for one mpi task they are different for all (??)
    3747              : !> \param fft_size_1 ...
    3748              : !> \param fft_size_2 ...
    3749              : !> \param equal ...
    3750              : ! **************************************************************************************************
    3751      3710626 :    SUBROUTINE is_equal(fft_size_1, fft_size_2, equal)
    3752              :       TYPE(fft_scratch_sizes)                            :: fft_size_1, fft_size_2
    3753              :       LOGICAL                                            :: equal
    3754              : 
    3755              :       equal = .TRUE.
    3756              : 
    3757      3710626 :       equal = equal .AND. fft_size_1%nx == fft_size_2%nx
    3758      3710626 :       equal = equal .AND. fft_size_1%ny == fft_size_2%ny
    3759      3710626 :       equal = equal .AND. fft_size_1%nz == fft_size_2%nz
    3760              : 
    3761      3710626 :       equal = equal .AND. fft_size_1%lmax == fft_size_2%lmax
    3762      3710626 :       equal = equal .AND. fft_size_1%mmax == fft_size_2%mmax
    3763      3710626 :       equal = equal .AND. fft_size_1%nmax == fft_size_2%nmax
    3764              : 
    3765      3710626 :       equal = equal .AND. fft_size_1%mx1 == fft_size_2%mx1
    3766      3710626 :       equal = equal .AND. fft_size_1%mx2 == fft_size_2%mx2
    3767      3710626 :       equal = equal .AND. fft_size_1%mx3 == fft_size_2%mx3
    3768              : 
    3769      3710626 :       equal = equal .AND. fft_size_1%my1 == fft_size_2%my1
    3770      3710626 :       equal = equal .AND. fft_size_1%my2 == fft_size_2%my2
    3771      3710626 :       equal = equal .AND. fft_size_1%my3 == fft_size_2%my3
    3772              : 
    3773      3710626 :       equal = equal .AND. fft_size_1%mcz1 == fft_size_2%mcz1
    3774      3710626 :       equal = equal .AND. fft_size_1%mcx2 == fft_size_2%mcx2
    3775      3710626 :       equal = equal .AND. fft_size_1%mcz2 == fft_size_2%mcz2
    3776      3710626 :       equal = equal .AND. fft_size_1%mcy3 == fft_size_2%mcy3
    3777              : 
    3778      3710626 :       equal = equal .AND. fft_size_1%lg == fft_size_2%lg
    3779      3710626 :       equal = equal .AND. fft_size_1%mg == fft_size_2%mg
    3780              : 
    3781      3710626 :       equal = equal .AND. fft_size_1%nbx == fft_size_2%nbx
    3782      3710626 :       equal = equal .AND. fft_size_1%nbz == fft_size_2%nbz
    3783              : 
    3784      3710626 :       equal = equal .AND. fft_size_1%nmray == fft_size_2%nmray
    3785      3710626 :       equal = equal .AND. fft_size_1%nyzray == fft_size_2%nyzray
    3786              : 
    3787      3710626 :       equal = equal .AND. fft_size_1%rs_group == fft_size_2%rs_group
    3788              : 
    3789     11131878 :       equal = equal .AND. ALL(fft_size_1%g_pos == fft_size_2%g_pos)
    3790     11131878 :       equal = equal .AND. ALL(fft_size_1%r_pos == fft_size_2%r_pos)
    3791     11131878 :       equal = equal .AND. ALL(fft_size_1%r_dim == fft_size_2%r_dim)
    3792              : 
    3793      3710626 :       equal = equal .AND. fft_size_1%numtask == fft_size_2%numtask
    3794              : 
    3795      3710626 :    END SUBROUTINE is_equal
    3796              : 
    3797            0 : END MODULE fft_tools
        

Generated by: LCOV version 2.0-1