(git:e70887f)
Loading...
Searching...
No Matches
fft_unittest.c
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/*----------------------------------------------------------------------------*/
7
8#include <omp.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12
13#include "../mpiwrap/cp_mpi.h"
15#include "fft_lib.h"
16#include "fft_lib_test.h"
17#include "fft_timer.h"
18
19/*******************************************************************************
20 * \brief Run all FFT unit tests for the selected backend.
21 * \param backend FFT backend selector.
22 * \param planning_mode FFTW planning mode.
23 * \param use_guru Whether to use the FFTW Guru interface.
24 * \param threshold Minimum relative timing value to report.
25 * \return Number of failed checks.
26 ******************************************************************************/
27int run_unittests(const int backend, const int planning_mode,
28 const bool use_guru, const double threshold) {
29 int errors = 0;
31 fft_finalize_lib(NULL);
32
33 fflush(stdout);
35
36 fft_init_lib(backend, planning_mode, use_guru, NULL);
37
38 errors += fft_test_local();
40
41 fft_finalize_lib(NULL);
43
44 return errors;
45}
46
47/*******************************************************************************
48 * \brief Entry point of the FFT unit-test program.
49 * \param argc Number of command-line arguments.
50 * \param argv Command-line arguments.
51 * \return Exit status (0 on success).
52 ******************************************************************************/
53int main(int argc, char *argv[]) {
54 cp_mpi_init(&argc, &argv);
55
56 const int my_rank = cp_mpi_comm_rank(cp_mpi_get_comm_world());
57 const int device_count = offload_get_device_count();
58 if (device_count > 0) {
59 offload_set_chosen_device(my_rank % device_count);
60 }
61
62 const double threshold = 1e-4;
63 const int backend = fft_lib_default_library();
64
65 if (my_rank == 0) {
66 printf("Number of MPI ranks: %i\n",
68 int number_of_threads = 1;
69#pragma omp parallel default(none) shared(number_of_threads)
70 {
71#pragma omp single
72 {
73 number_of_threads = omp_get_num_threads();
74 }
75 }
76 printf("Number of OpenMP threads: %i\n", number_of_threads);
77 fflush(stdout);
78 }
80
81 // First run tests with ESTIMATE mode, which is faster and should be
82 // sufficient to catch most errors
83 int errors = run_unittests(backend, FFT_ESTIMATE, true, threshold);
84
86
87 errors += run_unittests(backend, FFT_ESTIMATE, false, threshold);
88 }
89
90 // Rerun the tests with MEASURE mode, which is slower but more accurate and
91 // should catch any remaining errors
92 errors += run_unittests(backend, FFT_MEASURE, true, threshold);
93
95
96 errors += run_unittests(backend, FFT_MEASURE, false, threshold);
97 }
98
99 fft_finalize_lib(NULL);
101
103 if (errors == 0) {
104 printf("\nAll tests have passed :-)\n");
105 } else {
106 printf("\nFound %i errors :-(\n", errors);
107 }
108 fflush(stdout);
109 }
111
113
114 return errors;
115}
116
117// EOF
int cp_mpi_comm_size(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_size.
Definition cp_mpi.c:208
void cp_mpi_init(int *argc, char ***argv)
Initialize MPI with MPI_THREAD_MULTIPLE or attach to an active MPI.
Definition cp_mpi.c:78
void cp_mpi_barrier(const cp_mpi_comm_t comm)
Wrapper around MPI_Barrier; a null communicator is a no-op.
Definition cp_mpi.c:224
cp_mpi_comm_t cp_mpi_get_comm_world(void)
Returns MPI_COMM_WORLD.
Definition cp_mpi.c:138
void cp_mpi_finalize(void)
Detach from MPI and finalize it only if cp_mpi_init initialized it.
Definition cp_mpi.c:112
int cp_mpi_comm_rank(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_rank.
Definition cp_mpi.c:191
int fft_lib_default_library()
Get the default library (GPU if offloading was enabled, else FFTW3).
Definition fft_lib.c:144
void fft_finalize_lib(const char *wisdom_file)
Finalize the FFT library (if not done externally).
Definition fft_lib.c:134
bool fft_lib_has_guru_interface()
Whether a compound MPI implementation is available.
Definition fft_lib.c:156
int fft_init_lib(const fft_lib lib, const int fftw_planning_flag, const bool use_guru_interface, const char *wisdom_file)
Initialize the FFT library (if not done externally).
Definition fft_lib.c:84
@ FFT_MEASURE
@ FFT_ESTIMATE
int fft_test_local()
Function to test the local FFT backend (1-3D).
void fft_print_timing_report(const double threshold, const cp_mpi_comm_t comm)
Print the FFT timing report to standard output.
Definition fft_timer.c:357
void fft_finalize_timer()
Finalizes the internal timer.
Definition fft_timer.c:367
int run_unittests(const int backend, const int planning_mode, const bool use_guru, const double threshold)
Run all FFT unit tests for the selected backend.
int main()
Unit test of the C-interface provided via libcp2k.h.
int offload_get_device_count(void)
Returns the number of available devices.
void offload_set_chosen_device(int device_id)
Selects the chosen device to be used.