5#ifndef DUNE_ISTL_UMFPACK_HH
6#define DUNE_ISTL_UMFPACK_HH
8#if HAVE_SUITESPARSE_UMFPACK || defined DOXYGEN
15#include<dune/common/exceptions.hh>
16#include<dune/common/fmatrix.hh>
17#include<dune/common/fvector.hh>
39 template<
class M,
class T,
class TM,
class TD,
class TA>
40 class SeqOverlappingSchwarz;
42 template<
class T,
bool tag>
43 struct SeqOverlappingSchwarzAssemblerHelper;
50 static constexpr bool valid = false ;
56 static constexpr bool valid = true ;
58 template<
typename... A>
61 umfpack_dl_defaults(args...);
63 template<
typename... A>
66 umfpack_dl_free_numeric(args...);
68 template<
typename... A>
71 umfpack_dl_free_symbolic(args...);
73 template<
typename... A>
76 return umfpack_dl_load_numeric(args...);
78 template<
typename... A>
81 umfpack_dl_numeric(args...);
83 template<
typename... A>
86 umfpack_dl_report_info(args...);
88 template<
typename... A>
91 umfpack_dl_report_status(args...);
93 template<
typename... A>
96 return umfpack_dl_save_numeric(args...);
98 template<
typename... A>
101 umfpack_dl_solve(args...);
103 template<
typename... A>
106 umfpack_dl_symbolic(args...);
113 static constexpr bool valid = true ;
115 template<
typename... A>
118 umfpack_zl_defaults(args...);
120 template<
typename... A>
123 umfpack_zl_free_numeric(args...);
125 template<
typename... A>
128 umfpack_zl_free_symbolic(args...);
130 template<
typename... A>
133 return umfpack_zl_load_numeric(args...);
135 template<
typename... A>
136 static void numeric(
const long int* cs,
const long int* ri,
const double* val, A... args)
138 umfpack_zl_numeric(cs,ri,val,NULL,args...);
140 template<
typename... A>
143 umfpack_zl_report_info(args...);
145 template<
typename... A>
148 umfpack_zl_report_status(args...);
150 template<
typename... A>
153 return umfpack_zl_save_numeric(args...);
155 template<
typename... A>
156 static void solve(
long int m,
const long int* cs,
const long int* ri, std::complex<double>* val,
double* x,
const double* b,A... args)
158 const double* cval =
reinterpret_cast<const double*
>(val);
159 umfpack_zl_solve(m,cs,ri,cval,NULL,x,NULL,b,NULL,args...);
161 template<
typename... A>
162 static void symbolic(
long int m,
long int n,
const long int* cs,
const long int* ri,
const double* val, A... args)
164 umfpack_zl_symbolic(m,n,cs,ri,val,NULL,args...);
171 struct UMFPackVectorChooser
174 template<
typename T,
typename A,
int n,
int m>
175 struct UMFPackVectorChooser<BCRSMatrix<FieldMatrix<T,n,m>,A > >
178 using domain_type = BlockVector<
180 typename std::allocator_traits<A>::template rebind_alloc<FieldVector<T,m> > >;
182 using range_type = BlockVector<
184 typename std::allocator_traits<A>::template rebind_alloc<FieldVector<T,n> > >;
187 template<
typename T,
typename A>
188 struct UMFPackVectorChooser<BCRSMatrix<T,A> >
191 using domain_type = BlockVector<T, A>;
193 using range_type = BlockVector<T, A>;
213 typename Impl::UMFPackVectorChooser<M>::domain_type,
214 typename Impl::UMFPackVectorChooser<M>::range_type >
216 using T =
typename M::field_type;
223 typedef ISTL::Impl::BCCSMatrix<typename Matrix::field_type, long int>
UMFPackMatrix;
227 using domain_type =
typename Impl::UMFPackVectorChooser<M>::domain_type;
229 using range_type =
typename Impl::UMFPackVectorChooser<M>::range_type;
248 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
249 "Unsupported Type in UMFPack (only double and std::complex<double> supported)");
250 Caller::defaults(UMF_Control);
266 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
267 "Unsupported Type in UMFPack (only double and std::complex<double> supported)");
268 Caller::defaults(UMF_Control);
283 :
UMFPack(mat_, config.
get<int>(
"verbose", 0))
288 UMFPack() : matrixIsLoaded_(false), verbosity_(0)
291 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
292 "Unsupported Type in UMFPack (only double and std::complex<double> supported)");
293 Caller::defaults(UMF_Control);
309 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
310 "Unsupported Type in UMFPack (only double and std::complex<double> supported)");
311 Caller::defaults(UMF_Control);
313 int errcode = Caller::load_numeric(&UMF_Numeric,
const_cast<char*
>(file));
314 if ((errcode == UMFPACK_ERROR_out_of_memory) || (errcode == UMFPACK_ERROR_file_IO))
316 matrixIsLoaded_ =
false;
322 matrixIsLoaded_ =
true;
323 std::cout <<
"UMFPack decomposition successfully loaded from " << file << std::endl;
336 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
337 "Unsupported Type in UMFPack (only double and std::complex<double> supported)");
338 Caller::defaults(UMF_Control);
339 int errcode = Caller::load_numeric(&UMF_Numeric,
const_cast<char*
>(file));
340 if (errcode == UMFPACK_ERROR_out_of_memory)
341 DUNE_THROW(Dune::Exception,
"ran out of memory while loading UMFPack decomposition");
342 if (errcode == UMFPACK_ERROR_file_IO)
343 DUNE_THROW(Dune::Exception,
"IO error while loading UMFPack decomposition");
344 matrixIsLoaded_ =
true;
345 std::cout <<
"UMFPack decomposition successfully loaded from " << file << std::endl;
351 if ((umfpackMatrix_.N() + umfpackMatrix_.M() > 0) || matrixIsLoaded_)
360 if (umfpackMatrix_.N() != b.dim())
361 DUNE_THROW(
Dune::ISTLError,
"Size of right-hand-side vector b does not match the number of matrix rows!");
362 if (umfpackMatrix_.M() != x.dim())
363 DUNE_THROW(
Dune::ISTLError,
"Size of solution vector x does not match the number of matrix columns!");
367 double UMF_Apply_Info[UMFPACK_INFO];
368 Caller::solve(UMFPACK_A,
369 umfpackMatrix_.getColStart(),
370 umfpackMatrix_.getRowIndex(),
371 umfpackMatrix_.getValues(),
372 reinterpret_cast<double*
>(&x[0]),
373 reinterpret_cast<double*
>(&b[0]),
381 res.
elapsed = UMF_Apply_Info[UMFPACK_SOLVE_WALLTIME];
383 printOnApply(UMF_Apply_Info);
401 double UMF_Apply_Info[UMFPACK_INFO];
402 Caller::solve(UMFPACK_A,
403 umfpackMatrix_.getColStart(),
404 umfpackMatrix_.getRowIndex(),
405 umfpackMatrix_.getValues(),
411 printOnApply(UMF_Apply_Info);
427 if (option >= UMFPACK_CONTROL)
428 DUNE_THROW(RangeError,
"Requested non-existing UMFPack option");
430 UMF_Control[option] = value;
438 int errcode = Caller::save_numeric(UMF_Numeric,
const_cast<char*
>(file));
439 if (errcode != UMFPACK_OK)
440 DUNE_THROW(Dune::Exception,
"IO ERROR while trying to save UMFPack decomposition");
446 if ((umfpackMatrix_.N() + umfpackMatrix_.M() > 0) || matrixIsLoaded_)
448 if (matrix.N() == 0 or matrix.M() == 0)
451 if (umfpackMatrix_.N() + umfpackMatrix_.M() + umfpackMatrix_.nonzeroes() != 0)
452 umfpackMatrix_.free();
455 ISTL::Impl::BCCSMatrixInitializer<Matrix, long int> initializer(umfpackMatrix_);
457 copyToBCCSMatrix(initializer, matrix);
465 if ((umfpackMatrix_.N() + umfpackMatrix_.M() > 0) || matrixIsLoaded_)
468 if (umfpackMatrix_.N() + umfpackMatrix_.M() + umfpackMatrix_.nonzeroes() != 0)
469 umfpackMatrix_.free();
473 ISTL::Impl::BCCSMatrixInitializer<Matrix, long int> initializer(umfpackMatrix_);
475 copyToBCCSMatrix(initializer, ISTL::Impl::MatrixRowSubset<
Matrix,std::set<std::size_t> >(_mat,rowIndexSet));
492 UMF_Control[UMFPACK_PRL] = 1;
494 UMF_Control[UMFPACK_PRL] = 2;
496 UMF_Control[UMFPACK_PRL] = 4;
514 return umfpackMatrix_;
523 if (!matrixIsLoaded_)
525 Caller::free_symbolic(&UMF_Symbolic);
526 umfpackMatrix_.free();
528 Caller::free_numeric(&UMF_Numeric);
529 matrixIsLoaded_ =
false;
532 const char*
name() {
return "UMFPACK"; }
537 template<
class Mat,
class X,
class TM,
class TD,
class T1>
544 double UMF_Decomposition_Info[UMFPACK_INFO];
545 Caller::symbolic(
static_cast<int>(umfpackMatrix_.N()),
546 static_cast<int>(umfpackMatrix_.N()),
547 umfpackMatrix_.getColStart(),
548 umfpackMatrix_.getRowIndex(),
549 reinterpret_cast<double*
>(umfpackMatrix_.getValues()),
552 UMF_Decomposition_Info);
553 Caller::numeric(umfpackMatrix_.getColStart(),
554 umfpackMatrix_.getRowIndex(),
555 reinterpret_cast<double*
>(umfpackMatrix_.getValues()),
559 UMF_Decomposition_Info);
560 Caller::report_status(UMF_Control,UMF_Decomposition_Info[UMFPACK_STATUS]);
563 std::cout <<
"[UMFPack Decomposition]" << std::endl;
564 std::cout <<
"Wallclock Time taken: " << UMF_Decomposition_Info[UMFPACK_NUMERIC_WALLTIME] <<
" (CPU Time: " << UMF_Decomposition_Info[UMFPACK_NUMERIC_TIME] <<
")" << std::endl;
565 std::cout <<
"Flops taken: " << UMF_Decomposition_Info[UMFPACK_FLOPS] << std::endl;
566 std::cout <<
"Peak Memory Usage: " << UMF_Decomposition_Info[UMFPACK_PEAK_MEMORY]*UMF_Decomposition_Info[UMFPACK_SIZE_OF_UNIT] <<
" bytes" << std::endl;
567 std::cout <<
"Condition number estimate: " << 1./UMF_Decomposition_Info[UMFPACK_RCOND] << std::endl;
568 std::cout <<
"Numbers of non-zeroes in decomposition: L: " << UMF_Decomposition_Info[UMFPACK_LNZ] <<
" U: " << UMF_Decomposition_Info[UMFPACK_UNZ] << std::endl;
572 Caller::report_info(UMF_Control,UMF_Decomposition_Info);
576 void printOnApply(
double* UMF_Info)
578 Caller::report_status(UMF_Control,UMF_Info[UMFPACK_STATUS]);
581 std::cout <<
"[UMFPack Solve]" << std::endl;
582 std::cout <<
"Wallclock Time: " << UMF_Info[UMFPACK_SOLVE_WALLTIME] <<
" (CPU Time: " << UMF_Info[UMFPACK_SOLVE_TIME] <<
")" << std::endl;
583 std::cout <<
"Flops Taken: " << UMF_Info[UMFPACK_SOLVE_FLOPS] << std::endl;
584 std::cout <<
"Iterative Refinement steps taken: " << UMF_Info[UMFPACK_IR_TAKEN] << std::endl;
585 std::cout <<
"Error Estimate: " << UMF_Info[UMFPACK_OMEGA1] <<
" resp. " << UMF_Info[UMFPACK_OMEGA2] << std::endl;
590 bool matrixIsLoaded_;
594 double UMF_Control[UMFPACK_CONTROL];
597 template<
typename T,
typename A,
int n,
int m>
603 template<
typename T,
typename A>
611 template<
class B>
struct isValidBlock<B,
std::enable_if_t<std::is_same<typename FieldTraits<B>::real_type,double>::value>> : std::true_type {};
613 template<
typename TL,
typename M>
614 std::shared_ptr<Dune::InverseOperator<typename Dune::TypeListElement<1, TL>::type,
615 typename Dune::TypeListElement<2, TL>::type>>
618 isValidBlock<
typename Dune::TypeListElement<1, TL>::type::block_type>::value,
int> = 0)
const
620 int verbose = config.get(
"verbose", 0);
621 return std::make_shared<Dune::UMFPack<M>>(
mat,verbose);
625 template<
typename TL,
typename M>
626 std::shared_ptr<Dune::InverseOperator<typename Dune::TypeListElement<1, TL>::type,
627 typename Dune::TypeListElement<2, TL>::type>>
630 !
isValidBlock<
typename Dune::TypeListElement<1, TL>::type::block_type>::value,
int> = 0)
const
633 "Unsupported Type in UMFPack (only double and std::complex<double> supported)");
Implementation of the BCRSMatrix class.
#define DUNE_REGISTER_DIRECT_SOLVER(name,...)
Definition solverregistry.hh:13
Implementations of the inverse operator interface.
Templates characterizing the type of a solver.
void free()
free allocated space.
Definition umfpack.hh:521
virtual void apply(domain_type &x, range_type &b, InverseOperatorResult &res)
Apply inverse operator,.
Definition umfpack.hh:358
virtual SolverCategory::Category category() const
Category of the solver (see SolverCategory::Category)
Definition umfpack.hh:232
M matrix_type
Definition umfpack.hh:221
static void solve(long int m, const long int *cs, const long int *ri, std::complex< double > *val, double *x, const double *b, A... args)
Definition umfpack.hh:156
void setMatrix(const Matrix &matrix)
Initialize data from given matrix.
Definition umfpack.hh:444
static void report_info(A... args)
Definition umfpack.hh:141
UMFPack(const Matrix &mat_, const ParameterTree &config)
Construct a solver object from a matrix.
Definition umfpack.hh:282
static int load_numeric(A... args)
Definition umfpack.hh:131
static int load_numeric(A... args)
Definition umfpack.hh:74
std::shared_ptr< Dune::InverseOperator< typename Dune::TypeListElement< 1, TL >::type, typename Dune::TypeListElement< 2, TL >::type > > operator()(TL, const M &mat, const Dune::ParameterTree &config, std::enable_if_t< isValidBlock< typename Dune::TypeListElement< 1, TL >::type::block_type >::value, int >=0) const
Definition umfpack.hh:616
ISTL::Impl::BCCSMatrix< typename Matrix::field_type, long int > UMFPackMatrix
The corresponding UMFPack matrix type.
Definition umfpack.hh:223
static void report_status(A... args)
Definition umfpack.hh:89
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:306
typename Impl::UMFPackVectorChooser< M >::range_type range_type
The type of the range of the solver.
Definition umfpack.hh:229
UMFPack()
default constructor
Definition umfpack.hh:288
static void symbolic(A... args)
Definition umfpack.hh:104
static void report_info(A... args)
Definition umfpack.hh:84
static void symbolic(long int m, long int n, const long int *cs, const long int *ri, const double *val, A... args)
Definition umfpack.hh:162
static void free_symbolic(A... args)
Definition umfpack.hh:69
static int save_numeric(A... args)
Definition umfpack.hh:151
static void free_numeric(A... args)
Definition umfpack.hh:121
void setSubMatrix(const Matrix &_mat, const S &rowIndexSet)
Definition umfpack.hh:463
static int save_numeric(A... args)
Definition umfpack.hh:94
static void report_status(A... args)
Definition umfpack.hh:146
ISTL::Impl::BCCSMatrixInitializer< M, long int > MatrixInitializer
Type of an associated initializer class.
Definition umfpack.hh:225
void apply(T *x, T *b)
additional apply method with c-arrays in analogy to superlu
Definition umfpack.hh:399
static void defaults(A... args)
Definition umfpack.hh:116
static void free_numeric(A... args)
Definition umfpack.hh:64
void setVerbosity(int v)
sets the verbosity level for the UMFPack solver
Definition umfpack.hh:487
UMFPack(const char *file, int verbose=0)
try loading a decomposition from file
Definition umfpack.hh:333
static void numeric(A... args)
Definition umfpack.hh:79
static constexpr bool valid
Definition umfpack.hh:50
virtual ~UMFPack()
Definition umfpack.hh:349
const char * name()
Definition umfpack.hh:532
void saveDecomposition(const char *file)
saves a decomposition to a file
Definition umfpack.hh:436
UMFPackMatrix & getInternalMatrix()
Return the column compress matrix from UMFPack.
Definition umfpack.hh:512
typename Impl::UMFPackVectorChooser< M >::domain_type domain_type
The type of the domain of the solver.
Definition umfpack.hh:227
UMFPack(const Matrix &matrix, int verbose=0)
Construct a solver object from a matrix.
Definition umfpack.hh:245
virtual void apply(domain_type &x, range_type &b, double reduction, InverseOperatorResult &res)
apply inverse operator, with given convergence criteria.
Definition umfpack.hh:389
void setOption(unsigned int option, double value)
Set UMFPack-specific options.
Definition umfpack.hh:425
static void free_symbolic(A... args)
Definition umfpack.hh:126
M Matrix
The matrix type.
Definition umfpack.hh:220
static void numeric(const long int *cs, const long int *ri, const double *val, A... args)
Definition umfpack.hh:136
static void defaults(A... args)
Definition umfpack.hh:59
static void solve(A... args)
Definition umfpack.hh:99
UMFPack(const Matrix &matrix, int verbose, bool)
Constructor for compatibility with SuperLU standard constructor.
Definition umfpack.hh:263
void * getFactorization()
Return the matrix factorization.
Definition umfpack.hh:503
Matrix & mat
Definition matrixmatrix.hh:347
Definition allocator.hh:11
PropertyMapTypeSelector< Amg::VertexVisitedTag, Amg::PropertiesGraph< G, Amg::VertexProperties, EP, VM, EM > >::Type get(const Amg::VertexVisitedTag &tag, Amg::PropertiesGraph< G, Amg::VertexProperties, EP, VM, EM > &graph)
Definition dependency.hh:293
Definition matrixutils.hh:211
A sparse block matrix with compressed row storage.
Definition bcrsmatrix.hh:466
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
Definition matrixutils.hh:27
Statistics about the application of an inverse operator.
Definition solver.hh:48
double elapsed
Elapsed time in seconds.
Definition solver.hh:82
int iterations
Number of iterations.
Definition solver.hh:67
bool converged
True if convergence criterion has been met.
Definition solver.hh:73
Abstract base class for all solvers.
Definition solver.hh:99
Category
Definition solvercategory.hh:23
@ sequential
Category for sequential solvers.
Definition solvercategory.hh:25
Definition solverregistry.hh:77
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
The UMFPack direct sparse solver.
Definition umfpack.hh:215
Definition umfpack.hh:609
Definition umfpack.hh:610