SparseSolverBase.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) 2014 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_SPARSESOLVERBASE_H
11 #define EIGEN_SPARSESOLVERBASE_H
12 
13 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 
24 template <typename Decomposition, typename Rhs, typename Dest>
25 std::enable_if_t<Rhs::ColsAtCompileTime != 1 && Dest::ColsAtCompileTime != 1> solve_sparse_through_dense_panels(
26  const Decomposition& dec, const Rhs& rhs, Dest& dest) {
27  EIGEN_STATIC_ASSERT((Dest::Flags & RowMajorBit) == 0, THIS_METHOD_IS_ONLY_FOR_COLUMN_MAJOR_MATRICES);
28  typedef typename Dest::Scalar DestScalar;
29  // we process the sparse rhs per block of NbColsAtOnce columns temporarily stored into a dense matrix.
30  static const Index NbColsAtOnce = 4;
31  Index rhsCols = rhs.cols();
32  Index size = rhs.rows();
33  // the temporary matrices do not need more columns than NbColsAtOnce:
34  Index tmpCols = (std::min)(rhsCols, NbColsAtOnce);
37  for (Index k = 0; k < rhsCols; k += NbColsAtOnce) {
38  Index actualCols = std::min<Index>(rhsCols - k, NbColsAtOnce);
39  tmp.leftCols(actualCols) = rhs.middleCols(k, actualCols);
40  tmpX.leftCols(actualCols) = dec.solve(tmp.leftCols(actualCols));
41  dest.middleCols(k, actualCols) = tmpX.leftCols(actualCols).sparseView();
42  }
43 }
44 
45 // Overload for vector as rhs
46 template <typename Decomposition, typename Rhs, typename Dest>
47 std::enable_if_t<Rhs::ColsAtCompileTime == 1 || Dest::ColsAtCompileTime == 1> solve_sparse_through_dense_panels(
48  const Decomposition& dec, const Rhs& rhs, Dest& dest) {
49  typedef typename Dest::Scalar DestScalar;
50  Index size = rhs.rows();
53  dest_dense = dec.solve(rhs_dense);
54  dest = dest_dense.sparseView();
55 }
56 
57 } // end namespace internal
58 
66 template <typename Derived>
68  public:
71 
73 
75 
76  Derived& derived() { return *static_cast<Derived*>(this); }
77  const Derived& derived() const { return *static_cast<const Derived*>(this); }
78 
83  template <typename Rhs>
84  inline const Solve<Derived, Rhs> solve(const MatrixBase<Rhs>& b) const {
85  eigen_assert(m_isInitialized && "Solver is not initialized.");
86  eigen_assert(derived().rows() == b.rows() && "solve(): invalid number of rows of the right hand side matrix b");
87  return Solve<Derived, Rhs>(derived(), b.derived());
88  }
89 
94  template <typename Rhs>
95  inline const Solve<Derived, Rhs> solve(const SparseMatrixBase<Rhs>& b) const {
96  eigen_assert(m_isInitialized && "Solver is not initialized.");
97  eigen_assert(derived().rows() == b.rows() && "solve(): invalid number of rows of the right hand side matrix b");
98  return Solve<Derived, Rhs>(derived(), b.derived());
99  }
100 
101 #ifndef EIGEN_PARSED_BY_DOXYGEN
103  template <typename Rhs, typename Dest>
106  }
107 #endif // EIGEN_PARSED_BY_DOXYGEN
108 
109  protected:
110  mutable bool m_isInitialized;
111 };
112 
113 } // end namespace Eigen
114 
115 #endif // EIGEN_SPARSESOLVERBASE_H
#define eigen_assert(x)
Definition: Macros.h:910
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
int rows
Definition: Tutorial_commainit_02.cpp:1
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
Scalar * b
Definition: benchVecAdd.cpp:17
SCALAR Scalar
Definition: bench_gemm.cpp:45
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
Pseudo expression representing a solving operation.
Definition: Solve.h:62
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:30
const Derived & derived() const
Definition: SparseMatrixBase.h:144
A base class for sparse solvers.
Definition: SparseSolverBase.h:67
SparseSolverBase(SparseSolverBase &&other)
Definition: SparseSolverBase.h:72
const Solve< Derived, Rhs > solve(const SparseMatrixBase< Rhs > &b) const
Definition: SparseSolverBase.h:95
const Solve< Derived, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition: SparseSolverBase.h:84
void _solve_impl(const SparseMatrixBase< Rhs > &b, SparseMatrixBase< Dest > &dest) const
Definition: SparseSolverBase.h:104
SparseSolverBase()
Definition: SparseSolverBase.h:70
~SparseSolverBase()
Definition: SparseSolverBase.h:74
bool m_isInitialized
Definition: SparseSolverBase.h:110
Derived & derived()
Definition: SparseSolverBase.h:76
const Derived & derived() const
Definition: SparseSolverBase.h:77
Definition: Meta.h:281
EIGEN_DEVICE_FUNC noncopyable()
Definition: Meta.h:286
#define min(a, b)
Definition: datatypes.h:22
const unsigned int RowMajorBit
Definition: Constants.h:70
char char char int int * k
Definition: level2_impl.h:374
Eigen::Matrix< Scalar, Dynamic, Dynamic, ColMajor > tmp
Definition: level3_impl.h:365
@ Rhs
Definition: TensorContractionMapper.h:20
std::enable_if_t< Rhs::ColsAtCompileTime !=1 &&Dest::ColsAtCompileTime !=1 > solve_sparse_through_dense_panels(const Decomposition &dec, const Rhs &rhs, Dest &dest)
Definition: SparseSolverBase.h:25
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
Definition: Eigen_Colamd.h:49