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