(git:5a3db73)
Loading...
Searching...
No Matches
dbm_library.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 "dbm_library.h"
8#include "../mpiwrap/cp_mpi.h"
11
12#include "../base/cp_assert.h"
13#include <inttypes.h>
14#include <omp.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
19#define DBM_LIBRARY_PRINT(FN, MSG, OUTPUT_UNIT) \
20 ((FN)(MSG, (int)strlen(MSG), OUTPUT_UNIT))
21
22static int64_t **per_thread_counters = NULL;
23static double phase_seconds[DBM_NUM_PHASES] = {0};
24static int64_t phase_bytes[DBM_NUM_PHASES] = {0};
25static bool library_initialized = false;
26static int max_threads = 0;
27
28#if !defined(_OPENMP)
29#error "OpenMP is required. Please add -fopenmp to your C compiler flags."
30#endif
31
32/*******************************************************************************
33 * \brief Initializes the DBM library.
34 * \author Ole Schuett
35 ******************************************************************************/
36void dbm_library_init(void) {
37 cp_assert(omp_get_num_threads() == 1);
38
40 fprintf(stderr, "DBM library was already initialized.\n");
41 abort();
42 }
43
45
46 max_threads = omp_get_max_threads();
47 memset(phase_seconds, 0, sizeof(phase_seconds));
48 memset(phase_bytes, 0, sizeof(phase_bytes));
49 per_thread_counters = malloc(max_threads * sizeof(int64_t *));
51
52 // Using parallel regions to ensure memory is allocated near a thread's core.
53#pragma omp parallel default(none) shared(per_thread_counters) \
54 num_threads(max_threads)
55 {
56 const int ithread = omp_get_thread_num();
57 const size_t counters_size = DBM_NUM_COUNTERS * sizeof(int64_t);
58 per_thread_counters[ithread] = malloc(counters_size);
59 cp_assert(per_thread_counters[ithread] != NULL);
60 memset(per_thread_counters[ithread], 0, counters_size);
61 }
62
64}
65
66/*******************************************************************************
67 * \brief Finalizes the DBM library.
68 * \author Ole Schuett
69 ******************************************************************************/
71 cp_assert(omp_get_num_threads() == 1);
72
74 fprintf(stderr, "Error: DBM library is not initialized.\n");
75 abort();
76 }
77
78 for (int i = 0; i < max_threads; i++) {
80 }
83
85 library_initialized = false;
86}
87
88/*******************************************************************************
89 * \brief Add the calling thread's counts to the stats. This routine is
90 * thread-safe.
91 * \author Hans Pabst
92 ******************************************************************************/
93void dbm_library_counters_add(const int64_t counters[DBM_NUM_COUNTERS]) {
94 const int ithread = omp_get_thread_num();
95 cp_assert(ithread < max_threads);
96 for (int i = 0; i < DBM_NUM_COUNTERS; i++) {
97 per_thread_counters[ithread][i] += counters[i];
98 }
99}
100
101/*******************************************************************************
102 * \brief Add the durations (seconds) of a multiplication's phases to the
103 * stats. Called once per dbm_multiply outside of parallel regions.
104 * \author Hans Pabst
105 ******************************************************************************/
106void dbm_library_phases_add(const double seconds[DBM_NUM_PHASES],
107 const int64_t bytes[DBM_NUM_PHASES]) {
108 cp_assert(omp_get_num_threads() == 1);
109 for (int i = 0; i < DBM_NUM_PHASES; i++) {
110 phase_seconds[i] += seconds[i];
111 phase_bytes[i] += bytes[i];
112 }
113}
114
115/*******************************************************************************
116 * \brief Comperator passed to qsort to compare two counters.
117 * \author Ole Schuett
118 ******************************************************************************/
119static int compare_counters(const void *a, const void *b) {
120 // Descending order: the difference of two counts may overflow int.
121 const int64_t x = *(const int64_t *)a, y = *(const int64_t *)b;
122 return (x < y) - (x > y);
123}
124
125/*******************************************************************************
126 * \brief Prints statistics gathered by the DBM library.
127 * \author Ole Schuett
128 ******************************************************************************/
129void dbm_library_print_stats(const int fortran_comm,
130 void (*print_func)(const char *, int, int),
131 const int output_unit) {
132 cp_assert(omp_get_num_threads() == 1);
133
134 if (!library_initialized) {
135 fprintf(stderr, "Error: DBM library is not initialized.\n");
136 abort();
137 }
138
139 const cp_mpi_comm_t comm = cp_mpi_comm_f2c(fortran_comm);
140 // Sum all counters across threads and mpi ranks.
141 int64_t counters[DBM_NUM_COUNTERS][2] = {{0}};
142 double total = 0.0;
143 for (int i = 0; i < DBM_NUM_COUNTERS; i++) {
144 counters[i][1] = i; // needed as inverse index after qsort
145 for (int j = 0; j < max_threads; j++) {
146 counters[i][0] += per_thread_counters[j][i];
147 }
148 cp_mpi_sum_int64(&counters[i][0], 1, comm);
149 total += counters[i][0];
150 }
151
152 // Phases per rank: minimum, average, and maximum over ranks (last: total).
153 double phase_min[DBM_NUM_PHASES + 1], phase_avg[DBM_NUM_PHASES + 1];
154 double phase_max[DBM_NUM_PHASES + 1];
155 phase_max[DBM_NUM_PHASES] = 0.0;
156 for (int i = 0; i < DBM_NUM_PHASES; i++) {
157 phase_max[i] = phase_seconds[i];
158 phase_max[DBM_NUM_PHASES] += phase_seconds[i];
159 }
160 for (int i = 0; i <= DBM_NUM_PHASES; i++) {
161 phase_min[i] = -phase_max[i]; // minimum as maximum of the negated values
162 phase_avg[i] = phase_max[i];
163 }
164 cp_mpi_max_double(phase_max, DBM_NUM_PHASES + 1, comm);
165 cp_mpi_max_double(phase_min, DBM_NUM_PHASES + 1, comm);
166 cp_mpi_sum_double(phase_avg, DBM_NUM_PHASES + 1, comm);
167 int64_t bytes[DBM_NUM_PHASES];
168 memcpy(bytes, phase_bytes, sizeof(bytes));
169 cp_mpi_sum_int64(bytes, DBM_NUM_PHASES, comm);
170 const int nranks = cp_mpi_comm_size(comm);
171
172 // Sort counters.
173 qsort(counters, DBM_NUM_COUNTERS, 2 * sizeof(int64_t), &compare_counters);
174
175 // Determine if anything needs to be printed.
176 bool print = false;
177 for (int i = 0; i < DBM_NUM_COUNTERS && !print; i++) {
178 if (counters[i][0] != 0) {
179 print = true;
180 }
181 }
182 if (!print) {
183 return; // nothing to be printed
184 }
185
186 // Print counters.
187 DBM_LIBRARY_PRINT(print_func, "\n", output_unit);
190 " ----------------------------------------------------------------"
191 "---------------\n",
192 output_unit);
195 " - "
196 " -\n",
197 output_unit);
200 " - DBM STATISTICS "
201 " -\n",
202 output_unit);
205 " - "
206 " -\n",
207 output_unit);
210 " ----------------------------------------------------------------"
211 "---------------\n",
212 output_unit);
215 " M x N x K "
216 "COUNT PERCENT\n",
217 output_unit);
218
219 const char *labels[] = {"?", "??", "???", ">999"};
220 char buffer[100];
221 for (int i = 0; i < DBM_NUM_COUNTERS; i++) {
222 if (counters[i][0] == 0) {
223 continue; // skip empty counters
224 }
225 const double percent = 100.0 * counters[i][0] / total;
226 const int idx = counters[i][1];
227 const int m = (idx % 64) / 16;
228 const int n = (idx % 16) / 4;
229 const int k = (idx % 4) / 1;
230 snprintf(buffer, sizeof(buffer),
231 " %4s x %4s x %4s %46" PRId64 " %10.2f%%\n", labels[m],
232 labels[n], labels[k], counters[i][0], percent);
233 DBM_LIBRARY_PRINT(print_func, buffer, output_unit);
234 }
235
236 // Print phases, i.e., where a rank spends the time of dbm_multiply.
237 if (0.0 < phase_max[DBM_NUM_PHASES]) {
238 const char *const phases[] = {"setup", "pack", "alltoall",
239 "sort", "shift", "upload",
240 "multiply", "finish", "total"};
243 " ----------------------------------------------------------------"
244 "---------------\n",
245 output_unit);
246 snprintf(buffer, sizeof(buffer), " %-24s %12s %12s %12s %12s\n",
247 "PHASE PER RANK", "MIN [s]", "AVG [s]", "MAX [s]", "GB/s");
248 DBM_LIBRARY_PRINT(print_func, buffer, output_unit);
249 for (int i = 0; i <= DBM_NUM_PHASES; i++) {
250 // Bytes sent over the time all ranks spent in the phase.
251 const bool rate =
252 (i < DBM_NUM_PHASES && 0 < bytes[i] && 0.0 < phase_avg[i]);
253 const int n = snprintf(
254 buffer, sizeof(buffer), " %-24s %12.3f %12.3f %12.3f", phases[i],
255 -phase_min[i], phase_avg[i] / nranks, phase_max[i]);
256 if (rate) {
257 snprintf(buffer + n, sizeof(buffer) - n, " %12.1f\n",
258 1E-9 * bytes[i] / phase_avg[i]);
259 } else {
260 snprintf(buffer + n, sizeof(buffer) - n, "\n");
261 }
262 DBM_LIBRARY_PRINT(print_func, buffer, output_unit);
263 }
264 }
265
268 " ----------------------------------------------------------------"
269 "---------------\n",
270 output_unit);
271}
272
273// EOF
#define cp_assert(condition)
Definition cp_assert.h:22
int cp_mpi_comm_size(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_size.
Definition cp_mpi.c:208
void cp_mpi_sum_double(double *values, const int count, const cp_mpi_comm_t comm)
Wrapper around MPI_Allreduce for op MPI_SUM and datatype MPI_DOUBLE.
Definition cp_mpi.c:523
void cp_mpi_max_double(double *values, const int count, const cp_mpi_comm_t comm)
Wrapper around MPI_Allreduce for op MPI_MAX and datatype MPI_DOUBLE.
Definition cp_mpi.c:404
void cp_mpi_sum_int64(int64_t *values, const int count, const cp_mpi_comm_t comm)
Wrapper around MPI_Allreduce for op MPI_SUM and datatype MPI_INT64_T.
Definition cp_mpi.c:499
cp_mpi_comm_t cp_mpi_comm_f2c(const int fortran_comm)
Wrapper around MPI_Comm_f2c.
Definition cp_mpi.c:162
int cp_mpi_comm_t
Definition cp_mpi.h:18
static bool library_initialized
Definition dbm_library.c:25
void dbm_library_finalize(void)
Finalizes the DBM library.
Definition dbm_library.c:70
void dbm_library_counters_add(const int64_t counters[DBM_NUM_COUNTERS])
Add the calling thread's counts to the stats. This routine is thread-safe.
Definition dbm_library.c:93
#define DBM_LIBRARY_PRINT(FN, MSG, OUTPUT_UNIT)
Definition dbm_library.c:19
void dbm_library_print_stats(const int fortran_comm, void(*print_func)(const char *, int, int), const int output_unit)
Prints statistics gathered by the DBM library.
static int64_t phase_bytes[DBM_NUM_PHASES]
Definition dbm_library.c:24
void dbm_library_init(void)
Initializes the DBM library.
Definition dbm_library.c:36
static int max_threads
Definition dbm_library.c:26
static double phase_seconds[DBM_NUM_PHASES]
Definition dbm_library.c:23
static int compare_counters(const void *a, const void *b)
Comperator passed to qsort to compare two counters.
void dbm_library_phases_add(const double seconds[DBM_NUM_PHASES], const int64_t bytes[DBM_NUM_PHASES])
Add the durations (seconds) of a multiplication's phases to the stats. Called once per dbm_multiply o...
static int64_t ** per_thread_counters
Definition dbm_library.c:22
#define DBM_NUM_COUNTERS
Definition dbm_library.h:26
@ DBM_NUM_PHASES
Definition dbm_library.h:62
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:26
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 offload_init(void)
Initialize runtime.
void offload_mempool_clear(void)
Internal routine for freeing all memory in the pool.