(git:e70887f)
Loading...
Searching...
No Matches
fft_api_unittest.F
Go to the documentation of this file.
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!--------------------------------------------------------------------------------------------------!
8 USE fft_api, ONLY: &
14 USE kinds, ONLY: dp
15 USE message_passing, ONLY: mp_comm_type,&
20
21 IMPLICIT NONE
22
23 TYPE(mp_comm_type) :: mp_comm
24 INTEGER :: errors, my_rank, status
25
26 CALL mp_world_init(mp_comm)
27 my_rank = mp_comm%mepos
28 IF (offload_get_device_count() > 0) THEN
30 END IF
31
33 .true., "", status)
34 IF (status /= 0) error stop "FFT library initialization failed"
35
36 errors = 0
37 errors = errors + test_1d_c2c(15, 13, .true., .true.)
38 errors = errors + test_1d_c2c(9, 11, .true., .false.)
39 errors = errors + test_1d_c2c(10, 14, .false., .true.)
40 errors = errors + test_1d_c2c(14, 13, .false., .false.)
41
42 errors = errors + test_1d_c2c_inplace(15, 13, .true.)
43 errors = errors + test_1d_c2c_inplace(14, 13, .false.)
44
45 errors = errors + test_2d_c2c([10, 10], 10, .true., .true.)
46 errors = errors + test_2d_c2c([8, 9], 10, .true., .false.)
47 errors = errors + test_2d_c2c([7, 10], 11, .false., .true.)
48 errors = errors + test_2d_c2c([6, 7], 10, .false., .false.)
49
50 errors = errors + test_2d_c2c_inplace([8, 9], 10, .true.)
51 errors = errors + test_2d_c2c_inplace([7, 10], 11, .false.)
52
53 errors = errors + test_3d_c2c([8, 8, 8], .false., .false.)
54 errors = errors + test_3d_c2c([7, 5, 3], .true., .true.)
55
56 errors = errors + test_3d_c2c_inplace([8, 8, 8], .false.)
57 errors = errors + test_3d_c2c_inplace([7, 5, 3], .true.)
58
61
62 IF (my_rank == 0) THEN
63 IF (errors == 0) THEN
64 WRITE (*, '(A)') "All Fortran FFT-API tests have passed :-)"
65 ELSE
66 WRITE (*, '(A,I0)') "Fortran FFT-API tests failed: ", errors
67 END IF
68 END IF
69 IF (errors /= 0) error stop
70
71CONTAINS
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 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 COMPLEX(KIND=dp), ALLOCATABLE :: input(:), output(:), reference(:)
86 INTEGER :: i, ld_in, ld_out
87 REAL(kind=dp) :: max_error
88
89 ld_in = merge(number_of_ffts, fft_size, transpose_in)
90 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 reference(fft_size*number_of_ffts))
93 DO i = 1, SIZE(input)
94 input(i) = cmplx(sin(0.1_dp*i), cos(0.07_dp*i), kind=dp)
95 END DO
96 reference(:) = input(:)
97
98 CALL fft_register_1d_local(.true., fft_size, number_of_ffts, transpose_in, transpose_out, &
99 ld_in, ld_out, input, output)
100 CALL fft_1d_local(.true., fft_size, number_of_ffts, transpose_in, transpose_out, &
101 ld_in, ld_out, input, output)
102 CALL fft_register_1d_local(.false., fft_size, number_of_ffts, transpose_out, transpose_in, &
103 ld_out, ld_in, output, input)
104 CALL fft_1d_local(.false., fft_size, number_of_ffts, transpose_out, transpose_in, &
105 ld_out, ld_in, output, input)
106
107 max_error = maxval(abs(input - real(fft_size, kind=dp)*reference))
108 test_1d_c2c = 0
109 IF (max_error > 1.0e-8_dp) THEN
110 IF (my_rank == 0) WRITE (*, '(A,F12.3,A,2I4,2L2)') &
111 "1D C2C roundtrip error ", max_error, " size/batch ", fft_size, number_of_ffts, &
112 transpose_in, transpose_out
113 test_1d_c2c = 1
114 END IF
115 DEALLOCATE (input, output, reference)
116 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 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 COMPLEX(KIND=dp), ALLOCATABLE :: grid(:), reference(:)
130 INTEGER :: i, ld
131 REAL(kind=dp) :: max_error
132
133 ld = merge(number_of_ffts, fft_size, transpose)
134 ALLOCATE (grid(fft_size*number_of_ffts), reference(fft_size*number_of_ffts))
135 DO i = 1, SIZE(grid)
136 grid(i) = cmplx(sin(0.11_dp*i), cos(0.05_dp*i), kind=dp)
137 END DO
138 reference(:) = grid(:)
139 CALL fft_register_1d_local_inplace(.true., fft_size, number_of_ffts, transpose, ld, grid)
140 CALL fft_1d_local_inplace(.true., fft_size, number_of_ffts, transpose, ld, grid)
141 CALL fft_register_1d_local_inplace(.false., fft_size, number_of_ffts, transpose, ld, grid)
142 CALL fft_1d_local_inplace(.false., fft_size, number_of_ffts, transpose, ld, grid)
143
144 max_error = maxval(abs(grid - real(fft_size, kind=dp)*reference))
145 test_1d_c2c_inplace = 0
146 IF (max_error > 1.0e-8_dp) THEN
147 IF (my_rank == 0) WRITE (*, '(A,F12.3,A,2I4,L2)') &
148 "1D C2C inplace roundtrip error ", max_error, " size/batch ", fft_size, &
149 number_of_ffts, transpose
150 test_1d_c2c_inplace = 1
151 END IF
152 DEALLOCATE (grid, reference)
153 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 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 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 reference(product(fft_size)*number_of_ffts))
175 DO i = 1, SIZE(input)
176 input(i) = cmplx(sin(0.09_dp*i), cos(0.04_dp*i), kind=dp)
177 END DO
178 reference(:) = input(:)
179
180 CALL fft_register_2d_local(.true., fft_size, number_of_ffts, transpose_in, transpose_out, &
181 input, output)
182 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 output, input)
185 CALL fft_2d_local(.false., fft_size, number_of_ffts, transpose_out, transpose_in, output, input)
186
187 max_error = maxval(abs(input - real(product(fft_size), kind=dp)*reference))
188 test_2d_c2c = 0
189 IF (max_error > 1.0e-8_dp) THEN
190 IF (my_rank == 0) WRITE (*, '(A,F12.3,A,3I4,2L2)') &
191 "2D C2C roundtrip error ", max_error, " size/batch ", fft_size, number_of_ffts, &
192 transpose_in, transpose_out
193 test_2d_c2c = 1
194 END IF
195 DEALLOCATE (input, output, reference)
196 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 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 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 reference(product(fft_size)*number_of_ffts))
216 DO i = 1, SIZE(grid)
217 grid(i) = cmplx(sin(0.08_dp*i), cos(0.03_dp*i), kind=dp)
218 END DO
219 reference(:) = grid(:)
220 CALL fft_register_2d_local_inplace(.true., fft_size, number_of_ffts, transpose, grid)
221 CALL fft_2d_local_inplace(.true., fft_size, number_of_ffts, transpose, grid)
222 CALL fft_register_2d_local_inplace(.false., fft_size, number_of_ffts, transpose, grid)
223 CALL fft_2d_local_inplace(.false., fft_size, number_of_ffts, transpose, grid)
224
225 max_error = maxval(abs(grid - real(product(fft_size), kind=dp)*reference))
226 test_2d_c2c_inplace = 0
227 IF (max_error > 1.0e-8_dp) THEN
228 IF (my_rank == 0) WRITE (*, '(A,F12.3,A,3I4,L2)') &
229 "2D C2C inplace roundtrip error ", max_error, " size/batch ", fft_size, &
230 number_of_ffts, transpose
231 test_2d_c2c_inplace = 1
232 END IF
233 DEALLOCATE (grid, reference)
234 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 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 COMPLEX(KIND=dp), ALLOCATABLE :: input(:), output(:), reference(:)
248 INTEGER :: i, number_of_ffts
249 REAL(kind=dp) :: max_error
250
251 number_of_ffts = product(fft_size)
252 ALLOCATE (input(product(fft_size)*number_of_ffts), &
253 output(product(fft_size)*number_of_ffts), &
254 reference(product(fft_size)*number_of_ffts))
255 DO i = 1, SIZE(input)
256 input(i) = cmplx(sin(0.07_dp*i), cos(0.02_dp*i), kind=dp)
257 END DO
258 reference(:) = input(:)
259
260 CALL fft_register_3d_local(.true., fft_size, number_of_ffts, transpose_in, transpose_out, &
261 input, output)
262 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 output, input)
265 CALL fft_3d_local(.false., fft_size, number_of_ffts, transpose_out, transpose_in, output, input)
266
267 max_error = maxval(abs(input - real(product(fft_size), kind=dp)*reference))
268 test_3d_c2c = 0
269 IF (max_error > 1.0e-8_dp) THEN
270 IF (my_rank == 0) WRITE (*, '(A,F12.3,A,4I4,2L2)') &
271 "3D C2C roundtrip error ", max_error, " size/batch ", fft_size, number_of_ffts, &
272 transpose_in, transpose_out
273 test_3d_c2c = 1
274 END IF
275 DEALLOCATE (input, output, reference)
276 END FUNCTION test_3d_c2c
277
278! **************************************************************************************************
279!> \brief ...
280!> \param fft_size ...
281!> \param transpose ...
282!> \return ...
283! **************************************************************************************************
284 INTEGER FUNCTION test_3d_c2c_inplace(fft_size, transpose)
285 INTEGER, DIMENSION(3), INTENT(IN) :: fft_size
286 LOGICAL, INTENT(IN) :: transpose
287
288 COMPLEX(KIND=dp), ALLOCATABLE :: grid(:), reference(:)
289 INTEGER :: i, number_of_ffts
290 REAL(kind=dp) :: max_error
291
292 number_of_ffts = product(fft_size)
293 ALLOCATE (grid(product(fft_size)*number_of_ffts), &
294 reference(product(fft_size)*number_of_ffts))
295 DO i = 1, SIZE(grid)
296 grid(i) = cmplx(sin(0.06_dp*i), cos(0.015_dp*i), kind=dp)
297 END DO
298 reference(:) = grid(:)
299 CALL fft_register_3d_local_inplace(.true., fft_size, number_of_ffts, transpose, grid)
300 CALL fft_3d_local_inplace(.true., fft_size, number_of_ffts, transpose, grid)
301 CALL fft_register_3d_local_inplace(.false., fft_size, number_of_ffts, transpose, grid)
302 CALL fft_3d_local_inplace(.false., fft_size, number_of_ffts, transpose, grid)
303
304 max_error = maxval(abs(grid - real(product(fft_size), kind=dp)*reference))
305 test_3d_c2c_inplace = 0
306 IF (max_error > 1.0e-8_dp) THEN
307 IF (my_rank == 0) WRITE (*, '(A,F12.3,A,4I4,L2)') &
308 "3D C2C inplace roundtrip error ", max_error, " size/batch ", fft_size, &
309 number_of_ffts, transpose
310 test_3d_c2c_inplace = 1
311 END IF
312 DEALLOCATE (grid, reference)
313 END FUNCTION test_3d_c2c_inplace
314
315END PROGRAM fft_api_unittest
integer function test_1d_c2c(fft_size, number_of_ffts, transpose_in, transpose_out)
...
program fft_api_unittest
Fortran API for the FFT package, which is written in C.
Definition fft_api.F:12
subroutine, public fft_register_3d_local_inplace(direction, fft_size, number_of_ffts, transpose, grid)
Registers a complex-to-complex 3D FFT.
Definition fft_api.F:662
subroutine, public fft_register_1d_local_inplace(direction, fft_size, number_of_ffts, transpose, leading_dimension, grid)
Registers a complex-to-complex 1D FFT.
Definition fft_api.F:595
subroutine, public fft_2d_local(direction, fft_size, number_of_ffts, transpose_in, transpose_out, grid_in, grid_out)
Performs a complex-to-complex 2D FFT.
Definition fft_api.F:742
subroutine, public fft_2d_local_inplace(direction, fft_size, number_of_ffts, transpose, grid)
Performs a complex-to-complex 2D FFT.
Definition fft_api.F:849
subroutine, public fft_register_3d_local(direction, fft_size, number_of_ffts, transpose_in, transpose_out, grid_in, grid_out)
Registers a complex-to-complex 3D FFT.
Definition fft_api.F:557
subroutine, public fft_register_2d_local(direction, fft_size, number_of_ffts, transpose_in, transpose_out, grid_in, grid_out)
Registers a complex-to-complex 2D FFT.
Definition fft_api.F:521
subroutine, public fft_3d_local(direction, fft_size, number_of_ffts, transpose_in, transpose_out, grid_in, grid_out)
Performs a complex-to-complex 3D FFT.
Definition fft_api.F:777
subroutine, public fft_1d_local(direction, fft_size, number_of_ffts, transpose_in, transpose_out, leading_dimension_in, leading_dimension_out, grid_in, grid_out)
Performs a complex-to-complex 1D FFT.
Definition fft_api.F:701
integer, parameter, public fft_library_fftw_mode_estimate
Definition fft_api.F:49
subroutine, public fft_library_init(backend, planning_mode, use_guru_interface, wisdom_file, status)
Initialize FFT library.
Definition fft_api.F:113
integer, parameter, public fft_library_backend_default
Definition fft_api.F:46
subroutine, public fft_3d_local_inplace(direction, fft_size, number_of_ffts, transpose, grid)
Performs a complex-to-complex 3D FFT.
Definition fft_api.F:880
subroutine, public fft_1d_local_inplace(direction, fft_size, number_of_ffts, transpose, leading_dimension, grid)
Performs a complex-to-complex 1D FFT.
Definition fft_api.F:814
subroutine, public fft_library_finalize(wisdom_file)
Finalize FFT library.
Definition fft_api.F:151
subroutine, public fft_register_2d_local_inplace(direction, fft_size, number_of_ffts, transpose, grid)
Registers a complex-to-complex 2D FFT.
Definition fft_api.F:630
subroutine, public fft_register_1d_local(direction, fft_size, number_of_ffts, transpose_in, transpose_out, leading_dimension_in, leading_dimension_out, grid_in, grid_out)
Registers a complex-to-complex 1D FFT.
Definition fft_api.F:479
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
Interface to the message passing library MPI.
subroutine, public mp_world_init(mp_comm)
initializes the system default communicator
subroutine, public mp_world_finalize()
Finalize the system default communicator and MPI when CP2K owns MPI.
Fortran API for the offload package, which is written in C.
Definition offload_api.F:12
subroutine, public offload_set_chosen_device(device_id)
Selects the chosen device to be used.
integer function, public offload_get_device_count()
Returns the number of available devices.