11#include "../../mpiwrap/cp_mpi.h"
12#include "../../offload/offload_runtime.h"
21#define GRID_LIBRARY_PRINT(FN, MSG, OUTPUT_UNIT) \
22 ((FN)(MSG, (int)strlen(MSG), OUTPUT_UNIT))
25#define GRID_NBACKENDS 5
26#define GRID_NKERNELS 4
41#error "OpenMP is required. Please add -fopenmp to your C compiler flags."
46 "Please do not build CP2K with NDEBUG. There is no performance advantage and asserts will save your neck."
55 printf(
"Error: Grid library was already initialized.\n");
59#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_GRID)
65 offloadEnsureMallocHeapSize(64 * 1024 * 1024);
73#pragma omp parallel default(none) shared(per_thread_globals) \
74 num_threads(max_threads)
76 const int ithread = omp_get_thread_num();
91 printf(
"Error: Grid library is not initialized.\n");
109 const int ithread = omp_get_thread_num();
137 const int increment) {
144 const int ithread = omp_get_thread_num();
154 return *(
long *)b - *(
long *)a;
163 const int output_unit) {
165 printf(
"Error: Grid library is not initialized.\n");
172 long counters[ncounters][2];
173 memset(counters, 0, ncounters * 2 *
sizeof(
long));
175 for (
int i = 0;
i < ncounters;
i++) {
181 total += counters[
i][0];
189 for (
int i = 0;
i < ncounters && !print;
i++) {
190 if (counters[
i][0] != 0) {
202 " ----------------------------------------------------------------"
212 " - GRID STATISTICS "
222 " ----------------------------------------------------------------"
227 " LP KERNEL BACKEND "
231 const char *kernel_names[] = {
"collocate ortho",
"integrate ortho",
232 "collocate general",
"integrate general"};
233 const char *backend_names[] = {
"REF",
"CPU",
"DGEMM",
"GPU",
"HIP"};
235 for (
int i = 0;
i < ncounters;
i++) {
236 if (counters[
i][0] == 0)
238 const double percent = 100.0 * counters[
i][0] / total;
239 const int idx = counters[
i][1];
241 const int back =
idx / backend_stride;
245 snprintf(buffer,
sizeof(buffer),
" %-5i %-17s %-6s %34li %10.2f%%\n", lp,
246 kernel_names[kern], backend_names[back], counters[
i][0], percent);
252 " ----------------------------------------------------------------"
void cp_mpi_sum_long(long *values, const int count, const cp_mpi_comm_t comm)
Wrapper around MPI_Allreduce for op MPI_SUM and datatype MPI_LONG.
cp_mpi_comm_t cp_mpi_comm_f2c(const int fortran_comm)
Wrapper around MPI_Comm_f2c.
static void print_func(const char *msg, int msglen, int output_unit)
Wrapper for printf, passed to dbm_library_print_stats.
static int imin(int x, int y)
Returns the smaller of the two integers (missing from the C standard).
static GRID_HOST_DEVICE int idx(const orbital a)
Return coset index of given orbital angular momentum.
static void const int const int i
void apply_cutoff(void *ptr)
void grid_library_finalize(void)
Finalizes the grid library.
static bool library_initialized
#define GRID_LIBRARY_PRINT(FN, MSG, OUTPUT_UNIT)
static grid_library_config config
grid_sphere_cache * grid_library_get_sphere_cache(void)
Returns a pointer to the thread local sphere cache.
void grid_library_init(void)
Initializes the grid library.
grid_library_config grid_library_get_config(void)
Returns the library config.
void grid_library_counter_add(const int lp, const enum grid_backend backend, const enum grid_library_kernel kernel, const int increment)
Adds given increment to counter specified by lp, backend, and kernel.
void grid_library_set_config(const enum grid_backend backend, const bool validate, const bool apply_cutoff)
Configures 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.
static int compare_counters(const void *a, const void *b)
Comperator passed to qsort to compare two counters.
static grid_library_globals ** per_thread_globals
grid_library_kernel
Various kernels provided by the grid library.
void grid_sphere_cache_free(grid_sphere_cache *cache)
Free the memory of the sphere cache.
Configuration of the grid library.
enum grid_backend backend
grid_sphere_cache sphere_cache
long counters[GRID_NBACKENDS *GRID_NKERNELS *GRID_MAX_LP]
Struct holding the entire sphere cache, ie. for all grids.