Arcane  v4.1.10.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 typedef real value_type;
53 typedef ptrdiff_t index_type;
54 typedef ptrdiff_t col_type;
55 typedef ptrdiff_t ptr_type;
56
58 typedef typename BuiltinBackend<real>::vector vector;
59 typedef typename BuiltinBackend<real>::vector matrix_diagonal;
60 typedef solver::SkylineLUSolver<value_type> direct_solver;
61
62 struct provides_row_iterator : std::false_type
63 {};
64
66 struct params
67 {
70
71 explicit params(Int32 block_size = 4)
73 {}
74
75 params(const PropertyTree& p)
76 : ARCCORE_ALINA_PARAMS_IMPORT_VALUE(p, block_size)
77 {
78 p.check_params( { "block_size" });
79 }
80 void get(PropertyTree& p, const std::string& path) const
81 {
82 ARCCORE_ALINA_PARAMS_EXPORT_VALUE(p, path, block_size);
83 }
84 };
85
86 static std::string name() { return "block_crs"; }
87
89 static std::shared_ptr<matrix>
90 copy_matrix(std::shared_ptr<typename BuiltinBackend<real>::matrix> A,
91 const params& prm)
92 {
93 return std::make_shared<matrix>(*A, prm.block_size);
94 }
95
97 static std::shared_ptr<vector>
98 copy_vector(const vector& x, const params&)
99 {
100 return std::make_shared<vector>(x);
101 }
102
103 static std::shared_ptr<vector>
104 copy_vector(const std::vector<value_type>& x, const params&)
105 {
106 return std::make_shared<vector>(x);
107 }
108
110 static std::shared_ptr<vector>
111 copy_vector(std::shared_ptr<vector> x, const params&)
112 {
113 return x;
114 }
115
117 static std::shared_ptr<vector>
118 create_vector(size_t size, const params&)
119 {
120 return std::make_shared<vector>(size);
121 }
122
123 static std::shared_ptr<direct_solver>
124 create_solver(std::shared_ptr<typename BuiltinBackend<real>::matrix> A,
125 const params&)
126 {
127 return std::make_shared<direct_solver>(*A);
128 }
129};
130
131//---------------------------------------------------------------------------
132// Specialization of backend interface
133//---------------------------------------------------------------------------
134template <typename V, typename C, typename P>
135struct rows_impl<BlockCSRMatrix<V, C, P>>
136{
137 static size_t get(const BlockCSRMatrix<V, C, P>& A)
138 {
139 return A.m_nbRow;
140 }
141};
142
143template <typename V, typename C, typename P>
144struct cols_impl<BlockCSRMatrix<V, C, P>>
145{
146 static size_t get(const BlockCSRMatrix<V, C, P>& A)
147 {
148 return A.ncols;
149 }
150};
151
152template <typename V, typename C, typename P>
154{
155 static size_t get(const BlockCSRMatrix<V, C, P>& A)
156 {
157 return A.ptr.back() * A.block_size * A.block_size;
158 }
159};
160
161/*---------------------------------------------------------------------------*/
162/*---------------------------------------------------------------------------*/
163
164template <typename Alpha, typename Beta, typename V, typename C, typename P, class Vec1, class Vec2>
165struct spmv_impl<Alpha, BlockCSRMatrix<V, C, P>, Vec1, Beta, Vec2>
166{
167 typedef BlockCSRMatrix<V, C, P> matrix;
168
169 static void apply(Alpha alpha, const matrix& A, const Vec1& x, Beta beta, Vec2& y)
170 {
171 const size_t nb = A.brows;
172 const size_t na = A.nrows;
173 const size_t ma = A.ncols;
174 const size_t b1 = A.block_size;
175 const size_t b2 = b1 * b1;
176
177 if (!math::is_zero(beta)) {
178 if (beta != 1) {
179 arccoreParallelFor(0, na, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
180 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
181 y[i] *= beta;
182 }
183 });
184 }
185 }
186 else {
187 backend::clear(y);
188 }
189
190 arccoreParallelFor(0, nb, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
191 for (ptrdiff_t ib = begin; ib < (begin + size); ++ib) {
192 for (P jb = A.ptr[ib], eb = A.ptr[ib + 1]; jb < eb; ++jb) {
193 size_t x0 = A.col[jb] * b1;
194 size_t y0 = ib * b1;
195 block_prod(b1, std::min(b1, ma - x0), std::min(b1, na - y0),
196 alpha, &A.val[jb * b2], &x[x0], &y[y0]);
197 }
198 }
199 });
200 }
201
202 static void block_prod(size_t dim, size_t nx, size_t ny,
203 Alpha alpha, const V* A, const V* x, V* y)
204 {
205 for (size_t i = 0; i < ny; ++i, ++y) {
206 const V* xx = x;
207 V sum = 0;
208 for (size_t j = 0; j < dim; ++j, ++A, ++xx)
209 if (j < nx)
210 sum += (*A) * (*xx);
211 *y += alpha * sum;
212 }
213 }
214};
215
216/*---------------------------------------------------------------------------*/
217/*---------------------------------------------------------------------------*/
218
219template <typename V, typename C, typename P, class Vec1, class Vec2, class Vec3>
220struct residual_impl<BlockCSRMatrix<V, C, P>, Vec1, Vec2, Vec3>
221{
222 typedef BlockCSRMatrix<V, C, P> matrix;
223
224 static void apply(const Vec1& rhs, const matrix& A, const Vec2& x, Vec3& r)
225 {
226 typedef typename math::scalar_of<V>::type S;
227 const auto one = math::identity<S>();
228 backend::copy(rhs, r);
229 backend::spmv(-one, A, x, one, r);
230 }
231};
232
233/*---------------------------------------------------------------------------*/
234/*---------------------------------------------------------------------------*/
235
236} // namespace Arcane::Alina::backend
237
238/*---------------------------------------------------------------------------*/
239/*---------------------------------------------------------------------------*/
240
241#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.
block_crs 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.