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