(git:ccc2433)
grid_unittest.c
Go to the documentation of this file.
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 static void mpi_sum_func(long *number, int mpi_comm) {
26  (void)number; // mark used
27  (void)mpi_comm;
28 }
29 
30 /*******************************************************************************
31  * \brief Wrapper for printf, passed to grid_library_print_stats.
32  * \author Ole Schuett
33  ******************************************************************************/
34 static void print_func(char *message, int output_unit) {
35  (void)output_unit; // mark used
36  printf("%s", message);
37 }
38 
39 /*******************************************************************************
40  * \brief Unit test for the grid code.
41  * \author Ole Schuett
42  ******************************************************************************/
43 static int run_test(const char cp2k_root_dir[], const char task_file[]) {
44  if (strlen(cp2k_root_dir) > 512) {
45  fprintf(stderr, "Error: cp2k_root_dir too long.\n");
46  abort();
47  }
48 
49  char filename[1024];
50  strcpy(filename, cp2k_root_dir);
51  if (filename[strlen(filename) - 1] != '/') {
52  strcat(filename, "/");
53  }
54 
55  strcat(filename, "src/grid/sample_tasks/");
56  strcat(filename, task_file);
57 
58  const double tolerance = 1e-12;
59  int errors = 0;
60  for (int icol = 0; icol < 2; icol++) {
61  for (int ibatch = 0; ibatch < 2; ibatch++) {
62  const bool success =
63  grid_replay(filename, 1, icol == 1, ibatch == 1, 1, tolerance);
64  if (!success) {
65  printf("Max diff too high, test failed.\n\n");
66  errors++;
67  }
68  }
69  }
70  return errors;
71 }
72 
73 int main(int argc, char *argv[]) {
74 #if defined(__parallel)
75  MPI_Init(&argc, &argv);
76 #endif
77 
78  if (argc != 2) {
79  printf("Usage: grid_unittest.x <cp2k-root-dir>\n");
80  return 1;
81  }
82 
85 
86  int errors = 0;
87  errors += run_test(argv[1], "ortho_density_l0000.task");
88  errors += run_test(argv[1], "ortho_density_l0122.task");
89  errors += run_test(argv[1], "ortho_density_l2200.task");
90  errors += run_test(argv[1], "ortho_density_l3300.task");
91  errors += run_test(argv[1], "ortho_density_l3333.task");
92  errors += run_test(argv[1], "ortho_density_l0505.task");
93  errors += run_test(argv[1], "ortho_non_periodic.task");
94  errors += run_test(argv[1], "ortho_tau.task");
95  errors += run_test(argv[1], "general_density.task");
96  errors += run_test(argv[1], "general_tau.task");
97  errors += run_test(argv[1], "general_subpatch0.task");
98  errors += run_test(argv[1], "general_subpatch16.task");
99  errors += run_test(argv[1], "general_overflow.task");
100 
103 
104  if (errors == 0) {
105  printf("\nAll tests have passed :-)\n");
106  } else {
107  printf("\nFound %i errors :-(\n", errors);
108  }
109 
110 #if defined(__parallel)
111  MPI_Finalize();
112 #endif
113 
114  return errors;
115 }
116 
117 // EOF
void grid_library_finalize(void)
Finalizes the grid library.
Definition: grid_library.c:83
void grid_library_print_stats(void(*mpi_sum_func)(long *, int), const int mpi_comm, void(*print_func)(char *, int), const int output_unit)
Prints statistics gathered by the grid library.
Definition: grid_library.c:155
void grid_library_init(void)
Initializes the grid library.
Definition: grid_library.c:49
bool grid_replay(const char *filename, const int cycles, const bool collocate, const bool batch, const int cycles_per_block, const double tolerance)
Reads a .task file, collocates/integrates it, and compares results. See grid_replay....
Definition: grid_replay.c:222
int main(int argc, char *argv[])
Definition: grid_unittest.c:73
static void print_func(char *message, int output_unit)
Wrapper for printf, passed to grid_library_print_stats.
Definition: grid_unittest.c:34
static int run_test(const char cp2k_root_dir[], const char task_file[])
Unit test for the grid code.
Definition: grid_unittest.c:43
static void mpi_sum_func(long *number, int mpi_comm)
Standin for mpi_sum, passed to grid_library_print_stats.
Definition: grid_unittest.c:25
subroutine, public offload_set_chosen_device(device_id)
Selects the chosen device to be used.
Definition: offload_api.F:132