Line data Source code
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 : #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 : ******************************************************************************/
19 : typedef struct {
20 : dbm_distribution_t *dist;
21 : char *name;
22 : int nrows;
23 : int ncols;
24 : int *row_sizes;
25 : int *col_sizes;
26 :
27 : dbm_shard_t *shards;
28 : } dbm_matrix_t;
29 :
30 : /*******************************************************************************
31 : * \brief Internal struct for storing a block iterator.
32 : * \author Ole Schuett
33 : ******************************************************************************/
34 : typedef struct {
35 : const dbm_matrix_t *matrix;
36 : int next_block;
37 : int next_shard;
38 : } dbm_iterator_t;
39 :
40 : /*******************************************************************************
41 : * \brief Creates a new matrix.
42 : * \author Ole Schuett
43 : ******************************************************************************/
44 : void 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 : ******************************************************************************/
52 : void 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 : ******************************************************************************/
59 : void 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 : ******************************************************************************/
66 : void 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 : ******************************************************************************/
73 : void 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 : ******************************************************************************/
81 : void 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 : ******************************************************************************/
88 : void 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 : ******************************************************************************/
95 : void 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 : ******************************************************************************/
102 : void dbm_reserve_blocks(dbm_matrix_t *matrix, const int nblocks,
103 : const int rows[], const int cols[]);
104 :
105 : /*******************************************************************************
106 : * \brief Multiplies all entries in the given matrix by the given factor alpha.
107 : * \author Ole Schuett
108 : ******************************************************************************/
109 : void dbm_scale(dbm_matrix_t *matrix, const double alpha);
110 :
111 : /*******************************************************************************
112 : * \brief Sets all blocks in the given matrix to zero.
113 : * \author Ole Schuett
114 : ******************************************************************************/
115 : void dbm_zero(dbm_matrix_t *matrix);
116 :
117 : /*******************************************************************************
118 : * \brief Adds matrix_b to matrix_a.
119 : * \author Ole Schuett
120 : ******************************************************************************/
121 : void dbm_add(dbm_matrix_t *matrix_a, const dbm_matrix_t *matrix_b);
122 :
123 : /*******************************************************************************
124 : * \brief Creates an iterator for the blocks of the given matrix.
125 : * The iteration order is not stable.
126 : * This routine must always be called within an OpenMP parallel region.
127 : * \author Ole Schuett
128 : ******************************************************************************/
129 : void dbm_iterator_start(dbm_iterator_t **iter_out, const dbm_matrix_t *matrix);
130 :
131 : /*******************************************************************************
132 : * \brief Returns number of blocks the iterator will provide to calling thread.
133 : * \author Ole Schuett
134 : ******************************************************************************/
135 : int dbm_iterator_num_blocks(const dbm_iterator_t *iter);
136 :
137 : /*******************************************************************************
138 : * \brief Tests whether the given iterator has any block left.
139 : * \author Ole Schuett
140 : ******************************************************************************/
141 : bool dbm_iterator_blocks_left(const dbm_iterator_t *iter);
142 :
143 : /*******************************************************************************
144 : * \brief Returns the next block from the given iterator.
145 : * \author Ole Schuett
146 : ******************************************************************************/
147 : void dbm_iterator_next_block(dbm_iterator_t *iter, int *row, int *col,
148 : double **block, int *row_size, int *col_size);
149 :
150 : /*******************************************************************************
151 : * \brief Releases the given iterator.
152 : * \author Ole Schuett
153 : ******************************************************************************/
154 : void dbm_iterator_stop(dbm_iterator_t *iter);
155 :
156 : /*******************************************************************************
157 : * \brief Computes a checksum of the given matrix.
158 : * \author Ole Schuett
159 : ******************************************************************************/
160 : double dbm_checksum(const dbm_matrix_t *matrix);
161 :
162 : /*******************************************************************************
163 : * \brief Returns the absolute value of the larges element of the entire matrix.
164 : * \author Ole Schuett
165 : ******************************************************************************/
166 : double dbm_maxabs(const dbm_matrix_t *matrix);
167 :
168 : /*******************************************************************************
169 : * \brief Calculates maximum relative difference between matrix_a and matrix_b.
170 : * \author Hans Pabst
171 : ******************************************************************************/
172 : double dbm_maxeps(const dbm_matrix_t *matrix_a, const dbm_matrix_t *matrix_b);
173 :
174 : /*******************************************************************************
175 : * \brief Returns the name of the matrix of the given matrix.
176 : * \author Ole Schuett
177 : ******************************************************************************/
178 : const char *dbm_get_name(const dbm_matrix_t *matrix);
179 :
180 : /*******************************************************************************
181 : * \brief Returns the number of local Non-Zero Elements of the given matrix.
182 : * \author Ole Schuett
183 : ******************************************************************************/
184 : int dbm_get_nze(const dbm_matrix_t *matrix);
185 :
186 : /*******************************************************************************
187 : * \brief Returns the number of local blocks of the given matrix.
188 : * \author Ole Schuett
189 : ******************************************************************************/
190 : int dbm_get_num_blocks(const dbm_matrix_t *matrix);
191 :
192 : /*******************************************************************************
193 : * \brief Returns the row block sizes of the given matrix.
194 : * \author Ole Schuett
195 : ******************************************************************************/
196 : void dbm_get_row_sizes(const dbm_matrix_t *matrix, int *nrows,
197 : const int **row_sizes);
198 :
199 : /*******************************************************************************
200 : * \brief Returns the column block sizes of the given matrix.
201 : * \author Ole Schuett
202 : ******************************************************************************/
203 : void dbm_get_col_sizes(const dbm_matrix_t *matrix, int *ncols,
204 : const int **col_sizes);
205 :
206 : /*******************************************************************************
207 : * \brief Returns the local row block sizes of the given matrix.
208 : * \author Ole Schuett
209 : ******************************************************************************/
210 : void dbm_get_local_rows(const dbm_matrix_t *matrix, int *nlocal_rows,
211 : const int **local_rows);
212 :
213 : /*******************************************************************************
214 : * \brief Returns the local column block sizes of the given matrix.
215 : * \author Ole Schuett
216 : ******************************************************************************/
217 : void dbm_get_local_cols(const dbm_matrix_t *matrix, int *nlocal_cols,
218 : const int **local_cols);
219 :
220 : /*******************************************************************************
221 : * \brief Returns the MPI rank on which the given block should be stored.
222 : * \author Ole Schuett
223 : ******************************************************************************/
224 : int dbm_get_stored_coordinates(const dbm_matrix_t *matrix, const int row,
225 : const int col);
226 :
227 : /*******************************************************************************
228 : * \brief Returns the distribution of the given matrix.
229 : * \author Ole Schuett
230 : ******************************************************************************/
231 : const dbm_distribution_t *dbm_get_distribution(const dbm_matrix_t *matrix);
232 :
233 : /*******************************************************************************
234 : * \brief Internal routine that returns the number of shards for given matrix.
235 : * \author Ole Schuett
236 : ******************************************************************************/
237 146653314 : static inline int dbm_get_num_shards(const dbm_matrix_t *matrix) {
238 86889646 : return matrix->dist->rows.nshards * matrix->dist->cols.nshards;
239 : }
240 :
241 : /*******************************************************************************
242 : * \brief Internal routine for getting a block's shard index.
243 : * \author Ole Schuett
244 : ******************************************************************************/
245 84401485 : static inline int dbm_get_shard_index(const dbm_matrix_t *matrix, const int row,
246 : const int col) {
247 84401485 : const int shard_row = row % matrix->dist->rows.nshards;
248 84401485 : const int shard_col = col % matrix->dist->cols.nshards;
249 84401485 : return shard_row * matrix->dist->cols.nshards + shard_col;
250 : }
251 :
252 : #endif
253 :
254 : // EOF
|