Arcane  4.1.11.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
BlockCSRBackend.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/* BlockCSRBackend.h (C) 2000-2026 */
9/* */
10/* Sparse matrix in block-CSR format. . */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_ALINA_BLOCKCSRBACKEND_H
13#define ARCCORE_ALINA_BLOCKCSRBACKEND_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#include "arccore/alina/ValueTypeInterface.h"
28#include "arccore/alina/BuiltinBackend.h"
29#include "arccore/alina/SkylineLUSolver.h"
30#include "arccore/alina/BlockCSRMatrix.h"
31
32#include <algorithm>
33#include <numeric>
34
35/*---------------------------------------------------------------------------*/
36/*---------------------------------------------------------------------------*/
37
38namespace Arcane::Alina::backend
39{
40
41/*---------------------------------------------------------------------------*/
42/*---------------------------------------------------------------------------*/
49template <typename real>
51{
52 using BuiltinBackendType = BuiltinBackend<real>;
53 typedef real value_type;
54 typedef BuiltinBackendType::ptr_type index_type;
55 typedef BuiltinBackendType::col_type col_type;
56 typedef BuiltinBackendType::ptr_type ptr_type;
57
59 typedef typename BuiltinBackend<real>::vector vector;
60 typedef typename BuiltinBackend<real>::vector matrix_diagonal;
61 typedef solver::SkylineLUSolver<value_type> direct_solver;
62
63 struct provides_row_iterator : std::false_type
64 {};
65
67 struct params
68 {
71
72 explicit params(Int32 block_size = 4)
74 {}
75
76 params(const PropertyTree& p)
77 : ARCCORE_ALINA_PARAMS_IMPORT_VALUE(p, block_size)
78 {
79 p.check_params( { "block_size" });
80 }
81 void get(PropertyTree& p, const std::string& path) const
82 {
83 ARCCORE_ALINA_PARAMS_EXPORT_VALUE(p, path, block_size);
84 }
85 };
86
87 static std::string name() { return "block_crs"; }
88
90 static std::shared_ptr<matrix>
91 copy_matrix(std::shared_ptr<typename BuiltinBackend<real>::matrix> A,
92 const params& prm)
93 {
94 return std::make_shared<matrix>(*A, prm.block_size);
95 }
96
98 static std::shared_ptr<vector>
99 copy_vector(const vector& x, const params&)
100 {
101 return std::make_shared<vector>(x);
102 }
103
104 static std::shared_ptr<vector>
105 copy_vector(const std::vector<value_type>& x, const params&)
106 {
107 return std::make_shared<vector>(x);
108 }
109
111 static std::shared_ptr<vector>
112 copy_vector(std::shared_ptr<vector> x, const params&)
113 {
114 return x;
115 }
116
118 static std::shared_ptr<vector>
119 create_vector(size_t size, const params&)
120 {
121 return std::make_shared<vector>(size);
122 }
123
124 static std::shared_ptr<direct_solver>
125 create_solver(std::shared_ptr<typename BuiltinBackend<real>::matrix> A,
126 const params&)
127 {
128 return std::make_shared<direct_solver>(*A);
129 }
130};
131
132//---------------------------------------------------------------------------
133// Specialization of backend interface
134//---------------------------------------------------------------------------
135template <typename V, typename C, typename P>
136struct rows_impl<BlockCSRMatrix<V, C, P>>
137{
138 static size_t get(const BlockCSRMatrix<V, C, P>& A)
139 {
140 return A.m_nbRow;
141 }
142};
143
144template <typename V, typename C, typename P>
145struct cols_impl<BlockCSRMatrix<V, C, P>>
146{
147 static size_t get(const BlockCSRMatrix<V, C, P>& A)
148 {
149 return A.ncols;
150 }
151};
152
153template <typename V, typename C, typename P>
155{
156 static size_t get(const BlockCSRMatrix<V, C, P>& A)
157 {
158 return A.ptr.back() * A.block_size * A.block_size;
159 }
160};
161
162/*---------------------------------------------------------------------------*/
163/*---------------------------------------------------------------------------*/
164
165template <typename Alpha, typename Beta, typename V, typename C, typename P, class Vec1, class Vec2>
166struct spmv_impl<Alpha, BlockCSRMatrix<V, C, P>, Vec1, Beta, Vec2>
167{
168 typedef BlockCSRMatrix<V, C, P> matrix;
169
170 static void apply(Alpha alpha, const matrix& A, const Vec1& x, Beta beta, Vec2& y)
171 {
172 const size_t nb = A.brows;
173 const size_t na = A.nrows;
174 const size_t ma = A.ncols;
175 const size_t b1 = A.block_size;
176 const size_t b2 = b1 * b1;
177
178 if (!math::is_zero(beta)) {
179 if (beta != 1) {
180 arccoreParallelFor(0, na, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
181 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
182 y[i] *= beta;
183 }
184 });
185 }
186 }
187 else {
188 backend::clear(y);
189 }
190
191 arccoreParallelFor(0, nb, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
192 for (ptrdiff_t ib = begin; ib < (begin + size); ++ib) {
193 for (P jb = A.ptr[ib], eb = A.ptr[ib + 1]; jb < eb; ++jb) {
194 size_t x0 = A.col[jb] * b1;
195 size_t y0 = ib * b1;
196 block_prod(b1, std::min(b1, ma - x0), std::min(b1, na - y0),
197 alpha, &A.val[jb * b2], &x[x0], &y[y0]);
198 }
199 }
200 });
201 }
202
203 static void block_prod(size_t dim, size_t nx, size_t ny,
204 Alpha alpha, const V* A, const V* x, V* y)
205 {
206 for (size_t i = 0; i < ny; ++i, ++y) {
207 const V* xx = x;
208 V sum = 0;
209 for (size_t j = 0; j < dim; ++j, ++A, ++xx)
210 if (j < nx)
211 sum += (*A) * (*xx);
212 *y += alpha * sum;
213 }
214 }
215};
216
217/*---------------------------------------------------------------------------*/
218/*---------------------------------------------------------------------------*/
219
220template <typename V, typename C, typename P, class Vec1, class Vec2, class Vec3>
221struct residual_impl<BlockCSRMatrix<V, C, P>, Vec1, Vec2, Vec3>
222{
223 typedef BlockCSRMatrix<V, C, P> matrix;
224
225 static void apply(const Vec1& rhs, const matrix& A, const Vec2& x, Vec3& r)
226 {
227 typedef typename math::scalar_of<V>::type S;
228 const auto one = math::identity<S>();
229 backend::copy(rhs, r);
230 backend::spmv(-one, A, x, one, r);
231 }
232};
233
234/*---------------------------------------------------------------------------*/
235/*---------------------------------------------------------------------------*/
236
237} // namespace Arcane::Alina::backend
238
239/*---------------------------------------------------------------------------*/
240/*---------------------------------------------------------------------------*/
241
242#endif
Direct solver that uses Skyline LU factorization.
Informations d'exécution d'une boucle.
void arccoreParallelFor(const ComplexForLoopRanges< RankValue > &loop_ranges, const ForLoopRunInfo &run_info, const LambdaType &lambda_function, const ReducerArgs &... reducer_args)
Applique en concurrence la fonction lambda lambda_function sur l'intervalle d'itération donné par loo...
Definition ParallelFor.h:85
std::int32_t Int32
Type entier signé sur 32 bits.
Sparse matrix in Block CSR format.
Int32 block_size
Block size to use with the created matrices.
BlockCSRBackend backend definition.
static std::shared_ptr< vector > create_vector(size_t size, const params &)
Create vector of the specified size.
static std::shared_ptr< vector > copy_vector(std::shared_ptr< vector > x, const params &)
Copy vector from builtin backend.
static std::shared_ptr< matrix > copy_matrix(std::shared_ptr< typename BuiltinBackend< real >::matrix > A, const params &prm)
Copy matrix from builtin backend.
static std::shared_ptr< vector > copy_vector(const vector &x, const params &)
Copy vector from builtin backend.
Implementation for function returning the number of columns in a matrix.
Implementation for function returning the number of nonzeros in a matrix.
Implementation for residual error compuatation.
Implementation for function returning the number of rows in a matrix.
Implementation for matrix-vector product.