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 multDiag(const Matrix& A, Vector& y) const;
113 void computeDiag(const Matrix& a, Vector& inv_diag) const;
114
115 void multInvDiag(const Matrix& A, Vector& y) const;
116 void computeInvDiag(const Matrix& a, Vector& inv_diag) const;
117
118 void axpy(Real alpha, const Vector& x, Vector& r) const;
119 void aypx(Real alpha, Vector& y, const Vector& x) const;
120 void copy(const Vector& x, Vector& r) const;
121
122 void axpy(Real alpha, const Vector& x,Integer stride_x,Vector& r,Integer stride_r) const;
123 void aypx(Real alpha, Vector& y, Integer stride_y, const Vector& x,Integer stride_x) const;
124 void copy(const Vector& x, Integer stride_x, Vector& r, Integer stride_r) const;
125
126 Real dot(const Vector& x, const Vector& y) const;
127 void dot(const Vector& x, const Vector& y, FutureType& res) const;
128
129 void scal(Real alpha, Vector& x) const;
130 void scal(const Vector& x, Matrix& a) const;
131 void diagonal(const Matrix& a, Vector& x) const;
132 void reciprocal(Vector& x) const;
133 void pointwiseMult(const Vector& x, const Vector& y, Vector& w) const;
134 void assign(Vector& x, Real alpha) const;
135
136 template <typename LambdaT>
137 void assign(Vector& x, LambdaT const& lambda) const
138 {
139 auto x_ptr = x.getDataPtr();
140 for (Integer i = 0; i < x.getAllocSize(); ++i) {
141 x_ptr[i] = lambda(i);
142 }
143 }
144
145 template <typename PrecondT>
146 void exec(PrecondT& precond, Vector const& x, Vector& y)
147 {
148 return precond.solve(*this, x, y);
149 }
150
151 Integer computeCxr(const Matrix& a, Matrix& cxr_a) const ;
152 Integer computeCxr(const Matrix& a, Vector const& diag_scal, Matrix& cxr_a) const ;
153
154 static ResourceType resource(Matrix const& A);
155
156 void allocate(ResourceType resource, Vector& v);
157
158 template <typename T0, typename... T>
159 void allocate(ResourceType resource, T0& v0, T&... args)
160 {
161 allocate(resource, v0);
162 allocate(resource, args...);
163 }
164
165 void free(Vector& v);
166
167 template <typename T0, typename... T>
168 void free(T0& v0, T&... args)
169 {
170 free(v0);
171 free(args...);
172 }
173
174#ifdef ALIEN_USE_PERF_TIMER
175 private:
176 mutable TimerType m_timer;
177#endif
178};
179
180class SimpleCSRInternalLinearAlgebraExpr
181: public IInternalLinearAlgebraExpr<CSRMatrix, CSRVector>
182{
183 public:
184 SimpleCSRInternalLinearAlgebraExpr();
185 virtual ~SimpleCSRInternalLinearAlgebraExpr();
186
187 public:
188 // IInternalLinearAlgebra interface.
189 Real norm0(const Vector& x) const;
190 Real norm1(const Vector& x) const;
191 Real norm2(const Vector& x) const;
192 Real normInf(const Vector& x) const;
193 Real norm2(const Matrix& x) const;
194 void mult(const Matrix& a, const Vector& x, Vector& r) const;
195 void axpy(Real alpha, const Vector& x, Vector& r) const;
196 void aypx(Real alpha, Vector& y, const Vector& x) const;
197 void copy(const Vector& x, Vector& r) const;
198 Real dot(const Vector& x, const Vector& y) const;
199 void scal(Real alpha, Vector& x) const;
200 void diagonal(const Matrix& a, Vector& x) const;
201 void reciprocal(Vector& x) const;
202 void pointwiseMult(const Vector& x, const Vector& y, Vector& w) const;
203
204 // IInternalLinearAlgebraExpr interface.
205
206 void mult(const Matrix& a, const UniqueArray<Real>& x, UniqueArray<Real>& r) const;
207 void axpy(Real alpha, UniqueArray<Real> const& x, UniqueArray<Real>& r) const;
208 void aypx(Real alpha, UniqueArray<Real>& y, UniqueArray<Real> const& x) const;
209 void copy(const UniqueArray<Real>& x, UniqueArray<Real>& r) const;
210 Real dot(
211 Integer local_size, const UniqueArray<Real>& x, const UniqueArray<Real>& y) const;
212
213 void scal(Real alpha, UniqueArray<Real>& x) const;
214
215 void copy(const Matrix& a, Matrix& r) const;
216 void add(const Matrix& a, Matrix& r) const;
217 void scal(Real alpha, Matrix& r) const;
218
219 private:
220 // No member.
221};
222
223} // namespace Alien
224
225/*---------------------------------------------------------------------------*/
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