LCOV - code coverage report
Current view: top level - src/dbm - dbm_matrix.h Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 100.0 % 6 6
Test Date: 2026-11-24 01:22:08 Functions: - 0 0

            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              : #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 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              :  ******************************************************************************/
     110              : void dbm_reserve_blocks_template(dbm_matrix_t *matrix,
     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              :  ******************************************************************************/
     117              : void 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              :  ******************************************************************************/
     123              : void dbm_zero(dbm_matrix_t *matrix);
     124              : 
     125              : /*******************************************************************************
     126              :  * \brief Adds matrix_b to matrix_a.
     127              :  * \author Ole Schuett
     128              :  ******************************************************************************/
     129              : void 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              :  ******************************************************************************/
     137              : void 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              :  ******************************************************************************/
     143              : int dbm_iterator_num_blocks(const dbm_iterator_t *iter);
     144              : 
     145              : /*******************************************************************************
     146              :  * \brief Tests whether the given iterator has any block left.
     147              :  * \author Ole Schuett
     148              :  ******************************************************************************/
     149              : bool dbm_iterator_blocks_left(const dbm_iterator_t *iter);
     150              : 
     151              : /*******************************************************************************
     152              :  * \brief Returns the next block from the given iterator.
     153              :  * \author Ole Schuett
     154              :  ******************************************************************************/
     155              : void 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              :  ******************************************************************************/
     162              : void dbm_iterator_stop(dbm_iterator_t *iter);
     163              : 
     164              : /*******************************************************************************
     165              :  * \brief Computes a checksum of the given matrix.
     166              :  * \author Ole Schuett
     167              :  ******************************************************************************/
     168              : double 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              :  ******************************************************************************/
     174              : double 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              :  ******************************************************************************/
     180              : double 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              :  ******************************************************************************/
     186              : const 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              :  ******************************************************************************/
     192              : int 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              :  ******************************************************************************/
     198              : int 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              :  ******************************************************************************/
     204              : void 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              :  ******************************************************************************/
     211              : void 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              :  ******************************************************************************/
     218              : void 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              :  ******************************************************************************/
     225              : void 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              :  ******************************************************************************/
     232              : int 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              :  ******************************************************************************/
     239              : const dbm_distribution_t *dbm_get_distribution(const dbm_matrix_t *matrix);
     240              : 
     241              : /*******************************************************************************
     242              :  * \brief Internal routine that returns the number of shards for given matrix.
     243              :  * \author Ole Schuett
     244              :  ******************************************************************************/
     245    196010191 : static inline int dbm_get_num_shards(const dbm_matrix_t *matrix) {
     246    111241156 :   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              :  ******************************************************************************/
     253     61707028 : static inline int dbm_get_shard_index(const dbm_matrix_t *matrix, const int row,
     254              :                                       const int col) {
     255     61707028 :   const int shard_row = row % matrix->dist->rows.nshards;
     256     61707028 :   const int shard_col = col % matrix->dist->cols.nshards;
     257     61707028 :   return shard_row * matrix->dist->cols.nshards + shard_col;
     258              : }
     259              : 
     260              : #endif
     261              : 
     262              : // EOF
        

Generated by: LCOV version 2.0-1