5#ifndef DUNE_ISTL_UMFPACK_HH
6#define DUNE_ISTL_UMFPACK_HH
8#if HAVE_SUITESPARSE_UMFPACK || defined DOXYGEN
18#include<dune/istl/bccsmatrixinitializer.hh>
21#include<dune/istl/foreach.hh>
22#include<dune/istl/multitypeblockmatrix.hh>
23#include<dune/istl/multitypeblockvector.hh>
26#include <dune/istl/solverfactory.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;
52 struct UMFPackMethodChooser
54 static constexpr bool valid = false ;
58 struct UMFPackMethodChooser<double>
60 static constexpr bool valid = true ;
62 template<
typename... A>
63 static void defaults(A... args)
65 umfpack_dl_defaults(args...);
67 template<
typename... A>
68 static void free_numeric(A... args)
70 umfpack_dl_free_numeric(args...);
72 template<
typename... A>
73 static void free_symbolic(A... args)
75 umfpack_dl_free_symbolic(args...);
77 template<
typename... A>
78 static int load_numeric(A... args)
80 return umfpack_dl_load_numeric(args...);
82 template<
typename... A>
83 static void numeric(A... args)
85 umfpack_dl_numeric(args...);
87 template<
typename... A>
88 static void report_info(A... args)
90 umfpack_dl_report_info(args...);
92 template<
typename... A>
93 static void report_status(A... args)
95 umfpack_dl_report_status(args...);
97 template<
typename... A>
98 static int save_numeric(A... args)
100 return umfpack_dl_save_numeric(args...);
102 template<
typename... A>
103 static void solve(A... args)
105 umfpack_dl_solve(args...);
107 template<
typename... A>
108 static void symbolic(A... args)
110 umfpack_dl_symbolic(args...);
115 struct UMFPackMethodChooser<
std::complex<double> >
117 static constexpr bool valid = true ;
118 using size_type = SuiteSparse_long;
120 template<
typename... A>
121 static void defaults(A... args)
123 umfpack_zl_defaults(args...);
125 template<
typename... A>
126 static void free_numeric(A... args)
128 umfpack_zl_free_numeric(args...);
130 template<
typename... A>
131 static void free_symbolic(A... args)
133 umfpack_zl_free_symbolic(args...);
135 template<
typename... A>
136 static int load_numeric(A... args)
138 return umfpack_zl_load_numeric(args...);
140 template<
typename... A>
141 static void numeric(
const size_type* cs,
const size_type* ri,
const double* val, A... args)
143 umfpack_zl_numeric(cs,ri,val,NULL,args...);
145 template<
typename... A>
146 static void report_info(A... args)
148 umfpack_zl_report_info(args...);
150 template<
typename... A>
151 static void report_status(A... args)
153 umfpack_zl_report_status(args...);
155 template<
typename... A>
156 static int save_numeric(A... args)
158 return umfpack_zl_save_numeric(args...);
160 template<
typename... A>
161 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)
163 const double* cval =
reinterpret_cast<const double*
>(val);
164 umfpack_zl_solve(m,cs,ri,cval,NULL,x,NULL,b,NULL,args...);
166 template<
typename... A>
167 static void symbolic(size_type m, size_type n,
const size_type* cs,
const size_type* ri,
const double* val, A... args)
169 umfpack_zl_symbolic(m,n,cs,ri,val,NULL,args...);
175 template<
class M,
class =
void>
176 struct UMFPackVectorChooser;
179 template<
class M>
using UMFPackDomainType =
typename UMFPackVectorChooser<M>::domain_type;
182 template<
class M>
using UMFPackRangeType =
typename UMFPackVectorChooser<M>::range_type;
185 struct UMFPackVectorChooser<M,
186 std::enable_if_t<(std::is_same<M,double>::value) || (std::is_same<M,std::complex<double> >::value)>>
188 using domain_type = M;
189 using range_type = M;
192 template<
typename T,
int n,
int m>
193 struct UMFPackVectorChooser<FieldMatrix<T,n,m>,
194 std::enable_if_t<(std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value)>>
197 using domain_type = FieldVector<T,m>;
199 using range_type = FieldVector<T,n>;
202 template<
typename T,
typename A>
203 struct UMFPackVectorChooser<BCRSMatrix<T,A>,
204 std::
void_t<UMFPackDomainType<T>, UMFPackRangeType<T>>>
210 using domain_type = BlockVector<UMFPackDomainType<T>,
typename std::allocator_traits<A>::template rebind_alloc<UMFPackDomainType<T>>>;
212 using range_type = BlockVector<UMFPackRangeType<T>,
typename std::allocator_traits<A>::template rebind_alloc<UMFPackRangeType<T>>>;
215 template<
typename T,
typename A>
216 struct UMFPackVectorChooser<Matrix<T,A>,
217 std::
void_t<UMFPackDomainType<T>, UMFPackRangeType<T>>>
218 :
public UMFPackVectorChooser<BCRSMatrix<T,A>, std::void_t<UMFPackDomainType<T>, UMFPackRangeType<T>>>
222 template<
typename FirstBlock,
typename... Blocks>
223 struct UMFPackVectorChooser<MultiTypeBlockVector<FirstBlock, Blocks...>,
224 std::
void_t<UMFPackDomainType<FirstBlock>, UMFPackRangeType<FirstBlock>, UMFPackDomainType<Blocks>...>>
227 using domain_type = MultiTypeBlockVector<UMFPackDomainType<FirstBlock>, UMFPackDomainType<Blocks>...>;
229 using range_type = UMFPackRangeType<FirstBlock>;
233 template<
typename FirstRow,
typename... Rows>
234 struct UMFPackVectorChooser<MultiTypeBlockMatrix<FirstRow, Rows...>,
235 std::
void_t<UMFPackDomainType<FirstRow>, UMFPackRangeType<FirstRow>, UMFPackRangeType<Rows>...>>
238 using domain_type = UMFPackDomainType<FirstRow>;
240 using range_type = MultiTypeBlockVector< UMFPackRangeType<FirstRow>, UMFPackRangeType<Rows>... >;
266 using T =
typename M::field_type;
269 using size_type = SuiteSparse_long;
273 using matrix_type = M;
275 using UMFPackMatrix = ISTL::Impl::BCCSMatrix<typename Matrix::field_type, size_type>;
286 return SolverCategory::Category::sequential;
300 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
301 "Unsupported Type in UMFPack (only double and std::complex<double> supported)");
302 Caller::defaults(UMF_Control);
318 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
319 "Unsupported Type in UMFPack (only double and std::complex<double> supported)");
320 Caller::defaults(UMF_Control);
335 :
UMFPack(mat_, config.
get<int>(
"verbose", 0))
340 UMFPack() : matrixIsLoaded_(false), verbosity_(0)
343 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
344 "Unsupported Type in UMFPack (only double and std::complex<double> supported)");
345 Caller::defaults(UMF_Control);
361 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
362 "Unsupported Type in UMFPack (only double and std::complex<double> supported)");
363 Caller::defaults(UMF_Control);
365 int errcode = Caller::load_numeric(&UMF_Numeric,
const_cast<char*
>(file));
366 if ((errcode == UMFPACK_ERROR_out_of_memory) || (errcode == UMFPACK_ERROR_file_IO))
368 matrixIsLoaded_ =
false;
374 matrixIsLoaded_ =
true;
375 std::cout <<
"UMFPack decomposition successfully loaded from " << file << std::endl;
388 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
389 "Unsupported Type in UMFPack (only double and std::complex<double> supported)");
390 Caller::defaults(UMF_Control);
391 int errcode = Caller::load_numeric(&UMF_Numeric,
const_cast<char*
>(file));
392 if (errcode == UMFPACK_ERROR_out_of_memory)
394 if (errcode == UMFPACK_ERROR_file_IO)
396 matrixIsLoaded_ =
true;
397 std::cout <<
"UMFPack decomposition successfully loaded from " << file << std::endl;
403 if ((umfpackMatrix_.N() + umfpackMatrix_.M() > 0) || matrixIsLoaded_)
412 if (umfpackMatrix_.N() != b.dim())
414 if (umfpackMatrix_.M() != x.dim())
421 std::vector<T> xFlat(x.dim()), bFlat(b.dim());
425 xFlat[ offset ] = entry;
430 bFlat[ offset ] = entry;
433 double UMF_Apply_Info[UMFPACK_INFO];
434 Caller::solve(UMFPACK_A,
435 umfpackMatrix_.getColStart(),
436 umfpackMatrix_.getRowIndex(),
437 umfpackMatrix_.getValues(),
438 reinterpret_cast<double*
>(&xFlat[0]),
439 reinterpret_cast<double*
>(&bFlat[0]),
447 entry = xFlat[offset];
453 res.
elapsed = UMF_Apply_Info[UMFPACK_SOLVE_WALLTIME];
455 printOnApply(UMF_Apply_Info);
475 double UMF_Apply_Info[UMFPACK_INFO];
476 Caller::solve(UMFPACK_A,
477 umfpackMatrix_.getColStart(),
478 umfpackMatrix_.getRowIndex(),
479 umfpackMatrix_.getValues(),
485 printOnApply(UMF_Apply_Info);
501 if (option >= UMFPACK_CONTROL)
504 UMF_Control[option] = value;
512 int errcode = Caller::save_numeric(UMF_Numeric,
const_cast<char*
>(file));
513 if (errcode != UMFPACK_OK)
526 template<
class BitVector = Impl::NoBitVector>
529 if ((umfpackMatrix_.N() + umfpackMatrix_.M() > 0) || matrixIsLoaded_)
531 if (matrix.N() == 0 or matrix.M() == 0)
534 if (umfpackMatrix_.N() + umfpackMatrix_.M() + umfpackMatrix_.nonzeroes() != 0)
535 umfpackMatrix_.free();
537 constexpr bool useBitVector = not std::is_same_v<BitVector,Impl::NoBitVector>;
540 std::vector<bool> flatBitVector;
542 std::vector<size_type> subIndices;
544 [[maybe_unused]]
int numberOfIgnoredDofs = 0;
547 if constexpr ( useBitVector )
550 flatBitVector.resize(flatSize);
554 flatBitVector[ offset ] = entry;
557 numberOfIgnoredDofs++;
563 auto [flatRows,flatCols] =
flatMatrixForEach( matrix, [&](
auto&& ,
auto&& row,
auto&& col){
565 if constexpr ( useBitVector )
566 if ( flatBitVector[row] or flatBitVector[col] )
572 if constexpr ( useBitVector )
577 size_type subIndexCounter = 0;
578 for ( size_type i=0; i<size_type(flatRows); i++ )
579 if ( not flatBitVector[ i ] )
580 subIndices[ i ] = subIndexCounter++;
583 flatRows -= numberOfIgnoredDofs;
584 flatCols -= numberOfIgnoredDofs;
588 umfpackMatrix_.setSize(flatRows,flatCols);
589 umfpackMatrix_.Nnz_ = nonZeros;
592 umfpackMatrix_.colstart =
new size_type[flatCols+1];
593 umfpackMatrix_.rowindex =
new size_type[nonZeros];
594 umfpackMatrix_.values =
new T[nonZeros];
596 for ( size_type i=0; i<size_type(flatCols+1); i++ )
598 umfpackMatrix_.colstart[i] = 0;
606 if constexpr ( useBitVector )
607 if ( flatBitVector[flatRowIndex] or flatBitVector[flatColIndex] )
612 auto colIdx = useBitVector ? subIndices[flatColIndex] : flatColIndex;
614 umfpackMatrix_.colstart[colIdx+1]++;
618 for ( size_type i=0; i<(size_type)flatCols; i++ )
620 umfpackMatrix_.colstart[i+1] += umfpackMatrix_.colstart[i];
624 std::vector<size_type> colPosition(flatCols,0);
627 flatMatrixForEach(matrix, [&](
auto&& entry,
auto&& flatRowIndex,
auto&& flatColIndex)
630 if constexpr ( useBitVector )
631 if ( flatBitVector[flatRowIndex] or flatBitVector[flatColIndex] )
636 auto rowIdx = useBitVector ? subIndices[flatRowIndex] : flatRowIndex;
637 auto colIdx = useBitVector ? subIndices[flatColIndex] : flatColIndex;
640 auto colStart = umfpackMatrix_.colstart[colIdx];
642 auto colPos = colPosition[colIdx];
644 umfpackMatrix_.rowindex[ colStart + colPos ] = rowIdx;
645 umfpackMatrix_.values[ colStart + colPos ] = entry;
647 colPosition[colIdx]++;
658 void setSubMatrix(
const Matrix& _mat,
const S& rowIndexSet)
660 if ((umfpackMatrix_.N() + umfpackMatrix_.M() > 0) || matrixIsLoaded_)
663 if (umfpackMatrix_.N() + umfpackMatrix_.M() + umfpackMatrix_.nonzeroes() != 0)
664 umfpackMatrix_.free();
666 umfpackMatrix_.setSize(rowIndexSet.size()*MatrixDimension<Matrix>::rowdim(_mat) / _mat.N(),
667 rowIndexSet.size()*MatrixDimension<Matrix>::coldim(_mat) / _mat.M());
668 ISTL::Impl::BCCSMatrixInitializer<Matrix, SuiteSparse_long> initializer(umfpackMatrix_);
670 copyToBCCSMatrix(initializer, ISTL::Impl::MatrixRowSubset<
Matrix,std::set<std::size_t> >(_mat,rowIndexSet));
687 UMF_Control[UMFPACK_PRL] = 1;
689 UMF_Control[UMFPACK_PRL] = 2;
691 UMF_Control[UMFPACK_PRL] = 4;
709 return umfpackMatrix_;
718 if (!matrixIsLoaded_)
720 Caller::free_symbolic(&UMF_Symbolic);
721 umfpackMatrix_.free();
723 Caller::free_numeric(&UMF_Numeric);
724 matrixIsLoaded_ =
false;
727 const char* name() {
return "UMFPACK"; }
730 typedef typename Dune::UMFPackMethodChooser<T> Caller;
732 template<
class Mat,
class X,
class TM,
class TD,
class T1>
733 friend class SeqOverlappingSchwarz;
734 friend struct SeqOverlappingSchwarzAssemblerHelper<
UMFPack<
Matrix>,true>;
739 double UMF_Decomposition_Info[UMFPACK_INFO];
740 Caller::symbolic(
static_cast<SuiteSparse_long
>(umfpackMatrix_.N()),
741 static_cast<SuiteSparse_long
>(umfpackMatrix_.N()),
742 umfpackMatrix_.getColStart(),
743 umfpackMatrix_.getRowIndex(),
744 reinterpret_cast<double*
>(umfpackMatrix_.getValues()),
747 UMF_Decomposition_Info);
748 Caller::numeric(umfpackMatrix_.getColStart(),
749 umfpackMatrix_.getRowIndex(),
750 reinterpret_cast<double*
>(umfpackMatrix_.getValues()),
754 UMF_Decomposition_Info);
755 Caller::report_status(UMF_Control,UMF_Decomposition_Info[UMFPACK_STATUS]);
758 std::cout <<
"[UMFPack Decomposition]" << std::endl;
759 std::cout <<
"Wallclock Time taken: " << UMF_Decomposition_Info[UMFPACK_NUMERIC_WALLTIME] <<
" (CPU Time: " << UMF_Decomposition_Info[UMFPACK_NUMERIC_TIME] <<
")" << std::endl;
760 std::cout <<
"Flops taken: " << UMF_Decomposition_Info[UMFPACK_FLOPS] << std::endl;
761 std::cout <<
"Peak Memory Usage: " << UMF_Decomposition_Info[UMFPACK_PEAK_MEMORY]*UMF_Decomposition_Info[UMFPACK_SIZE_OF_UNIT] <<
" bytes" << std::endl;
762 std::cout <<
"Condition number estimate: " << 1./UMF_Decomposition_Info[UMFPACK_RCOND] << std::endl;
763 std::cout <<
"Numbers of non-zeroes in decomposition: L: " << UMF_Decomposition_Info[UMFPACK_LNZ] <<
" U: " << UMF_Decomposition_Info[UMFPACK_UNZ] << std::endl;
767 Caller::report_info(UMF_Control,UMF_Decomposition_Info);
771 void printOnApply(
double* UMF_Info)
773 Caller::report_status(UMF_Control,UMF_Info[UMFPACK_STATUS]);
776 std::cout <<
"[UMFPack Solve]" << std::endl;
777 std::cout <<
"Wallclock Time: " << UMF_Info[UMFPACK_SOLVE_WALLTIME] <<
" (CPU Time: " << UMF_Info[UMFPACK_SOLVE_TIME] <<
")" << std::endl;
778 std::cout <<
"Flops Taken: " << UMF_Info[UMFPACK_SOLVE_FLOPS] << std::endl;
779 std::cout <<
"Iterative Refinement steps taken: " << UMF_Info[UMFPACK_IR_TAKEN] << std::endl;
780 std::cout <<
"Error Estimate: " << UMF_Info[UMFPACK_OMEGA1] <<
" resp. " << UMF_Info[UMFPACK_OMEGA2] << std::endl;
785 bool matrixIsLoaded_;
789 double UMF_Control[UMFPACK_CONTROL];
792 template<
typename T,
typename A,
int n,
int m>
793 struct IsDirectSolver<UMFPack<BCRSMatrix<FieldMatrix<T,n,m>,A> > >
798 template<
typename T,
typename A>
799 struct StoresColumnCompressed<UMFPack<BCRSMatrix<T,A> > >
801 enum { value =
true };
804 namespace UMFPackImpl {
805 template<
class OpTraits,
class=
void>
struct isValidBlock : std::false_type{};
806 template<
class OpTraits>
struct isValidBlock<OpTraits,
808 std::is_same_v<Impl::UMFPackDomainType<typename OpTraits::matrix_type>, typename OpTraits::domain_type>
809 && std::is_same_v<Impl::UMFPackRangeType<typename OpTraits::matrix_type>, typename OpTraits::range_type>
810 && std::is_same_v<typename FieldTraits<typename OpTraits::domain_type::field_type>::real_type, double>
811 && std::is_same_v<typename FieldTraits<typename OpTraits::range_type::field_type>::real_type, double>
812 >> : std::true_type {};
814 DUNE_REGISTER_SOLVER(
"umfpack",
816 -> std::shared_ptr<
typename decltype(opTraits)::solver_type>
818 using OpTraits =
decltype(opTraits);
820 if constexpr (OpTraits::isParallel){
821 if(opTraits.getCommOrThrow(op).communicator().size() > 1)
824 if constexpr (OpTraits::isAssembled){
825 using M =
typename OpTraits::matrix_type;
829 if constexpr (UMFPackImpl::isValidBlock<OpTraits>::value) {
830 const auto& A = opTraits.getAssembledOpOrThrow(op);
831 const M& mat = A->getmat();
832 int verbose = config.
get(
"verbose", 0);
833 return std::make_shared<Dune::UMFPack<M>>(mat,verbose);
837 "Unsupported Type in UMFPack (only double and std::complex<double> supported)");
Base class for Dune-Exceptions.
Definition: exceptions.hh:96
derive error class from the base class in common
Definition: istlexception.hh:19
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:373
Abstract base class for all solvers.
Definition: solver.hh:101
Hierarchical structure of string parameters.
Definition: parametertree.hh:37
std::string get(const std::string &key, const std::string &defaultValue) const
get value as string
Definition: parametertree.cc:188
Default exception class for range errors.
Definition: exceptions.hh:346
The UMFPack direct sparse solver.
Definition: umfpack.hh:265
A few common exception classes.
Implementation of the BCRSMatrix class.
A dynamic dense block matrix class.
Implementations of the inverse operator interface.
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.
typename Impl::voider< Types... >::type void_t
Is void for all valid input types. The workhorse for C++11 SFINAE-techniques.
Definition: typetraits.hh:40
#define DUNE_THROW(E,...)
Definition: exceptions.hh:312
constexpr auto max
Function object that returns the greater of the given values.
Definition: hybridutilities.hh:485
void free()
free allocated space.
Definition: umfpack.hh:716
UMFPack(const Matrix &mat_, const ParameterTree &config)
Construct a solver object from a matrix.
Definition: umfpack.hh:334
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:358
Impl::UMFPackRangeType< M > range_type
The type of the range of the solver.
Definition: umfpack.hh:281
UMFPack()
default constructor
Definition: umfpack.hh:340
Impl::UMFPackDomainType< M > domain_type
The type of the domain of the solver.
Definition: umfpack.hh:279
void apply(T *x, T *b)
additional apply method with c-arrays in analogy to superlu
Definition: umfpack.hh:473
SolverCategory::Category category() const override
Category of the solver (see SolverCategory::Category)
Definition: umfpack.hh:284
void setVerbosity(int v)
sets the verbosity level for the UMFPack solver
Definition: umfpack.hh:682
UMFPack(const char *file, int verbose=0)
try loading a decomposition from file
Definition: umfpack.hh:385
void apply(domain_type &x, range_type &b, double reduction, InverseOperatorResult &res) override
apply inverse operator, with given convergence criteria.
Definition: umfpack.hh:461
void setMatrix(const Matrix &matrix, const BitVector &bitVector={})
Initialize data from given matrix.
Definition: umfpack.hh:527
void saveDecomposition(const char *file)
saves a decomposition to a file
Definition: umfpack.hh:510
UMFPackMatrix & getInternalMatrix()
Return the column compress matrix from UMFPack.
Definition: umfpack.hh:707
UMFPack(const Matrix &matrix, int verbose=0)
Construct a solver object from a matrix.
Definition: umfpack.hh:297
ISTL::Impl::BCCSMatrixInitializer< M, size_type > MatrixInitializer
Type of an associated initializer class.
Definition: umfpack.hh:277
ISTL::Impl::BCCSMatrix< typename Matrix::field_type, size_type > UMFPackMatrix
The corresponding (scalar) UMFPack matrix type.
Definition: umfpack.hh:275
void setOption(unsigned int option, double value)
Set UMFPack-specific options.
Definition: umfpack.hh:499
M Matrix
The matrix type.
Definition: umfpack.hh:272
void apply(domain_type &x, range_type &b, InverseOperatorResult &res) override
Apply inverse operator,.
Definition: umfpack.hh:410
UMFPack(const Matrix &matrix, int verbose, bool)
Constructor for compatibility with SuperLU standard constructor.
Definition: umfpack.hh:315
void * getFactorization()
Return the matrix factorization.
Definition: umfpack.hh:698
Dune namespace.
Definition: alignedallocator.hh:13
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
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
constexpr auto get(std::integer_sequence< T, II... >, std::integral_constant< std::size_t, pos >={})
Return the entry at position pos of the given sequence.
Definition: integersequence.hh:22
Templates characterizing the type of a solver.
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
Category
Definition: solvercategory.hh:23