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