(git:bb35279)
Loading...
Searching...
No Matches
grid_unittest.c
Go to the documentation of this file.
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"
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 ******************************************************************************/
25static void print_func(const char *msg, int msglen, int output_unit) {
26 (void)msglen; // mark used
27 if (output_unit == 0) { // i.e. my_rank == 0
28 printf("%s", msg);
29 }
30}
31
32/*******************************************************************************
33 * \brief Unit test for the grid code.
34 * \author Ole Schuett
35 ******************************************************************************/
36static int run_test(const char cp2k_root_dir[], const char task_file[]) {
37 if (strlen(cp2k_root_dir) > 512) {
38 fprintf(stderr, "Error: cp2k_root_dir too long.\n");
39 abort();
40 }
41
42 char filename[1024];
43 strcpy(filename, cp2k_root_dir);
44 if (filename[strlen(filename) - 1] != '/') {
45 strcat(filename, "/");
46 }
47
48 strcat(filename, "src/grid/sample_tasks/");
49 strcat(filename, task_file);
50
51 const double tolerance = 1e-12;
52 int errors = 0;
53 for (int icol = 0; icol < 2; icol++) {
54 for (int ibatch = 0; ibatch < 2; ibatch++) {
55 const bool success =
56 grid_replay(filename, 1, icol == 1, ibatch == 1, 1, tolerance);
57 if (!success) {
58 printf("Max diff too high, test failed.\n\n");
59 errors++;
60 }
61 }
62 }
63 return errors;
64}
65
66int main(int argc, char *argv[]) {
67#if defined(__parallel)
68 MPI_Init(&argc, &argv);
69#endif
70
71 if (argc != 2) {
72 printf("Usage: grid_unittest.x <cp2k-root-dir>\n");
73 return 1;
74 }
75
76 offload_set_chosen_device(0);
78
79 int errors = 0;
80 errors += run_test(argv[1], "ortho_density_l0000.task");
81 errors += run_test(argv[1], "ortho_density_l0122.task");
82 errors += run_test(argv[1], "ortho_density_l2200.task");
83 errors += run_test(argv[1], "ortho_density_l3300.task");
84 errors += run_test(argv[1], "ortho_density_l3333.task");
85 errors += run_test(argv[1], "ortho_density_l0505.task");
86 errors += run_test(argv[1], "ortho_non_periodic.task");
87 errors += run_test(argv[1], "ortho_tau.task");
88 errors += run_test(argv[1], "general_density.task");
89 errors += run_test(argv[1], "general_tau.task");
90 errors += run_test(argv[1], "general_subpatch0.task");
91 errors += run_test(argv[1], "general_subpatch16.task");
92 errors += run_test(argv[1], "general_overflow.task");
93
94 if (errors == 0) {
95 grid_library_print_stats(0 /*fortran_comm*/, &print_func, 0 /*rank*/);
96 offload_mempool_stats_print(0 /*fortran_comm*/, &print_func, 0 /*rank*/);
98 printf("\nAll tests have passed :-)\n");
99 } else {
101 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
void grid_library_finalize(void)
Finalizes the grid library.
void grid_library_init(void)
Initializes the grid library.
void grid_library_print_stats(const int fortran_comm, void(*print_func)(const char *, int, int), const int output_unit)
Prints statistics gathered by the grid library.
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....
static void print_func(const char *msg, int msglen, int output_unit)
Wrapper for printf, passed to grid_library_print_stats.
static int run_test(const char cp2k_root_dir[], const char task_file[])
Unit test for the grid code.
int main()
Unit test of the C-interface provided via libcp2k.h.