Line data Source code
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_matrix.h"
8 : #include "dbm_hyperparams.h"
9 :
10 : #include "../base/cp_assert.h"
11 : #include <math.h>
12 : #include <omp.h>
13 : #include <stdbool.h>
14 : #include <stddef.h>
15 : #include <stdlib.h>
16 : #include <string.h>
17 :
18 : /*******************************************************************************
19 : * \brief Creates a new matrix.
20 : * \author Ole Schuett
21 : ******************************************************************************/
22 2155702 : void dbm_create(dbm_matrix_t **matrix_out, dbm_distribution_t *dist,
23 : const char name[], const int nrows, const int ncols,
24 : const int row_sizes[nrows], const int col_sizes[ncols]) {
25 2155702 : cp_assert(omp_get_num_threads() == 1);
26 :
27 2155702 : dbm_matrix_t *const matrix = calloc(1, sizeof(dbm_matrix_t));
28 :
29 2155702 : cp_assert(dist->rows.length == nrows);
30 2155702 : cp_assert(dist->cols.length == ncols);
31 2155702 : dbm_distribution_hold(dist);
32 2155702 : matrix->dist = dist;
33 :
34 2155702 : size_t size = (strlen(name) + 1) * sizeof(char);
35 2155702 : matrix->name = malloc(size);
36 2155702 : cp_assert(matrix->name != NULL && name != NULL);
37 2155702 : memcpy(matrix->name, name, size);
38 :
39 2155702 : matrix->nrows = nrows;
40 2155702 : matrix->ncols = ncols;
41 :
42 2155702 : size = nrows * sizeof(int);
43 2155702 : matrix->row_sizes = malloc(size);
44 2155702 : cp_assert(matrix->row_sizes != NULL || size == 0);
45 2155702 : if (size != 0) {
46 2154664 : memcpy(matrix->row_sizes, row_sizes, size);
47 : }
48 :
49 2155702 : size = ncols * sizeof(int);
50 2155702 : matrix->col_sizes = malloc(size);
51 2155702 : cp_assert(matrix->col_sizes != NULL || size == 0);
52 2155702 : if (size != 0) {
53 2152269 : memcpy(matrix->col_sizes, col_sizes, size);
54 : }
55 :
56 2155702 : const int num_shards = dbm_get_num_shards(matrix);
57 2155702 : matrix->shards = malloc(num_shards * sizeof(dbm_shard_t));
58 2155702 : cp_assert(matrix->shards != NULL || num_shards == 0);
59 2155702 : #pragma omp parallel for
60 : for (int ishard = 0; ishard < num_shards; ishard++) {
61 : dbm_shard_init(&matrix->shards[ishard]);
62 : }
63 :
64 2155702 : cp_assert(*matrix_out == NULL);
65 2155702 : *matrix_out = matrix;
66 2155702 : }
67 :
68 : /*******************************************************************************
69 : * \brief Releases a matrix and all its ressources.
70 : * \author Ole Schuett
71 : ******************************************************************************/
72 2155702 : void dbm_release(dbm_matrix_t *matrix) {
73 2155702 : cp_assert(omp_get_num_threads() == 1);
74 4311404 : for (int ishard = 0; ishard < dbm_get_num_shards(matrix); ishard++) {
75 2155702 : dbm_shard_release(&matrix->shards[ishard]);
76 : }
77 2155702 : dbm_distribution_release(matrix->dist);
78 2155702 : free(matrix->row_sizes);
79 2155702 : free(matrix->col_sizes);
80 2155702 : free(matrix->shards);
81 2155702 : free(matrix->name);
82 2155702 : free(matrix);
83 2155702 : }
84 :
85 : /*******************************************************************************
86 : * \brief Copies content of matrix_b into matrix_a.
87 : * Matrices must have the same row/col block sizes and distribution.
88 : * \author Ole Schuett
89 : ******************************************************************************/
90 216328 : void dbm_copy(dbm_matrix_t *matrix_a, const dbm_matrix_t *matrix_b) {
91 216328 : cp_assert(omp_get_num_threads() == 1);
92 :
93 216328 : cp_assert(matrix_b->nrows == matrix_a->nrows);
94 4169938 : for (int i = 0; i < matrix_b->nrows; i++) {
95 3953610 : cp_assert(matrix_b->row_sizes[i] == matrix_a->row_sizes[i]);
96 : }
97 216328 : cp_assert(matrix_b->ncols == matrix_a->ncols);
98 35979994 : for (int i = 0; i < matrix_b->ncols; i++) {
99 35763666 : cp_assert(matrix_b->col_sizes[i] == matrix_a->col_sizes[i]);
100 : }
101 :
102 216328 : cp_assert(matrix_a->dist == matrix_b->dist);
103 :
104 216328 : #pragma omp parallel for DBM_OMP_SCHEDULE
105 : for (int ishard = 0; ishard < dbm_get_num_shards(matrix_a); ishard++) {
106 : dbm_shard_copy(&matrix_a->shards[ishard], &matrix_b->shards[ishard]);
107 : }
108 216328 : }
109 :
110 : /*******************************************************************************
111 : * \brief Copies content of matrix_b into matrix_a.
112 : * Matrices may have different distributions.
113 : * \author Ole Schuett
114 : ******************************************************************************/
115 144 : void dbm_redistribute(const dbm_matrix_t *matrix, dbm_matrix_t *redist) {
116 144 : cp_assert(omp_get_num_threads() == 1);
117 144 : cp_assert(matrix->nrows == redist->nrows);
118 6384 : for (int i = 0; i < matrix->nrows; i++) {
119 6240 : cp_assert(matrix->row_sizes[i] == redist->row_sizes[i]);
120 : }
121 144 : cp_assert(matrix->ncols == redist->ncols);
122 6384 : for (int i = 0; i < matrix->ncols; i++) {
123 6240 : cp_assert(matrix->col_sizes[i] == redist->col_sizes[i]);
124 : }
125 :
126 144 : cp_assert(cp_mpi_comms_are_similar(matrix->dist->comm, redist->dist->comm));
127 144 : const cp_mpi_comm_t comm = redist->dist->comm;
128 144 : const int nranks = cp_mpi_comm_size(comm);
129 :
130 : // 1st pass: Compute send_count.
131 144 : int send_count[nranks];
132 144 : memset(send_count, 0, nranks * sizeof(int));
133 288 : for (int ishard = 0; ishard < dbm_get_num_shards(matrix); ishard++) {
134 144 : const dbm_shard_t *const shard = &matrix->shards[ishard];
135 8936 : for (int iblock = 0; iblock < shard->nblocks; iblock++) {
136 8792 : const dbm_block_t *const blk = &shard->blocks[iblock];
137 8792 : const int row_size = matrix->row_sizes[blk->row];
138 8792 : const int col_size = matrix->col_sizes[blk->col];
139 8792 : const int block_size = row_size * col_size;
140 8792 : const int rank = dbm_get_stored_coordinates(redist, blk->row, blk->col);
141 8792 : cp_assert(0 <= rank && rank < nranks);
142 8792 : send_count[rank] += 2 + block_size;
143 : }
144 : }
145 :
146 : // 1st communication: Exchange counts.
147 144 : int recv_count[nranks];
148 144 : cp_mpi_alltoall_int(send_count, 1, recv_count, 1, comm);
149 :
150 : // Compute displacements and allocate data buffers.
151 144 : int send_displ[nranks + 1], recv_displ[nranks + 1];
152 144 : send_displ[0] = recv_displ[0] = 0;
153 432 : for (int irank = 1; irank <= nranks; irank++) {
154 288 : send_displ[irank] = send_displ[irank - 1] + send_count[irank - 1];
155 288 : recv_displ[irank] = recv_displ[irank - 1] + recv_count[irank - 1];
156 : }
157 144 : const int total_send_count = send_displ[nranks];
158 144 : const int total_recv_count = recv_displ[nranks];
159 144 : double *const data_send = cp_mpi_alloc_mem(total_send_count * sizeof(double));
160 144 : double *const data_recv = cp_mpi_alloc_mem(total_recv_count * sizeof(double));
161 :
162 : // 2nd pass: Fill send_data.
163 144 : int send_data_positions[nranks];
164 144 : memcpy(send_data_positions, send_displ, nranks * sizeof(int));
165 288 : for (int ishard = 0; ishard < dbm_get_num_shards(matrix); ishard++) {
166 144 : const dbm_shard_t *const shard = &matrix->shards[ishard];
167 8936 : for (int iblock = 0; iblock < shard->nblocks; iblock++) {
168 8792 : const dbm_block_t *const blk = &shard->blocks[iblock];
169 8792 : const double *const blk_data = &shard->data[blk->offset];
170 8792 : const int row_size = matrix->row_sizes[blk->row];
171 8792 : const int col_size = matrix->col_sizes[blk->col];
172 8792 : const int block_size = row_size * col_size;
173 8792 : const int rank = dbm_get_stored_coordinates(redist, blk->row, blk->col);
174 8792 : const int pos = send_data_positions[rank];
175 8792 : data_send[pos + 0] = blk->row; // send integers as doubles
176 8792 : data_send[pos + 1] = blk->col;
177 8792 : memcpy(&data_send[pos + 2], blk_data, block_size * sizeof(double));
178 8792 : send_data_positions[rank] += 2 + block_size;
179 : }
180 : }
181 432 : for (int irank = 0; irank < nranks; irank++) {
182 288 : cp_assert(send_data_positions[irank] == send_displ[irank + 1]);
183 : }
184 :
185 : // 2nd communication: Exchange data.
186 144 : cp_mpi_alltoallv_double(data_send, send_count, send_displ, data_recv,
187 : recv_count, recv_displ, comm);
188 144 : cp_mpi_free_mem(data_send);
189 :
190 : // 3rd pass: Unpack data.
191 144 : dbm_clear(redist);
192 144 : int recv_data_pos = 0;
193 8936 : while (recv_data_pos < total_recv_count) {
194 8792 : const int row = (int)data_recv[recv_data_pos + 0];
195 8792 : const int col = (int)data_recv[recv_data_pos + 1];
196 8792 : cp_assert(data_recv[recv_data_pos + 0] == (double)row);
197 8792 : cp_assert(data_recv[recv_data_pos + 1] == (double)col);
198 8792 : dbm_put_block(redist, row, col, false, &data_recv[recv_data_pos + 2]);
199 8792 : const int row_size = matrix->row_sizes[row];
200 8792 : const int col_size = matrix->col_sizes[col];
201 8792 : const int block_size = row_size * col_size;
202 8792 : recv_data_pos += 2 + block_size;
203 : }
204 144 : cp_assert(recv_data_pos == total_recv_count);
205 144 : cp_mpi_free_mem(data_recv);
206 144 : }
207 :
208 : /*******************************************************************************
209 : * \brief Looks up a block from given matrics. This routine is thread-safe.
210 : * If the block is not found then a null pointer is returned.
211 : * \author Ole Schuett
212 : ******************************************************************************/
213 24138230 : void dbm_get_block_p(dbm_matrix_t *matrix, const int row, const int col,
214 : double **block, int *row_size, int *col_size) {
215 24138230 : cp_assert(0 <= row && row < matrix->nrows);
216 24138230 : cp_assert(0 <= col && col < matrix->ncols);
217 24138230 : cp_assert(dbm_get_stored_coordinates(matrix, row, col) ==
218 : matrix->dist->my_rank);
219 24138230 : *row_size = matrix->row_sizes[row];
220 24138230 : *col_size = matrix->col_sizes[col];
221 24138230 : *block = NULL;
222 :
223 24138230 : const int ishard = dbm_get_shard_index(matrix, row, col);
224 24138230 : const dbm_shard_t *const shard = &matrix->shards[ishard];
225 24138230 : const dbm_block_t *const blk = dbm_shard_lookup(shard, row, col);
226 24138230 : if (blk != NULL) {
227 22227552 : *block = &shard->data[blk->offset];
228 : }
229 24138230 : }
230 :
231 : /*******************************************************************************
232 : * \brief Adds a block to given matrix. This routine is thread-safe.
233 : * If block already exist then it gets overwritten (or summed).
234 : * \author Ole Schuett
235 : ******************************************************************************/
236 37568798 : void dbm_put_block(dbm_matrix_t *matrix, const int row, const int col,
237 : const bool summation, const double *block) {
238 37568798 : cp_assert(0 <= row && row < matrix->nrows);
239 37568798 : cp_assert(0 <= col && col < matrix->ncols);
240 37568798 : cp_assert(dbm_get_stored_coordinates(matrix, row, col) ==
241 : matrix->dist->my_rank);
242 37568798 : const int row_size = matrix->row_sizes[row];
243 37568798 : const int col_size = matrix->col_sizes[col];
244 37568798 : const int block_size = row_size * col_size;
245 :
246 37568798 : const int ishard = dbm_get_shard_index(matrix, row, col);
247 37568798 : dbm_shard_t *const shard = &matrix->shards[ishard];
248 37568798 : omp_set_lock(&shard->lock);
249 37568798 : const dbm_block_t *const blk =
250 37568798 : dbm_shard_get_or_allocate_block(shard, row, col, block_size);
251 37568798 : double *const blk_data = &shard->data[blk->offset];
252 37568798 : if (summation) {
253 5172935 : cp_assert(blk_data != NULL || 0 == block_size);
254 1624839184 : for (int i = 0; i < block_size; i++) {
255 1619666249 : blk_data[i] += block[i];
256 : }
257 32395863 : } else if (block_size != 0) {
258 32390764 : memcpy(blk_data, block, block_size * sizeof(double));
259 : }
260 37568798 : omp_unset_lock(&shard->lock);
261 37568798 : }
262 :
263 : /*******************************************************************************
264 : * \brief Remove all blocks from matrix, but does not release underlying memory.
265 : * \author Ole Schuett
266 : ******************************************************************************/
267 3207411 : void dbm_clear(dbm_matrix_t *matrix) {
268 3207411 : cp_assert(omp_get_num_threads() == 1);
269 :
270 3207411 : #pragma omp parallel for DBM_OMP_SCHEDULE
271 : for (int ishard = 0; ishard < dbm_get_num_shards(matrix); ishard++) {
272 : dbm_shard_t *const shard = &matrix->shards[ishard];
273 : shard->data_promised = shard->data_size = shard->nblocks = 0;
274 : // Does not deallocate memory, hence data_allocated remains unchanged.
275 : memset(shard->hashtable, 0, shard->hashtable_size * sizeof(int));
276 : }
277 3207411 : }
278 :
279 : /*******************************************************************************
280 : * \brief Removes all blocks from the matrix whose norm is below the threshold.
281 : * Blocks of size zero are always kept.
282 : * \author Ole Schuett
283 : ******************************************************************************/
284 1039030 : void dbm_filter(dbm_matrix_t *matrix, const double eps) {
285 1039030 : cp_assert(omp_get_num_threads() == 1);
286 :
287 1039030 : if (eps == 0.0) {
288 : return;
289 : }
290 961888 : const double eps2 = eps * eps;
291 :
292 961888 : #pragma omp parallel for DBM_OMP_SCHEDULE
293 : for (int ishard = 0; ishard < dbm_get_num_shards(matrix); ishard++) {
294 : dbm_shard_t *const shard = &matrix->shards[ishard];
295 : const int old_nblocks = shard->nblocks;
296 : shard->nblocks = shard->data_promised = 0;
297 : memset(shard->hashtable, 0, shard->hashtable_size * sizeof(int));
298 :
299 : for (int iblock = 0; iblock < old_nblocks; iblock++) {
300 : const dbm_block_t old_blk = shard->blocks[iblock];
301 : const double *old_blk_data = &shard->data[old_blk.offset];
302 : const int row_size = matrix->row_sizes[old_blk.row];
303 : const int col_size = matrix->col_sizes[old_blk.col];
304 : const int block_size = row_size * col_size;
305 : double norm = 0.0;
306 : for (int i = 0; i < block_size; i++) {
307 : const double value = old_blk_data[i];
308 : norm += value * value;
309 : if (eps2 <= norm) {
310 : break;
311 : }
312 : }
313 : // For historic reasons zero-sized blocks are never filtered.
314 : if (block_size > 0 && norm < eps2) {
315 : continue; // filter the block
316 : }
317 : // Re-create block.
318 : dbm_block_t *const new_blk = dbm_shard_promise_new_block(
319 : shard, old_blk.row, old_blk.col, block_size);
320 : cp_assert(new_blk->offset <= old_blk.offset);
321 : if (new_blk->offset != old_blk.offset) {
322 : // Using memmove because it can handle overlapping buffers.
323 : double *new_blk_data = &shard->data[new_blk->offset];
324 : memmove(new_blk_data, old_blk_data, block_size * sizeof(double));
325 : }
326 : }
327 : shard->data_size = shard->data_promised;
328 : // TODO: Could call realloc to release excess memory.
329 : }
330 : }
331 :
332 : /*******************************************************************************
333 : * \brief Adds list of blocks efficiently. The blocks will be filled with zeros.
334 : * This routine must always be called within an OpenMP parallel region.
335 : * \author Ole Schuett and Hans Pabst
336 : ******************************************************************************/
337 1685477 : void dbm_reserve_blocks(dbm_matrix_t *matrix, const int nblocks,
338 : const int rows[], const int cols[]) {
339 1685477 : cp_assert(omp_get_num_threads() == omp_get_max_threads() &&
340 : "Please call dbm_reserve_blocks within an OpenMP parallel region.");
341 1685477 : const int my_rank = matrix->dist->my_rank;
342 1685477 : const int nshards = dbm_get_num_shards(matrix);
343 1685477 : const int nthreads = omp_get_num_threads();
344 1685477 : const int ithread = omp_get_thread_num();
345 : // Fast-track for single-threaded runs.
346 1685477 : if (nthreads == 1 && nshards == 1) {
347 1685477 : dbm_shard_t *const shard = &matrix->shards[0];
348 1685477 : dbm_shard_reserve(shard, shard->nblocks + nblocks);
349 29138497 : for (int i = 0; i < nblocks; i++) {
350 27453020 : const int row = rows[i], col = cols[i];
351 27453020 : cp_assert(0 <= row && row < matrix->nrows);
352 27453020 : cp_assert(0 <= col && col < matrix->ncols);
353 27453020 : cp_assert(dbm_get_stored_coordinates(matrix, row, col) == my_rank);
354 27453020 : const int block_size = matrix->row_sizes[row] * matrix->col_sizes[col];
355 27453020 : dbm_shard_get_or_promise_block(shard, row, col, block_size);
356 : }
357 1685477 : dbm_shard_allocate_promised_blocks(shard);
358 1685477 : return;
359 : }
360 :
361 : // Shared by the threads: blocks are binned by shard such that a single
362 : // thread owns a shard (no locks).
363 0 : int *workspace = NULL;
364 0 : int *reserve_rowcol = NULL;
365 :
366 0 : #pragma omp single copyprivate(workspace)
367 : {
368 0 : const size_t count = (size_t)nthreads * nshards + nshards + 1;
369 0 : workspace = calloc(count, sizeof(*workspace));
370 0 : cp_assert(workspace != NULL);
371 : }
372 0 : int *const reserve_offset = workspace;
373 0 : int *const reserve_start = reserve_offset + (size_t)nthreads * nshards;
374 0 : int *const offset = reserve_offset + (size_t)ithread * nshards;
375 0 : for (int i = 0; i < nblocks; i++) {
376 0 : const int row = rows[i], col = cols[i];
377 0 : cp_assert(0 <= row && row < matrix->nrows);
378 0 : cp_assert(0 <= col && col < matrix->ncols);
379 0 : cp_assert(dbm_get_stored_coordinates(matrix, row, col) == my_rank);
380 0 : ++offset[dbm_get_shard_index(matrix, row, col)];
381 : }
382 0 : #pragma omp barrier
383 :
384 0 : #pragma omp single copyprivate(reserve_rowcol)
385 : { // exclusive prefix sum over shards (major) and threads (minor)
386 : int total = 0;
387 0 : for (int ishard = 0; ishard < nshards; ishard++) {
388 0 : reserve_start[ishard] = total;
389 0 : for (int i = 0; i < nthreads; i++) {
390 0 : int *const count = reserve_offset + (size_t)i * nshards + ishard;
391 0 : const int n = *count;
392 0 : *count = total;
393 0 : total += n;
394 : }
395 : }
396 0 : reserve_start[nshards] = total;
397 0 : reserve_rowcol =
398 0 : (0 < total ? malloc(2 * (size_t)total * sizeof(int)) : NULL);
399 0 : cp_assert(reserve_rowcol != NULL || 0 == total);
400 : }
401 0 : for (int i = 0; i < nblocks; i++) {
402 0 : const int j = offset[dbm_get_shard_index(matrix, rows[i], cols[i])]++;
403 0 : reserve_rowcol[2 * j + 0] = rows[i];
404 0 : reserve_rowcol[2 * j + 1] = cols[i];
405 : }
406 0 : #pragma omp barrier
407 :
408 : #pragma omp for DBM_OMP_SCHEDULE
409 0 : for (int ishard = 0; ishard < nshards; ishard++) {
410 0 : const int reserve_nblocks_ishard =
411 0 : reserve_start[ishard + 1] - reserve_start[ishard];
412 0 : if (reserve_nblocks_ishard == 0) {
413 0 : continue;
414 : }
415 0 : dbm_shard_t *const shard = &matrix->shards[ishard];
416 : // one rebuild of the hashtable rather than one per growth
417 0 : dbm_shard_reserve(shard, shard->nblocks + reserve_nblocks_ishard);
418 0 : for (int j = reserve_start[ishard]; j < reserve_start[ishard + 1]; j++) {
419 0 : const int row = reserve_rowcol[2 * j + 0];
420 0 : const int col = reserve_rowcol[2 * j + 1];
421 0 : const int block_size = matrix->row_sizes[row] * matrix->col_sizes[col];
422 0 : dbm_shard_get_or_promise_block(shard, row, col, block_size);
423 : }
424 0 : dbm_shard_allocate_promised_blocks(shard);
425 : }
426 :
427 : #pragma omp single nowait
428 : {
429 0 : free(reserve_rowcol);
430 0 : free(workspace);
431 : }
432 : }
433 :
434 : /*******************************************************************************
435 : * \brief Reserves template blocks in a matrix sharing the distribution object.
436 : * Block sizes may differ. Existing values are kept, new blocks zeroed.
437 : * Call outside an OpenMP parallel region.
438 : ******************************************************************************/
439 304118 : void dbm_reserve_blocks_template(dbm_matrix_t *matrix,
440 : const dbm_matrix_t *matrix_template) {
441 304118 : cp_assert(omp_get_num_threads() == 1);
442 304118 : cp_assert(matrix->dist == matrix_template->dist);
443 304118 : if (matrix == matrix_template) {
444 : return;
445 : }
446 :
447 304118 : #pragma omp parallel for DBM_OMP_SCHEDULE
448 : for (int ishard = 0; ishard < dbm_get_num_shards(matrix); ishard++) {
449 : dbm_shard_t *const shard = &matrix->shards[ishard];
450 : const dbm_shard_t *const source = &matrix_template->shards[ishard];
451 : if (source->nblocks == 0) {
452 : continue;
453 : }
454 : if (shard->nblocks == 0) {
455 : dbm_shard_reserve(shard, source->nblocks);
456 : }
457 : for (int i = 0; i < source->nblocks; i++) {
458 : const dbm_block_t *const block = &source->blocks[i];
459 : const int block_size =
460 : matrix->row_sizes[block->row] * matrix->col_sizes[block->col];
461 : dbm_shard_get_or_promise_block(shard, block->row, block->col, block_size);
462 : }
463 : dbm_shard_allocate_promised_blocks(shard);
464 : }
465 : }
466 :
467 : /*******************************************************************************
468 : * \brief Multiplies all entries in the given matrix by the given factor alpha.
469 : * \author Ole Schuett
470 : ******************************************************************************/
471 848058 : void dbm_scale(dbm_matrix_t *matrix, const double alpha) {
472 848058 : cp_assert(omp_get_num_threads() == 1);
473 848058 : if (alpha == 1.0) {
474 : return;
475 : }
476 629305 : if (alpha == 0.0) {
477 607659 : dbm_zero(matrix);
478 607659 : return;
479 : }
480 :
481 21646 : #pragma omp parallel for DBM_OMP_SCHEDULE
482 : for (int ishard = 0; ishard < dbm_get_num_shards(matrix); ishard++) {
483 : dbm_shard_t *const shard = &matrix->shards[ishard];
484 : for (int i = 0; i < shard->data_size; i++) {
485 : shard->data[i] *= alpha;
486 : }
487 : }
488 : }
489 :
490 : /*******************************************************************************
491 : * \brief Sets all blocks in the given matrix to zero.
492 : * \author Ole Schuett
493 : ******************************************************************************/
494 607659 : void dbm_zero(dbm_matrix_t *matrix) {
495 607659 : cp_assert(omp_get_num_threads() == 1);
496 :
497 607659 : #pragma omp parallel for DBM_OMP_SCHEDULE
498 : for (int ishard = 0; ishard < dbm_get_num_shards(matrix); ishard++) {
499 : dbm_shard_t *const shard = &matrix->shards[ishard];
500 : if (shard->data != NULL) {
501 : memset(shard->data, 0, shard->data_size * sizeof(double));
502 : }
503 : }
504 607659 : }
505 :
506 : /*******************************************************************************
507 : * \brief Adds matrix_b to matrix_a.
508 : * \author Ole Schuett
509 : ******************************************************************************/
510 384000 : void dbm_add(dbm_matrix_t *matrix_a, const dbm_matrix_t *matrix_b) {
511 384000 : cp_assert(omp_get_num_threads() == 1);
512 384000 : cp_assert(matrix_a->dist == matrix_b->dist);
513 :
514 384000 : #pragma omp parallel for DBM_OMP_SCHEDULE
515 : for (int ishard = 0; ishard < dbm_get_num_shards(matrix_b); ishard++) {
516 : dbm_shard_t *const shard_a = &matrix_a->shards[ishard];
517 : const dbm_shard_t *shard_b = &matrix_b->shards[ishard];
518 : for (int iblock = 0; iblock < shard_b->nblocks; iblock++) {
519 : const dbm_block_t blk_b = shard_b->blocks[iblock];
520 : const int row_size = matrix_b->row_sizes[blk_b.row];
521 : const int col_size = matrix_b->col_sizes[blk_b.col];
522 : cp_assert(row_size == matrix_a->row_sizes[blk_b.row]);
523 : cp_assert(col_size == matrix_a->col_sizes[blk_b.col]);
524 : const int block_size = row_size * col_size;
525 : const dbm_block_t *const blk_a = dbm_shard_get_or_allocate_block(
526 : shard_a, blk_b.row, blk_b.col, block_size);
527 : double *const data_a = &shard_a->data[blk_a->offset];
528 : const double *const data_b = &shard_b->data[blk_b.offset];
529 : for (int i = 0; i < block_size; i++) {
530 : data_a[i] += data_b[i];
531 : }
532 : }
533 : }
534 384000 : }
535 :
536 : /*******************************************************************************
537 : * \brief Creates an iterator for the blocks of the given matrix.
538 : * The iteration order is not stable.
539 : * This routine must always be called within an OpenMP parallel region.
540 : * \author Ole Schuett
541 : ******************************************************************************/
542 4934300 : void dbm_iterator_start(dbm_iterator_t **iter_out, const dbm_matrix_t *matrix) {
543 4934300 : cp_assert(omp_get_num_threads() == omp_get_max_threads() &&
544 : "Please call dbm_iterator_start within an OpenMP parallel region.");
545 4934300 : dbm_iterator_t *iter = malloc(sizeof(dbm_iterator_t));
546 4934300 : cp_assert(iter != NULL);
547 4934300 : iter->matrix = matrix;
548 4934300 : iter->next_block = 0;
549 4934300 : iter->next_shard = omp_get_thread_num();
550 5661848 : while (iter->next_shard < dbm_get_num_shards(matrix) &&
551 4934300 : matrix->shards[iter->next_shard].nblocks == 0) {
552 727548 : iter->next_shard += omp_get_num_threads();
553 : }
554 4934300 : cp_assert(*iter_out == NULL);
555 4934300 : *iter_out = iter;
556 4934300 : }
557 :
558 : /*******************************************************************************
559 : * \brief Returns number of blocks the iterator will provide to calling thread.
560 : * \author Ole Schuett
561 : ******************************************************************************/
562 359003 : int dbm_iterator_num_blocks(const dbm_iterator_t *iter) {
563 359003 : int num_blocks = 0;
564 359003 : for (int ishard = omp_get_thread_num();
565 718006 : ishard < dbm_get_num_shards(iter->matrix);
566 359003 : ishard += omp_get_num_threads()) {
567 359003 : num_blocks += iter->matrix->shards[ishard].nblocks;
568 : }
569 359003 : return num_blocks;
570 : }
571 :
572 : /*******************************************************************************
573 : * \brief Tests whether the given iterator has any block left.
574 : * \author Ole Schuett
575 : ******************************************************************************/
576 84769035 : bool dbm_iterator_blocks_left(const dbm_iterator_t *iter) {
577 84769035 : return iter->next_shard < dbm_get_num_shards(iter->matrix);
578 : }
579 :
580 : /*******************************************************************************
581 : * \brief Returns the next block from the given iterator.
582 : * \author Ole Schuett
583 : ******************************************************************************/
584 83208861 : void dbm_iterator_next_block(dbm_iterator_t *iter, int *row, int *col,
585 : double **block, int *row_size, int *col_size) {
586 83208861 : const dbm_matrix_t *matrix = iter->matrix;
587 83208861 : cp_assert(iter->next_shard < dbm_get_num_shards(matrix));
588 83208861 : const dbm_shard_t *shard = &matrix->shards[iter->next_shard];
589 83208861 : cp_assert(iter->next_block < shard->nblocks);
590 83208861 : dbm_block_t *blk = &shard->blocks[iter->next_block];
591 :
592 83208861 : *row = blk->row;
593 83208861 : *col = blk->col;
594 83208861 : *row_size = matrix->row_sizes[blk->row];
595 83208861 : *col_size = matrix->col_sizes[blk->col];
596 83208861 : *block = &shard->data[blk->offset];
597 :
598 83208861 : iter->next_block++;
599 83208861 : if (iter->next_block >= shard->nblocks) {
600 : // Advance to the next non-empty shard...
601 4206752 : iter->next_shard += omp_get_num_threads();
602 4206752 : while (iter->next_shard < dbm_get_num_shards(matrix) &&
603 0 : matrix->shards[iter->next_shard].nblocks == 0) {
604 0 : iter->next_shard += omp_get_num_threads();
605 : }
606 4206752 : iter->next_block = 0; // ...and continue with its first block.
607 : }
608 83208861 : }
609 :
610 : /*******************************************************************************
611 : * \brief Releases the given iterator.
612 : * \author Ole Schuett
613 : ******************************************************************************/
614 4934300 : void dbm_iterator_stop(dbm_iterator_t *iter) { free(iter); }
615 :
616 : /*******************************************************************************
617 : * \brief Private routine to accumulate using Kahan's summation.
618 : * \author Hans Pabst
619 : ******************************************************************************/
620 11023661 : static double kahan_sum(double value, double *accumulator,
621 : double *compensation) {
622 11023661 : double r, c;
623 11023661 : cp_assert(NULL != accumulator && NULL != compensation);
624 11023661 : c = value - *compensation;
625 11023661 : r = *accumulator + c;
626 11023661 : *compensation = (r - *accumulator) - c;
627 11023661 : *accumulator = r;
628 11023661 : return r;
629 : }
630 :
631 : /*******************************************************************************
632 : * \brief Computes a checksum of the given matrix.
633 : * \author Ole Schuett
634 : ******************************************************************************/
635 190 : double dbm_checksum(const dbm_matrix_t *matrix) {
636 190 : double checksum = 0.0, compensation = 0.0;
637 380 : for (int ishard = 0; ishard < dbm_get_num_shards(matrix); ishard++) {
638 190 : const dbm_shard_t *shard = &matrix->shards[ishard];
639 11023851 : for (int i = 0; i < shard->data_size; i++) {
640 11023661 : const double value = shard->data[i];
641 11023661 : kahan_sum(value * value, &checksum, &compensation);
642 : }
643 : }
644 190 : cp_mpi_sum_double(&checksum, 1, matrix->dist->comm);
645 190 : return checksum;
646 : }
647 :
648 : /*******************************************************************************
649 : * \brief Returns the largest absolute value of all matrix elements.
650 : * \author Ole Schuett
651 : ******************************************************************************/
652 48 : double dbm_maxabs(const dbm_matrix_t *matrix) {
653 48 : double maxabs = 0.0;
654 96 : for (int ishard = 0; ishard < dbm_get_num_shards(matrix); ishard++) {
655 48 : const dbm_shard_t *shard = &matrix->shards[ishard];
656 1599050 : for (int i = 0; i < shard->data_size; i++) {
657 1599002 : maxabs = fmax(maxabs, fabs(shard->data[i]));
658 : }
659 : }
660 48 : cp_mpi_max_double(&maxabs, 1, matrix->dist->comm);
661 48 : return maxabs;
662 : }
663 :
664 : /*******************************************************************************
665 : * \brief Calculates maximum relative difference between matrix_a and matrix_b.
666 : * \author Hans Pabst
667 : ******************************************************************************/
668 0 : double dbm_maxeps(const dbm_matrix_t *matrix_a, const dbm_matrix_t *matrix_b) {
669 0 : const int num_shards = dbm_get_num_shards(matrix_a);
670 0 : double epsilon = 0;
671 :
672 0 : cp_assert(omp_get_num_threads() == 1);
673 0 : cp_assert(matrix_a->dist == matrix_b->dist);
674 0 : cp_assert(num_shards == dbm_get_num_shards(matrix_b));
675 :
676 : // Handles asymmetric sparsity: blocks in A but not B are compared against
677 : // zero, and blocks in B but not A are checked in a second pass. The
678 : // reduction(max) avoids the previous data race on the shared epsilon.
679 0 : #pragma omp parallel for reduction(max : epsilon) DBM_OMP_SCHEDULE
680 : for (int ishard = 0; ishard < num_shards; ++ishard) {
681 : const dbm_shard_t *const shard_a = &matrix_a->shards[ishard];
682 : const dbm_shard_t *const shard_b = &matrix_b->shards[ishard];
683 : for (int iblock = 0; iblock < shard_a->nblocks; ++iblock) {
684 : const dbm_block_t *const blk_a = &shard_a->blocks[iblock];
685 : const dbm_block_t *const blk_b =
686 : dbm_shard_lookup(shard_b, blk_a->row, blk_a->col);
687 : const int row_size = matrix_a->row_sizes[blk_a->row];
688 : const int col_size = matrix_a->col_sizes[blk_a->col];
689 : cp_assert(NULL == blk_b || row_size == matrix_b->row_sizes[blk_b->row]);
690 : cp_assert(NULL == blk_b || col_size == matrix_b->col_sizes[blk_b->col]);
691 : const double *const data_a = &shard_a->data[blk_a->offset];
692 : const double *const data_b =
693 : (NULL == blk_b) ? NULL : &shard_b->data[blk_b->offset];
694 : const int block_size = row_size * col_size;
695 : for (int i = 0; i < block_size; ++i) {
696 : const double d = data_a[i] - (NULL == data_b ? 0.0 : data_b[i]);
697 : const double e = fabs(0 != data_a[i] ? (d / data_a[i]) : d);
698 : if (epsilon < e) {
699 : epsilon = e;
700 : }
701 : }
702 : }
703 : // Second pass: blocks present in B but absent from A (treat A as zero).
704 : for (int iblock = 0; iblock < shard_b->nblocks; ++iblock) {
705 : const dbm_block_t *const blk_b = &shard_b->blocks[iblock];
706 : if (NULL != dbm_shard_lookup(shard_a, blk_b->row, blk_b->col)) {
707 : continue;
708 : }
709 : const int row_size = matrix_b->row_sizes[blk_b->row];
710 : const int col_size = matrix_b->col_sizes[blk_b->col];
711 : const double *const data_b = &shard_b->data[blk_b->offset];
712 : const int block_size = row_size * col_size;
713 : for (int i = 0; i < block_size; ++i) {
714 : const double e = fabs(data_b[i]);
715 : if (epsilon < e) {
716 : epsilon = e;
717 : }
718 : }
719 : }
720 : }
721 :
722 0 : cp_mpi_max_double(&epsilon, 1, matrix_a->dist->comm);
723 0 : return epsilon;
724 : }
725 :
726 : /*******************************************************************************
727 : * \brief Returns the name of the matrix of the given matrix.
728 : * \author Ole Schuett
729 : ******************************************************************************/
730 1918304 : const char *dbm_get_name(const dbm_matrix_t *matrix) { return matrix->name; }
731 :
732 : /*******************************************************************************
733 : * \brief Returns the number of local Non-Zero Elements of the given matrix.
734 : * \author Ole Schuett
735 : ******************************************************************************/
736 2577447 : int dbm_get_nze(const dbm_matrix_t *matrix) {
737 2577447 : int nze = 0;
738 5154894 : for (int ishard = 0; ishard < dbm_get_num_shards(matrix); ishard++) {
739 2577447 : nze += matrix->shards[ishard].data_size;
740 : }
741 2577447 : return nze;
742 : }
743 :
744 : /*******************************************************************************
745 : * \brief Returns the number of local blocks of the given matrix.
746 : * \author Ole Schuett
747 : ******************************************************************************/
748 2068580 : int dbm_get_num_blocks(const dbm_matrix_t *matrix) {
749 2068580 : int nblocks = 0;
750 4137160 : for (int ishard = 0; ishard < dbm_get_num_shards(matrix); ishard++) {
751 2068580 : nblocks += matrix->shards[ishard].nblocks;
752 : }
753 2068580 : return nblocks;
754 : }
755 :
756 : /*******************************************************************************
757 : * \brief Returns the row block sizes of the given matrix.
758 : * \author Ole Schuett
759 : ******************************************************************************/
760 6150402 : void dbm_get_row_sizes(const dbm_matrix_t *matrix, int *nrows,
761 : const int **row_sizes) {
762 6150402 : *nrows = matrix->nrows;
763 6150402 : *row_sizes = matrix->row_sizes;
764 6150402 : }
765 :
766 : /*******************************************************************************
767 : * \brief Returns the column block sizes of the given matrix.
768 : * \author Ole Schuett
769 : ******************************************************************************/
770 2708508 : void dbm_get_col_sizes(const dbm_matrix_t *matrix, int *ncols,
771 : const int **col_sizes) {
772 2708508 : *ncols = matrix->ncols;
773 2708508 : *col_sizes = matrix->col_sizes;
774 2708508 : }
775 :
776 : /*******************************************************************************
777 : * \brief Returns the local row block sizes of the given matrix.
778 : * \author Ole Schuett
779 : ******************************************************************************/
780 508820 : void dbm_get_local_rows(const dbm_matrix_t *matrix, int *nlocal_rows,
781 : const int **local_rows) {
782 508820 : *nlocal_rows = matrix->dist->rows.nlocals;
783 508820 : *local_rows = matrix->dist->rows.local_indicies;
784 508820 : }
785 :
786 : /*******************************************************************************
787 : * \brief Returns the local column block sizes of the given matrix.
788 : * \author Ole Schuett
789 : ******************************************************************************/
790 193368 : void dbm_get_local_cols(const dbm_matrix_t *matrix, int *nlocal_cols,
791 : const int **local_cols) {
792 193368 : *nlocal_cols = matrix->dist->cols.nlocals;
793 193368 : *local_cols = matrix->dist->cols.local_indicies;
794 193368 : }
795 :
796 : /*******************************************************************************
797 : * \brief Returns the MPI rank on which the given block should be stored.
798 : * \author Ole Schuett
799 : ******************************************************************************/
800 98686052 : int dbm_get_stored_coordinates(const dbm_matrix_t *matrix, const int row,
801 : const int col) {
802 98686052 : return dbm_distribution_stored_coords(matrix->dist, row, col);
803 : }
804 :
805 : /*******************************************************************************
806 : * \brief Returns the distribution of the given matrix.
807 : * \author Ole Schuett
808 : ******************************************************************************/
809 1296006 : const dbm_distribution_t *dbm_get_distribution(const dbm_matrix_t *matrix) {
810 1296006 : return matrix->dist;
811 : }
812 :
813 : // EOF
|