Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
PaStiXSupport.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2012 Désiré Nuentsa-Wakam <desire.nuentsa_wakam@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// SPDX-License-Identifier: MPL-2.0
10// SPDX-FileCopyrightText: The Eigen Authors
11
12#ifndef EIGEN_PASTIXSUPPORT_H
13#define EIGEN_PASTIXSUPPORT_H
14
15// IWYU pragma: private
16#include "./InternalHeaderCheck.h"
17
18namespace Eigen {
19
28template <typename MatrixType_, bool IsStrSym = false>
29class PastixLU;
30template <typename MatrixType_, int Options>
31class PastixLLT;
32template <typename MatrixType_, int Options>
33class PastixLDLT;
34
35namespace internal {
36
37template <class Pastix>
38struct pastix_traits;
39
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;
46};
47
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;
54};
55
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;
62};
63
64} // namespace internal
65
66// PaStiX 6 task interface:
67// https://solverstack.gitlabpages.inria.fr/pastix/group__pastix__users.html
68template <typename Derived>
69class PastixBase : public SparseSolverBase<Derived> {
70 protected:
71 using Base = SparseSolverBase<Derived>;
72 using Base::m_isInitialized;
73
74 public:
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;
84
85 PastixBase() {
86 EIGEN_STATIC_ASSERT(
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;
92 }
93
94 ~PastixBase() { clean(); }
95
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(); }
111 ComputationInfo info() const { return m_info; }
112
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());
117 // PaStiX overwrites a contiguous, column-major RHS. Refine needs the original RHS as well.
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());
122 int error = 0;
123 if (n != 0 && nrhs != 0) {
124#if PASTIX_VERSION_MINOR >= 3
125 error = pastix_task_solve(m_pastixdata, n, nrhs, solution.data(), n);
126#else
127 error = pastix_task_solve(m_pastixdata, nrhs, solution.data(), n);
128#endif
129 // PaStiX refinement accepts only one right-hand side, even when solve accepts several.
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);
133 }
134 }
135 m_info = error == PASTIX_SUCCESS && solution.allFinite() ? Success : NumericalIssue;
136 x = solution;
137 return m_info == Success;
138 }
139
140 protected:
141 void clean() {
142 if (m_pastixdata) pastixFinalize(&m_pastixdata);
143 m_isInitialized = false;
144 m_analysisIsOk = false;
145 }
146
147 void compute(ColSpMatrix &matrix) {
148 analyzePattern(matrix);
149 if (m_analysisIsOk) factorize(m_matrix);
150 }
151
152 void analyzePattern(ColSpMatrix &matrix) {
153 eigen_assert(matrix.rows() == matrix.cols() && "The input matrix should be square");
154 clean();
155 m_matrix.swap(matrix);
156 if (rows() == 0) {
157 m_analysisIsOk = true;
158 m_info = Success;
159 return;
160 }
161 setMatrix();
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;
165 m_info = m_analysisIsOk ? Success : NumericalIssue;
166 }
167
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);
172 if (rows() == 0) {
173 m_isInitialized = true;
174 m_info = Success;
175 return;
176 }
177 setMatrix();
178 const int error = pastix_task_numfact(m_pastixdata, &m_spm);
179 m_isInitialized = error == PASTIX_SUCCESS;
180 m_info = m_isInitialized ? Success : NumericalIssue;
181 }
182
183 Array<pastix_int_t, IPARM_SIZE, 1> m_iparm;
184 Array<double, DPARM_SIZE, 1> m_dparm;
185
186 private:
187 void setMatrix() {
188 // Eigen owns the CSC arrays: do not pass this descriptor to spmExit().
189 m_iparm(IPARM_FREE_CSCUSER) = 0;
190 spmInit(&m_spm);
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;
196 m_spm.baseval = 0;
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());
199 m_spm.dof = 1;
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);
204 }
205
206 ColSpMatrix m_matrix;
207 spmatrix_t m_spm;
208 pastix_data_t *m_pastixdata = nullptr;
209 bool m_analysisIsOk = false;
210 mutable ComputationInfo m_info = InvalidInput;
211};
212
234template <typename MatrixType_, bool IsStrSym>
235class PastixLU : public PastixBase<PastixLU<MatrixType_> > {
236 public:
237 using MatrixType = MatrixType_;
238 using Base = PastixBase<PastixLU<MatrixType>>;
239 using ColSpMatrix = typename Base::ColSpMatrix;
240 using StorageIndex = typename MatrixType::StorageIndex;
241
242 public:
243 PastixLU() : Base() { init(); }
244
245 explicit PastixLU(const MatrixType &matrix) : Base() {
246 init();
247 compute(matrix);
248 }
254 void compute(const MatrixType &matrix) {
255 m_structureIsUptodate = false;
256 ColSpMatrix temp;
257 grabMatrix(matrix, temp);
258 Base::compute(temp);
259 }
260
265 void analyzePattern(const MatrixType &matrix) {
266 m_structureIsUptodate = false;
267 ColSpMatrix temp;
268 grabMatrix(matrix, temp);
269 Base::analyzePattern(temp);
270 }
271
277 void factorize(const MatrixType &matrix) {
278 ColSpMatrix temp;
279 grabMatrix(matrix, temp);
280 Base::factorize(temp);
281 }
282
283 protected:
284 void init() {
285 m_structureIsUptodate = false;
286 m_iparm(IPARM_MTX_TYPE) = PastixGeneral;
287 m_iparm(IPARM_FACTORIZATION) = PastixFactGETRF;
288 }
289
290 void grabMatrix(const MatrixType &matrix, ColSpMatrix &out) {
291 EIGEN_IF_CONSTEXPR (IsStrSym)
292 out = matrix;
293 else {
294 if (!m_structureIsUptodate) {
295 // update the transposed structure
296 m_transposedStructure = matrix.transpose();
297
298 // Set the elements of the matrix to zero
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;
301
302 m_structureIsUptodate = true;
303 }
304
305 const ColSpMatrix &converted = matrix;
306 out = m_transposedStructure + converted;
307 }
308 out.makeCompressed();
309 }
310
311 using Base::m_dparm;
312 using Base::m_iparm;
313
314 ColSpMatrix m_transposedStructure;
315 bool m_structureIsUptodate;
316};
317
335template <typename MatrixType_, int UpLo_>
336class PastixLLT : public PastixBase<PastixLLT<MatrixType_, UpLo_> > {
337 public:
338 using MatrixType = MatrixType_;
339 using Base = PastixBase<PastixLLT<MatrixType, UpLo_>>;
340 using ColSpMatrix = typename Base::ColSpMatrix;
341
342 public:
343 enum { UpLo = UpLo_ };
344 PastixLLT() : Base() { init(); }
345
346 explicit PastixLLT(const MatrixType &matrix) : Base() {
347 init();
348 compute(matrix);
349 }
350
354 void compute(const MatrixType &matrix) {
355 ColSpMatrix temp;
356 grabMatrix(matrix, temp);
357 Base::compute(temp);
358 }
359
364 void analyzePattern(const MatrixType &matrix) {
365 ColSpMatrix temp;
366 grabMatrix(matrix, temp);
367 Base::analyzePattern(temp);
368 }
369
372 void factorize(const MatrixType &matrix) {
373 ColSpMatrix temp;
374 grabMatrix(matrix, temp);
375 Base::factorize(temp);
376 }
377
378 protected:
379 using Base::m_iparm;
380
381 void init() {
382 m_iparm(IPARM_MTX_TYPE) = NumTraits<typename Base::Scalar>::IsComplex ? PastixHermitian : PastixSymmetric;
383 m_iparm(IPARM_FACTORIZATION) = PastixFactPOTRF;
384 }
385
386 void grabMatrix(const MatrixType &matrix, ColSpMatrix &out) {
387 out.resize(matrix.rows(), matrix.cols());
388 // Pastix supports only lower, column-major matrices
389 const ColSpMatrix &converted = matrix;
390 out.template selfadjointView<Lower>() = converted.template selfadjointView<UpLo>();
391 out.makeCompressed();
392 }
393};
394
411template <typename MatrixType_, int UpLo_>
412class PastixLDLT : public PastixBase<PastixLDLT<MatrixType_, UpLo_> > {
413 public:
414 using MatrixType = MatrixType_;
415 using Base = PastixBase<PastixLDLT<MatrixType, UpLo_>>;
416 using ColSpMatrix = typename Base::ColSpMatrix;
417
418 public:
419 enum { UpLo = UpLo_ };
420 PastixLDLT() : Base() { init(); }
421
422 explicit PastixLDLT(const MatrixType &matrix) : Base() {
423 init();
424 compute(matrix);
425 }
426
430 void compute(const MatrixType &matrix) {
431 ColSpMatrix temp;
432 grabMatrix(matrix, temp);
433 Base::compute(temp);
434 }
435
440 void analyzePattern(const MatrixType &matrix) {
441 ColSpMatrix temp;
442 grabMatrix(matrix, temp);
443 Base::analyzePattern(temp);
444 }
445
448 void factorize(const MatrixType &matrix) {
449 ColSpMatrix temp;
450 grabMatrix(matrix, temp);
451 Base::factorize(temp);
452 }
453
454 protected:
455 using Base::m_iparm;
456
457 void init() {
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;
460 }
461
462 void grabMatrix(const MatrixType &matrix, ColSpMatrix &out) {
463 // Pastix supports only lower, column-major matrices
464 out.resize(matrix.rows(), matrix.cols());
465 const ColSpMatrix &converted = matrix;
466 out.template selfadjointView<Lower>() = converted.template selfadjointView<UpLo>();
467 out.makeCompressed();
468 }
469};
470
471} // end namespace Eigen
472
473#endif
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
ComputationInfo
Definition Constants.h:455
@ NumericalIssue
Definition Constants.h:459
@ InvalidInput
Definition Constants.h:464
@ Success
Definition Constants.h:457