LCOV - code coverage report
Current view: top level - src/fft - fft_unittest.c (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 95.5 % 44 42
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 2 2

            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              : 
       8              : #include <omp.h>
       9              : #include <stdio.h>
      10              : #include <stdlib.h>
      11              : #include <string.h>
      12              : 
      13              : #include "../mpiwrap/cp_mpi.h"
      14              : #include "../offload/offload_library.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              :  ******************************************************************************/
      27            8 : int run_unittests(const int backend, const int planning_mode,
      28              :                   const bool use_guru, const double threshold) {
      29            8 :   int errors = 0;
      30            8 :   fft_finalize_timer();
      31            8 :   fft_finalize_lib(NULL);
      32              : 
      33            8 :   fflush(stdout);
      34            8 :   cp_mpi_barrier(cp_mpi_get_comm_world());
      35              : 
      36            8 :   fft_init_lib(backend, planning_mode, use_guru, NULL);
      37              : 
      38            8 :   errors += fft_test_local();
      39            8 :   fft_print_timing_report(threshold, cp_mpi_get_comm_world());
      40              : 
      41            8 :   fft_finalize_lib(NULL);
      42            8 :   fft_finalize_timer();
      43              : 
      44            8 :   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              :  ******************************************************************************/
      53            2 : int main(int argc, char *argv[]) {
      54            2 :   cp_mpi_init(&argc, &argv);
      55              : 
      56            2 :   const int my_rank = cp_mpi_comm_rank(cp_mpi_get_comm_world());
      57            2 :   const int device_count = offload_get_device_count();
      58            2 :   if (device_count > 0) {
      59            0 :     offload_set_chosen_device(my_rank % device_count);
      60              :   }
      61              : 
      62            2 :   const double threshold = 1e-4;
      63            2 :   const int backend = fft_lib_default_library();
      64              : 
      65            2 :   if (my_rank == 0) {
      66            1 :     printf("Number of MPI ranks: %i\n",
      67            1 :            cp_mpi_comm_size(cp_mpi_get_comm_world()));
      68            1 :     int number_of_threads = 1;
      69            1 : #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            1 :     printf("Number of OpenMP threads: %i\n", number_of_threads);
      77            1 :     fflush(stdout);
      78              :   }
      79            2 :   cp_mpi_barrier(cp_mpi_get_comm_world());
      80              : 
      81              :   // First run tests with ESTIMATE mode, which is faster and should be
      82              :   // sufficient to catch most errors
      83            2 :   int errors = run_unittests(backend, FFT_ESTIMATE, true, threshold);
      84              : 
      85            2 :   if (fft_lib_has_guru_interface()) {
      86              : 
      87            2 :     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            2 :   errors += run_unittests(backend, FFT_MEASURE, true, threshold);
      93              : 
      94            2 :   if (fft_lib_has_guru_interface()) {
      95              : 
      96            2 :     errors += run_unittests(backend, FFT_MEASURE, false, threshold);
      97              :   }
      98              : 
      99            2 :   fft_finalize_lib(NULL);
     100            2 :   fft_finalize_timer();
     101              : 
     102            2 :   if (cp_mpi_comm_rank(cp_mpi_get_comm_world()) == 0) {
     103            1 :     if (errors == 0) {
     104            1 :       printf("\nAll tests have passed :-)\n");
     105              :     } else {
     106            0 :       printf("\nFound %i errors :-(\n", errors);
     107              :     }
     108            1 :     fflush(stdout);
     109              :   }
     110            2 :   cp_mpi_barrier(cp_mpi_get_comm_world());
     111              : 
     112            2 :   cp_mpi_finalize();
     113              : 
     114            2 :   return errors;
     115              : }
     116              : 
     117              : // EOF
        

Generated by: LCOV version 2.0-1