3#ifndef DUNE_ISTL_UMFPACK_HH
4#define DUNE_ISTL_UMFPACK_HH
8#if ENABLE_UMFPACK && not defined(ENABLE_SUITESPARSE)
9#define ENABLE_SUITESPARSE ENABLE_UMFPACK
12#if HAVE_UMFPACK || defined DOXYGEN
27#include"colcompmatrix.hh"
43 template<
class M,
class T,
class TM,
class TD,
class TA>
44 class SeqOverlappingSchwarz;
46 template<
class T,
bool tag>
47 struct SeqOverlappingSchwarzAssemblerHelper;
54 template<
class Matrix>
61 struct UMFPackMethodChooser
65 struct UMFPackMethodChooser<double>
67 template<
typename... A>
68 static void defaults(A... args)
70 umfpack_di_defaults(args...);
72 template<
typename... A>
73 static void free_numeric(A... args)
75 umfpack_di_free_numeric(args...);
77 template<
typename... A>
78 static void free_symbolic(A... args)
80 umfpack_di_free_symbolic(args...);
82 template<
typename... A>
83 static int load_numeric(A... args)
85 return umfpack_di_load_numeric(args...);
87 template<
typename... A>
88 static void numeric(A... args)
90 umfpack_di_numeric(args...);
92 template<
typename... A>
93 static void report_info(A... args)
95 umfpack_di_report_info(args...);
97 template<
typename... A>
98 static void report_status(A... args)
100 umfpack_di_report_status(args...);
102 template<
typename... A>
103 static int save_numeric(A... args)
105 return umfpack_di_save_numeric(args...);
107 template<
typename... A>
108 static void solve(A... args)
110 umfpack_di_solve(args...);
112 template<
typename... A>
113 static void symbolic(A... args)
115 umfpack_di_symbolic(args...);
120 struct UMFPackMethodChooser<
std::complex<double> >
122 template<
typename... A>
123 static void defaults(A... args)
125 umfpack_zi_defaults(args...);
127 template<
typename... A>
128 static void free_numeric(A... args)
130 umfpack_zi_free_numeric(args...);
132 template<
typename... A>
133 static void free_symbolic(A... args)
135 umfpack_zi_free_symbolic(args...);
137 template<
typename... A>
138 static int load_numeric(A... args)
140 return umfpack_zi_load_numeric(args...);
142 template<
typename... A>
143 static void numeric(
const int* cs,
const int* ri,
const double* val, A... args)
145 umfpack_zi_numeric(cs,ri,val,NULL,args...);
147 template<
typename... A>
148 static void report_info(A... args)
150 umfpack_zi_report_info(args...);
152 template<
typename... A>
153 static void report_status(A... args)
155 umfpack_zi_report_status(args...);
157 template<
typename... A>
158 static int save_numeric(A... args)
160 return umfpack_zi_save_numeric(args...);
162 template<
typename... A>
163 static void solve(
int m,
const int* cs,
const int* ri, std::complex<double>* val,
double* x,
const double* b,A... args)
165 const double* cval =
reinterpret_cast<const double*
>(val);
166 umfpack_zi_solve(m,cs,ri,cval,NULL,x,NULL,b,NULL,args...);
168 template<
typename... A>
169 static void symbolic(
int m,
int n,
const int* cs,
const int* ri,
const double* val, A... args)
171 umfpack_zi_symbolic(m,n,cs,ri,val,NULL,args...);
188 template<
typename T,
typename A,
int n,
int m>
191 BlockVector<FieldVector<T,m>,
192 typename A::template rebind<FieldVector<T,m> >::other>,
193 BlockVector<FieldVector<T,n>,
194 typename A::template rebind<FieldVector<T,n> >::other> >
207 typename A::template rebind<FieldVector<T,m> >::other>
domain_type;
211 typename A::template rebind<FieldVector<T,n> >::other>
range_type;
224 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
225 "Unsupported Type in UMFPack (only double and std::complex<double> supported)");
226 Caller::defaults(UMF_Control);
227 setVerbosity(verbose);
242 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
243 "Unsupported Type in UMFPack (only double and std::complex<double> supported)");
244 Caller::defaults(UMF_Control);
245 setVerbosity(verbose);
251 UMFPack() : matrixIsLoaded_(false), verbosity_(0)
254 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
255 "Unsupported Type in UMFPack (only double and std::complex<double> supported)");
256 Caller::defaults(UMF_Control);
272 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
273 "Unsupported Type in UMFPack (only double and std::complex<double> supported)");
274 Caller::defaults(UMF_Control);
275 setVerbosity(verbose);
276 int errcode = Caller::load_numeric(&UMF_Numeric,
const_cast<char*
>(file));
277 if ((errcode == UMFPACK_ERROR_out_of_memory) || (errcode == UMFPACK_ERROR_file_IO))
279 matrixIsLoaded_ =
false;
281 saveDecomposition(file);
285 matrixIsLoaded_ =
true;
286 std::cout <<
"UMFPack decomposition successfully loaded from " << file << std::endl;
299 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
300 "Unsupported Type in UMFPack (only double and std::complex<double> supported)");
301 Caller::defaults(UMF_Control);
302 int errcode = Caller::load_numeric(&UMF_Numeric,
const_cast<char*
>(file));
303 if (errcode == UMFPACK_ERROR_out_of_memory)
305 if (errcode == UMFPACK_ERROR_file_IO)
307 matrixIsLoaded_ =
true;
308 std::cout <<
"UMFPack decomposition successfully loaded from " << file << std::endl;
309 setVerbosity(verbose);
314 if ((umfpackMatrix_.N() + umfpackMatrix_.M() > 0) || (matrixIsLoaded_))
323 if (umfpackMatrix_.N() != b.
dim())
325 if (umfpackMatrix_.M() != x.
dim())
328 double UMF_Apply_Info[UMFPACK_INFO];
329 Caller::solve(UMFPACK_A,
330 umfpackMatrix_.getColStart(),
331 umfpackMatrix_.getRowIndex(),
332 umfpackMatrix_.getValues(),
333 reinterpret_cast<double*
>(&x[0]),
334 reinterpret_cast<double*
>(&b[0]),
342 res.
elapsed = UMF_Apply_Info[UMFPACK_SOLVE_WALLTIME];
344 printOnApply(UMF_Apply_Info);
363 double UMF_Apply_Info[UMFPACK_INFO];
364 Caller::solve(UMFPACK_A,
365 umfpackMatrix_.getColStart(),
366 umfpackMatrix_.getRowIndex(),
367 umfpackMatrix_.getValues(),
373 printOnApply(UMF_Apply_Info);
389 if (option >= UMFPACK_CONTROL)
392 UMF_Control[option] = value;
400 int errcode = Caller::save_numeric(UMF_Numeric,
const_cast<char*
>(file));
401 if (errcode != UMFPACK_OK)
408 if ((umfpackMatrix_.N() + umfpackMatrix_.M() > 0) || (matrixIsLoaded_))
410 umfpackMatrix_ = matrix;
415 void setSubMatrix(
const Matrix& _mat,
const S& rowIndexSet)
417 if ((umfpackMatrix_.N() + umfpackMatrix_.M() > 0) || (matrixIsLoaded_))
419 umfpackMatrix_.setMatrix(_mat,rowIndexSet);
435 UMF_Control[UMFPACK_PRL] = 1;
437 UMF_Control[UMFPACK_PRL] = 2;
439 UMF_Control[UMFPACK_PRL] = 4;
448 if (!matrixIsLoaded_)
450 Caller::free_symbolic(&UMF_Symbolic);
451 umfpackMatrix_.free();
453 Caller::free_numeric(&UMF_Numeric);
454 matrixIsLoaded_ =
false;
457 const char* name() {
return "UMFPACK"; }
460 typedef typename Dune::UMFPackMethodChooser<T> Caller;
462 template<
class M,
class X,
class TM,
class TD,
class T1>
463 friend class SeqOverlappingSchwarz;
464 friend struct SeqOverlappingSchwarzAssemblerHelper<UMFPack<Matrix>,true>;
469 double UMF_Decomposition_Info[UMFPACK_INFO];
470 Caller::symbolic(
static_cast<int>(umfpackMatrix_.N()),
471 static_cast<int>(umfpackMatrix_.N()),
472 umfpackMatrix_.getColStart(),
473 umfpackMatrix_.getRowIndex(),
474 reinterpret_cast<double*
>(umfpackMatrix_.getValues()),
477 UMF_Decomposition_Info);
478 Caller::numeric(umfpackMatrix_.getColStart(),
479 umfpackMatrix_.getRowIndex(),
480 reinterpret_cast<double*
>(umfpackMatrix_.getValues()),
484 UMF_Decomposition_Info);
485 Caller::report_status(UMF_Control,UMF_Decomposition_Info[UMFPACK_STATUS]);
488 std::cout <<
"[UMFPack Decomposition]" << std::endl;
489 std::cout <<
"Wallclock Time taken: " << UMF_Decomposition_Info[UMFPACK_NUMERIC_WALLTIME] <<
" (CPU Time: " << UMF_Decomposition_Info[UMFPACK_NUMERIC_TIME] <<
")" << std::endl;
490 std::cout <<
"Flops taken: " << UMF_Decomposition_Info[UMFPACK_FLOPS] << std::endl;
491 std::cout <<
"Peak Memory Usage: " << UMF_Decomposition_Info[UMFPACK_PEAK_MEMORY]*UMF_Decomposition_Info[UMFPACK_SIZE_OF_UNIT] <<
" bytes" << std::endl;
492 std::cout <<
"Condition number estimate: " << 1./UMF_Decomposition_Info[UMFPACK_RCOND] << std::endl;
493 std::cout <<
"Numbers of non-zeroes in decomposition: L: " << UMF_Decomposition_Info[UMFPACK_LNZ] <<
" U: " << UMF_Decomposition_Info[UMFPACK_UNZ] << std::endl;
497 Caller::report_info(UMF_Control,UMF_Decomposition_Info);
501 void printOnApply(
double* UMF_Info)
503 Caller::report_status(UMF_Control,UMF_Info[UMFPACK_STATUS]);
506 std::cout <<
"[UMFPack Solve]" << std::endl;
507 std::cout <<
"Wallclock Time: " << UMF_Info[UMFPACK_SOLVE_WALLTIME] <<
" (CPU Time: " << UMF_Info[UMFPACK_SOLVE_TIME] <<
")" << std::endl;
508 std::cout <<
"Flops Taken: " << UMF_Info[UMFPACK_SOLVE_FLOPS] << std::endl;
509 std::cout <<
"Iterative Refinement steps taken: " << UMF_Info[UMFPACK_IR_TAKEN] << std::endl;
510 std::cout <<
"Error Estimate: " << UMF_Info[UMFPACK_OMEGA1] <<
" resp. " << UMF_Info[UMFPACK_OMEGA2] << std::endl;
514 UMFPackMatrix& getInternalMatrix() {
return umfpackMatrix_; }
516 UMFPackMatrix umfpackMatrix_;
517 bool matrixIsLoaded_;
521 double UMF_Control[UMFPACK_CONTROL];
524 template<
typename T,
typename A,
int n,
int m>
525 struct IsDirectSolver<UMFPack<BCRSMatrix<FieldMatrix<T,n,m>,A> > >
530 template<
typename T,
typename A,
int n,
int m>
531 struct StoresColumnCompressed<UMFPack<BCRSMatrix<FieldMatrix<T,n,m>,A> > >
533 enum { value =
true };
Implementation of the BCRSMatrix class.
A sparse block matrix with compressed row storage.
Definition: bcrsmatrix.hh:413
A vector of blocks with memory management.
Definition: bvector.hh:253
Base class for Dune-Exceptions.
Definition: exceptions.hh:91
A dense n x m matrix.
Definition: fmatrix.hh:67
vector space out of a tensor product of fields.
Definition: fvector.hh:94
derive error class from the base class in common
Definition: istlexception.hh:16
Abstract base class for all solvers.
Definition: solver.hh:79
A generic dynamic dense matrix.
Definition: matrix.hh:25
Default exception class for range errors.
Definition: exceptions.hh:279
Use the UMFPack package to directly solve linear systems – empty default class.
Definition: umfpack.hh:56
size_type dim() const
dimension of the vector space
Definition: bvector.hh:223
A few common exception classes.
Implements a matrix constructed from a given type representing a field and compile-time given number ...
Implements a vector constructed from a given type representing a field and a compile-time given size.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:243
UMFPack(const Matrix &matrix, int verbose, bool)
Constructor for compatibility with SuperLU standard constructor.
Definition: umfpack.hh:239
void setVerbosity(int v)
sets the verbosity level for the UMFPack solver
Definition: umfpack.hh:430
UMFPack(const Matrix &mat_, const char *file, int verbose=0)
Try loading a decomposition from file and do a decomposition if unsuccessful.
Definition: umfpack.hh:269
Dune::BlockVector< FieldVector< T, m >, typename A::template rebind< FieldVector< T, m > >::other > domain_type
The type of the domain of the solver.
Definition: umfpack.hh:207
void setMatrix(const Matrix &matrix)
Initialize data from given matrix.
Definition: umfpack.hh:406
virtual void apply(domain_type &x, range_type &b, InverseOperatorResult &res)
Apply inverse operator,.
Definition: umfpack.hh:321
void free()
free allocated space.
Definition: umfpack.hh:446
ColCompMatrixInitializer< BCRSMatrix< FieldMatrix< T, n, m >, A > > MatrixInitializer
Type of an associated initializer class.
Definition: umfpack.hh:203
UMFPack(const Matrix &matrix, int verbose=0)
Construct a solver object from a BCRSMatrix.
Definition: umfpack.hh:221
void setOption(unsigned int option, double value)
Set UMFPack-specific options.
Definition: umfpack.hh:387
Dune::ColCompMatrix< Matrix > UMFPackMatrix
The corresponding SuperLU Matrix type.
Definition: umfpack.hh:201
Dune::BlockVector< FieldVector< T, n >, typename A::template rebind< FieldVector< T, n > >::other > range_type
The type of the range of the solver.
Definition: umfpack.hh:211
void apply(T *x, T *b)
additional apply method with c-arrays in analogy to superlu
Definition: umfpack.hh:361
UMFPack()
default constructor
Definition: umfpack.hh:251
UMFPack(const char *file, int verbose=0)
try loading a decomposition from file
Definition: umfpack.hh:296
virtual void apply(domain_type &x, range_type &b, double reduction, InverseOperatorResult &res)
apply inverse operator, with given convergence criteria.
Definition: umfpack.hh:350
void saveDecomposition(const char *file)
saves a decomposition to a file
Definition: umfpack.hh:398
Dune namespace.
Definition: alignment.hh:10
Implementations of the inverse operator interface.
Templates characterizing the type of a solver.
Inititializer for the ColCompMatrix as needed by OverlappingSchwarz.
Definition: colcompmatrix.hh:154
Statistics about the application of an inverse operator.
Definition: solver.hh:32
double elapsed
Elapsed time in seconds.
Definition: solver.hh:62
int iterations
Number of iterations.
Definition: solver.hh:50
bool converged
True if convergence criterion has been met.
Definition: solver.hh:56
Definition of the DUNE_UNUSED macro for the case that config.h is not available.
#define DUNE_UNUSED_PARAMETER(parm)
A macro to mark intentional unused function parameters with.
Definition: unused.hh:18