Alien  1.3.0
Developer documentation
Loading...
Searching...
No Matches
SimpleCSRInternalLinearAlgebra.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#pragma once
9
10#include <alien/utils/Precomp.h>
11
13#include <alien/core/backend/IInternalLinearAlgebraT.h>
14
15#include <alien/kernels/simple_csr/SimpleCSRBackEnd.h>
16#include <alien/kernels/simple_csr/SimpleCSRVector.h>
17#include <alien/kernels/simple_csr/SendRecvOp.h>
18#include <alien/kernels/simple_csr/LUSendRecvOp.h>
19
22
23#include <alien/utils/ExceptionUtils.h>
24
25#include <alien/utils/StdTimer.h>
26/*---------------------------------------------------------------------------*/
27
28namespace Alien
29{
30
33
34template <>
35struct LUSendRecvTraits<BackEnd::tag::simplecsr>
36{
37 // clang-format off
38 typedef CSRMatrix matrix_type ;
39 typedef CSRVector vector_type ;
41 typedef SimpleCSRInternal::LUSendRecvOp<CSRMatrix> matrix_op_type ;
42 typedef SimpleCSRInternal::SendRecvOp<value_type> vector_op_type ;
43 // clang-format on
44};
45
46class ALIEN_EXPORT SimpleCSRInternalLinearAlgebra
47: public IInternalLinearAlgebra<CSRMatrix, CSRVector>
48{
49 public:
50 typedef BackEnd::tag::simplecsr BackEndType;
51
52 typedef std::tuple<VectorDistribution const*,Integer> ResourceType;
53
54 class NullValueException
56 {
57 public:
58 typedef Exception::NumericException BaseType;
59 NullValueException(std::string const& type)
60 : BaseType(type, __LINE__)
61 {}
62 };
63
64 template <typename T>
65 class Future
66 {
67 public:
68 Future(T& value)
69 : m_value(value)
70 {}
71
72 T& operator()()
73 {
74 return m_value;
75 }
76
77 T operator()() const
78 {
79 return m_value;
80 }
81
82 T get()
83 {
84 return m_value;
85 }
86
87 private:
88 T& m_value;
89 };
90
91 typedef Future<Real> FutureType;
92
93 typedef Alien::StdTimer TimerType;
94 typedef TimerType::Sentry SentryType;
95
96 SimpleCSRInternalLinearAlgebra();
97 virtual ~SimpleCSRInternalLinearAlgebra();
98
99 public:
100 // IInternalLinearAlgebra interface.
101 Real norm0(const Vector& x) const;
102 Real norm1(const Vector& x) const;
103 Real norm2(const Vector& x) const;
104 Real normInf(const Vector& x) const;
105
106 void synchronize(const Matrix& A, Vector& x) const;
107
108 void mult(const Matrix& A, const Vector& x, Vector& r) const;
109 void addLMult(Real alpha, const Matrix& A, const Vector& x, Vector& y) const;
110 void addUMult(Real alpha, const Matrix& A, const Vector& x, Vector& y) const;
111
112 void endDistAddLMult(Real alpha, const Matrix& A, const Vector& x, Vector& y) const;
113 void endDistAddUMult(Real alpha, const Matrix& A, const Vector& x, Vector& y) const;
114
115 void lowerUpperSynchronize(const Matrix& A, Vector& y) const;
116 void upperLowerSynchronize(const Matrix& A, Vector& y) const;
117
118 void multDiag(const Matrix& A, Vector& y) const;
119 void computeDiag(const Matrix& a, Vector& inv_diag) const;
120
121 void multInvDiag(const Matrix& A, Vector& y) const;
122 void computeInvDiag(const Matrix& a, Vector& inv_diag) const;
123
124 void axpy(Real alpha, const Vector& x, Vector& r) const;
125 void aypx(Real alpha, Vector& y, const Vector& x) const;
126 void copy(const Vector& x, Vector& r) const;
127
128 void axpy(Real alpha, const Vector& x, Integer stride_x, Vector& r, Integer stride_r) const;
129 void axpy(Real alpha, const Vector& x, Vector& r, ConstArrayView<Integer> stride_r) const;
130 void aypx(Real alpha, Vector& y, Integer stride_y, const Vector& x,Integer stride_x) const;
131
132 void copy(const Vector& x, Integer stride_x, Vector& r, Integer stride_r) const;
133 void copy(const Vector& x, ConstArrayView<Integer> strides_x, Vector& r) const;
134 void copy(const Vector& x, Vector& r, ConstArrayView<Integer> strides_r) const;
135
136 Real dot(const Vector& x, const Vector& y) const;
137 void dot(const Vector& x, const Vector& y, FutureType& res) const;
138
139 void scal(Real alpha, Vector& x) const;
140 void scal(const Vector& x, Matrix& a) const;
141 void diagonal(const Matrix& a, Vector& x) const;
142 void reciprocal(Vector& x) const;
143 void pointwiseMult(const Vector& x, const Vector& y, Vector& w) const;
144 void assign(Vector& x, Real alpha) const;
145
146 template <typename LambdaT>
147 void assign(Vector& x, LambdaT const& lambda) const
148 {
149 auto x_ptr = x.getDataPtr();
150 for (Integer i = 0; i < x.getAllocSize(); ++i) {
151 x_ptr[i] = lambda(i);
152 }
153 }
154
155 template <typename PrecondT>
156 void exec(PrecondT& precond, Vector const& x, Vector& y)
157 {
158 return precond.solve(*this, x, y);
159 }
160
161 Integer computeCxr(const Matrix& a, Matrix& cxr_a) const ;
162 Integer computeCxr(const Matrix& a, Matrix& cxr_a, UniqueArray<Integer>& strides) const ;
163 Integer computeCxr(const Matrix& a, Vector const& diag_scal, Matrix& cxr_a) const ;
164
165 static ResourceType resource(Matrix const& A);
166
167 void allocate(ResourceType resource, Vector& v);
168
169 template <typename T0, typename... T>
170 void allocate(ResourceType resource, T0& v0, T&... args)
171 {
172 allocate(resource, v0);
173 allocate(resource, args...);
174 }
175
176 void free(Vector& v);
177
178 template <typename T0, typename... T>
179 void free(T0& v0, T&... args)
180 {
181 free(v0);
182 free(args...);
183 }
184
185#ifdef ALIEN_USE_PERF_TIMER
186 private:
187 mutable TimerType m_timer;
188#endif
189};
190
191class SimpleCSRInternalLinearAlgebraExpr
192: public IInternalLinearAlgebraExpr<CSRMatrix, CSRVector>
193{
194 public:
195 SimpleCSRInternalLinearAlgebraExpr();
196 virtual ~SimpleCSRInternalLinearAlgebraExpr();
197
198 public:
199 // IInternalLinearAlgebra interface.
200 Real norm0(const Vector& x) const;
201 Real norm1(const Vector& x) const;
202 Real norm2(const Vector& x) const;
203 Real normInf(const Vector& x) const;
204 Real norm2(const Matrix& x) const;
205 void mult(const Matrix& a, const Vector& x, Vector& r) const;
206 void axpy(Real alpha, const Vector& x, Vector& r) const;
207 void aypx(Real alpha, Vector& y, const Vector& x) const;
208 void copy(const Vector& x, Vector& r) const;
209 Real dot(const Vector& x, const Vector& y) const;
210 void scal(Real alpha, Vector& x) const;
211 void diagonal(const Matrix& a, Vector& x) const;
212 void reciprocal(Vector& x) const;
213 void pointwiseMult(const Vector& x, const Vector& y, Vector& w) const;
214
215 // IInternalLinearAlgebraExpr interface.
216
217 void mult(const Matrix& a, const UniqueArray<Real>& x, UniqueArray<Real>& r) const;
218 void axpy(Real alpha, UniqueArray<Real> const& x, UniqueArray<Real>& r) const;
219 void aypx(Real alpha, UniqueArray<Real>& y, UniqueArray<Real> const& x) const;
220 void copy(const UniqueArray<Real>& x, UniqueArray<Real>& r) const;
221 Real dot(
222 Integer local_size, const UniqueArray<Real>& x, const UniqueArray<Real>& y) const;
223
224 void scal(Real alpha, UniqueArray<Real>& x) const;
225
226 void copy(const Matrix& a, Matrix& r) const;
227 void add(const Matrix& a, Matrix& r) const;
228 void scal(Real alpha, Matrix& r) const;
229
230 private:
231 // No member.
232};
233
234} // namespace Alien
235
236/*---------------------------------------------------------------------------*/
IInternalLinearAlgebraExprT.h.
MatrixDistribution.h.
VectorDistribution.h.
Internal linear algebra interface.
Internal linear algebra interface.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17