(git:5a3db73)
Loading...
Searching...
No Matches
dbm_matrix.h
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#ifndef DBM_MATRIX_H
8#define DBM_MATRIX_H
9
10#include "dbm_distribution.h"
11#include "dbm_shard.h"
12
13#include <stdbool.h>
14
15/*******************************************************************************
16 * \brief Internal struct for storing a matrix.
17 * \author Ole Schuett
18 ******************************************************************************/
29
30/*******************************************************************************
31 * \brief Internal struct for storing a block iterator.
32 * \author Ole Schuett
33 ******************************************************************************/
34typedef struct {
39
40/*******************************************************************************
41 * \brief Creates a new matrix.
42 * \author Ole Schuett
43 ******************************************************************************/
44void dbm_create(dbm_matrix_t **matrix_out, dbm_distribution_t *dist,
45 const char name[], const int nrows, const int ncols,
46 const int row_sizes[nrows], const int col_sizes[ncols]);
47
48/*******************************************************************************
49 * \brief Releases a matrix and all its ressources.
50 * \author Ole Schuett
51 ******************************************************************************/
52void dbm_release(dbm_matrix_t *matrix);
53
54/*******************************************************************************
55 * \brief Copies content of matrix_b into matrix_a.
56 * Matrices must have the same row/col block sizes and distribution.
57 * \author Ole Schuett
58 ******************************************************************************/
59void dbm_copy(dbm_matrix_t *matrix_a, const dbm_matrix_t *matrix_b);
60
61/*******************************************************************************
62 * \brief Copies content of matrix_b into matrix_a.
63 * Matrices may have different distributions.
64 * \author Ole Schuett
65 ******************************************************************************/
66void dbm_redistribute(const dbm_matrix_t *matrix, dbm_matrix_t *redist);
67
68/*******************************************************************************
69 * \brief Looks up a block from given matrics. This routine is thread-safe.
70 * If the block is not found then a null pointer is returned.
71 * \author Ole Schuett
72 ******************************************************************************/
73void dbm_get_block_p(dbm_matrix_t *matrix, const int row, const int col,
74 double **block, int *row_size, int *col_size);
75
76/*******************************************************************************
77 * \brief Adds a block to given matrix. This routine is thread-safe.
78 * If block already exist then it gets overwritten (or summed).
79 * \author Ole Schuett
80 ******************************************************************************/
81void dbm_put_block(dbm_matrix_t *matrix, const int row, const int col,
82 const bool summation, const double *block);
83
84/*******************************************************************************
85 * \brief Remove all blocks from matrix, but does not release underlying memory.
86 * \author Ole Schuett
87 ******************************************************************************/
88void dbm_clear(dbm_matrix_t *matrix);
89
90/*******************************************************************************
91 * \brief Removes all blocks from the matrix whose norm is below the threshold.
92 * Blocks of size zero are always kept.
93 * \author Ole Schuett
94 ******************************************************************************/
95void dbm_filter(dbm_matrix_t *matrix, const double eps);
96
97/*******************************************************************************
98 * \brief Adds list of blocks efficiently. The blocks will be filled with zeros.
99 * This routine must always be called within an OpenMP parallel region.
100 * \author Ole Schuett
101 ******************************************************************************/
102void dbm_reserve_blocks(dbm_matrix_t *matrix, const int nblocks,
103 const int rows[], const int cols[]);
104
105/*******************************************************************************
106 * \brief Reserves template blocks in a matrix sharing the distribution object.
107 * Block sizes may differ. Existing values are kept, new blocks zeroed.
108 * Call outside an OpenMP parallel region.
109 ******************************************************************************/
111 const dbm_matrix_t *matrix_template);
112
113/*******************************************************************************
114 * \brief Multiplies all entries in the given matrix by the given factor alpha.
115 * \author Ole Schuett
116 ******************************************************************************/
117void dbm_scale(dbm_matrix_t *matrix, const double alpha);
118
119/*******************************************************************************
120 * \brief Sets all blocks in the given matrix to zero.
121 * \author Ole Schuett
122 ******************************************************************************/
123void dbm_zero(dbm_matrix_t *matrix);
124
125/*******************************************************************************
126 * \brief Adds matrix_b to matrix_a.
127 * \author Ole Schuett
128 ******************************************************************************/
129void dbm_add(dbm_matrix_t *matrix_a, const dbm_matrix_t *matrix_b);
130
131/*******************************************************************************
132 * \brief Creates an iterator for the blocks of the given matrix.
133 * The iteration order is not stable.
134 * This routine must always be called within an OpenMP parallel region.
135 * \author Ole Schuett
136 ******************************************************************************/
137void dbm_iterator_start(dbm_iterator_t **iter_out, const dbm_matrix_t *matrix);
138
139/*******************************************************************************
140 * \brief Returns number of blocks the iterator will provide to calling thread.
141 * \author Ole Schuett
142 ******************************************************************************/
144
145/*******************************************************************************
146 * \brief Tests whether the given iterator has any block left.
147 * \author Ole Schuett
148 ******************************************************************************/
150
151/*******************************************************************************
152 * \brief Returns the next block from the given iterator.
153 * \author Ole Schuett
154 ******************************************************************************/
155void dbm_iterator_next_block(dbm_iterator_t *iter, int *row, int *col,
156 double **block, int *row_size, int *col_size);
157
158/*******************************************************************************
159 * \brief Releases the given iterator.
160 * \author Ole Schuett
161 ******************************************************************************/
163
164/*******************************************************************************
165 * \brief Computes a checksum of the given matrix.
166 * \author Ole Schuett
167 ******************************************************************************/
168double dbm_checksum(const dbm_matrix_t *matrix);
169
170/*******************************************************************************
171 * \brief Returns the absolute value of the larges element of the entire matrix.
172 * \author Ole Schuett
173 ******************************************************************************/
174double dbm_maxabs(const dbm_matrix_t *matrix);
175
176/*******************************************************************************
177 * \brief Calculates maximum relative difference between matrix_a and matrix_b.
178 * \author Hans Pabst
179 ******************************************************************************/
180double dbm_maxeps(const dbm_matrix_t *matrix_a, const dbm_matrix_t *matrix_b);
181
182/*******************************************************************************
183 * \brief Returns the name of the matrix of the given matrix.
184 * \author Ole Schuett
185 ******************************************************************************/
186const char *dbm_get_name(const dbm_matrix_t *matrix);
187
188/*******************************************************************************
189 * \brief Returns the number of local Non-Zero Elements of the given matrix.
190 * \author Ole Schuett
191 ******************************************************************************/
192int dbm_get_nze(const dbm_matrix_t *matrix);
193
194/*******************************************************************************
195 * \brief Returns the number of local blocks of the given matrix.
196 * \author Ole Schuett
197 ******************************************************************************/
198int dbm_get_num_blocks(const dbm_matrix_t *matrix);
199
200/*******************************************************************************
201 * \brief Returns the row block sizes of the given matrix.
202 * \author Ole Schuett
203 ******************************************************************************/
204void dbm_get_row_sizes(const dbm_matrix_t *matrix, int *nrows,
205 const int **row_sizes);
206
207/*******************************************************************************
208 * \brief Returns the column block sizes of the given matrix.
209 * \author Ole Schuett
210 ******************************************************************************/
211void dbm_get_col_sizes(const dbm_matrix_t *matrix, int *ncols,
212 const int **col_sizes);
213
214/*******************************************************************************
215 * \brief Returns the local row block sizes of the given matrix.
216 * \author Ole Schuett
217 ******************************************************************************/
218void dbm_get_local_rows(const dbm_matrix_t *matrix, int *nlocal_rows,
219 const int **local_rows);
220
221/*******************************************************************************
222 * \brief Returns the local column block sizes of the given matrix.
223 * \author Ole Schuett
224 ******************************************************************************/
225void dbm_get_local_cols(const dbm_matrix_t *matrix, int *nlocal_cols,
226 const int **local_cols);
227
228/*******************************************************************************
229 * \brief Returns the MPI rank on which the given block should be stored.
230 * \author Ole Schuett
231 ******************************************************************************/
232int dbm_get_stored_coordinates(const dbm_matrix_t *matrix, const int row,
233 const int col);
234
235/*******************************************************************************
236 * \brief Returns the distribution of the given matrix.
237 * \author Ole Schuett
238 ******************************************************************************/
240
241/*******************************************************************************
242 * \brief Internal routine that returns the number of shards for given matrix.
243 * \author Ole Schuett
244 ******************************************************************************/
245static inline int dbm_get_num_shards(const dbm_matrix_t *matrix) {
246 return matrix->dist->rows.nshards * matrix->dist->cols.nshards;
247}
248
249/*******************************************************************************
250 * \brief Internal routine for getting a block's shard index.
251 * \author Ole Schuett
252 ******************************************************************************/
253static inline int dbm_get_shard_index(const dbm_matrix_t *matrix, const int row,
254 const int col) {
255 const int shard_row = row % matrix->dist->rows.nshards;
256 const int shard_col = col % matrix->dist->cols.nshards;
257 return shard_row * matrix->dist->cols.nshards + shard_col;
258}
259
260#endif
261
262// EOF
static int dbm_get_shard_index(const dbm_matrix_t *matrix, const int row, const int col)
Internal routine for getting a block's shard index.
Definition dbm_matrix.h:253
void dbm_reserve_blocks_template(dbm_matrix_t *matrix, const dbm_matrix_t *matrix_template)
Reserves template blocks in a matrix sharing the distribution object. Block sizes may differ....
Definition dbm_matrix.c:440
void dbm_get_block_p(dbm_matrix_t *matrix, const int row, const int col, double **block, int *row_size, int *col_size)
Looks up a block from given matrics. This routine is thread-safe. If the block is not found then a nu...
Definition dbm_matrix.c:214
void dbm_redistribute(const dbm_matrix_t *matrix, dbm_matrix_t *redist)
Copies content of matrix_b into matrix_a. Matrices may have different distributions.
Definition dbm_matrix.c:116
const dbm_distribution_t * dbm_get_distribution(const dbm_matrix_t *matrix)
Returns the distribution of the given matrix.
Definition dbm_matrix.c:810
const char * dbm_get_name(const dbm_matrix_t *matrix)
Returns the name of the matrix of the given matrix.
Definition dbm_matrix.c:731
void dbm_reserve_blocks(dbm_matrix_t *matrix, const int nblocks, const int rows[], const int cols[])
Adds list of blocks efficiently. The blocks will be filled with zeros. This routine must always be ca...
Definition dbm_matrix.c:338
static int dbm_get_num_shards(const dbm_matrix_t *matrix)
Internal routine that returns the number of shards for given matrix.
Definition dbm_matrix.h:245
bool dbm_iterator_blocks_left(const dbm_iterator_t *iter)
Tests whether the given iterator has any block left.
Definition dbm_matrix.c:577
double dbm_maxabs(const dbm_matrix_t *matrix)
Returns the absolute value of the larges element of the entire matrix.
Definition dbm_matrix.c:653
void dbm_filter(dbm_matrix_t *matrix, const double eps)
Removes all blocks from the matrix whose norm is below the threshold. Blocks of size zero are always ...
Definition dbm_matrix.c:285
void dbm_get_local_cols(const dbm_matrix_t *matrix, int *nlocal_cols, const int **local_cols)
Returns the local column block sizes of the given matrix.
Definition dbm_matrix.c:791
void dbm_add(dbm_matrix_t *matrix_a, const dbm_matrix_t *matrix_b)
Adds matrix_b to matrix_a.
Definition dbm_matrix.c:511
void dbm_get_local_rows(const dbm_matrix_t *matrix, int *nlocal_rows, const int **local_rows)
Returns the local row block sizes of the given matrix.
Definition dbm_matrix.c:781
void dbm_iterator_stop(dbm_iterator_t *iter)
Releases the given iterator.
Definition dbm_matrix.c:615
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:771
void dbm_create(dbm_matrix_t **matrix_out, dbm_distribution_t *dist, const char name[], const int nrows, const int ncols, const int row_sizes[nrows], const int col_sizes[ncols])
Creates a new matrix.
Definition dbm_matrix.c:22
void dbm_put_block(dbm_matrix_t *matrix, const int row, const int col, const bool summation, const double *block)
Adds a block to given matrix. This routine is thread-safe. If block already exist then it gets overwr...
Definition dbm_matrix.c:237
int dbm_iterator_num_blocks(const dbm_iterator_t *iter)
Returns number of blocks the iterator will provide to calling thread.
Definition dbm_matrix.c:563
void dbm_iterator_start(dbm_iterator_t **iter_out, const dbm_matrix_t *matrix)
Creates an iterator for the blocks of the given matrix. The iteration order is not stable....
Definition dbm_matrix.c:543
void dbm_iterator_next_block(dbm_iterator_t *iter, int *row, int *col, double **block, int *row_size, int *col_size)
Returns the next block from the given iterator.
Definition dbm_matrix.c:585
double dbm_checksum(const dbm_matrix_t *matrix)
Computes a checksum of the given matrix.
Definition dbm_matrix.c:636
int dbm_get_num_blocks(const dbm_matrix_t *matrix)
Returns the number of local blocks of the given matrix.
Definition dbm_matrix.c:749
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:761
void dbm_scale(dbm_matrix_t *matrix, const double alpha)
Multiplies all entries in the given matrix by the given factor alpha.
Definition dbm_matrix.c:472
void dbm_copy(dbm_matrix_t *matrix_a, const dbm_matrix_t *matrix_b)
Copies content of matrix_b into matrix_a. Matrices must have the same row/col block sizes and distrib...
Definition dbm_matrix.c:90
void dbm_clear(dbm_matrix_t *matrix)
Remove all blocks from matrix, but does not release underlying memory.
Definition dbm_matrix.c:268
void dbm_zero(dbm_matrix_t *matrix)
Sets all blocks in the given matrix to zero.
Definition dbm_matrix.c:495
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:669
int dbm_get_stored_coordinates(const dbm_matrix_t *matrix, const int row, const int col)
Returns the MPI rank on which the given block should be stored.
Definition dbm_matrix.c:801
int dbm_get_nze(const dbm_matrix_t *matrix)
Returns the number of local Non-Zero Elements of the given matrix.
Definition dbm_matrix.c:737
void dbm_release(dbm_matrix_t *matrix)
Releases a matrix and all its ressources.
Definition dbm_matrix.c:72
Internal struct for storing a two dimensional distribution.
Internal struct for storing a block iterator.
Definition dbm_matrix.h:34
const dbm_matrix_t * matrix
Definition dbm_matrix.h:35
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_shard_t * shards
Definition dbm_matrix.h:27
dbm_distribution_t * dist
Definition dbm_matrix.h:20
Internal struct for storing a matrix shard.
Definition dbm_shard.h:30