12#ifndef EIGEN_PASTIXSUPPORT_H
13#define EIGEN_PASTIXSUPPORT_H
16#include "./InternalHeaderCheck.h"
28template <
typename MatrixType_,
bool IsStrSym = false>
30template <
typename MatrixType_,
int Options>
32template <
typename MatrixType_,
int Options>
37template <
class Pastix>
40template <
typename MatrixType_>
41struct pastix_traits<PastixLU<MatrixType_> > {
42 using MatrixType = MatrixType_;
43 using Scalar =
typename MatrixType_::Scalar;
44 using RealScalar =
typename MatrixType_::RealScalar;
45 using StorageIndex =
typename MatrixType_::StorageIndex;
48template <
typename MatrixType_,
int Options>
49struct pastix_traits<PastixLLT<MatrixType_, Options> > {
50 using MatrixType = MatrixType_;
51 using Scalar =
typename MatrixType_::Scalar;
52 using RealScalar =
typename MatrixType_::RealScalar;
53 using StorageIndex =
typename MatrixType_::StorageIndex;
56template <
typename MatrixType_,
int Options>
57struct pastix_traits<PastixLDLT<MatrixType_, Options> > {
58 using MatrixType = MatrixType_;
59 using Scalar =
typename MatrixType_::Scalar;
60 using RealScalar =
typename MatrixType_::RealScalar;
61 using StorageIndex =
typename MatrixType_::StorageIndex;
68template <
typename Derived>
72 using Base::m_isInitialized;
75 using Base::_solve_impl;
76 using MatrixType =
typename internal::pastix_traits<Derived>::MatrixType;
77 using Scalar =
typename MatrixType::Scalar;
78 using RealScalar =
typename MatrixType::RealScalar;
79 using StorageIndex =
typename MatrixType::StorageIndex;
80 using Vector = Matrix<Scalar, Dynamic, 1>;
81 using ColSpMatrix = SparseMatrix<Scalar, ColMajor, pastix_int_t>;
82 static constexpr int ColsAtCompileTime = MatrixType::ColsAtCompileTime;
83 static constexpr int MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime;
87 (std::is_same<Scalar, float>::value || std::is_same<Scalar, double>::value ||
88 std::is_same<Scalar, std::complex<float>>::value || std::is_same<Scalar, std::complex<double>>::value),
89 THIS_TYPE_IS_NOT_SUPPORTED)
90 pastixInitParam(m_iparm.data(), m_dparm.data());
91 m_iparm(IPARM_VERBOSE) = PastixVerboseNot;
94 ~PastixBase() { clean(); }
97 Array<pastix_int_t, IPARM_SIZE, 1> &iparm() {
return m_iparm; }
99 pastix_int_t &iparm(
int parameter) {
return m_iparm(parameter); }
101 Array<double, DPARM_SIZE, 1> &dparm() {
return m_dparm; }
103 double &dparm(
int parameter) {
return m_dparm(parameter); }
104 Index rows()
const {
return m_matrix.rows(); }
105 Index cols()
const {
return m_matrix.cols(); }
113 template <
typename Rhs,
typename Dest>
114 bool _solve_impl(
const MatrixBase<Rhs> &b, MatrixBase<Dest> &x)
const {
115 eigen_assert(m_isInitialized &&
"The matrix should be factorized first");
116 eigen_assert(b.rows() == rows());
118 Matrix<Scalar, Dynamic, Dynamic, ColMajor> solution = b;
119 Matrix<Scalar, Dynamic, Dynamic, ColMajor> rhs = b;
120 const pastix_int_t n = internal::convert_index<pastix_int_t>(rows());
121 const pastix_int_t nrhs = internal::convert_index<pastix_int_t>(b.cols());
123 if (n != 0 && nrhs != 0) {
124#if PASTIX_VERSION_MINOR >= 3
125 error = pastix_task_solve(m_pastixdata, n, nrhs, solution.data(), n);
127 error = pastix_task_solve(m_pastixdata, nrhs, solution.data(), n);
130 for (pastix_int_t col = 0; error == PASTIX_SUCCESS && col < nrhs; ++col) {
131 if (!solution.col(col).allFinite())
break;
132 error = pastix_task_refine(m_pastixdata, n, 1, rhs.col(col).data(), n, solution.col(col).data(), n);
142 if (m_pastixdata) pastixFinalize(&m_pastixdata);
143 m_isInitialized =
false;
144 m_analysisIsOk =
false;
147 void compute(ColSpMatrix &matrix) {
148 analyzePattern(matrix);
149 if (m_analysisIsOk) factorize(m_matrix);
152 void analyzePattern(ColSpMatrix &matrix) {
153 eigen_assert(matrix.rows() == matrix.cols() &&
"The input matrix should be square");
155 m_matrix.swap(matrix);
157 m_analysisIsOk =
true;
162 pastixInit(&m_pastixdata, MPI_COMM_WORLD, m_iparm.data(), m_dparm.data());
163 const int error = pastix_task_analyze(m_pastixdata, &m_spm);
164 m_analysisIsOk = error == PASTIX_SUCCESS;
168 void factorize(ColSpMatrix &matrix) {
169 eigen_assert(m_analysisIsOk &&
"The analysis phase should be called before the factorization phase");
170 eigen_assert(matrix.rows() == rows() && matrix.cols() == cols());
171 if (&matrix != &m_matrix) m_matrix.swap(matrix);
173 m_isInitialized =
true;
178 const int error = pastix_task_numfact(m_pastixdata, &m_spm);
179 m_isInitialized = error == PASTIX_SUCCESS;
183 Array<pastix_int_t, IPARM_SIZE, 1> m_iparm;
184 Array<double, DPARM_SIZE, 1> m_dparm;
189 m_iparm(IPARM_FREE_CSCUSER) = 0;
191 m_spm.mtxtype =
static_cast<spm_mtxtype_t
>(m_iparm(IPARM_MTX_TYPE));
192 m_spm.flttype = NumTraits<Scalar>::IsComplex
193 ? (std::is_same<RealScalar, float>::value ? SpmComplex32 : SpmComplex64)
194 : (std::is_same<Scalar, float>::value ? SpmFloat : SpmDouble);
195 m_spm.fmttype = SpmCSC;
197 m_spm.n = internal::convert_index<spm_int_t>(m_matrix.rows());
198 m_spm.nnz = internal::convert_index<spm_int_t>(m_matrix.nonZeros());
200 m_spm.colptr = m_matrix.outerIndexPtr();
201 m_spm.rowptr = m_matrix.innerIndexPtr();
202 m_spm.values = m_matrix.valuePtr();
203 spmUpdateComputedFields(&m_spm);
206 ColSpMatrix m_matrix;
208 pastix_data_t *m_pastixdata =
nullptr;
209 bool m_analysisIsOk =
false;
234template <
typename MatrixType_,
bool IsStrSym>
235class PastixLU :
public PastixBase<PastixLU<MatrixType_> > {
237 using MatrixType = MatrixType_;
238 using Base = PastixBase<PastixLU<MatrixType>>;
239 using ColSpMatrix =
typename Base::ColSpMatrix;
240 using StorageIndex =
typename MatrixType::StorageIndex;
243 PastixLU() : Base() { init(); }
245 explicit PastixLU(
const MatrixType &matrix) : Base() {
255 m_structureIsUptodate =
false;
257 grabMatrix(matrix, temp);
266 m_structureIsUptodate =
false;
268 grabMatrix(matrix, temp);
269 Base::analyzePattern(temp);
279 grabMatrix(matrix, temp);
280 Base::factorize(temp);
285 m_structureIsUptodate =
false;
286 m_iparm(IPARM_MTX_TYPE) = PastixGeneral;
287 m_iparm(IPARM_FACTORIZATION) = PastixFactGETRF;
290 void grabMatrix(
const MatrixType &matrix, ColSpMatrix &out) {
291 EIGEN_IF_CONSTEXPR (IsStrSym)
294 if (!m_structureIsUptodate) {
296 m_transposedStructure = matrix.transpose();
299 for (Index j = 0; j < m_transposedStructure.outerSize(); ++j)
300 for (
typename ColSpMatrix::InnerIterator it(m_transposedStructure, j); it; ++it) it.valueRef() = 0.0;
302 m_structureIsUptodate =
true;
305 const ColSpMatrix &converted = matrix;
306 out = m_transposedStructure + converted;
308 out.makeCompressed();
314 ColSpMatrix m_transposedStructure;
315 bool m_structureIsUptodate;
335template <
typename MatrixType_,
int UpLo_>
336class PastixLLT :
public PastixBase<PastixLLT<MatrixType_, UpLo_> > {
338 using MatrixType = MatrixType_;
339 using Base = PastixBase<PastixLLT<MatrixType, UpLo_>>;
340 using ColSpMatrix =
typename Base::ColSpMatrix;
343 enum { UpLo = UpLo_ };
344 PastixLLT() : Base() { init(); }
346 explicit PastixLLT(
const MatrixType &matrix) : Base() {
356 grabMatrix(matrix, temp);
366 grabMatrix(matrix, temp);
367 Base::analyzePattern(temp);
374 grabMatrix(matrix, temp);
375 Base::factorize(temp);
382 m_iparm(IPARM_MTX_TYPE) = NumTraits<typename Base::Scalar>::IsComplex ? PastixHermitian : PastixSymmetric;
383 m_iparm(IPARM_FACTORIZATION) = PastixFactPOTRF;
386 void grabMatrix(
const MatrixType &matrix, ColSpMatrix &out) {
387 out.resize(matrix.rows(), matrix.cols());
389 const ColSpMatrix &converted = matrix;
390 out.template selfadjointView<Lower>() = converted.template selfadjointView<UpLo>();
391 out.makeCompressed();
411template <
typename MatrixType_,
int UpLo_>
412class PastixLDLT :
public PastixBase<PastixLDLT<MatrixType_, UpLo_> > {
414 using MatrixType = MatrixType_;
415 using Base = PastixBase<PastixLDLT<MatrixType, UpLo_>>;
416 using ColSpMatrix =
typename Base::ColSpMatrix;
419 enum { UpLo = UpLo_ };
420 PastixLDLT() : Base() { init(); }
422 explicit PastixLDLT(
const MatrixType &matrix) : Base() {
432 grabMatrix(matrix, temp);
442 grabMatrix(matrix, temp);
443 Base::analyzePattern(temp);
450 grabMatrix(matrix, temp);
451 Base::factorize(temp);
458 m_iparm(IPARM_MTX_TYPE) = NumTraits<typename Base::Scalar>::IsComplex ? PastixHermitian : PastixSymmetric;
459 m_iparm(IPARM_FACTORIZATION) = NumTraits<typename Base::Scalar>::IsComplex ? PastixFactHETRF : PastixFactSYTRF;
462 void grabMatrix(
const MatrixType &matrix, ColSpMatrix &out) {
464 out.resize(matrix.rows(), matrix.cols());
465 const ColSpMatrix &converted = matrix;
466 out.template selfadjointView<Lower>() = converted.template selfadjointView<UpLo>();
467 out.makeCompressed();
A sparse direct supernodal LDLT factorization and solver based on the PaStiX library.
Definition PaStiXSupport.h:412
void compute(const MatrixType &matrix)
Definition PaStiXSupport.h:430
void analyzePattern(const MatrixType &matrix)
Definition PaStiXSupport.h:440
void factorize(const MatrixType &matrix)
Definition PaStiXSupport.h:448
A sparse direct supernodal Cholesky (LLT) factorization and solver based on the PaStiX library.
Definition PaStiXSupport.h:336
void compute(const MatrixType &matrix)
Definition PaStiXSupport.h:354
void factorize(const MatrixType &matrix)
Definition PaStiXSupport.h:372
void analyzePattern(const MatrixType &matrix)
Definition PaStiXSupport.h:364
Interface to the PaStix solver.
Definition PaStiXSupport.h:235
void analyzePattern(const MatrixType &matrix)
Definition PaStiXSupport.h:265
void factorize(const MatrixType &matrix)
Definition PaStiXSupport.h:277
void compute(const MatrixType &matrix)
Definition PaStiXSupport.h:254
A base class for sparse solvers.
Definition SparseSolverBase.h:68
SparseSolverBase()=default
ComputationInfo
Definition Constants.h:455
@ NumericalIssue
Definition Constants.h:459
@ InvalidInput
Definition Constants.h:464
@ Success
Definition Constants.h:457