LCOV - code coverage report
Current view: top level - src/fft - fft_api_unittest.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 86.1 % 144 124
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 8 8

            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: BSD-3-Clause                                                          !
       6              : !--------------------------------------------------------------------------------------------------!
       7            2 : PROGRAM fft_api_unittest
       8              :    USE fft_api,                         ONLY: &
       9              :         FFT_LIBRARY_BACKEND_DEFAULT, FFT_LIBRARY_FFTW_MODE_ESTIMATE, fft_1d_local, &
      10              :         fft_1d_local_inplace, fft_2d_local, fft_2d_local_inplace, fft_3d_local, &
      11              :         fft_3d_local_inplace, fft_library_finalize, fft_library_init, fft_register_1d_local, &
      12              :         fft_register_1d_local_inplace, fft_register_2d_local, fft_register_2d_local_inplace, &
      13            2 :         fft_register_3d_local, fft_register_3d_local_inplace
      14              :    USE kinds,                           ONLY: dp
      15              :    USE message_passing,                 ONLY: mp_comm_type,&
      16              :                                               mp_world_finalize,&
      17              :                                               mp_world_init
      18              :    USE offload_api,                     ONLY: offload_get_device_count,&
      19              :                                               offload_set_chosen_device
      20              : 
      21              :    IMPLICIT NONE
      22              : 
      23              :    TYPE(mp_comm_type)                                  :: mp_comm
      24              :    INTEGER                                             :: errors, my_rank, status
      25              : 
      26            2 :    CALL mp_world_init(mp_comm)
      27            2 :    my_rank = mp_comm%mepos
      28            2 :    IF (offload_get_device_count() > 0) THEN
      29            0 :       CALL offload_set_chosen_device(MOD(my_rank, offload_get_device_count()))
      30              :    END IF
      31              : 
      32              :    CALL fft_library_init(FFT_LIBRARY_BACKEND_DEFAULT, FFT_LIBRARY_FFTW_MODE_ESTIMATE, &
      33            2 :                          .TRUE., "", status)
      34            2 :    IF (status /= 0) ERROR STOP "FFT library initialization failed"
      35              : 
      36              :    errors = 0
      37            2 :    errors = errors + test_1d_c2c(15, 13, .TRUE., .TRUE.)
      38            2 :    errors = errors + test_1d_c2c(9, 11, .TRUE., .FALSE.)
      39            2 :    errors = errors + test_1d_c2c(10, 14, .FALSE., .TRUE.)
      40            2 :    errors = errors + test_1d_c2c(14, 13, .FALSE., .FALSE.)
      41              : 
      42            2 :    errors = errors + test_1d_c2c_inplace(15, 13, .TRUE.)
      43            2 :    errors = errors + test_1d_c2c_inplace(14, 13, .FALSE.)
      44              : 
      45            2 :    errors = errors + test_2d_c2c([10, 10], 10, .TRUE., .TRUE.)
      46            2 :    errors = errors + test_2d_c2c([8, 9], 10, .TRUE., .FALSE.)
      47            2 :    errors = errors + test_2d_c2c([7, 10], 11, .FALSE., .TRUE.)
      48            2 :    errors = errors + test_2d_c2c([6, 7], 10, .FALSE., .FALSE.)
      49              : 
      50            2 :    errors = errors + test_2d_c2c_inplace([8, 9], 10, .TRUE.)
      51            2 :    errors = errors + test_2d_c2c_inplace([7, 10], 11, .FALSE.)
      52              : 
      53            2 :    errors = errors + test_3d_c2c([8, 8, 8], .FALSE., .FALSE.)
      54            2 :    errors = errors + test_3d_c2c([7, 5, 3], .TRUE., .TRUE.)
      55              : 
      56            2 :    errors = errors + test_3d_c2c_inplace([8, 8, 8], .FALSE.)
      57            2 :    errors = errors + test_3d_c2c_inplace([7, 5, 3], .TRUE.)
      58              : 
      59            2 :    CALL fft_library_finalize("")
      60            2 :    CALL mp_world_finalize()
      61              : 
      62            2 :    IF (my_rank == 0) THEN
      63            1 :       IF (errors == 0) THEN
      64            1 :          WRITE (*, '(A)') "All Fortran FFT-API tests have passed :-)"
      65              :       ELSE
      66            0 :          WRITE (*, '(A,I0)') "Fortran FFT-API tests failed: ", errors
      67              :       END IF
      68              :    END IF
      69            2 :    IF (errors /= 0) ERROR STOP
      70              : 
      71              : CONTAINS
      72              : 
      73              : ! **************************************************************************************************
      74              : !> \brief ...
      75              : !> \param fft_size ...
      76              : !> \param number_of_ffts ...
      77              : !> \param transpose_in ...
      78              : !> \param transpose_out ...
      79              : !> \return ...
      80              : ! **************************************************************************************************
      81            8 :    INTEGER FUNCTION test_1d_c2c(fft_size, number_of_ffts, transpose_in, transpose_out)
      82              :       INTEGER, INTENT(IN)                                :: fft_size, number_of_ffts
      83              :       LOGICAL, INTENT(IN)                                :: transpose_in, transpose_out
      84              : 
      85            8 :       COMPLEX(KIND=dp), ALLOCATABLE                      :: input(:), output(:), reference(:)
      86              :       INTEGER                                            :: i, ld_in, ld_out
      87              :       REAL(KIND=dp)                                      :: max_error
      88              : 
      89            8 :       ld_in = MERGE(number_of_ffts, fft_size, transpose_in)
      90            8 :       ld_out = MERGE(number_of_ffts, fft_size, transpose_out)
      91              :       ALLOCATE (input(fft_size*number_of_ffts), output(fft_size*number_of_ffts), &
      92           40 :                 reference(fft_size*number_of_ffts))
      93         1240 :       DO i = 1, SIZE(input)
      94         1240 :          input(i) = CMPLX(SIN(0.1_dp*i), COS(0.07_dp*i), KIND=dp)
      95              :       END DO
      96         1240 :       reference(:) = input(:)
      97              : 
      98              :       CALL fft_register_1d_local(.TRUE., fft_size, number_of_ffts, transpose_in, transpose_out, &
      99            8 :                                  ld_in, ld_out, input, output)
     100              :       CALL fft_1d_local(.TRUE., fft_size, number_of_ffts, transpose_in, transpose_out, &
     101            8 :                         ld_in, ld_out, input, output)
     102              :       CALL fft_register_1d_local(.FALSE., fft_size, number_of_ffts, transpose_out, transpose_in, &
     103            8 :                                  ld_out, ld_in, output, input)
     104              :       CALL fft_1d_local(.FALSE., fft_size, number_of_ffts, transpose_out, transpose_in, &
     105            8 :                         ld_out, ld_in, output, input)
     106              : 
     107         1240 :       max_error = MAXVAL(ABS(input - REAL(fft_size, KIND=dp)*reference))
     108            8 :       test_1d_c2c = 0
     109            8 :       IF (max_error > 1.0E-8_dp) THEN
     110            0 :          IF (my_rank == 0) WRITE (*, '(A,F12.3,A,2I4,2L2)') &
     111            0 :             "1D C2C roundtrip error ", max_error, " size/batch ", fft_size, number_of_ffts, &
     112            0 :             transpose_in, transpose_out
     113              :          test_1d_c2c = 1
     114              :       END IF
     115            8 :       DEALLOCATE (input, output, reference)
     116            8 :    END FUNCTION test_1d_c2c
     117              : 
     118              : ! **************************************************************************************************
     119              : !> \brief ...
     120              : !> \param fft_size ...
     121              : !> \param number_of_ffts ...
     122              : !> \param transpose ...
     123              : !> \return ...
     124              : ! **************************************************************************************************
     125            4 :    INTEGER FUNCTION test_1d_c2c_inplace(fft_size, number_of_ffts, transpose)
     126              :       INTEGER, INTENT(IN)                                :: fft_size, number_of_ffts
     127              :       LOGICAL, INTENT(IN)                                :: transpose
     128              : 
     129            4 :       COMPLEX(KIND=dp), ALLOCATABLE                      :: grid(:), reference(:)
     130              :       INTEGER                                            :: i, ld
     131              :       REAL(KIND=dp)                                      :: max_error
     132              : 
     133            4 :       ld = MERGE(number_of_ffts, fft_size, transpose)
     134           16 :       ALLOCATE (grid(fft_size*number_of_ffts), reference(fft_size*number_of_ffts))
     135          758 :       DO i = 1, SIZE(grid)
     136          758 :          grid(i) = CMPLX(SIN(0.11_dp*i), COS(0.05_dp*i), KIND=dp)
     137              :       END DO
     138          758 :       reference(:) = grid(:)
     139            4 :       CALL fft_register_1d_local_inplace(.TRUE., fft_size, number_of_ffts, transpose, ld, grid)
     140            4 :       CALL fft_1d_local_inplace(.TRUE., fft_size, number_of_ffts, transpose, ld, grid)
     141            4 :       CALL fft_register_1d_local_inplace(.FALSE., fft_size, number_of_ffts, transpose, ld, grid)
     142            4 :       CALL fft_1d_local_inplace(.FALSE., fft_size, number_of_ffts, transpose, ld, grid)
     143              : 
     144          758 :       max_error = MAXVAL(ABS(grid - REAL(fft_size, KIND=dp)*reference))
     145            4 :       test_1d_c2c_inplace = 0
     146            4 :       IF (max_error > 1.0E-8_dp) THEN
     147            0 :          IF (my_rank == 0) WRITE (*, '(A,F12.3,A,2I4,L2)') &
     148            0 :             "1D C2C inplace roundtrip error ", max_error, " size/batch ", fft_size, &
     149            0 :             number_of_ffts, transpose
     150              :          test_1d_c2c_inplace = 1
     151              :       END IF
     152            4 :       DEALLOCATE (grid, reference)
     153            4 :    END FUNCTION test_1d_c2c_inplace
     154              : 
     155              : ! **************************************************************************************************
     156              : !> \brief ...
     157              : !> \param fft_size ...
     158              : !> \param number_of_ffts ...
     159              : !> \param transpose_in ...
     160              : !> \param transpose_out ...
     161              : !> \return ...
     162              : ! **************************************************************************************************
     163            8 :    INTEGER FUNCTION test_2d_c2c(fft_size, number_of_ffts, transpose_in, transpose_out)
     164              :       INTEGER, DIMENSION(2), INTENT(IN)                  :: fft_size
     165              :       INTEGER, INTENT(IN)                                :: number_of_ffts
     166              :       LOGICAL, INTENT(IN)                                :: transpose_in, transpose_out
     167              : 
     168            8 :       COMPLEX(KIND=dp), ALLOCATABLE                      :: input(:), output(:), reference(:)
     169              :       INTEGER                                            :: i
     170              :       REAL(KIND=dp)                                      :: max_error
     171              : 
     172              :       ALLOCATE (input(PRODUCT(fft_size)*number_of_ffts), &
     173              :                 output(PRODUCT(fft_size)*number_of_ffts), &
     174          104 :                 reference(PRODUCT(fft_size)*number_of_ffts))
     175         5828 :       DO i = 1, SIZE(input)
     176         5828 :          input(i) = CMPLX(SIN(0.09_dp*i), COS(0.04_dp*i), KIND=dp)
     177              :       END DO
     178         5828 :       reference(:) = input(:)
     179              : 
     180              :       CALL fft_register_2d_local(.TRUE., fft_size, number_of_ffts, transpose_in, transpose_out, &
     181            8 :                                  input, output)
     182            8 :       CALL fft_2d_local(.TRUE., fft_size, number_of_ffts, transpose_in, transpose_out, input, output)
     183              :       CALL fft_register_2d_local(.FALSE., fft_size, number_of_ffts, transpose_out, transpose_in, &
     184            8 :                                  output, input)
     185            8 :       CALL fft_2d_local(.FALSE., fft_size, number_of_ffts, transpose_out, transpose_in, output, input)
     186              : 
     187         5844 :       max_error = MAXVAL(ABS(input - REAL(PRODUCT(fft_size), KIND=dp)*reference))
     188            8 :       test_2d_c2c = 0
     189            8 :       IF (max_error > 1.0E-8_dp) THEN
     190            0 :          IF (my_rank == 0) WRITE (*, '(A,F12.3,A,3I4,2L2)') &
     191            0 :             "2D C2C roundtrip error ", max_error, " size/batch ", fft_size, number_of_ffts, &
     192            0 :             transpose_in, transpose_out
     193              :          test_2d_c2c = 1
     194              :       END IF
     195            8 :       DEALLOCATE (input, output, reference)
     196            8 :    END FUNCTION test_2d_c2c
     197              : 
     198              : ! **************************************************************************************************
     199              : !> \brief ...
     200              : !> \param fft_size ...
     201              : !> \param number_of_ffts ...
     202              : !> \param transpose ...
     203              : !> \return ...
     204              : ! **************************************************************************************************
     205            4 :    INTEGER FUNCTION test_2d_c2c_inplace(fft_size, number_of_ffts, transpose)
     206              :       INTEGER, DIMENSION(2), INTENT(IN)                  :: fft_size
     207              :       INTEGER, INTENT(IN)                                :: number_of_ffts
     208              :       LOGICAL, INTENT(IN)                                :: transpose
     209              : 
     210            4 :       COMPLEX(KIND=dp), ALLOCATABLE                      :: grid(:), reference(:)
     211              :       INTEGER                                            :: i
     212              :       REAL(KIND=dp)                                      :: max_error
     213              : 
     214              :       ALLOCATE (grid(PRODUCT(fft_size)*number_of_ffts), &
     215           36 :                 reference(PRODUCT(fft_size)*number_of_ffts))
     216         2984 :       DO i = 1, SIZE(grid)
     217         2984 :          grid(i) = CMPLX(SIN(0.08_dp*i), COS(0.03_dp*i), KIND=dp)
     218              :       END DO
     219         2984 :       reference(:) = grid(:)
     220            4 :       CALL fft_register_2d_local_inplace(.TRUE., fft_size, number_of_ffts, transpose, grid)
     221            4 :       CALL fft_2d_local_inplace(.TRUE., fft_size, number_of_ffts, transpose, grid)
     222            4 :       CALL fft_register_2d_local_inplace(.FALSE., fft_size, number_of_ffts, transpose, grid)
     223            4 :       CALL fft_2d_local_inplace(.FALSE., fft_size, number_of_ffts, transpose, grid)
     224              : 
     225         2992 :       max_error = MAXVAL(ABS(grid - REAL(PRODUCT(fft_size), KIND=dp)*reference))
     226            4 :       test_2d_c2c_inplace = 0
     227            4 :       IF (max_error > 1.0E-8_dp) THEN
     228            0 :          IF (my_rank == 0) WRITE (*, '(A,F12.3,A,3I4,L2)') &
     229            0 :             "2D C2C inplace roundtrip error ", max_error, " size/batch ", fft_size, &
     230            0 :             number_of_ffts, transpose
     231              :          test_2d_c2c_inplace = 1
     232              :       END IF
     233            4 :       DEALLOCATE (grid, reference)
     234            4 :    END FUNCTION test_2d_c2c_inplace
     235              : 
     236              : ! **************************************************************************************************
     237              : !> \brief ...
     238              : !> \param fft_size ...
     239              : !> \param transpose_in ...
     240              : !> \param transpose_out ...
     241              : !> \return ...
     242              : ! **************************************************************************************************
     243            4 :    INTEGER FUNCTION test_3d_c2c(fft_size, transpose_in, transpose_out)
     244              :       INTEGER, DIMENSION(3), INTENT(IN)                  :: fft_size
     245              :       LOGICAL, INTENT(IN)                                :: transpose_in, transpose_out
     246              : 
     247            4 :       COMPLEX(KIND=dp), ALLOCATABLE                      :: input(:), output(:), reference(:)
     248              :       INTEGER                                            :: i, number_of_ffts
     249              :       REAL(KIND=dp)                                      :: max_error
     250              : 
     251           16 :       number_of_ffts = PRODUCT(fft_size)
     252              :       ALLOCATE (input(PRODUCT(fft_size)*number_of_ffts), &
     253              :                 output(PRODUCT(fft_size)*number_of_ffts), &
     254           64 :                 reference(PRODUCT(fft_size)*number_of_ffts))
     255       546342 :       DO i = 1, SIZE(input)
     256       546342 :          input(i) = CMPLX(SIN(0.07_dp*i), COS(0.02_dp*i), KIND=dp)
     257              :       END DO
     258       546342 :       reference(:) = input(:)
     259              : 
     260              :       CALL fft_register_3d_local(.TRUE., fft_size, number_of_ffts, transpose_in, transpose_out, &
     261            4 :                                  input, output)
     262            4 :       CALL fft_3d_local(.TRUE., fft_size, number_of_ffts, transpose_in, transpose_out, input, output)
     263              :       CALL fft_register_3d_local(.FALSE., fft_size, number_of_ffts, transpose_out, transpose_in, &
     264            4 :                                  output, input)
     265            4 :       CALL fft_3d_local(.FALSE., fft_size, number_of_ffts, transpose_out, transpose_in, output, input)
     266              : 
     267       546354 :       max_error = MAXVAL(ABS(input - REAL(PRODUCT(fft_size), KIND=dp)*reference))
     268            4 :       test_3d_c2c = 0
     269            4 :       IF (max_error > 1.0E-8_dp) THEN
     270            0 :          IF (my_rank == 0) WRITE (*, '(A,F12.3,A,4I4,2L2)') &
     271            0 :             "3D C2C roundtrip error ", max_error, " size/batch ", fft_size, number_of_ffts, &
     272            0 :             transpose_in, transpose_out
     273              :          test_3d_c2c = 1
     274              :       END IF
     275            4 :       DEALLOCATE (input, output, reference)
     276            4 :    END FUNCTION test_3d_c2c
     277              : 
     278              : ! **************************************************************************************************
     279              : !> \brief ...
     280              : !> \param fft_size ...
     281              : !> \param transpose ...
     282              : !> \return ...
     283              : ! **************************************************************************************************
     284            4 :    INTEGER FUNCTION test_3d_c2c_inplace(fft_size, transpose)
     285              :       INTEGER, DIMENSION(3), INTENT(IN)                  :: fft_size
     286              :       LOGICAL, INTENT(IN)                                :: transpose
     287              : 
     288            4 :       COMPLEX(KIND=dp), ALLOCATABLE                      :: grid(:), reference(:)
     289              :       INTEGER                                            :: i, number_of_ffts
     290              :       REAL(KIND=dp)                                      :: max_error
     291              : 
     292           16 :       number_of_ffts = PRODUCT(fft_size)
     293              :       ALLOCATE (grid(PRODUCT(fft_size)*number_of_ffts), &
     294           44 :                 reference(PRODUCT(fft_size)*number_of_ffts))
     295       546342 :       DO i = 1, SIZE(grid)
     296       546342 :          grid(i) = CMPLX(SIN(0.06_dp*i), COS(0.015_dp*i), KIND=dp)
     297              :       END DO
     298       546342 :       reference(:) = grid(:)
     299            4 :       CALL fft_register_3d_local_inplace(.TRUE., fft_size, number_of_ffts, transpose, grid)
     300            4 :       CALL fft_3d_local_inplace(.TRUE., fft_size, number_of_ffts, transpose, grid)
     301            4 :       CALL fft_register_3d_local_inplace(.FALSE., fft_size, number_of_ffts, transpose, grid)
     302            4 :       CALL fft_3d_local_inplace(.FALSE., fft_size, number_of_ffts, transpose, grid)
     303              : 
     304       546354 :       max_error = MAXVAL(ABS(grid - REAL(PRODUCT(fft_size), KIND=dp)*reference))
     305            4 :       test_3d_c2c_inplace = 0
     306            4 :       IF (max_error > 1.0E-8_dp) THEN
     307            0 :          IF (my_rank == 0) WRITE (*, '(A,F12.3,A,4I4,L2)') &
     308            0 :             "3D C2C inplace roundtrip error ", max_error, " size/batch ", fft_size, &
     309            0 :             number_of_ffts, transpose
     310              :          test_3d_c2c_inplace = 1
     311              :       END IF
     312            4 :       DEALLOCATE (grid, reference)
     313            4 :    END FUNCTION test_3d_c2c_inplace
     314              : 
     315              : END PROGRAM fft_api_unittest
        

Generated by: LCOV version 2.0-1