10#ifndef EIGEN_SUPERLUSUPPORT_H
11#define EIGEN_SUPERLUSUPPORT_H
15#define DECL_GSSVX(PREFIX,FLOATTYPE,KEYTYPE) \
17 extern void PREFIX##gssvx(superlu_options_t *, SuperMatrix *, int *, int *, int *, \
18 char *, FLOATTYPE *, FLOATTYPE *, SuperMatrix *, SuperMatrix *, \
19 void *, int, SuperMatrix *, SuperMatrix *, \
20 FLOATTYPE *, FLOATTYPE *, FLOATTYPE *, FLOATTYPE *, \
21 mem_usage_t *, SuperLUStat_t *, int *); \
23 inline float SuperLU_gssvx(superlu_options_t *options, SuperMatrix *A, \
24 int *perm_c, int *perm_r, int *etree, char *equed, \
25 FLOATTYPE *R, FLOATTYPE *C, SuperMatrix *L, \
26 SuperMatrix *U, void *work, int lwork, \
27 SuperMatrix *B, SuperMatrix *X, \
28 FLOATTYPE *recip_pivot_growth, \
29 FLOATTYPE *rcond, FLOATTYPE *ferr, FLOATTYPE *berr, \
30 SuperLUStat_t *stats, int *info, KEYTYPE) { \
31 mem_usage_t mem_usage; \
32 PREFIX##gssvx(options, A, perm_c, perm_r, etree, equed, R, C, L, \
33 U, work, lwork, B, X, recip_pivot_growth, rcond, \
34 ferr, berr, &mem_usage, stats, info); \
35 return mem_usage.for_lu; \
38DECL_GSSVX(s,
float,
float)
39DECL_GSSVX(c,
float,std::complex<float>)
40DECL_GSSVX(d,
double,
double)
41DECL_GSSVX(z,
double,std::complex<double>)
44#define EIGEN_SUPERLU_HAS_ILU
47#ifdef EIGEN_SUPERLU_HAS_ILU
50#define DECL_GSISX(PREFIX,FLOATTYPE,KEYTYPE) \
52 extern void PREFIX##gsisx(superlu_options_t *, SuperMatrix *, int *, int *, int *, \
53 char *, FLOATTYPE *, FLOATTYPE *, SuperMatrix *, SuperMatrix *, \
54 void *, int, SuperMatrix *, SuperMatrix *, FLOATTYPE *, FLOATTYPE *, \
55 mem_usage_t *, SuperLUStat_t *, int *); \
57 inline float SuperLU_gsisx(superlu_options_t *options, SuperMatrix *A, \
58 int *perm_c, int *perm_r, int *etree, char *equed, \
59 FLOATTYPE *R, FLOATTYPE *C, SuperMatrix *L, \
60 SuperMatrix *U, void *work, int lwork, \
61 SuperMatrix *B, SuperMatrix *X, \
62 FLOATTYPE *recip_pivot_growth, \
64 SuperLUStat_t *stats, int *info, KEYTYPE) { \
65 mem_usage_t mem_usage; \
66 PREFIX##gsisx(options, A, perm_c, perm_r, etree, equed, R, C, L, \
67 U, work, lwork, B, X, recip_pivot_growth, rcond, \
68 &mem_usage, stats, info); \
69 return mem_usage.for_lu; \
72DECL_GSISX(s,
float,
float)
73DECL_GSISX(c,
float,std::complex<float>)
74DECL_GSISX(d,
double,
double)
75DECL_GSISX(z,
double,std::complex<double>)
79template<
typename MatrixType>
100 storage = other.storage;
105 SuperMatrix::operator=(
static_cast<const SuperMatrix&
>(other));
107 storage = other.storage;
113 union {
int nnz;
int lda;};
126 eigen_assert(
false &&
"storage type not supported");
131 template<
typename Scalar>
144 eigen_assert(
false &&
"Scalar type not supported by SuperLU");
148 template<
typename MatrixType>
151 MatrixType&
mat(
_mat.derived());
152 eigen_assert( ((MatrixType::Flags&
RowMajorBit)!=
RowMajorBit) &&
"row-major dense matrices are not supported by SuperLU");
154 res.setStorageType(
SLU_DN);
155 res.setScalarType<
typename MatrixType::Scalar>();
158 res.nrow = internal::convert_index<int>(
mat.rows());
159 res.ncol = internal::convert_index<int>(
mat.cols());
161 res.storage.lda = internal::convert_index<int>(MatrixType::IsVectorAtCompileTime ?
mat.size() :
mat.outerStride());
162 res.storage.values = (
void*)(
mat.data());
166 template<
typename MatrixType>
173 res.setStorageType(
SLU_NR);
174 res.nrow = internal::convert_index<int>(
mat.cols());
175 res.ncol = internal::convert_index<int>(
mat.rows());
179 res.setStorageType(
SLU_NC);
180 res.nrow = internal::convert_index<int>(
mat.rows());
181 res.ncol = internal::convert_index<int>(
mat.cols());
186 res.storage.nnz = internal::convert_index<int>(
mat.nonZeros());
187 res.storage.values =
mat.valuePtr();
188 res.storage.innerInd =
mat.innerIndexPtr();
189 res.storage.outerInd =
mat.outerIndexPtr();
191 res.setScalarType<
typename MatrixType::Scalar>();
194 if (MatrixType::Flags &
Upper)
196 if (MatrixType::Flags &
Lower)
199 eigen_assert(((MatrixType::Flags &
SelfAdjoint)==0) &&
"SelfAdjoint matrix shape not supported by SuperLU");
205template<
typename Scalar,
int Rows,
int Cols,
int Options,
int MRows,
int MCols>
211 eigen_assert( ((Options&
RowMajor)!=
RowMajor) &&
"row-major dense matrices is not supported by SuperLU");
212 res.setStorageType(
SLU_DN);
213 res.setScalarType<Scalar>();
216 res.nrow =
mat.rows();
217 res.ncol =
mat.cols();
219 res.storage.lda =
mat.outerStride();
220 res.storage.values =
mat.data();
224template<
typename Derived>
227 typedef Derived MatrixType;
232 res.setStorageType(
SLU_NR);
233 res.nrow =
mat.cols();
234 res.ncol =
mat.rows();
238 res.setStorageType(
SLU_NC);
239 res.nrow =
mat.rows();
240 res.ncol =
mat.cols();
245 res.storage.nnz =
mat.nonZeros();
246 res.storage.values =
mat.valuePtr();
247 res.storage.innerInd =
mat.innerIndexPtr();
248 res.storage.outerInd =
mat.outerIndexPtr();
250 res.setScalarType<
typename MatrixType::Scalar>();
253 if (MatrixType::Flags &
Upper)
255 if (MatrixType::Flags &
Lower)
258 eigen_assert(((MatrixType::Flags &
SelfAdjoint)==0) &&
"SelfAdjoint matrix shape not supported by SuperLU");
264template<
typename MatrixType>
267 return SluMatrix::Map(
mat);
271template<
typename Scalar,
int Flags,
typename Index>
272MappedSparseMatrix<Scalar,Flags,Index> map_superlu(SluMatrix& sluMat)
279 return MappedSparseMatrix<Scalar,Flags,Index>(
280 sluMat.nrow, sluMat.ncol, sluMat.storage.outerInd[outerSize],
281 sluMat.storage.outerInd, sluMat.storage.innerInd,
reinterpret_cast<Scalar*
>(sluMat.storage.values) );
290template<
typename _MatrixType,
typename Derived>
296 using Base::m_isInitialized;
298 typedef _MatrixType MatrixType;
299 typedef typename MatrixType::Scalar Scalar;
300 typedef typename MatrixType::RealScalar RealScalar;
301 typedef typename MatrixType::StorageIndex StorageIndex;
308 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
309 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
321 inline Index rows()
const {
return m_matrix.
rows(); }
322 inline Index cols()
const {
return m_matrix.
cols(); }
334 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
341 derived().analyzePattern(matrix);
342 derived().factorize(matrix);
353 m_isInitialized =
true;
355 m_analysisIsOk =
true;
356 m_factorizationIsOk =
false;
359 template<
typename Stream>
360 void dumpMemory(Stream& )
365 void initFactorization(
const MatrixType& a)
367 set_default_options(&this->m_sluOptions);
369 const Index size = a.rows();
372 m_sluA = internal::asSluMatrix(m_matrix);
379 m_sluEtree.resize(size);
382 m_sluB.setStorageType(SLU_DN);
383 m_sluB.setScalarType<Scalar>();
384 m_sluB.Mtype = SLU_GE;
385 m_sluB.storage.values = 0;
388 m_sluB.storage.lda = internal::convert_index<int>(size);
391 m_extractedDataAreDirty =
true;
397 m_isInitialized =
false;
402 void extractData()
const;
407 Destroy_SuperNode_Matrix(&m_sluL);
409 Destroy_CompCol_Matrix(&m_sluU);
414 memset(&m_sluL,0,
sizeof m_sluL);
415 memset(&m_sluU,0,
sizeof m_sluU);
419 mutable LUMatrixType m_l;
420 mutable LUMatrixType m_u;
421 mutable IntColVectorType m_p;
422 mutable IntRowVectorType m_q;
424 mutable LUMatrixType m_matrix;
425 mutable SluMatrix m_sluA;
426 mutable SuperMatrix m_sluL, m_sluU;
427 mutable SluMatrix m_sluB, m_sluX;
428 mutable SuperLUStat_t m_sluStat;
429 mutable superlu_options_t m_sluOptions;
430 mutable std::vector<int> m_sluEtree;
433 mutable char m_sluEqued;
436 int m_factorizationIsOk;
438 mutable bool m_extractedDataAreDirty;
441 SuperLUBase(SuperLUBase& ) { }
461template<
typename _MatrixType>
466 typedef _MatrixType MatrixType;
467 typedef typename Base::Scalar Scalar;
468 typedef typename Base::RealScalar RealScalar;
469 typedef typename Base::StorageIndex StorageIndex;
478 using Base::_solve_impl;
482 explicit SuperLU(
const MatrixType& matrix) :
Base()
501 m_isInitialized =
false;
511 void factorize(
const MatrixType& matrix);
514 template<
typename Rhs,
typename Dest>
517 inline const LMatrixType& matrixL()
const
519 if (m_extractedDataAreDirty) this->extractData();
523 inline const UMatrixType& matrixU()
const
525 if (m_extractedDataAreDirty) this->extractData();
529 inline const IntColVectorType& permutationP()
const
531 if (m_extractedDataAreDirty) this->extractData();
535 inline const IntRowVectorType& permutationQ()
const
537 if (m_extractedDataAreDirty) this->extractData();
541 Scalar determinant()
const;
545 using Base::m_matrix;
546 using Base::m_sluOptions;
552 using Base::m_sluEtree;
553 using Base::m_sluEqued;
554 using Base::m_sluRscale;
555 using Base::m_sluCscale;
558 using Base::m_sluStat;
559 using Base::m_sluFerr;
560 using Base::m_sluBerr;
564 using Base::m_analysisIsOk;
565 using Base::m_factorizationIsOk;
566 using Base::m_extractedDataAreDirty;
567 using Base::m_isInitialized;
574 set_default_options(&this->m_sluOptions);
575 m_sluOptions.PrintStat = NO;
576 m_sluOptions.ConditionNumber = NO;
577 m_sluOptions.Trans = NOTRANS;
578 m_sluOptions.ColPerm = COLAMD;
583 SuperLU(SuperLU& ) { }
586template<
typename MatrixType>
589 eigen_assert(m_analysisIsOk &&
"You must first call analyzePattern()");
596 this->initFactorization(a);
598 m_sluOptions.ColPerm =
COLAMD;
604 SuperLU_gssvx(&m_sluOptions, &m_sluA, m_q.data(), m_p.data(), &m_sluEtree[0],
605 &m_sluEqued, &m_sluRscale[0], &m_sluCscale[0],
611 &m_sluStat, &info, Scalar());
614 m_extractedDataAreDirty =
true;
618 m_factorizationIsOk =
true;
621template<
typename MatrixType>
622template<
typename Rhs,
typename Dest>
625 eigen_assert(m_factorizationIsOk &&
"The decomposition is not in a valid state for solving, you must first call either compute() or analyzePattern()/factorize()");
627 const Index size = m_matrix.rows();
628 const Index
rhsCols = b.cols();
629 eigen_assert(size==b.rows());
642 m_sluB = SluMatrix::Map(
b_ref.const_cast_derived());
643 m_sluX = SluMatrix::Map(
x_ref.const_cast_derived());
645 typename Rhs::PlainObject
b_cpy;
649 m_sluB = SluMatrix::Map(
b_cpy.const_cast_derived());
652 StatInit(&m_sluStat);
654 RealScalar recip_pivot_growth, rcond;
655 SuperLU_gssvx(&m_sluOptions, &m_sluA,
656 m_q.data(), m_p.data(),
657 &m_sluEtree[0], &m_sluEqued,
658 &m_sluRscale[0], &m_sluCscale[0],
662 &recip_pivot_growth, &rcond,
663 &m_sluFerr[0], &m_sluBerr[0],
664 &m_sluStat, &info, Scalar());
665 StatFree(&m_sluStat);
667 if(x.derived().data() != x_ref.data())
680template<
typename MatrixType,
typename Derived>
681void SuperLUBase<MatrixType,Derived>::extractData()
const
683 eigen_assert(m_factorizationIsOk &&
"The decomposition is not in a valid state for extracting factors, you must first call either compute() or analyzePattern()/factorize()");
684 if (m_extractedDataAreDirty)
687 int fsupc, istart, nsupr;
688 int lastl = 0, lastu = 0;
689 SCformat *Lstore =
static_cast<SCformat*
>(m_sluL.Store);
690 NCformat *Ustore =
static_cast<NCformat*
>(m_sluU.Store);
693 const Index size = m_matrix.rows();
694 m_l.resize(size,size);
695 m_l.resizeNonZeros(Lstore->nnz);
696 m_u.resize(size,size);
697 m_u.resizeNonZeros(Ustore->nnz);
699 int* Lcol = m_l.outerIndexPtr();
700 int* Lrow = m_l.innerIndexPtr();
701 Scalar* Lval = m_l.valuePtr();
703 int* Ucol = m_u.outerIndexPtr();
704 int* Urow = m_u.innerIndexPtr();
705 Scalar* Uval = m_u.valuePtr();
711 for (
int k = 0; k <= Lstore->nsuper; ++k)
713 fsupc = L_FST_SUPC(k);
714 istart = L_SUB_START(fsupc);
715 nsupr = L_SUB_START(fsupc+1) - istart;
719 for (
int j = fsupc; j < L_FST_SUPC(k+1); ++j)
721 SNptr = &((Scalar*)Lstore->nzval)[L_NZ_START(j)];
724 for (
int i = U_NZ_START(j); i < U_NZ_START(j+1); ++i)
726 Uval[lastu] = ((Scalar*)Ustore->nzval)[i];
728 if (Uval[lastu] != 0.0)
729 Urow[lastu++] = U_SUB(i);
731 for (
int i = 0; i < upper; ++i)
734 Uval[lastu] = SNptr[i];
736 if (Uval[lastu] != 0.0)
737 Urow[lastu++] = L_SUB(istart+i);
743 Lrow[lastl++] = L_SUB(istart + upper - 1);
744 for (
int i = upper; i < nsupr; ++i)
746 Lval[lastl] = SNptr[i];
748 if (Lval[lastl] != 0.0)
749 Lrow[lastl++] = L_SUB(istart+i);
759 m_l.resizeNonZeros(lastl);
760 m_u.resizeNonZeros(lastu);
762 m_extractedDataAreDirty =
false;
766template<
typename MatrixType>
767typename SuperLU<MatrixType>::Scalar SuperLU<MatrixType>::determinant()
const
769 eigen_assert(m_factorizationIsOk &&
"The decomposition is not in a valid state for computing the determinant, you must first call either compute() or analyzePattern()/factorize()");
771 if (m_extractedDataAreDirty)
774 Scalar det = Scalar(1);
775 for (
int j=0; j<m_u.cols(); ++j)
777 if (m_u.outerIndexPtr()[j+1]-m_u.outerIndexPtr()[j] > 0)
779 int lastId = m_u.outerIndexPtr()[j+1]-1;
780 eigen_assert(m_u.innerIndexPtr()[lastId]<=j);
781 if (m_u.innerIndexPtr()[lastId]==j)
782 det *= m_u.valuePtr()[lastId];
785 if(PermutationMap(m_p.data(),m_p.size()).determinant()*PermutationMap(m_q.data(),m_q.size()).determinant()<0)
788 return det/m_sluRscale.prod()/m_sluCscale.prod();
793#ifdef EIGEN_PARSED_BY_DOXYGEN
794#define EIGEN_SUPERLU_HAS_ILU
797#ifdef EIGEN_SUPERLU_HAS_ILU
815template<
typename _MatrixType>
816class SuperILU :
public SuperLUBase<_MatrixType,SuperILU<_MatrixType> >
819 typedef SuperLUBase<_MatrixType,SuperILU> Base;
820 typedef _MatrixType MatrixType;
821 typedef typename Base::Scalar Scalar;
822 typedef typename Base::RealScalar RealScalar;
825 using Base::_solve_impl;
827 SuperILU() : Base() {
init(); }
829 SuperILU(
const MatrixType& matrix) : Base()
832 Base::compute(matrix);
845 void analyzePattern(
const MatrixType& matrix)
847 Base::analyzePattern(matrix);
856 void factorize(
const MatrixType& matrix);
858 #ifndef EIGEN_PARSED_BY_DOXYGEN
860 template<
typename Rhs,
typename Dest>
861 void _solve_impl(
const MatrixBase<Rhs> &b, MatrixBase<Dest> &dest)
const;
866 using Base::m_matrix;
867 using Base::m_sluOptions;
873 using Base::m_sluEtree;
874 using Base::m_sluEqued;
875 using Base::m_sluRscale;
876 using Base::m_sluCscale;
879 using Base::m_sluStat;
880 using Base::m_sluFerr;
881 using Base::m_sluBerr;
885 using Base::m_analysisIsOk;
886 using Base::m_factorizationIsOk;
887 using Base::m_extractedDataAreDirty;
888 using Base::m_isInitialized;
895 ilu_set_default_options(&m_sluOptions);
896 m_sluOptions.PrintStat = NO;
897 m_sluOptions.ConditionNumber = NO;
898 m_sluOptions.Trans = NOTRANS;
899 m_sluOptions.ColPerm = MMD_AT_PLUS_A;
902 m_sluOptions.ILU_MILU = SILU;
906 m_sluOptions.ILU_DropRule = DROP_BASIC;
907 m_sluOptions.ILU_DropTol = NumTraits<Scalar>::dummy_precision()*10;
911 SuperILU(SuperILU& ) { }
914template<
typename MatrixType>
915void SuperILU<MatrixType>::factorize(
const MatrixType& a)
917 eigen_assert(m_analysisIsOk &&
"You must first call analyzePattern()");
924 this->initFactorization(a);
927 RealScalar recip_pivot_growth, rcond;
929 StatInit(&m_sluStat);
930 SuperLU_gsisx(&m_sluOptions, &m_sluA, m_q.data(), m_p.data(), &m_sluEtree[0],
931 &m_sluEqued, &m_sluRscale[0], &m_sluCscale[0],
935 &recip_pivot_growth, &rcond,
936 &m_sluStat, &info, Scalar());
937 StatFree(&m_sluStat);
941 m_factorizationIsOk =
true;
944template<
typename MatrixType>
945template<
typename Rhs,
typename Dest>
946void SuperILU<MatrixType>::_solve_impl(
const MatrixBase<Rhs> &b, MatrixBase<Dest>& x)
const
948 eigen_assert(m_factorizationIsOk &&
"The decomposition is not in a valid state for solving, you must first call either compute() or analyzePattern()/factorize()");
950 const int size = m_matrix.rows();
951 const int rhsCols = b.cols();
952 eigen_assert(size==b.rows());
954 m_sluOptions.Trans = NOTRANS;
955 m_sluOptions.Fact = FACTORED;
956 m_sluOptions.IterRefine = NOREFINE;
958 m_sluFerr.resize(rhsCols);
959 m_sluBerr.resize(rhsCols);
961 Ref<const Matrix<typename Rhs::Scalar,Dynamic,Dynamic,ColMajor> > b_ref(b);
962 Ref<const Matrix<typename Dest::Scalar,Dynamic,Dynamic,ColMajor> > x_ref(x);
964 m_sluB = SluMatrix::Map(b_ref.const_cast_derived());
965 m_sluX = SluMatrix::Map(x_ref.const_cast_derived());
967 typename Rhs::PlainObject b_cpy;
971 m_sluB = SluMatrix::Map(b_cpy.const_cast_derived());
975 RealScalar recip_pivot_growth, rcond;
977 StatInit(&m_sluStat);
978 SuperLU_gsisx(&m_sluOptions, &m_sluA,
979 m_q.data(), m_p.data(),
980 &m_sluEtree[0], &m_sluEqued,
981 &m_sluRscale[0], &m_sluCscale[0],
985 &recip_pivot_growth, &rcond,
986 &m_sluStat, &info, Scalar());
987 StatFree(&m_sluStat);
989 if(&x.coeffRef(0) != x_ref.data())
A matrix or vector expression mapping an existing array of data.
Definition Map.h:91
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:180
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize(Index rows, Index cols)
Resizes *this to a rows x cols matrix.
Definition PlainObjectBase.h:252
Pseudo expression representing a solving operation.
Definition Solve.h:63
Base class of any sparse matrices or sparse expressions.
Definition SparseMatrixBase.h:34
Index rows() const
Definition SparseMatrix.h:131
Index cols() const
Definition SparseMatrix.h:133
A base class for sparse solvers.
Definition SparseSolverBase.h:54
The base class for the direct and incomplete LU factorization of SuperLU.
Definition SuperLUSupport.h:292
void compute(const MatrixType &matrix)
Computes the sparse Cholesky decomposition of matrix.
Definition SuperLUSupport.h:339
void analyzePattern(const MatrixType &)
Performs a symbolic decomposition on the sparcity of matrix.
Definition SuperLUSupport.h:351
superlu_options_t & options()
Definition SuperLUSupport.h:325
ComputationInfo info() const
Reports whether previous computation was successful.
Definition SuperLUSupport.h:332
A sparse direct LU factorization and solver based on the SuperLU library.
Definition SuperLUSupport.h:463
void factorize(const MatrixType &matrix)
Performs a numeric decomposition of matrix.
Definition SuperLUSupport.h:587
void analyzePattern(const MatrixType &matrix)
Performs a symbolic decomposition on the sparcity of matrix.
Definition SuperLUSupport.h:498
ComputationInfo
Enum for reporting the status of a computation.
Definition Constants.h:430
@ SelfAdjoint
Used in BandMatrix and SelfAdjointView to indicate that the matrix is self-adjoint.
Definition Constants.h:220
@ Lower
View matrix as a lower triangular matrix.
Definition Constants.h:204
@ Upper
View matrix as an upper triangular matrix.
Definition Constants.h:206
@ NumericalIssue
The provided data did not satisfy the prerequisites.
Definition Constants.h:434
@ InvalidInput
The inputs are invalid, or the algorithm has been improperly called.
Definition Constants.h:439
@ Success
Computation was successful.
Definition Constants.h:432
@ ColMajor
Storage order is column major (see TopicStorageOrders).
Definition Constants.h:320
@ RowMajor
Storage order is row major (see TopicStorageOrders).
Definition Constants.h:322
const unsigned int RowMajorBit
for a matrix, this means that the storage order is row-major.
Definition Constants.h:61
None init(**Any args)
Definition collective.py:17
Definition SuperLUSupport.h:80
Definition SuperLUSupport.h:90
Definition inference.c:32