LCOV - code coverage report
Current view: top level - src/grid - grid_unittest.c (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:936074a) Lines: 84.6 % 52 44
Test Date: 2025-12-04 06:27:48 Functions: 100.0 % 3 3

            Line data    Source code
       1              : /*----------------------------------------------------------------------------*/
       2              : /*  CP2K: A general program to perform molecular dynamics simulations         */
       3              : /*  Copyright 2000-2025 CP2K developers group <https://cp2k.org>              */
       4              : /*                                                                            */
       5              : /*  SPDX-License-Identifier: BSD-3-Clause                                     */
       6              : /*----------------------------------------------------------------------------*/
       7              : #include "../offload/offload_library.h"
       8              : #include "../offload/offload_mempool.h"
       9              : #include "common/grid_library.h"
      10              : #include "grid_replay.h"
      11              : 
      12              : #include <stdio.h>
      13              : #include <stdlib.h>
      14              : #include <string.h>
      15              : 
      16              : // Only used to call MPI_Init and MPI_Finalize to avoid spurious MPI error.
      17              : #if defined(__parallel)
      18              : #include <mpi.h>
      19              : #endif
      20              : 
      21              : /*******************************************************************************
      22              :  * \brief Wrapper for printf, passed to grid_library_print_stats.
      23              :  * \author Ole Schuett
      24              :  ******************************************************************************/
      25           64 : static void print_func(const char *msg, int msglen, int output_unit) {
      26           64 :   (void)msglen;           // mark used
      27           64 :   if (output_unit == 0) { // i.e. my_rank == 0
      28           64 :     printf("%s", msg);
      29              :   }
      30           64 : }
      31              : 
      32              : /*******************************************************************************
      33              :  * \brief Unit test for the grid code.
      34              :  * \author Ole Schuett
      35              :  ******************************************************************************/
      36           26 : static int run_test(const char cp2k_root_dir[], const char task_file[]) {
      37           26 :   if (strlen(cp2k_root_dir) > 512) {
      38            0 :     fprintf(stderr, "Error: cp2k_root_dir too long.\n");
      39            0 :     abort();
      40              :   }
      41              : 
      42           26 :   char filename[1024];
      43           26 :   strcpy(filename, cp2k_root_dir);
      44           26 :   if (filename[strlen(filename) - 1] != '/') {
      45           26 :     strcat(filename, "/");
      46              :   }
      47              : 
      48           26 :   strcat(filename, "src/grid/sample_tasks/");
      49           26 :   strcat(filename, task_file);
      50              : 
      51           26 :   const double tolerance = 1e-12;
      52           26 :   int errors = 0;
      53           78 :   for (int icol = 0; icol < 2; icol++) {
      54          156 :     for (int ibatch = 0; ibatch < 2; ibatch++) {
      55          104 :       const bool success =
      56          104 :           grid_replay(filename, 1, icol == 1, ibatch == 1, 1, tolerance);
      57          104 :       if (!success) {
      58            0 :         printf("Max diff too high, test failed.\n\n");
      59            0 :         errors++;
      60              :       }
      61              :     }
      62              :   }
      63           26 :   return errors;
      64              : }
      65              : 
      66            2 : int main(int argc, char *argv[]) {
      67              : #if defined(__parallel)
      68              :   MPI_Init(&argc, &argv);
      69              : #endif
      70              : 
      71            2 :   if (argc != 2) {
      72            0 :     printf("Usage: grid_unittest.x <cp2k-root-dir>\n");
      73            0 :     return 1;
      74              :   }
      75              : 
      76            2 :   offload_set_chosen_device(0);
      77            2 :   grid_library_init();
      78              : 
      79            2 :   int errors = 0;
      80            2 :   errors += run_test(argv[1], "ortho_density_l0000.task");
      81            2 :   errors += run_test(argv[1], "ortho_density_l0122.task");
      82            2 :   errors += run_test(argv[1], "ortho_density_l2200.task");
      83            2 :   errors += run_test(argv[1], "ortho_density_l3300.task");
      84            2 :   errors += run_test(argv[1], "ortho_density_l3333.task");
      85            2 :   errors += run_test(argv[1], "ortho_density_l0505.task");
      86            2 :   errors += run_test(argv[1], "ortho_non_periodic.task");
      87            2 :   errors += run_test(argv[1], "ortho_tau.task");
      88            2 :   errors += run_test(argv[1], "general_density.task");
      89            2 :   errors += run_test(argv[1], "general_tau.task");
      90            2 :   errors += run_test(argv[1], "general_subpatch0.task");
      91            2 :   errors += run_test(argv[1], "general_subpatch16.task");
      92            2 :   errors += run_test(argv[1], "general_overflow.task");
      93              : 
      94            2 :   if (errors == 0) {
      95            2 :     grid_library_print_stats(0 /*fortran_comm*/, &print_func, 0 /*rank*/);
      96            2 :     offload_mempool_stats_print(0 /*fortran_comm*/, &print_func, 0 /*rank*/);
      97            2 :     grid_library_finalize();
      98            2 :     printf("\nAll tests have passed :-)\n");
      99              :   } else {
     100            0 :     grid_library_finalize();
     101            0 :     printf("\nFound %i errors :-(\n", errors);
     102              :   }
     103              : 
     104              : #if defined(__parallel)
     105              :   MPI_Finalize();
     106              : #endif
     107              : 
     108              :   return errors;
     109              : }
     110              : 
     111              : // EOF
        

Generated by: LCOV version 2.0-1