SolveTriangular.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_SOLVETRIANGULAR_H
11 #define EIGEN_SOLVETRIANGULAR_H
12 
13 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 
20 // Forward declarations:
21 // The following two routines are implemented in the products/TriangularSolver*.h files
22 template <typename LhsScalar, typename RhsScalar, typename Index, int Side, int Mode, bool Conjugate, int StorageOrder>
24 
25 template <typename Scalar, typename Index, int Side, int Mode, bool Conjugate, int TriStorageOrder,
26  int OtherStorageOrder, int OtherInnerStride>
28 
29 // small helper struct extracting some traits on the underlying solver operation
30 template <typename Lhs, typename Rhs, int Side>
32  private:
33  enum { RhsIsVectorAtCompileTime = (Side == OnTheLeft ? Rhs::ColsAtCompileTime : Rhs::RowsAtCompileTime) == 1 };
34 
35  public:
36  enum {
37  Unrolling = (RhsIsVectorAtCompileTime && Rhs::SizeAtCompileTime != Dynamic && Rhs::SizeAtCompileTime <= 8)
41  };
42 };
43 
44 template <typename Lhs, typename Rhs,
45  int Side, // can be OnTheLeft/OnTheRight
46  int Mode, // can be Upper/Lower | UnitDiag
50 
51 template <typename Lhs, typename Rhs, int Side, int Mode>
53  typedef typename Lhs::Scalar LhsScalar;
54  typedef typename Rhs::Scalar RhsScalar;
58  static EIGEN_DEVICE_FUNC void run(const Lhs& lhs, Rhs& rhs) {
59  ActualLhsType actualLhs = LhsProductTraits::extract(lhs);
60 
61  // FIXME find a way to allow an inner stride if packet_traits<Scalar>::size==1
62 
63  bool useRhsDirectly = Rhs::InnerStrideAtCompileTime == 1 || rhs.innerStride() == 1;
64 
65  ei_declare_aligned_stack_constructed_variable(RhsScalar, actualRhs, rhs.size(), (useRhsDirectly ? rhs.data() : 0));
66 
67  if (!useRhsDirectly) MappedRhs(actualRhs, rhs.size()) = rhs;
68 
69  triangular_solve_vector<LhsScalar, RhsScalar, Index, Side, Mode, LhsProductTraits::NeedToConjugate,
70  (int(Lhs::Flags) & RowMajorBit) ? RowMajor : ColMajor>::run(actualLhs.cols(),
71  actualLhs.data(),
72  actualLhs.outerStride(),
73  actualRhs);
74 
75  if (!useRhsDirectly) rhs = MappedRhs(actualRhs, rhs.size());
76  }
77 };
78 
79 // the rhs is a matrix
80 template <typename Lhs, typename Rhs, int Side, int Mode>
82  typedef typename Rhs::Scalar Scalar;
85 
86  static EIGEN_DEVICE_FUNC void run(const Lhs& lhs, Rhs& rhs) {
87  add_const_on_value_type_t<ActualLhsType> actualLhs = LhsProductTraits::extract(lhs);
88 
89  const Index size = lhs.rows();
90  const Index othersize = Side == OnTheLeft ? rhs.cols() : rhs.rows();
91 
93  Rhs::MaxRowsAtCompileTime, Rhs::MaxColsAtCompileTime,
94  Lhs::MaxRowsAtCompileTime, 4>
95  BlockingType;
96 
97  // Nothing to solve.
98  if (actualLhs.size() == 0 || rhs.size() == 0) {
99  return;
100  }
101 
102  BlockingType blocking(rhs.rows(), rhs.cols(), size, 1, false);
103 
104  triangular_solve_matrix<Scalar, Index, Side, Mode, LhsProductTraits::NeedToConjugate,
105  (int(Lhs::Flags) & RowMajorBit) ? RowMajor : ColMajor,
106  (Rhs::Flags & RowMajorBit) ? RowMajor : ColMajor,
107  Rhs::InnerStrideAtCompileTime>::run(size, othersize, &actualLhs.coeffRef(0, 0),
108  actualLhs.outerStride(), &rhs.coeffRef(0, 0),
109  rhs.innerStride(), rhs.outerStride(), blocking);
110  }
111 };
112 
113 /***************************************************************************
114  * meta-unrolling implementation
115  ***************************************************************************/
116 
117 template <typename Lhs, typename Rhs, int Mode, int LoopIndex, int Size, bool Stop = LoopIndex == Size>
119 
120 template <typename Lhs, typename Rhs, int Mode, int LoopIndex, int Size>
121 struct triangular_solver_unroller<Lhs, Rhs, Mode, LoopIndex, Size, false> {
122  enum {
123  IsLower = ((Mode & Lower) == Lower),
124  DiagIndex = IsLower ? LoopIndex : Size - LoopIndex - 1,
125  StartIndex = IsLower ? 0 : DiagIndex + 1
126  };
127  static EIGEN_DEVICE_FUNC void run(const Lhs& lhs, Rhs& rhs) {
128  if (LoopIndex > 0)
129  rhs.coeffRef(DiagIndex) -= lhs.row(DiagIndex)
130  .template segment<LoopIndex>(StartIndex)
131  .transpose()
132  .cwiseProduct(rhs.template segment<LoopIndex>(StartIndex))
133  .sum();
134 
135  if (!(Mode & UnitDiag)) rhs.coeffRef(DiagIndex) /= lhs.coeff(DiagIndex, DiagIndex);
136 
138  }
139 };
140 
141 template <typename Lhs, typename Rhs, int Mode, int LoopIndex, int Size>
142 struct triangular_solver_unroller<Lhs, Rhs, Mode, LoopIndex, Size, true> {
143  static EIGEN_DEVICE_FUNC void run(const Lhs&, Rhs&) {}
144 };
145 
146 template <typename Lhs, typename Rhs, int Mode>
148  static EIGEN_DEVICE_FUNC void run(const Lhs& lhs, Rhs& rhs) {
150  }
151 };
152 
153 template <typename Lhs, typename Rhs, int Mode>
155  static EIGEN_DEVICE_FUNC void run(const Lhs& lhs, Rhs& rhs) {
156  Transpose<const Lhs> trLhs(lhs);
157  Transpose<Rhs> trRhs(rhs);
158 
160  ((Mode & Upper) == Upper ? Lower : Upper) | (Mode & UnitDiag), 0,
161  Rhs::SizeAtCompileTime>::run(trLhs, trRhs);
162  }
163 };
164 
165 } // end namespace internal
166 
167 /***************************************************************************
168  * TriangularView methods
169  ***************************************************************************/
170 
171 #ifndef EIGEN_PARSED_BY_DOXYGEN
172 template <typename MatrixType, unsigned int Mode>
173 template <int Side, typename OtherDerived>
174 EIGEN_DEVICE_FUNC void TriangularViewImpl<MatrixType, Mode, Dense>::solveInPlace(
175  const MatrixBase<OtherDerived>& _other) const {
176  OtherDerived& other = _other.const_cast_derived();
177  eigen_assert(derived().cols() == derived().rows() && ((Side == OnTheLeft && derived().cols() == other.rows()) ||
178  (Side == OnTheRight && derived().cols() == other.cols())));
179  eigen_assert((!(int(Mode) & int(ZeroDiag))) && bool(int(Mode) & (int(Upper) | int(Lower))));
180  // If solving for a 0x0 matrix, nothing to do, simply return.
181  if (derived().cols() == 0) return;
182 
183  enum {
184  copy = (internal::traits<OtherDerived>::Flags & RowMajorBit) && OtherDerived::IsVectorAtCompileTime &&
185  OtherDerived::SizeAtCompileTime != 1
186  };
188  OtherCopy;
189  OtherCopy otherCopy(other);
190 
191  internal::triangular_solver_selector<MatrixType, std::remove_reference_t<OtherCopy>, Side, Mode>::run(
192  derived().nestedExpression(), otherCopy);
193 
194  if (copy) other = otherCopy;
195 }
196 
197 template <typename Derived, unsigned int Mode>
198 template <int Side, typename Other>
199 const internal::triangular_solve_retval<Side, TriangularView<Derived, Mode>, Other>
200 TriangularViewImpl<Derived, Mode, Dense>::solve(const MatrixBase<Other>& other) const {
201  return internal::triangular_solve_retval<Side, TriangularViewType, Other>(derived(), other.derived());
202 }
203 #endif
204 
205 namespace internal {
206 
207 template <int Side, typename TriangularType, typename Rhs>
208 struct traits<triangular_solve_retval<Side, TriangularType, Rhs> > {
210 };
211 
212 template <int Side, typename TriangularType, typename Rhs>
213 struct triangular_solve_retval : public ReturnByValue<triangular_solve_retval<Side, TriangularType, Rhs> > {
216 
217  triangular_solve_retval(const TriangularType& tri, const Rhs& rhs) : m_triangularMatrix(tri), m_rhs(rhs) {}
218 
219  inline EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT { return m_rhs.rows(); }
220  inline EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT { return m_rhs.cols(); }
221 
222  template <typename Dest>
223  inline void evalTo(Dest& dst) const {
224  if (!is_same_dense(dst, m_rhs)) dst = m_rhs;
225  m_triangularMatrix.template solveInPlace<Side>(dst);
226  }
227 
228  protected:
229  const TriangularType& m_triangularMatrix;
230  typename Rhs::Nested m_rhs;
231 };
232 
233 } // namespace internal
234 
235 } // end namespace Eigen
236 
237 #endif // EIGEN_SOLVETRIANGULAR_H
#define EIGEN_NOEXCEPT
Definition: Macros.h:1267
#define EIGEN_CONSTEXPR
Definition: Macros.h:758
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define eigen_assert(x)
Definition: Macros.h:910
#define ei_declare_aligned_stack_constructed_variable(TYPE, NAME, SIZE, BUFFER)
Definition: Memory.h:806
Side
Definition: Side.h:9
Tridiagonalization< MatrixXf > tri
Definition: Tridiagonalization_compute.cpp:1
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
SCALAR Scalar
Definition: bench_gemm.cpp:45
Definition: ForwardDeclarations.h:102
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:79
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: CwiseBinaryOp.h:116
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:96
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
Definition: ReturnByValue.h:50
Expression of the transpose of a matrix.
Definition: Transpose.h:56
Definition: GeneralMatrixMatrix.h:223
Definition: SolveTriangular.h:31
@ RhsVectors
Definition: SolveTriangular.h:40
@ Unrolling
Definition: SolveTriangular.h:37
@ RhsIsVectorAtCompileTime
Definition: SolveTriangular.h:33
@ UnitDiag
Definition: Constants.h:215
@ ZeroDiag
Definition: Constants.h:217
@ Lower
Definition: Constants.h:211
@ Upper
Definition: Constants.h:213
@ Aligned
Definition: Constants.h:242
@ ColMajor
Definition: Constants.h:318
@ RowMajor
Definition: Constants.h:320
@ CompleteUnrolling
Definition: Constants.h:306
@ NoUnrolling
Definition: Constants.h:301
@ OnTheLeft
Definition: Constants.h:331
@ OnTheRight
Definition: Constants.h:333
const unsigned int RowMajorBit
Definition: Constants.h:70
return int(ret)+1
EIGEN_BLAS_FUNC() copy(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_impl.h:32
@ Lhs
Definition: TensorContractionMapper.h:20
@ Rhs
Definition: TensorContractionMapper.h:20
typename remove_all< T >::type remove_all_t
Definition: Meta.h:142
EIGEN_DEVICE_FUNC bool is_same_dense(const T1 &mat1, const T2 &mat2, std::enable_if_t< possibly_same_dense< T1, T2 >::value > *=0)
Definition: XprHelper.h:869
typename add_const_on_value_type< T >::type add_const_on_value_type_t
Definition: Meta.h:274
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
auto run(Kernel kernel, Args &&... args) -> decltype(kernel(args...))
Definition: gpu_test_helper.h:414
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
const int Dynamic
Definition: Constants.h:25
type
Definition: compute_granudrum_aor.py:141
Definition: Eigen_Colamd.h:49
Update the problem specs before solve
Definition: steady_axisym_advection_diffusion.cc:353
Definition: BlasUtil.h:459
std::conditional_t< bool(HasUsableDirectAccess), ExtractType, typename ExtractType_::PlainObject > DirectLinearAccessType
Definition: BlasUtil.h:475
internal::plain_matrix_type_column_major< Rhs >::type ReturnType
Definition: SolveTriangular.h:209
Definition: ForwardDeclarations.h:21
Definition: SolveTriangular.h:27
Definition: SolveTriangular.h:213
const TriangularType & m_triangularMatrix
Definition: SolveTriangular.h:229
void evalTo(Dest &dst) const
Definition: SolveTriangular.h:223
triangular_solve_retval(const TriangularType &tri, const Rhs &rhs)
Definition: SolveTriangular.h:217
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: SolveTriangular.h:220
ReturnByValue< triangular_solve_retval > Base
Definition: SolveTriangular.h:215
remove_all_t< typename Rhs::Nested > RhsNestedCleaned
Definition: SolveTriangular.h:214
Rhs::Nested m_rhs
Definition: SolveTriangular.h:230
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: SolveTriangular.h:219
Definition: SolveTriangular.h:23
static EIGEN_DEVICE_FUNC void run(const Lhs &lhs, Rhs &rhs)
Definition: SolveTriangular.h:148
static EIGEN_DEVICE_FUNC void run(const Lhs &lhs, Rhs &rhs)
Definition: SolveTriangular.h:155
static EIGEN_DEVICE_FUNC void run(const Lhs &lhs, Rhs &rhs)
Definition: SolveTriangular.h:58
Map< Matrix< RhsScalar, Dynamic, 1 >, Aligned > MappedRhs
Definition: SolveTriangular.h:57
LhsProductTraits::ExtractType ActualLhsType
Definition: SolveTriangular.h:56
LhsProductTraits::DirectLinearAccessType ActualLhsType
Definition: SolveTriangular.h:84
static EIGEN_DEVICE_FUNC void run(const Lhs &lhs, Rhs &rhs)
Definition: SolveTriangular.h:86
Definition: SolveTriangular.h:49
static EIGEN_DEVICE_FUNC void run(const Lhs &lhs, Rhs &rhs)
Definition: SolveTriangular.h:127
static EIGEN_DEVICE_FUNC void run(const Lhs &, Rhs &)
Definition: SolveTriangular.h:143
Definition: SolveTriangular.h:118
void run(const string &dir_name, LinearSolver *linear_solver_pt, const unsigned nel_1d, bool mess_up_order)
Definition: two_d_poisson_compare_solvers.cc:317