Arcane  v4.1.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
BlockCSRMatrix.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* BlockCSRMatrix.h (C) 2000-2026 */
9/* */
10/* Sparse matrix in block-CSR format. . */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_ALINA_BLOCKCSRMATRIX_H
13#define ARCCORE_ALINA_BLOCKCSRMATRIX_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16/*
17 * This file is based on the work on AMGCL library (version march 2026)
18 * which can be found at https://github.com/ddemidov/amgcl.
19 *
20 * Copyright (c) 2012-2022 Denis Demidov <dennis.demidov@gmail.com>
21 * SPDX-License-Identifier: MIT
22 */
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
26#include "arccore/alina/AlinaUtils.h"
27
28#include <vector>
29#include <algorithm>
30#include <numeric>
31
32/*---------------------------------------------------------------------------*/
33/*---------------------------------------------------------------------------*/
34
35namespace Arcane::Alina
36{
37
38/*---------------------------------------------------------------------------*/
39/*---------------------------------------------------------------------------*/
47template <typename V, typename C, typename P>
49{
50 typedef V value_type;
51 typedef V val_type;
52 typedef C col_type;
53 typedef P ptr_type;
54
55 size_t block_size;
56 size_t nrows, ncols;
57 size_t brows, bcols;
58
59 std::vector<ptr_type> ptr;
60 std::vector<col_type> col;
61 std::vector<val_type> val;
62
72 template <class Matrix>
73 BlockCSRMatrix(const Matrix& A, size_t block_size)
74 : block_size(block_size)
75 , nrows(backend::nbRow(A))
76 , ncols(backend::nbColumn(A))
77 , brows((nrows + block_size - 1) / block_size)
78 , bcols((ncols + block_size - 1) / block_size)
79 , ptr(brows + 1, 0)
80 {
81 std::vector<ptrdiff_t> marker(bcols, -1);
82
83 // Count number of nonzeros in block matrix.
84 for (ptr_type ib = 0; ib < static_cast<ptr_type>(brows); ++ib) {
85 ptr_type ia = ib * block_size;
86
87 for (size_t k = 0; k < block_size && ia < static_cast<ptr_type>(nrows); ++k, ++ia) {
88 for (auto a = backend::row_begin(A, ia); a; ++a) {
89 col_type cb = a.col() / block_size;
90
91 if (marker[cb] != static_cast<col_type>(ib)) {
92 marker[cb] = static_cast<col_type>(ib);
93 ++ptr[ib + 1];
94 }
95 }
96 }
97 }
98
99 {
100 std::partial_sum(ptr.begin(), ptr.end(), ptr.begin());
101 col.resize(ptr.back());
102 val.resize(ptr.back() * block_size * block_size, 0);
103 }
104
105 std::fill(marker.begin(), marker.end(), -1);
106
107 // Fill the block matrix.
108 for (ptr_type ib = 0; ib < static_cast<ptr_type>(brows); ++ib) {
109 ptr_type ia = ib * block_size;
110 ptr_type row_beg = ptr[ib];
111 ptr_type row_end = row_beg;
112
113 for (size_t k = 0; k < block_size && ia < static_cast<ptr_type>(nrows); ++k, ++ia) {
114 for (auto a = backend::row_begin(A, ia); a; ++a) {
115 col_type cb = a.col() / block_size;
116 col_type cc = a.col() % block_size;
117 val_type va = a.value();
118
119 if (marker[cb] < row_beg) {
120 marker[cb] = row_end;
121 col[row_end] = cb;
122 val[block_size * (block_size * row_end + k) + cc] = va;
123 ++row_end;
124 }
125 else {
126 val[block_size * (block_size * marker[cb] + k) + cc] = va;
127 }
128 }
129 }
130 }
131 }
132};
133
134/*---------------------------------------------------------------------------*/
135/*---------------------------------------------------------------------------*/
136
137} // namespace Arcane::Alina
138
139/*---------------------------------------------------------------------------*/
140/*---------------------------------------------------------------------------*/
141
142#endif
Matrix class, to be used by user.
BlockCSRMatrix(const Matrix &A, size_t block_size)
Converts matrix in CRS format to Block CRS format.