(git:f2099e5)
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-2026 CP2K developers group <https://cp2k.org> */
4/* */
5/* SPDX-License-Identifier: BSD-3-Clause */
6/*----------------------------------------------------------------------------*/
7#include "../mpiwrap/cp_mpi.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 ******************************************************************************/
21static void print_func(const char *msg, int msglen, int output_unit) {
22 (void)msglen; // mark used
23 if (output_unit == 0) { // i.e. my_rank == 0
24 printf("%s", msg);
25 }
26}
27
28/*******************************************************************************
29 * \brief Unit test for the grid code.
30 * \author Ole Schuett
31 ******************************************************************************/
32static int run_test(const char cp2k_root_dir[], const char task_file[]) {
33 if (strlen(cp2k_root_dir) > 512) {
34 fprintf(stderr, "Error: cp2k_root_dir too long.\n");
35 abort();
36 }
37
38 char filename[1024];
39 strcpy(filename, cp2k_root_dir);
40 if (filename[strlen(filename) - 1] != '/') {
41 strcat(filename, "/");
42 }
43
44 strcat(filename, "src/grid/sample_tasks/");
45 strcat(filename, task_file);
46
47 const double tolerance = 1e-12;
48 int errors = 0;
49 for (int icol = 0; icol < 2; icol++) {
50 for (int ibatch = 0; ibatch < 2; ibatch++) {
51 const bool success =
52 grid_replay(filename, 1, icol == 1, ibatch == 1, 1, tolerance);
53 if (!success) {
54 printf("Max diff too high, test failed.\n\n");
55 errors++;
56 }
57 }
58 }
59 return errors;
60}
61
62int main(int argc, char *argv[]) {
63 cp_mpi_init(&argc, &argv);
64
65 if (argc != 2) {
66 printf("Usage: grid_unittest.x <cp2k-root-dir>\n");
68 return 1;
69 }
70
73
74 int errors = 0;
75 errors += run_test(argv[1], "ortho_density_l0000.task");
76 errors += run_test(argv[1], "ortho_density_l0122.task");
77 errors += run_test(argv[1], "ortho_density_l2200.task");
78 errors += run_test(argv[1], "ortho_density_l3300.task");
79 errors += run_test(argv[1], "ortho_density_l3333.task");
80 errors += run_test(argv[1], "ortho_density_l0505.task");
81 errors += run_test(argv[1], "ortho_non_periodic.task");
82 errors += run_test(argv[1], "ortho_tau.task");
83 errors += run_test(argv[1], "general_density.task");
84 errors += run_test(argv[1], "general_tau.task");
85 errors += run_test(argv[1], "general_subpatch0.task");
86 errors += run_test(argv[1], "general_subpatch16.task");
87 errors += run_test(argv[1], "general_overflow.task");
88
89 if (errors == 0) {
90 grid_library_print_stats(0 /*fortran_comm*/, &print_func, 0 /*rank*/);
91 offload_mempool_stats_print(0 /*fortran_comm*/, &print_func, 0 /*rank*/);
93 printf("\nAll tests have passed :-)\n");
94 } else {
96 printf("\nFound %i errors :-(\n", errors);
97 }
98
100
101 return errors;
102}
103
104// EOF
void cp_mpi_init(int *argc, char ***argv)
Initialize MPI with MPI_THREAD_MULTIPLE or attach to an active MPI.
Definition cp_mpi.c:78
void cp_mpi_finalize(void)
Detach from MPI and finalize it only if cp_mpi_init initialized it.
Definition cp_mpi.c:112
static void print_func(const char *msg, int msglen, int output_unit)
Wrapper for printf, passed to dbm_library_print_stats.
Definition dbm_miniapp.c:25
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.
void offload_set_chosen_device(int device_id)
Selects the chosen device to be used.
void offload_mempool_stats_print(int fortran_comm, void(*print_func)(const char *, int, int), int output_unit)
Print allocation statistics..