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>
42 template<
class M,
class T,
class TM,
class TD,
class TA>
43 class SeqOverlappingSchwarz;
45 template<
class T,
bool tag>
46 struct SeqOverlappingSchwarzAssemblerHelper;
53 static constexpr bool valid = false ;
59 static constexpr bool valid = true ;
61 template<
typename... A>
64 umfpack_dl_defaults(args...);
66 template<
typename... A>
69 umfpack_dl_free_numeric(args...);
71 template<
typename... A>
74 umfpack_dl_free_symbolic(args...);
76 template<
typename... A>
79 return umfpack_dl_load_numeric(args...);
81 template<
typename... A>
84 umfpack_dl_numeric(args...);
86 template<
typename... A>
89 umfpack_dl_report_info(args...);
91 template<
typename... A>
94 umfpack_dl_report_status(args...);
96 template<
typename... A>
99 return umfpack_dl_save_numeric(args...);
101 template<
typename... A>
104 umfpack_dl_solve(args...);
106 template<
typename... A>
109 umfpack_dl_symbolic(args...);
116 static constexpr bool valid = true ;
119 template<
typename... A>
122 umfpack_zl_defaults(args...);
124 template<
typename... A>
127 umfpack_zl_free_numeric(args...);
129 template<
typename... A>
132 umfpack_zl_free_symbolic(args...);
134 template<
typename... A>
137 return umfpack_zl_load_numeric(args...);
139 template<
typename... A>
142 umfpack_zl_numeric(cs,ri,val,NULL,args...);
144 template<
typename... A>
147 umfpack_zl_report_info(args...);
149 template<
typename... A>
152 umfpack_zl_report_status(args...);
154 template<
typename... A>
157 return umfpack_zl_save_numeric(args...);
159 template<
typename... A>
162 const double* cval =
reinterpret_cast<const double*
>(val);
163 umfpack_zl_solve(m,cs,ri,cval,NULL,x,NULL,b,NULL,args...);
165 template<
typename... A>
168 umfpack_zl_symbolic(m,n,cs,ri,val,NULL,args...);
174 template<
class M,
class =
void>
175 struct UMFPackVectorChooser;
178 template<
class M>
using UMFPackDomainType =
typename UMFPackVectorChooser<M>::domain_type;
181 template<
class M>
using UMFPackRangeType =
typename UMFPackVectorChooser<M>::range_type;
184 struct UMFPackVectorChooser<M,
185 std::enable_if_t<(std::is_same<M,double>::value) || (std::is_same<M,std::complex<double> >::value)>>
187 using domain_type = M;
188 using range_type = M;
191 template<
typename T,
int n,
int m>
192 struct UMFPackVectorChooser<FieldMatrix<T,n,m>,
193 std::enable_if_t<(std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value)>>
196 using domain_type = FieldVector<T,m>;
198 using range_type = FieldVector<T,n>;
201 template<
typename T,
typename A>
202 struct UMFPackVectorChooser<BCRSMatrix<T,A>,
203 std::void_t<UMFPackDomainType<T>, UMFPackRangeType<T>>>
209 using domain_type = BlockVector<UMFPackDomainType<T>,
typename std::allocator_traits<A>::template rebind_alloc<UMFPackDomainType<T>>>;
211 using range_type = BlockVector<UMFPackRangeType<T>,
typename std::allocator_traits<A>::template rebind_alloc<UMFPackRangeType<T>>>;
215 template<
typename FirstBlock,
typename... Blocks>
216 struct UMFPackVectorChooser<MultiTypeBlockVector<FirstBlock, Blocks...>,
217 std::void_t<UMFPackDomainType<FirstBlock>, UMFPackRangeType<FirstBlock>, UMFPackDomainType<Blocks>...>>
220 using domain_type = MultiTypeBlockVector<UMFPackDomainType<FirstBlock>, UMFPackDomainType<Blocks>...>;
222 using range_type = UMFPackRangeType<FirstBlock>;
226 template<
typename FirstRow,
typename... Rows>
227 struct UMFPackVectorChooser<MultiTypeBlockMatrix<FirstRow, Rows...>,
228 std::void_t<UMFPackDomainType<FirstRow>, UMFPackRangeType<FirstRow>, UMFPackRangeType<Rows>...>>
231 using domain_type = UMFPackDomainType<FirstRow>;
233 using range_type = MultiTypeBlockVector< UMFPackRangeType<FirstRow>, UMFPackRangeType<Rows>... >;
259 using T =
typename M::field_type;
268 using UMFPackMatrix = ISTL::Impl::BCCSMatrix<typename Matrix::field_type, size_type>;
293 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
294 "Unsupported Type in UMFPack (only double and std::complex<double> supported)");
295 Caller::defaults(UMF_Control);
311 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
312 "Unsupported Type in UMFPack (only double and std::complex<double> supported)");
313 Caller::defaults(UMF_Control);
328 :
UMFPack(mat_, config.
get<int>(
"verbose", 0))
333 UMFPack() : matrixIsLoaded_(false), verbosity_(0)
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);
354 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
355 "Unsupported Type in UMFPack (only double and std::complex<double> supported)");
356 Caller::defaults(UMF_Control);
358 int errcode = Caller::load_numeric(&UMF_Numeric,
const_cast<char*
>(file));
359 if ((errcode == UMFPACK_ERROR_out_of_memory) || (errcode == UMFPACK_ERROR_file_IO))
361 matrixIsLoaded_ =
false;
367 matrixIsLoaded_ =
true;
368 std::cout <<
"UMFPack decomposition successfully loaded from " << file << std::endl;
381 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
382 "Unsupported Type in UMFPack (only double and std::complex<double> supported)");
383 Caller::defaults(UMF_Control);
384 int errcode = Caller::load_numeric(&UMF_Numeric,
const_cast<char*
>(file));
385 if (errcode == UMFPACK_ERROR_out_of_memory)
386 DUNE_THROW(Dune::Exception,
"ran out of memory while loading UMFPack decomposition");
387 if (errcode == UMFPACK_ERROR_file_IO)
388 DUNE_THROW(Dune::Exception,
"IO error while loading UMFPack decomposition");
389 matrixIsLoaded_ =
true;
390 std::cout <<
"UMFPack decomposition successfully loaded from " << file << std::endl;
396 if ((umfpackMatrix_.N() + umfpackMatrix_.M() > 0) || matrixIsLoaded_)
405 if (umfpackMatrix_.N() != b.dim())
406 DUNE_THROW(
Dune::ISTLError,
"Size of right-hand-side vector b does not match the number of matrix rows!");
407 if (umfpackMatrix_.M() != x.dim())
408 DUNE_THROW(
Dune::ISTLError,
"Size of solution vector x does not match the number of matrix columns!");
414 std::vector<T> xFlat(x.dim()), bFlat(b.dim());
418 xFlat[ offset ] = entry;
423 bFlat[ offset ] = entry;
426 double UMF_Apply_Info[UMFPACK_INFO];
427 Caller::solve(UMFPACK_A,
428 umfpackMatrix_.getColStart(),
429 umfpackMatrix_.getRowIndex(),
430 umfpackMatrix_.getValues(),
431 reinterpret_cast<double*
>(&xFlat[0]),
432 reinterpret_cast<double*
>(&bFlat[0]),
440 entry = xFlat[offset];
446 res.
elapsed = UMF_Apply_Info[UMFPACK_SOLVE_WALLTIME];
448 printOnApply(UMF_Apply_Info);
468 double UMF_Apply_Info[UMFPACK_INFO];
469 Caller::solve(UMFPACK_A,
470 umfpackMatrix_.getColStart(),
471 umfpackMatrix_.getRowIndex(),
472 umfpackMatrix_.getValues(),
478 printOnApply(UMF_Apply_Info);
494 if (option >= UMFPACK_CONTROL)
495 DUNE_THROW(RangeError,
"Requested non-existing UMFPack option");
497 UMF_Control[option] = value;
505 int errcode = Caller::save_numeric(UMF_Numeric,
const_cast<char*
>(file));
506 if (errcode != UMFPACK_OK)
507 DUNE_THROW(Dune::Exception,
"IO ERROR while trying to save UMFPack decomposition");
519 template<
class BitVector = Impl::NoBitVector>
522 if ((umfpackMatrix_.N() + umfpackMatrix_.M() > 0) || matrixIsLoaded_)
524 if (matrix.N() == 0 or matrix.M() == 0)
527 if (umfpackMatrix_.N() + umfpackMatrix_.M() + umfpackMatrix_.nonzeroes() != 0)
528 umfpackMatrix_.free();
530 constexpr bool useBitVector = not std::is_same_v<BitVector,Impl::NoBitVector>;
533 std::vector<bool> flatBitVector;
535 std::vector<size_type> subIndices;
537 int numberOfIgnoredDofs = 0;
540 if constexpr ( useBitVector )
543 flatBitVector.resize(flatSize);
547 flatBitVector[ offset ] = entry;
550 numberOfIgnoredDofs++;
558 if constexpr ( useBitVector )
559 if ( flatBitVector[row] or flatBitVector[
col] )
565 if constexpr ( useBitVector )
568 subIndices.resize(flatRows,std::numeric_limits<std::size_t>::max());
572 if ( not flatBitVector[ i ] )
573 subIndices[ i ] = subIndexCounter++;
576 flatRows -= numberOfIgnoredDofs;
577 flatCols -= numberOfIgnoredDofs;
581 umfpackMatrix_.setSize(flatRows,flatCols);
582 umfpackMatrix_.Nnz_ = nonZeros;
585 umfpackMatrix_.colstart =
new size_type[flatCols+1];
586 umfpackMatrix_.rowindex =
new size_type[nonZeros];
587 umfpackMatrix_.values =
new T[nonZeros];
591 umfpackMatrix_.colstart[i] = 0;
599 if constexpr ( useBitVector )
600 if ( flatBitVector[flatRowIndex] or flatBitVector[flatColIndex] )
605 auto colIdx = useBitVector ? subIndices[flatColIndex] : flatColIndex;
607 umfpackMatrix_.colstart[colIdx+1]++;
613 umfpackMatrix_.colstart[i+1] += umfpackMatrix_.colstart[i];
617 std::vector<size_type> colPosition(flatCols,0);
620 flatMatrixForEach(matrix, [&](
auto&& entry,
auto&& flatRowIndex,
auto&& flatColIndex)
623 if constexpr ( useBitVector )
624 if ( flatBitVector[flatRowIndex] or flatBitVector[flatColIndex] )
629 auto rowIdx = useBitVector ? subIndices[flatRowIndex] : flatRowIndex;
630 auto colIdx = useBitVector ? subIndices[flatColIndex] : flatColIndex;
633 auto colStart = umfpackMatrix_.colstart[colIdx];
635 auto colPos = colPosition[colIdx];
637 umfpackMatrix_.rowindex[ colStart + colPos ] = rowIdx;
638 umfpackMatrix_.values[ colStart + colPos ] = entry;
640 colPosition[colIdx]++;
653 if ((umfpackMatrix_.N() + umfpackMatrix_.M() > 0) || matrixIsLoaded_)
656 if (umfpackMatrix_.N() + umfpackMatrix_.M() + umfpackMatrix_.nonzeroes() != 0)
657 umfpackMatrix_.free();
661 ISTL::Impl::BCCSMatrixInitializer<Matrix, SuiteSparse_long> initializer(umfpackMatrix_);
663 copyToBCCSMatrix(initializer, ISTL::Impl::MatrixRowSubset<
Matrix,std::set<std::size_t> >(_mat,rowIndexSet));
680 UMF_Control[UMFPACK_PRL] = 1;
682 UMF_Control[UMFPACK_PRL] = 2;
684 UMF_Control[UMFPACK_PRL] = 4;
702 return umfpackMatrix_;
711 if (!matrixIsLoaded_)
713 Caller::free_symbolic(&UMF_Symbolic);
714 umfpackMatrix_.free();
716 Caller::free_numeric(&UMF_Numeric);
717 matrixIsLoaded_ =
false;
720 const char*
name() {
return "UMFPACK"; }
725 template<
class Mat,
class X,
class TM,
class TD,
class T1>
732 double UMF_Decomposition_Info[UMFPACK_INFO];
733 Caller::symbolic(
static_cast<SuiteSparse_long
>(umfpackMatrix_.N()),
734 static_cast<SuiteSparse_long
>(umfpackMatrix_.N()),
735 umfpackMatrix_.getColStart(),
736 umfpackMatrix_.getRowIndex(),
737 reinterpret_cast<double*
>(umfpackMatrix_.getValues()),
740 UMF_Decomposition_Info);
741 Caller::numeric(umfpackMatrix_.getColStart(),
742 umfpackMatrix_.getRowIndex(),
743 reinterpret_cast<double*
>(umfpackMatrix_.getValues()),
747 UMF_Decomposition_Info);
748 Caller::report_status(UMF_Control,UMF_Decomposition_Info[UMFPACK_STATUS]);
751 std::cout <<
"[UMFPack Decomposition]" << std::endl;
752 std::cout <<
"Wallclock Time taken: " << UMF_Decomposition_Info[UMFPACK_NUMERIC_WALLTIME] <<
" (CPU Time: " << UMF_Decomposition_Info[UMFPACK_NUMERIC_TIME] <<
")" << std::endl;
753 std::cout <<
"Flops taken: " << UMF_Decomposition_Info[UMFPACK_FLOPS] << std::endl;
754 std::cout <<
"Peak Memory Usage: " << UMF_Decomposition_Info[UMFPACK_PEAK_MEMORY]*UMF_Decomposition_Info[UMFPACK_SIZE_OF_UNIT] <<
" bytes" << std::endl;
755 std::cout <<
"Condition number estimate: " << 1./UMF_Decomposition_Info[UMFPACK_RCOND] << std::endl;
756 std::cout <<
"Numbers of non-zeroes in decomposition: L: " << UMF_Decomposition_Info[UMFPACK_LNZ] <<
" U: " << UMF_Decomposition_Info[UMFPACK_UNZ] << std::endl;
760 Caller::report_info(UMF_Control,UMF_Decomposition_Info);
764 void printOnApply(
double* UMF_Info)
766 Caller::report_status(UMF_Control,UMF_Info[UMFPACK_STATUS]);
769 std::cout <<
"[UMFPack Solve]" << std::endl;
770 std::cout <<
"Wallclock Time: " << UMF_Info[UMFPACK_SOLVE_WALLTIME] <<
" (CPU Time: " << UMF_Info[UMFPACK_SOLVE_TIME] <<
")" << std::endl;
771 std::cout <<
"Flops Taken: " << UMF_Info[UMFPACK_SOLVE_FLOPS] << std::endl;
772 std::cout <<
"Iterative Refinement steps taken: " << UMF_Info[UMFPACK_IR_TAKEN] << std::endl;
773 std::cout <<
"Error Estimate: " << UMF_Info[UMFPACK_OMEGA1] <<
" resp. " << UMF_Info[UMFPACK_OMEGA2] << std::endl;
778 bool matrixIsLoaded_;
782 double UMF_Control[UMFPACK_CONTROL];
785 template<
typename T,
typename A,
int n,
int m>
791 template<
typename T,
typename A>
799 template<
class TL,
class M,
class=
void>
struct isValidBlock : std::false_type{};
802 std::is_same_v<Impl::UMFPackDomainType<M>, typename Dune::TypeListElement<1,TL>::type>
803 && std::is_same_v<Impl::UMFPackRangeType<M>, typename Dune::TypeListElement<2,TL>::type>
804 >> : std::true_type {};
806 template<
typename TL,
typename M>
807 std::shared_ptr<Dune::InverseOperator<Impl::UMFPackDomainType<M>,Impl::UMFPackRangeType<M>>>
811 int verbose = config.get(
"verbose", 0);
812 return std::make_shared<Dune::UMFPack<M>>(
mat,verbose);
816 template<
typename TL,
typename M>
817 std::shared_ptr<Dune::InverseOperator<typename Dune::TypeListElement<1, TL>::type,
818 typename Dune::TypeListElement<2, TL>::type>>
822 using D =
typename Dune::TypeListElement<1,TL>::type;
823 using R =
typename Dune::TypeListElement<2,TL>::type;
824 using DU = Std::detected_t< Impl::UMFPackDomainType, M>;
825 using RU = Std::detected_t< Impl::UMFPackRangeType, M>;
827 "Unsupported Types in UMFPack:\n"
828 "Matrix: " << className<M>() <<
""
829 "Domain provided: " << className<D>() <<
"\n"
830 "Domain required: " << className<DU>() <<
"\n"
831 "Range provided: " << className<R>() <<
"\n"
832 "Range required: " << className<RU>() <<
"\n"
Templates characterizing the type of a solver.
#define DUNE_REGISTER_DIRECT_SOLVER(name,...)
Definition solverregistry.hh:13
Implementation of the BCRSMatrix class.
Implementations of the inverse operator interface.
void free()
free allocated space.
Definition umfpack.hh:709
std::shared_ptr< Dune::InverseOperator< Impl::UMFPackDomainType< M >, Impl::UMFPackRangeType< M > > > operator()(TL, const M &mat, const Dune::ParameterTree &config, std::enable_if_t< isValidBlock< TL, M >::value, int >=0) const
Definition umfpack.hh:808
virtual void apply(domain_type &x, range_type &b, InverseOperatorResult &res)
Apply inverse operator,.
Definition umfpack.hh:403
SuiteSparse_long size_type
Definition umfpack.hh:262
static void symbolic(size_type m, size_type n, const size_type *cs, const size_type *ri, const double *val, A... args)
Definition umfpack.hh:166
static void solve(size_type m, const size_type *cs, const size_type *ri, std::complex< double > *val, double *x, const double *b, A... args)
Definition umfpack.hh:160
virtual SolverCategory::Category category() const
Category of the solver (see SolverCategory::Category)
Definition umfpack.hh:277
M matrix_type
Definition umfpack.hh:266
static void numeric(const size_type *cs, const size_type *ri, const double *val, A... args)
Definition umfpack.hh:140
static void report_info(A... args)
Definition umfpack.hh:145
UMFPack(const Matrix &mat_, const ParameterTree &config)
Construct a solver object from a matrix.
Definition umfpack.hh:327
static int load_numeric(A... args)
Definition umfpack.hh:135
static int load_numeric(A... args)
Definition umfpack.hh:77
static void report_status(A... args)
Definition umfpack.hh:92
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:351
Impl::UMFPackRangeType< M > range_type
The type of the range of the solver.
Definition umfpack.hh:274
UMFPack()
default constructor
Definition umfpack.hh:333
static void symbolic(A... args)
Definition umfpack.hh:107
static void report_info(A... args)
Definition umfpack.hh:87
static void free_symbolic(A... args)
Definition umfpack.hh:72
static int save_numeric(A... args)
Definition umfpack.hh:155
static void free_numeric(A... args)
Definition umfpack.hh:125
void setSubMatrix(const Matrix &_mat, const S &rowIndexSet)
Definition umfpack.hh:651
static int save_numeric(A... args)
Definition umfpack.hh:97
static void report_status(A... args)
Definition umfpack.hh:150
Impl::UMFPackDomainType< M > domain_type
The type of the domain of the solver.
Definition umfpack.hh:272
void apply(T *x, T *b)
additional apply method with c-arrays in analogy to superlu
Definition umfpack.hh:466
static void defaults(A... args)
Definition umfpack.hh:120
static void free_numeric(A... args)
Definition umfpack.hh:67
void setVerbosity(int v)
sets the verbosity level for the UMFPack solver
Definition umfpack.hh:675
UMFPack(const char *file, int verbose=0)
try loading a decomposition from file
Definition umfpack.hh:378
static void numeric(A... args)
Definition umfpack.hh:82
static constexpr bool valid
Definition umfpack.hh:53
virtual ~UMFPack()
Definition umfpack.hh:394
const char * name()
Definition umfpack.hh:720
void setMatrix(const Matrix &matrix, const BitVector &bitVector={})
Initialize data from given matrix.
Definition umfpack.hh:520
void saveDecomposition(const char *file)
saves a decomposition to a file
Definition umfpack.hh:503
UMFPackMatrix & getInternalMatrix()
Return the column compress matrix from UMFPack.
Definition umfpack.hh:700
SuiteSparse_long size_type
Definition umfpack.hh:117
UMFPack(const Matrix &matrix, int verbose=0)
Construct a solver object from a matrix.
Definition umfpack.hh:290
ISTL::Impl::BCCSMatrixInitializer< M, size_type > MatrixInitializer
Type of an associated initializer class.
Definition umfpack.hh:270
virtual void apply(domain_type &x, range_type &b, double reduction, InverseOperatorResult &res)
apply inverse operator, with given convergence criteria.
Definition umfpack.hh:454
ISTL::Impl::BCCSMatrix< typename Matrix::field_type, size_type > UMFPackMatrix
The corresponding (scalar) UMFPack matrix type.
Definition umfpack.hh:268
void setOption(unsigned int option, double value)
Set UMFPack-specific options.
Definition umfpack.hh:492
static void free_symbolic(A... args)
Definition umfpack.hh:130
M Matrix
The matrix type.
Definition umfpack.hh:265
static void defaults(A... args)
Definition umfpack.hh:62
static void solve(A... args)
Definition umfpack.hh:102
UMFPack(const Matrix &matrix, int verbose, bool)
Constructor for compatibility with SuperLU standard constructor.
Definition umfpack.hh:308
void * getFactorization()
Return the matrix factorization.
Definition umfpack.hh:691
Col col
Definition matrixmatrix.hh:351
Matrix & mat
Definition matrixmatrix.hh:347
Definition allocator.hh:11
std::pair< std::size_t, std::size_t > flatMatrixForEach(Matrix &&matrix, F &&f, std::size_t rowOffset=0, std::size_t colOffset=0)
Traverse a blocked matrix and call a functor at each scalar entry.
Definition foreach.hh:132
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
std::size_t flatVectorForEach(Vector &&vector, F &&f, std::size_t offset=0)
Traverse a blocked vector and call a functor at each scalar entry.
Definition foreach.hh:95
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:50
double elapsed
Elapsed time in seconds.
Definition solver.hh:84
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: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:258
Definition umfpack.hh:797
Definition umfpack.hh:799