LCOV - code coverage report
Current view: top level - src/grid - grid_unittest.c (source / functions) Hit Total Coverage
Test: CP2K Regtests (git:262480d) Lines: 49 56 87.5 %
Date: 2024-11-22 07:00:40 Functions: 4 4 100.0 %

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

Generated by: LCOV version 1.15