3 #ifndef DUNE_ISTL_SUPERLU_HH
4 #define DUNE_ISTL_SUPERLU_HH
14 #define SUPERLU_NTYPE 1
16 #ifdef SUPERLU_POST_2005_VERSION
19 #include "slu_sdefs.h"
23 #include "slu_ddefs.h"
27 #include "slu_cdefs.h"
31 #include "slu_zdefs.h"
42 #warning Support for SuperLU older than SuperLU 3.0 from August 2005 is deprecated.
61 #include <dune/common/fmatrix.hh>
62 #include <dune/common/fvector.hh>
63 #include <dune/common/stdstreams.hh>
79 template<
class Matrix>
83 template<
class M,
class T,
class TM,
class TD,
class TA>
86 template<
class T,
bool tag>
109 static void create(SuperMatrix *
mat,
int n,
int m,
float *dat,
int n1,
110 Stype_t stype, Dtype_t dtype, Mtype_t mtype)
112 sCreate_Dense_Matrix(mat, n, m, dat, n1, stype, dtype, mtype);
116 static void destroy(SuperMatrix*)
121 struct SuperLUSolveChooser<float>
123 static void solve(superlu_options_t *options, SuperMatrix *
mat,
int *perm_c,
int *perm_r,
int *etree,
124 char *equed,
float *R,
float *C, SuperMatrix *L, SuperMatrix *U,
125 void *work,
int lwork, SuperMatrix *B, SuperMatrix *X,
126 float *rpg,
float *rcond,
float *ferr,
float *berr,
127 mem_usage_t *memusage, SuperLUStat_t *stat,
int *info)
129 sgssvx(options, mat, perm_c, perm_r, etree, equed, R, C,
130 L, U, work, lwork, B, X, rpg, rcond, ferr, berr,
131 memusage, stat, info);
136 struct QuerySpaceChooser<float>
138 static void querySpace(SuperMatrix* L, SuperMatrix* U, mem_usage_t* memusage)
140 sQuerySpace(L,U,memusage);
151 static void create(SuperMatrix *
mat,
int n,
int m,
double *dat,
int n1,
152 Stype_t stype, Dtype_t dtype, Mtype_t mtype)
154 dCreate_Dense_Matrix(mat, n, m, dat, n1, stype, dtype, mtype);
164 static void solve(superlu_options_t *options, SuperMatrix *
mat,
int *perm_c,
int *perm_r,
int *etree,
165 char *equed,
double *R,
double *C, SuperMatrix *L, SuperMatrix *U,
166 void *work,
int lwork, SuperMatrix *B, SuperMatrix *X,
167 double *rpg,
double *rcond,
double *ferr,
double *berr,
168 mem_usage_t *memusage, SuperLUStat_t *stat,
int *info)
170 dgssvx(options, mat, perm_c, perm_r, etree, equed, R, C,
171 L, U, work, lwork, B, X, rpg, rcond, ferr, berr,
172 memusage, stat, info);
179 static void querySpace(SuperMatrix* L, SuperMatrix* U, mem_usage_t* memusage)
181 dQuerySpace(L,U,memusage);
188 struct SuperLUDenseMatChooser<
std::complex<double> >
190 static void create(SuperMatrix *
mat,
int n,
int m, std::complex<double> *dat,
int n1,
191 Stype_t stype, Dtype_t dtype, Mtype_t mtype)
193 zCreate_Dense_Matrix(mat, n, m, reinterpret_cast<doublecomplex*>(dat), n1, stype, dtype, mtype);
197 static void destroy(SuperMatrix*)
202 struct SuperLUSolveChooser<
std::complex<double> >
204 static void solve(superlu_options_t *options, SuperMatrix *
mat,
int *perm_c,
int *perm_r,
int *etree,
205 char *equed,
double *R,
double *C, SuperMatrix *L, SuperMatrix *U,
206 void *work,
int lwork, SuperMatrix *B, SuperMatrix *X,
207 double *rpg,
double *rcond,
double *ferr,
double *berr,
208 mem_usage_t *memusage, SuperLUStat_t *stat,
int *info)
210 zgssvx(options, mat, perm_c, perm_r, etree, equed, R, C,
211 L, U, work, lwork, B, X, rpg, rcond, ferr, berr,
212 memusage, stat, info);
217 struct QuerySpaceChooser<
std::complex<double> >
219 static void querySpace(SuperMatrix* L, SuperMatrix* U, mem_usage_t* memusage)
221 zQuerySpace(L,U,memusage);
228 struct SuperLUDenseMatChooser<
std::complex<float> >
230 static void create(SuperMatrix *
mat,
int n,
int m, std::complex<float> *dat,
int n1,
231 Stype_t stype, Dtype_t dtype, Mtype_t mtype)
233 cCreate_Dense_Matrix(mat, n, m, reinterpret_cast< ::complex*>(dat), n1, stype, dtype, mtype);
237 static void destroy(SuperMatrix* )
242 struct SuperLUSolveChooser<
std::complex<float> >
244 static void solve(superlu_options_t *options, SuperMatrix *
mat,
int *perm_c,
int *perm_r,
int *etree,
245 char *equed,
float *R,
float *C, SuperMatrix *L, SuperMatrix *U,
246 void *work,
int lwork, SuperMatrix *B, SuperMatrix *X,
247 float *rpg,
float *rcond,
float *ferr,
float *berr,
248 mem_usage_t *memusage, SuperLUStat_t *stat,
int *info)
250 cgssvx(options, mat, perm_c, perm_r, etree, equed, R, C,
251 L, U, work, lwork, B, X, rpg, rcond, ferr, berr,
252 memusage, stat, info);
257 struct QuerySpaceChooser<
std::complex<float> >
259 static void querySpace(SuperMatrix* L, SuperMatrix* U, mem_usage_t* memusage)
261 cQuerySpace(L,U,memusage);
279 template<
typename T,
typename A,
int n,
int m>
282 BlockVector<FieldVector<T,m>,
283 typename A::template rebind<FieldVector<T,m> >::other>,
284 BlockVector<FieldVector<T,n>,
285 typename A::template rebind<FieldVector<T,n> >::other> >
298 typename A::template rebind<FieldVector<T,m> >::other>
domain_type;
302 typename A::template rebind<FieldVector<T,n> >::other>
range_type;
317 explicit SuperLU(
const Matrix&
mat,
bool verbose=
false,
318 bool reusevector=
true);
339 DUNE_UNUSED_PARAMETER(reduction);
346 void apply(T* x, T* b);
349 void setMatrix(
const Matrix&
mat);
351 typename SuperLUMatrix::size_type
nnz()
const
357 void setSubMatrix(
const Matrix&
mat,
const S& rowIndexSet);
359 void setVerbosity(
bool v);
367 const char*
name() {
return "SuperLU"; }
369 friend class std::mem_fun_ref_t<void,
SuperLU>;
370 template<
class M,
class X,
class TM,
class TD,
class T1>
374 SuperLUMatrix& getInternalMatrix() {
return mat; }
380 SuperMatrix L, U, B, X;
381 int *perm_c, *perm_r, *etree;
384 superlu_options_t options;
388 bool first, verbose, reusevector;
391 template<
typename T,
typename A,
int n,
int m>
392 SuperLU<BCRSMatrix<FieldMatrix<T,n,m>,
A> >
399 template<
typename T,
typename A,
int n,
int m>
400 void SuperLU<BCRSMatrix<FieldMatrix<T,n,m>,
A> >::free()
408 Destroy_SuperNode_Matrix(&L);
409 Destroy_CompCol_Matrix(&U);
412 if(!first && reusevector) {
413 SUPERLU_FREE(B.Store);
414 SUPERLU_FREE(X.Store);
419 template<
typename T,
typename A,
int n,
int m>
420 SuperLU<BCRSMatrix<FieldMatrix<T,n,m>,
A> >
422 : work(0), lwork(0), first(true), verbose(verbose_),
423 reusevector(reusevector_)
428 template<
typename T,
typename A,
int n,
int m>
430 : work(0), lwork(0),verbose(false),
433 template<
typename T,
typename A,
int n,
int m>
434 void SuperLU<BCRSMatrix<FieldMatrix<T,n,m>,
A> >::setVerbosity(
bool v)
439 template<
typename T,
typename A,
int n,
int m>
440 void SuperLU<BCRSMatrix<FieldMatrix<T,n,m>,
A> >::setMatrix(
const Matrix& mat_)
452 template<
typename T,
typename A,
int n,
int m>
454 void SuperLU<BCRSMatrix<FieldMatrix<T,n,m>,
A> >::setSubMatrix(
const Matrix& mat_,
463 mat.setMatrix(mat_,mrs);
467 template<
typename T,
typename A,
int n,
int m>
468 void SuperLU<BCRSMatrix<FieldMatrix<T,n,m>,
A> >::decompose()
472 perm_c =
new int[
mat.
M()];
473 perm_r =
new int[
mat.
N()];
474 etree =
new int[
mat.
M()];
478 set_default_options(&options);
485 fakeFormat.lda=
mat.
N();
495 mem_usage_t memusage;
500 &L, &U, work, lwork, &B, &X, &rpg, &rcond, &ferr,
501 &berr, &memusage, &stat, &info);
504 dinfo<<
"LU factorization: dgssvx() returns info "<< info<<std::endl;
506 if ( info == 0 || info == n+1 ) {
508 if ( options.PivotGrowth )
509 dinfo<<
"Recip. pivot growth = "<<rpg<<std::endl;
510 if ( options.ConditionNumber )
511 dinfo<<
"Recip. condition number = %e\n"<< rcond<<std::endl;
512 SCformat* Lstore = (SCformat *) L.Store;
513 NCformat* Ustore = (NCformat *) U.Store;
514 dinfo<<
"No of nonzeros in factor L = "<< Lstore->nnz<<std::endl;
515 dinfo<<
"No of nonzeros in factor U = "<< Ustore->nnz<<std::endl;
516 dinfo<<
"No of nonzeros in L+U = "<< Lstore->nnz + Ustore->nnz - n<<std::endl;
518 dinfo<<
"L\\U MB "<<memusage.for_lu/1e6<<
" \ttotal MB needed "<<memusage.total_needed/1e6
521 #ifdef HAVE_MEM_USAGE_T_EXPANSIONS
522 std::cout<<memusage.expansions<<std::endl;
524 std::cout<<stat.expansions<<std::endl;
526 }
else if ( info > 0 && lwork == -1 ) {
527 dinfo<<
"** Estimated memory: "<< info - n<<std::endl;
529 if ( options.PrintStat ) StatPrint(&stat);
557 options.Fact = FACTORED;
560 template<
typename T,
typename A,
int n,
int m>
561 void SuperLU<BCRSMatrix<FieldMatrix<T,n,m>,
A> >
565 DUNE_THROW(
ISTLError,
"Size of right-hand-side vector b does not match the number of matrix rows!");
567 DUNE_THROW(
ISTLError,
"Size of solution vector x does not match the number of matrix columns!");
569 DUNE_THROW(
ISTLError,
"Matrix of SuperLU is null!");
571 SuperMatrix* mB = &B;
572 SuperMatrix* mX = &X;
580 ((DNformat*)B.Store)->nzval=&b[0];
581 ((DNformat*)X.Store)->nzval=&x[0];
591 mem_usage_t memusage;
601 #ifdef SUPERLU_MIN_VERSION_4_3
602 options.IterRefine=SLU_DOUBLE;
604 options.IterRefine=DOUBLE;
608 &L, &U, work, lwork, mB, mX, &rpg, &rcond, &ferr, &berr,
609 &memusage, &stat, &info);
629 dinfo<<
"Triangular solve: dgssvx() returns info "<< info<<std::endl;
631 if ( info == 0 || info == n+1 ) {
633 if ( options.IterRefine ) {
634 std::cout<<
"Iterative Refinement: steps="
635 <<stat.RefineSteps<<
" FERR="<<ferr<<
" BERR="<<berr<<std::endl;
637 std::cout<<
" FERR="<<ferr<<
" BERR="<<berr<<std::endl;
638 }
else if ( info > 0 && lwork == -1 ) {
639 std::cout<<
"** Estimated memory: "<< info - n<<
" bytes"<<std::endl;
642 if ( options.PrintStat ) StatPrint(&stat);
646 SUPERLU_FREE(rB.Store);
647 SUPERLU_FREE(rX.Store);
651 template<
typename T,
typename A,
int n,
int m>
652 void SuperLU<BCRSMatrix<FieldMatrix<T,n,m>,
A> >
656 DUNE_THROW(
ISTLError,
"Matrix of SuperLU is null!");
658 SuperMatrix* mB = &B;
659 SuperMatrix* mX = &X;
667 ((DNformat*) B.Store)->nzval=b;
668 ((DNformat*)X.Store)->nzval=x;
679 mem_usage_t memusage;
684 #ifdef SUPERLU_MIN_VERSION_4_3
685 options.IterRefine=SLU_DOUBLE;
687 options.IterRefine=DOUBLE;
691 &L, &U, work, lwork, mB, mX, &rpg, &rcond, &ferr, &berr,
692 &memusage, &stat, &info);
695 dinfo<<
"Triangular solve: dgssvx() returns info "<< info<<std::endl;
697 if ( info == 0 || info == n+1 ) {
699 if ( options.IterRefine ) {
700 dinfo<<
"Iterative Refinement: steps="
701 <<stat.RefineSteps<<
" FERR="<<ferr<<
" BERR="<<berr<<std::endl;
703 dinfo<<
" FERR="<<ferr<<
" BERR="<<berr<<std::endl;
704 }
else if ( info > 0 && lwork == -1 ) {
705 dinfo<<
"** Estimated memory: "<< info - n<<
" bytes"<<std::endl;
707 if ( options.PrintStat ) StatPrint(&stat);
712 SUPERLU_FREE(rB.Store);
713 SUPERLU_FREE(rX.Store);
718 template<
typename T,
typename A,
int n,
int m>
724 template<
typename T,
typename A,
int n,
int m>
732 #undef FIRSTCOL_OF_SNODE
737 #undef SUPERLU_MALLOC
757 #undef DROP_SECONDARY
762 #endif // HAVE_SUPERLU
763 #endif // DUNE_SUPERLU_HH
Definition: supermatrix.hh:214
Dune::BlockVector< FieldVector< T, n >, typename A::template rebind< FieldVector< T, n > >::other > range_type
The type of the range of the solver.
Definition: superlu.hh:302
void apply(domain_type &x, range_type &b, double reduction, InverseOperatorResult &res)
apply inverse operator, with given convergence criteria.
Definition: superlu.hh:337
size_type dim() const
dimension of the vector space
Definition: bvector.hh:223
Definition: superlu.hh:102
SuperLUMatrix::size_type nnz() const
Definition: superlu.hh:351
Definition: superlu.hh:94
derive error class from the base class in common
Definition: istlexception.hh:16
size_type M() const
Return the number of columns.
Definition: matrix.hh:165
A vector of blocks with memory management.
Definition: bvector.hh:252
int iterations
Number of iterations.
Definition: solver.hh:50
Sequential overlapping Schwarz preconditioner.
Definition: colcompmatrix.hh:157
Matrix & A
Definition: matrixmatrix.hh:216
A sparse block matrix with compressed row storage.
Definition: bcrsmatrix.hh:412
Dune::BlockVector< FieldVector< T, m >, typename A::template rebind< FieldVector< T, m > >::other > domain_type
The type of the domain of the solver.
Definition: superlu.hh:298
static void create(SuperMatrix *mat, int n, int m, double *dat, int n1, Stype_t stype, Dtype_t dtype, Mtype_t mtype)
Definition: superlu.hh:151
const char * name()
Definition: superlu.hh:367
Templates characterizing the type of a solver.
Definition: solvertype.hh:27
Whether this is a direct solver.
Definition: solvertype.hh:22
static void destroy(SuperMatrix *)
Definition: superlu.hh:158
Matrix & mat
Definition: matrixmatrix.hh:343
Definition: overlappingschwarz.hh:690
Definition: supermatrix.hh:167
size_type N() const
Return the number of rows.
Definition: matrix.hh:160
SuperMatrixInitializer< BCRSMatrix< FieldMatrix< T, n, m >, A > > MatrixInitializer
Type of an associated initializer class.
Definition: superlu.hh:294
Definition: superlu.hh:90
Definition: matrixutils.hh:25
Abstract base class for all solvers.
Definition: solver.hh:79
whether the solver internally uses column compressed storage
Definition: solvertype.hh:34
Definition: solvertype.hh:13
Definition: superlu.hh:80
Dune::BCRSMatrix< FieldMatrix< T, n, m >, A > matrix_type
Definition: superlu.hh:290
Implementation of the BCRSMatrix class.
Implementations of the inverse operator interface.
This file implements a vector space as a tensor product of a given vector space. The number of compon...
Dune::BCRSMatrix< FieldMatrix< T, n, m >, A > Matrix
The matrix type.
Definition: superlu.hh:289
bool converged
True if convergence criterion has been met.
Definition: solver.hh:56
static void querySpace(SuperMatrix *L, SuperMatrix *U, mem_usage_t *memusage)
Definition: superlu.hh:179
Definition: basearray.hh:19
Statistics about the application of an inverse operator.
Definition: solver.hh:31
Dune::SuperLUMatrix< Matrix > SuperLUMatrix
The corresponding SuperLU Matrix type.
Definition: superlu.hh:292
static void solve(superlu_options_t *options, SuperMatrix *mat, int *perm_c, int *perm_r, int *etree, char *equed, double *R, double *C, SuperMatrix *L, SuperMatrix *U, void *work, int lwork, SuperMatrix *B, SuperMatrix *X, double *rpg, double *rcond, double *ferr, double *berr, mem_usage_t *memusage, SuperLUStat_t *stat, int *info)
Definition: superlu.hh:164
Definition: superlu.hh:98