5#ifndef DUNE_ISTL_SUPERLU_HH
6#define DUNE_ISTL_SUPERLU_HH
37 template<
class M,
class T,
class TM,
class TD,
class TA>
38 class SeqOverlappingSchwarz;
40 template<
class T,
bool tag>
41 struct SeqOverlappingSchwarzAssemblerHelper;
59#if __has_include("slu_sdefs.h")
63 static void create(SuperMatrix *
mat,
int n,
int m,
float *dat,
int n1,
64 Stype_t stype, Dtype_t dtype, Mtype_t mtype)
66 sCreate_Dense_Matrix(
mat, n, m, dat, n1, stype, dtype, mtype);
70 static void destroy(SuperMatrix*)
75 struct SuperLUSolveChooser<float>
77 static void solve(superlu_options_t *options, SuperMatrix *
mat,
int *perm_c,
int *perm_r,
int *etree,
78 char *equed,
float *R,
float *C, SuperMatrix *L, SuperMatrix *U,
79 void *work,
int lwork, SuperMatrix *B, SuperMatrix *X,
80 float *rpg,
float *rcond,
float *ferr,
float *berr,
81 mem_usage_t *memusage, SuperLUStat_t *stat,
int *info)
84 sgssvx(options,
mat, perm_c, perm_r, etree, equed, R, C,
85 L, U, work, lwork, B, X, rpg, rcond, ferr, berr,
86 &gLU, memusage, stat, info);
91 struct QuerySpaceChooser<float>
93 static void querySpace(SuperMatrix* L, SuperMatrix* U, mem_usage_t* memusage)
95 sQuerySpace(L,U,memusage);
101#if __has_include("slu_ddefs.h")
104 struct SuperLUDenseMatChooser<double>
106 static void create(SuperMatrix *
mat,
int n,
int m,
double *dat,
int n1,
107 Stype_t stype, Dtype_t dtype, Mtype_t mtype)
109 dCreate_Dense_Matrix(
mat, n, m, dat, n1, stype, dtype, mtype);
113 static void destroy(SuperMatrix * )
117 struct SuperLUSolveChooser<double>
119 static void solve(superlu_options_t *options, SuperMatrix *
mat,
int *perm_c,
int *perm_r,
int *etree,
120 char *equed,
double *R,
double *C, SuperMatrix *L, SuperMatrix *U,
121 void *work,
int lwork, SuperMatrix *B, SuperMatrix *X,
122 double *rpg,
double *rcond,
double *ferr,
double *berr,
123 mem_usage_t *memusage, SuperLUStat_t *stat,
int *info)
126 dgssvx(options,
mat, perm_c, perm_r, etree, equed, R, C,
127 L, U, work, lwork, B, X, rpg, rcond, ferr, berr,
128 &gLU, memusage, stat, info);
133 struct QuerySpaceChooser<double>
135 static void querySpace(SuperMatrix* L, SuperMatrix* U, mem_usage_t* memusage)
137 dQuerySpace(L,U,memusage);
142#if __has_include("slu_zdefs.h")
144 struct SuperLUDenseMatChooser<
std::complex<double> >
147 Stype_t stype, Dtype_t dtype, Mtype_t mtype)
149 zCreate_Dense_Matrix(
mat, n, m,
reinterpret_cast<doublecomplex*
>(dat), n1, stype, dtype, mtype);
153 static void destroy(SuperMatrix*)
158 struct SuperLUSolveChooser<
std::complex<double> >
160 static void solve(superlu_options_t *options, SuperMatrix *
mat,
int *perm_c,
int *perm_r,
int *etree,
161 char *equed,
double *R,
double *C, SuperMatrix *L, SuperMatrix *U,
162 void *work,
int lwork, SuperMatrix *B, SuperMatrix *X,
163 double *rpg,
double *rcond,
double *ferr,
double *berr,
164 mem_usage_t *memusage, SuperLUStat_t *stat,
int *info)
167 zgssvx(options,
mat, perm_c, perm_r, etree, equed, R, C,
168 L, U, work, lwork, B, X, rpg, rcond, ferr, berr,
169 &gLU, memusage, stat, info);
174 struct QuerySpaceChooser<
std::complex<double> >
176 static void querySpace(SuperMatrix* L, SuperMatrix* U, mem_usage_t* memusage)
178 zQuerySpace(L,U,memusage);
183#if __has_include("slu_cdefs.h")
185 struct SuperLUDenseMatChooser<
std::complex<float> >
188 Stype_t stype, Dtype_t dtype, Mtype_t mtype)
190 cCreate_Dense_Matrix(
mat, n, m,
reinterpret_cast< singlecomplex*
>(dat), n1, stype, dtype, mtype);
194 static void destroy(SuperMatrix* )
199 struct SuperLUSolveChooser<
std::complex<float> >
201 static void solve(superlu_options_t *options, SuperMatrix *
mat,
int *perm_c,
int *perm_r,
int *etree,
202 char *equed,
float *R,
float *C, SuperMatrix *L, SuperMatrix *U,
203 void *work,
int lwork, SuperMatrix *B, SuperMatrix *X,
204 float *rpg,
float *rcond,
float *ferr,
float *berr,
205 mem_usage_t *memusage, SuperLUStat_t *stat,
int *info)
208 cgssvx(options,
mat, perm_c, perm_r, etree, equed, R, C,
209 L, U, work, lwork, B, X, rpg, rcond, ferr, berr,
210 &gLU, memusage, stat, info);
215 struct QuerySpaceChooser<
std::complex<float> >
217 static void querySpace(SuperMatrix* L, SuperMatrix* U, mem_usage_t* memusage)
219 cQuerySpace(L,U,memusage);
227 struct SuperLUVectorChooser
230 template<
typename T,
typename A,
int n,
int m>
231 struct SuperLUVectorChooser<BCRSMatrix<FieldMatrix<T,n,m>,A > >
234 using domain_type = BlockVector<
238 using range_type = BlockVector<
243 template<
typename T,
typename A>
244 struct SuperLUVectorChooser<BCRSMatrix<T,A> >
247 using domain_type = BlockVector<T, A>;
249 using range_type = BlockVector<T, A>;
267 typename Dom =
typename Impl::SuperLUVectorChooser<M>::domain_type,
268 typename Ran =
typename Impl::SuperLUVectorChooser<M>::range_type>
272 using T =
typename M::field_type;
307 bool reusevector=
true);
321 :
SuperLU(matrix, config.
get<bool>(
"verbose", false), config.
get<bool>(
"reuseVector", true))
350 void apply(T* x, T* b);
355 typename SuperLUMatrix::size_type
nnz()
const
357 return mat.nonzeroes();
371 const char*
name() {
return "SuperLU"; }
373 template<
class Mat,
class X,
class TM,
class TD,
class T1>
383 SuperMatrix L, U, B, X;
384 int *perm_c, *perm_r, *etree;
387 superlu_options_t options;
391 bool first, verbose, reusevector;
394 template<
typename M,
typename Dom,
typename Ran>
402 template<
typename M,
typename Dom,
typename Ran>
411 Destroy_SuperNode_Matrix(&L);
412 Destroy_CompCol_Matrix(&U);
415 if(!first && reusevector) {
416 SUPERLU_FREE(B.Store);
417 SUPERLU_FREE(X.Store);
422 template<
typename M,
typename Dom,
typename Ran>
425 : work(0), lwork(0), first(true), verbose(verbose_),
426 reusevector(reusevector_)
431 template<
typename M,
typename Dom,
typename Ran>
433 : work(0), lwork(0),verbose(false),
436 template<
typename M,
typename Dom,
typename Ran>
442 template<
typename M,
typename Dom,
typename Ran>
455 template<
typename M,
typename Dom,
typename Ran>
466 mat.setMatrix(mat_,mrs);
470 template<
typename M,
typename Dom,
typename Ran>
475 perm_c =
new int[
mat.
M()];
476 perm_r =
new int[
mat.
N()];
477 etree =
new int[
mat.
M()];
481 set_default_options(&options);
488 fakeFormat.lda=
mat.
N();
498 mem_usage_t memusage;
503 &L, &U, work, lwork, &B, &X, &rpg, &rcond, &ferr,
504 &berr, &memusage, &stat, &info);
507 dinfo<<
"LU factorization: dgssvx() returns info "<< info<<
std::endl;
509 auto nSuperLUCol =
static_cast<SuperMatrix&
>(
mat).ncol;
511 if ( info == 0 || info == nSuperLUCol+1 ) {
517 SCformat* Lstore = (SCformat *) L.Store;
518 NCformat* Ustore = (NCformat *) U.Store;
521 dinfo<<
"No of nonzeros in L+U = "<< Lstore->nnz + Ustore->nnz - nSuperLUCol<<
std::endl;
523 dinfo<<
"L\\U MB "<<memusage.for_lu/1e6<<
" \ttotal MB needed "<<memusage.total_needed/1e6
527 }
else if ( info > 0 && lwork == -1 ) {
530 if (
options.PrintStat ) StatPrint(&stat);
561 template<
typename M,
typename Dom,
typename Ran>
562 void SuperLU<M,Dom,Ran>
565 if (
mat.
N() != b.dim())
566 DUNE_THROW(
ISTLError,
"Size of right-hand-side vector b does not match the number of matrix rows!");
567 if (
mat.
M() != x.dim())
568 DUNE_THROW(
ISTLError,
"Size of solution vector x does not match the number of matrix columns!");
580 SuperMatrix* mB = &B;
581 SuperMatrix* mX = &X;
589 ((DNformat*)B.Store)->nzval=b.data();
590 ((DNformat*)X.Store)->nzval=x.data();
600 mem_usage_t memusage;
613 &L, &U, work, lwork, mB, mX, &rpg, &rcond, &ferr, &berr,
614 &memusage, &stat, &info);
634 dinfo<<
"Triangular solve: dgssvx() returns info "<< info<<
std::endl;
636 auto nSuperLUCol =
static_cast<SuperMatrix&
>(
mat).ncol;
638 if ( info == 0 || info == nSuperLUCol+1 ) {
641 std::cout<<
"Iterative Refinement: steps="
642 <<stat.RefineSteps<<
" FERR="<<ferr<<
" BERR="<<berr<<
std::endl;
645 }
else if ( info > 0 && lwork == -1 ) {
649 if (
options.PrintStat ) StatPrint(&stat);
653 SUPERLU_FREE(rB.Store);
654 SUPERLU_FREE(rX.Store);
658 template<
typename M,
typename Dom,
typename Ran>
665 SuperMatrix* mB = &B;
666 SuperMatrix* mX = &X;
674 ((DNformat*) B.Store)->nzval=b;
675 ((DNformat*)X.Store)->nzval=x;
686 mem_usage_t memusage;
694 &L, &U, work, lwork, mB, mX, &rpg, &rcond, &ferr, &berr,
695 &memusage, &stat, &info);
698 dinfo<<
"Triangular solve: dgssvx() returns info "<< info<<
std::endl;
700 auto nSuperLUCol =
static_cast<SuperMatrix&
>(
mat).ncol;
702 if ( info == 0 || info == nSuperLUCol+1 ) {
705 dinfo<<
"Iterative Refinement: steps="
706 <<stat.RefineSteps<<
" FERR="<<ferr<<
" BERR="<<berr<<
std::endl;
709 }
else if ( info > 0 && lwork == -1 ) {
710 dinfo<<
"** Estimated memory: "<< info - nSuperLUCol<<
" bytes"<<
std::endl;
712 if (
options.PrintStat ) StatPrint(&stat);
717 SUPERLU_FREE(rB.Store);
718 SUPERLU_FREE(rX.Store);
723 template<
typename T,
typename A,
typename Dom,
typename Ran>
729 template<
typename T,
typename A,
typename Dom,
typename Ran>
740 using OpTraits =
decltype(opTraits);
741 using M =
typename OpTraits::matrix_type;
742 using X =
typename OpTraits::domain_type;
743 using Y =
typename OpTraits::range_type;
745 if constexpr (OpTraits::isParallel){
746 if(opTraits.getCommOrThrow(op).communicator().size() > 1)
749 if constexpr (OpTraits::isAssembled &&
751 std::is_same_v<typename FieldTraits<M>::real_type,
double>){
755 if constexpr (std::is_convertible_v<SuperLU<M,X,Y>*,
757 typename OpTraits::range_type>*>
759 const auto& A = opTraits.getAssembledOpOrThrow(op);
760 const M&
mat = A->getmat();
761 int verbose = config.
get(
"verbose", 0);
762 return std::make_shared<Dune::SuperLU<M,X,Y>>(
mat,verbose);
766 "Unsupported Type in SuperLU (only double and std::complex<double> supported)");
771#undef FIRSTCOL_OF_SNODE
#define DUNE_REGISTER_SOLVER(name,...)
Definition solverregistry.hh:17
This file implements a vector space as a tensor product of a given vector space. The number of compon...
Dune::ParameterTree options
Templates characterizing the type of a solver.
Implementations of the inverse operator interface.
void apply(domain_type &x, range_type &b, InverseOperatorResult &res) override
Apply inverse operator,.
Definition superlu.hh:563
~SuperLU()
Definition superlu.hh:396
void setVerbosity(bool v)
Definition superlu.hh:437
void setMatrix(const Matrix &mat)
Initialize data from given matrix.
Definition superlu.hh:443
void setSubMatrix(const Matrix &mat, const S &rowIndexSet)
Definition superlu.hh:457
void free()
free allocated space.
Definition superlu.hh:403
SuperLU()
Empty default constructor.
Definition superlu.hh:432
Matrix & mat
Definition matrixmatrix.hh:347
static constexpr size_type M()
void solve(V &x, const V &b) const
#define DUNE_THROW(E,...)
constexpr auto get(std::integer_sequence< T, II... >, std::integral_constant< std::size_t, pos >={})
A sparse block matrix with compressed row storage.
Definition bcrsmatrix.hh:468
A generic dynamic dense matrix.
Definition matrix.hh:561
size_type M() const
Return the number of columns.
Definition matrix.hh:696
size_type N() const
Return the number of rows.
Definition matrix.hh:691
std::string get(const std::string &key, const std::string &defaultValue) const
derive error class from the base class in common
Definition istlexception.hh:19
Sequential overlapping Schwarz preconditioner.
Definition overlappingschwarz.hh:755
Definition overlappingschwarz.hh:694
Statistics about the application of an inverse operator.
Definition solver.hh:50
int iterations
Number of iterations.
Definition solver.hh:69
bool converged
True if convergence criterion has been met.
Definition solver.hh:75
Abstract base class for all solvers.
Definition solver.hh:101
Category
Definition solvercategory.hh:23
@ sequential
Category for sequential solvers.
Definition solvercategory.hh:25
Definition solverregistry.hh:37
Definition solvertype.hh:16
@ value
Whether this is a direct solver.
Definition solvertype.hh:24
Definition solvertype.hh:30
@ value
whether the solver internally uses column compressed storage
Definition solvertype.hh:36
SuperLu Solver.
Definition superlu.hh:271
M matrix_type
Definition superlu.hh:276
SuperMatrixInitializer< Matrix > MatrixInitializer
Type of an associated initializer class.
Definition superlu.hh:280
M Matrix
The matrix type.
Definition superlu.hh:275
void apply(domain_type &x, range_type &b, double reduction, InverseOperatorResult &res) override
apply inverse operator, with given convergence criteria.
Definition superlu.hh:342
Ran range_type
The type of the range of the solver.
Definition superlu.hh:284
SuperLUMatrix::size_type nnz() const
Definition superlu.hh:355
const char * name()
Definition superlu.hh:371
SolverCategory::Category category() const override
Category of the solver (see SolverCategory::Category)
Definition superlu.hh:287
Dom domain_type
The type of the domain of the solver.
Definition superlu.hh:282
Dune::SuperLUMatrix< Matrix > SuperLUMatrix
The corresponding SuperLU Matrix type.
Definition superlu.hh:278
SuperLU(const Matrix &matrix, const ParameterTree &config)
Constructs the SuperLU solver.
Definition superlu.hh:320
Definition supermatrix.hh:132
Utility class for converting an ISTL Matrix into a SuperLU Matrix.
Definition supermatrix.hh:175
Definition supermatrix.hh:179