(git:9ca3469)
Loading...
Searching...
No Matches
dbm_miniapp.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 "dbm_matrix.h"
9
10#include "../mpiwrap/cp_mpi.h"
11#include "../offload/offload_library.h"
12#include "../offload/offload_mempool.h"
13
14#include <assert.h>
15#include <omp.h>
16#include <stdint.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20
21/*******************************************************************************
22 * \brief Wrapper for printf, passed to dbm_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 Returns the smaller of the two integers (missing from the C standard).
34 * \author Ole Schuett
35 ******************************************************************************/
36static inline int imin(int x, int y) { return (x < y ? x : y); }
37
38/*******************************************************************************
39 * \brief Private routine for creating a distribution.
40 * \author Ole Schuett
41 ******************************************************************************/
42static dbm_distribution_t *create_dist(const int nrows, const int ncols,
43 const cp_mpi_comm_t comm) {
44 int cart_dims[2], cart_periods[2], cart_coords[2];
45 cp_mpi_cart_get(comm, 2, cart_dims, cart_periods, cart_coords);
46
47 // Create distribution.
48 assert(0 < nrows && 0 < ncols);
49 int *row_dist = malloc(nrows * sizeof(int));
50 int *col_dist = malloc(ncols * sizeof(int));
51 assert(row_dist != NULL && col_dist != NULL);
52 for (int i = 0; i < nrows; i++) {
53 row_dist[i] = i % cart_dims[0];
54 }
55 for (int i = 0; i < ncols; i++) {
56 col_dist[i] = i % cart_dims[1];
57 }
58 const int fortran_comm = cp_mpi_comm_c2f(comm);
59 dbm_distribution_t *dist = NULL;
60 dbm_distribution_new(&dist, fortran_comm, nrows, ncols, row_dist, col_dist);
61 free(row_dist);
62 free(col_dist);
63 return dist;
64}
65
66/*******************************************************************************
67 * \brief Private routine for creating a distribution and an empty matrix.
68 * \author Ole Schuett
69 ******************************************************************************/
70static dbm_matrix_t *
71create_some_matrix(const int nrows, const int ncols, const int nrows_min,
72 const int nrows_max, const int ncols_min,
73 const int ncols_max, const cp_mpi_comm_t comm) {
74 // Create distribution.
75 dbm_distribution_t *dist = create_dist(nrows, ncols, comm);
76
77 // Create matrix.
78 assert(0 < nrows && 0 < ncols);
79 int *row_sizes = malloc(nrows * sizeof(int));
80 int *col_sizes = malloc(ncols * sizeof(int));
81 assert(row_sizes != NULL && col_sizes != NULL);
82 assert(0 < nrows_min && nrows_min <= nrows_max);
83 assert(0 < ncols_min && ncols_min <= ncols_max);
84 if (nrows_min != nrows_max) {
85 const int row_size = nrows_max - nrows_min + 1;
86 for (int i = 0; i < nrows; i++) {
87 row_sizes[i] = rand() % row_size + 1;
88 }
89 } else {
90 for (int i = 0; i < nrows; i++) {
91 row_sizes[i] = nrows_max;
92 }
93 }
94 if (ncols_min != ncols_max) {
95 const int col_size = ncols_max - ncols_min + 1;
96 for (int i = 0; i < ncols; i++) {
97 col_sizes[i] = rand() % col_size + 1;
98 }
99 } else {
100 for (int i = 0; i < ncols; i++) {
101 col_sizes[i] = ncols_max;
102 }
103 }
104 dbm_matrix_t *matrix = NULL;
105 dbm_create(&matrix, dist, "some name", nrows, ncols, row_sizes, col_sizes);
106 dbm_distribution_release(dist);
107 free(row_sizes);
108 free(col_sizes);
109 return matrix;
110}
111
112/*******************************************************************************
113 * \brief Private routine for reserving all blocks of the given matrix.
114 * \author Ole Schuett
115 ******************************************************************************/
116static void reserve_all_blocks(dbm_matrix_t *matrix) {
117 int nrows, ncols;
118 const int *row_sizes, *col_sizes;
119 dbm_get_row_sizes(matrix, &nrows, &row_sizes);
120 dbm_get_col_sizes(matrix, &ncols, &col_sizes);
121
122#pragma omp parallel
123 {
124 int nblocks = 0;
125#pragma omp for collapse(2)
126 for (int row = 0; row < nrows; row++) {
127 for (int col = 0; col < ncols; col++) {
128 if (dbm_get_stored_coordinates(matrix, row, col) ==
129 matrix->dist->my_rank) {
130 ++nblocks;
131 }
132 }
133 }
134 int *reserve_row = NULL, *reserve_col = NULL;
135 if (0 < nblocks) {
136 reserve_row = malloc(nblocks * sizeof(int));
137 reserve_col = malloc(nblocks * sizeof(int));
138 assert(reserve_row != NULL && reserve_col != NULL);
139 }
140 int iblock = 0;
141#pragma omp for collapse(2)
142 for (int row = 0; row < nrows; row++) {
143 for (int col = 0; col < ncols; col++) {
144 if (dbm_get_stored_coordinates(matrix, row, col) ==
145 matrix->dist->my_rank) {
146 reserve_row[iblock] = row;
147 reserve_col[iblock] = col;
148 ++iblock;
149 }
150 }
151 }
152 assert(iblock == nblocks);
153 dbm_reserve_blocks(matrix, nblocks, reserve_row, reserve_col);
154 free(reserve_row);
155 free(reserve_col);
156 }
157}
158
159/*******************************************************************************
160 * \brief Private routine for setting all blocks.
161 * \author Ole Schuett
162 ******************************************************************************/
163static void set_all_blocks(dbm_matrix_t *matrix) {
164#pragma omp parallel
165 {
166 dbm_iterator_t *iter = NULL;
167 dbm_iterator_start(&iter, matrix);
168 while (dbm_iterator_blocks_left(iter)) {
169 int row, col, row_size, col_size;
170 double *block;
171 dbm_iterator_next_block(iter, &row, &col, &block, &row_size, &col_size);
172 const int block_size = row_size * col_size;
173 for (int i = 0; i < block_size; i++) {
174 block[i] = 1.0 / (i + 1);
175 }
176 }
177 dbm_iterator_stop(iter);
178 }
179}
180/*******************************************************************************
181 * \brief Run a benchmark of dbm_multiply with given block sizes.
182 * \author Ole Schuett
183 ******************************************************************************/
184void benchmark_multiply(const int M, const int N, const int K, const int m,
185 const int n, const int k, const cp_mpi_comm_t comm) {
186 dbm_matrix_t *matrix_a = create_some_matrix(M, K, 1, m, k, k, comm);
187 dbm_matrix_t *matrix_b = create_some_matrix(K, N, k, k, 1, n, comm);
188 dbm_distribution_t *dist_c = create_dist(M, N, comm);
189 dbm_matrix_t *matrix_c = NULL, *matrix_d = NULL;
190 dbm_create(&matrix_c, dist_c, "result", M, N, matrix_a->row_sizes,
191 matrix_b->col_sizes);
192 dbm_distribution_release(dist_c);
193
194 reserve_all_blocks(matrix_a);
195 reserve_all_blocks(matrix_b);
196 set_all_blocks(matrix_a);
197 set_all_blocks(matrix_b);
198
199 const char *const verify_env = getenv("DBM_MULTIPLY_VERIFY");
200 const int skip_verify = (NULL == verify_env ? 0 : (atoi(verify_env) + 1));
201
202 if (0 == skip_verify) {
203 dbm_distribution_t *const dist_shared = matrix_c->dist;
204 dbm_create(&matrix_d, dist_shared, matrix_c->name, matrix_c->nrows,
205 matrix_c->ncols, matrix_c->row_sizes, matrix_c->col_sizes);
206 dbm_copy(matrix_d, matrix_c);
207 }
208
209 int64_t flop = 0;
210 const double time_start_multiply = omp_get_wtime();
211 dbm_multiply(false, false, 1.0, matrix_a, matrix_b, 1.0, matrix_c, false,
212 1e-8, &flop);
213 const double time_end_multiply = omp_get_wtime();
214
215 if (cp_mpi_comm_rank(comm) == 0) {
216 printf("%5i x %5i x %5i with %3i x %3i x %3i blocks: ", M, N, K, m, n, k);
217 }
218
219 if (NULL != matrix_d) { // Calculate result on the host for validation.
220 dbm_multiply(false, false, 1.0, matrix_a, matrix_b, 1.0, matrix_d, false,
221 1e-8, NULL);
222
223 const double maxeps = 1E-5, epsilon = dbm_maxeps(matrix_d, matrix_c);
224 if (maxeps < epsilon) {
225 printf("ERROR\n");
226 fprintf(stderr, "Failed validation (epsilon=%f).\n", epsilon);
227 exit(1);
228 }
229 dbm_release(matrix_d);
230 }
231
232 cp_mpi_sum_int64(&flop, 1, comm);
233 if (cp_mpi_comm_rank(comm) == 0) {
234 const double duration = time_end_multiply - time_start_multiply;
235 printf("%6.3f s => %6.1f GFLOP/s\n", duration, 1e-9 * flop / duration);
236 fflush(stdout);
237 }
238
239 dbm_release(matrix_a);
240 dbm_release(matrix_b);
241 dbm_release(matrix_c);
242}
243
244/*******************************************************************************
245 * \brief Stand-alone miniapp for smoke-testing and benchmarking dbm_multiply.
246 * \author Ole Schuett
247 ******************************************************************************/
248int main(int argc, char *argv[]) {
249 int result = EXIT_SUCCESS;
250
251 srand(25071975); // seed rng
252
253 cp_mpi_init(&argc, &argv);
254 dbm_library_init();
255
256 const cp_mpi_comm_t world_comm = cp_mpi_get_comm_world();
257 const int nranks = cp_mpi_comm_size(world_comm);
258 const int my_rank = cp_mpi_comm_rank(world_comm);
259
260 if (offload_get_device_count() > 0) {
261 offload_set_chosen_device(my_rank % offload_get_device_count());
262 }
263
264 // Create 2D cart.
265 int dims[2] = {0, 0};
266 cp_mpi_dims_create(nranks, 2, dims);
267 const int periods[2] = {true, true};
268 cp_mpi_comm_t comm = cp_mpi_cart_create(world_comm, 2, dims, periods, false);
269
270 if (my_rank == 0) {
271 printf("OpenMP-threads: %i GPUs: %i", omp_get_max_threads(),
272 imin(offload_get_device_count(), nranks));
273#if defined(__parallel)
274 printf(" MPI-ranks: %i MPI-cart: %i x %i", nranks, dims[0], dims[1]);
275#else
276 printf(" MPI: n/a");
277#endif
278 printf("\n\n");
279 fflush(stdout);
280 }
281
282 if (1 >= argc) {
283 benchmark_multiply(16384, 128, 128, 4, 4, 4, comm);
284 benchmark_multiply(128, 16384, 128, 4, 4, 4, comm);
285 benchmark_multiply(128, 128, 16384, 4, 4, 4, comm);
286 benchmark_multiply(645, 645, 645, 4, 4, 4, comm);
287 if (my_rank == 0)
288 printf("\n");
289
290 benchmark_multiply(60, 500, 500, 128, 4, 4, comm);
291 benchmark_multiply(500, 60, 500, 4, 128, 4, comm);
292 benchmark_multiply(500, 500, 60, 4, 4, 128, comm);
293 if (my_rank == 0)
294 printf("\n");
295
296 benchmark_multiply(500, 60, 60, 4, 128, 128, comm);
297 benchmark_multiply(60, 500, 60, 128, 4, 128, comm);
298 benchmark_multiply(60, 60, 500, 128, 128, 4, comm);
299 if (my_rank == 0)
300 printf("\n");
301
302 benchmark_multiply(350, 350, 350, 23, 23, 23, comm);
303 benchmark_multiply(250, 250, 250, 32, 32, 32, comm);
304 benchmark_multiply(60, 60, 60, 128, 128, 128, comm);
305 } else { /* read triplet(s) from file or one triplet from command line */
306 FILE *const file = fopen(argv[1], "r"); /* try 1st arg as filename */
307 char buffer[1024];
308 const char delims[] = "x,;:|/\t ";
309 int mnk[] = {0, 0, 0}, i = 1, j = 0;
310 while (i < argc &&
311 (NULL == file || NULL != fgets(buffer, sizeof(buffer), file))) {
312 const char *arg = strtok(NULL != file ? buffer : argv[i], delims);
313 for (; NULL != arg && j < 3; arg = strtok(NULL, delims), ++j) {
314 mnk[j] = atoi(arg);
315 }
316 if (NULL != file) {
317 j = 0;
318 } else if (++i < argc) {
319 continue;
320 }
321 if (0 < mnk[0]) { /* valid MxNxK? */
322 const int m = mnk[0];
323 const int n = (0 < mnk[1] ? mnk[1] : m);
324 const int k = (0 < mnk[2] ? mnk[2] : m);
325 int M = (NULL == arg ? 0 : atoi(arg)), N, K;
326 if (0 < M) {
327 arg = strtok(NULL, delims);
328 N = (NULL == arg ? 1 : atoi(arg));
329 arg = strtok(NULL, delims);
330 K = (NULL == arg ? 1 : atoi(arg));
331 } else { /* default */
332 M = N = K = 128;
333 }
334 benchmark_multiply(M, N, K, m, n, k, comm);
335 mnk[0] = mnk[1] = mnk[2] = 0;
336 } else {
337 fprintf(stderr, "ERROR: invalid argument(s)\n");
338 result = EXIT_FAILURE;
339 i = argc; /* break */
340 }
341 }
342 if (NULL != file) {
343 fclose(file);
344 }
345 }
346
347 if (EXIT_SUCCESS == result) {
348 const int fortran_comm = cp_mpi_comm_c2f(comm);
349 dbm_library_print_stats(fortran_comm, &print_func, my_rank);
350 offload_mempool_stats_print(fortran_comm, &print_func, my_rank);
351 }
352 dbm_library_finalize();
353 cp_mpi_comm_free(&comm);
355 return result;
356}
357
358// EOF
int cp_mpi_comm_size(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_size.
Definition cp_mpi.c:113
cp_mpi_comm_t cp_mpi_cart_create(const cp_mpi_comm_t comm_old, const int ndims, const int dims[], const int periods[], const int reorder)
Wrapper around MPI_Cart_create.
Definition cp_mpi.c:145
void cp_mpi_init(int *argc, char ***argv)
Wrapper around MPI_Init.
Definition cp_mpi.c:34
int cp_mpi_comm_c2f(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_c2f.
Definition cp_mpi.c:83
cp_mpi_comm_t cp_mpi_get_comm_world(void)
Returns MPI_COMM_WORLD.
Definition cp_mpi.c:57
void cp_mpi_cart_get(const cp_mpi_comm_t comm, int maxdims, int dims[], int periods[], int coords[])
Wrapper around MPI_Cart_get.
Definition cp_mpi.c:166
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:363
void cp_mpi_comm_free(cp_mpi_comm_t *comm)
Wrapper around MPI_Comm_free.
Definition cp_mpi.c:217
void cp_mpi_dims_create(const int nnodes, const int ndims, int dims[])
Wrapper around MPI_Dims_create.
Definition cp_mpi.c:130
void cp_mpi_finalize(void)
Wrapper around MPI_Finalize.
Definition cp_mpi.c:47
int cp_mpi_comm_rank(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_rank.
Definition cp_mpi.c:96
int cp_mpi_comm_t
Definition cp_mpi.h:18
void dbm_get_col_sizes(const dbm_matrix_t *matrix, int *ncols, const int **col_sizes)
Returns the column block sizes of the given matrix.
Definition dbm_matrix.c:663
void dbm_get_row_sizes(const dbm_matrix_t *matrix, int *nrows, const int **row_sizes)
Returns the row block sizes of the given matrix.
Definition dbm_matrix.c:653
double dbm_maxeps(const dbm_matrix_t *matrix_a, const dbm_matrix_t *matrix_b)
Calculates maximum relative difference between matrix_a and matrix_b.
Definition dbm_matrix.c:565
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
static dbm_distribution_t * create_dist(const int nrows, const int ncols, const cp_mpi_comm_t comm)
Private routine for creating a distribution.
Definition dbm_miniapp.c:42
static int imin(int x, int y)
Returns the smaller of the two integers (missing from the C standard).
Definition dbm_miniapp.c:36
void benchmark_multiply(const int M, const int N, const int K, const int m, const int n, const int k, const cp_mpi_comm_t comm)
Run a benchmark of dbm_multiply with given block sizes.
static void set_all_blocks(dbm_matrix_t *matrix)
Private routine for setting all blocks.
static dbm_matrix_t * create_some_matrix(const int nrows, const int ncols, const int nrows_min, const int nrows_max, const int ncols_min, const int ncols_max, const cp_mpi_comm_t comm)
Private routine for creating a distribution and an empty matrix.
Definition dbm_miniapp.c:71
static void reserve_all_blocks(dbm_matrix_t *matrix)
Private routine for reserving all blocks of the given matrix.
static void const int const int i
int main()
Unit test of the C-interface provided via libcp2k.h.
Internal struct for storing a two dimensional distribution.
Internal struct for storing a block iterator.
Definition dbm_matrix.h:34
Internal struct for storing a matrix.
Definition dbm_matrix.h:19
int * row_sizes
Definition dbm_matrix.h:24
char * name
Definition dbm_matrix.h:21
int * col_sizes
Definition dbm_matrix.h:25
dbm_distribution_t * dist
Definition dbm_matrix.h:20