dune-istl  2.4
umfpack.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_ISTL_UMFPACK_HH
4 #define DUNE_ISTL_UMFPACK_HH
5 
6 #if HAVE_UMFPACK || defined DOXYGEN
7 
8 #include<complex>
9 #include<type_traits>
10 
11 #include<umfpack.h>
12 
13 #include<dune/common/exceptions.hh>
14 #include<dune/common/fmatrix.hh>
15 #include<dune/common/fvector.hh>
16 #include<dune/common/unused.hh>
18 #include<dune/istl/solvers.hh>
20 
21 #include"colcompmatrix.hh"
22 
23 
24 namespace Dune {
36  // FORWARD DECLARATIONS
37  template<class M, class T, class TM, class TD, class TA>
38  class SeqOverlappingSchwarz;
39 
40  template<class T, bool tag>
41  struct SeqOverlappingSchwarzAssemblerHelper;
42 
48  template<class Matrix>
49  class UMFPack
50  {};
51 
52  // wrapper class for C-Function Calls in the backend. Choose the right function namespace
53  // depending on the template parameter used.
54  template<typename T>
56  {};
57 
58  template<>
59  struct UMFPackMethodChooser<double>
60  {
61  template<typename... A>
62  static void defaults(A... args)
63  {
64  umfpack_di_defaults(args...);
65  }
66  template<typename... A>
67  static void free_numeric(A... args)
68  {
69  umfpack_di_free_numeric(args...);
70  }
71  template<typename... A>
72  static void free_symbolic(A... args)
73  {
74  umfpack_di_free_symbolic(args...);
75  }
76  template<typename... A>
77  static int load_numeric(A... args)
78  {
79  return umfpack_di_load_numeric(args...);
80  }
81  template<typename... A>
82  static void numeric(A... args)
83  {
84  umfpack_di_numeric(args...);
85  }
86  template<typename... A>
87  static void report_info(A... args)
88  {
89  umfpack_di_report_info(args...);
90  }
91  template<typename... A>
92  static void report_status(A... args)
93  {
94  umfpack_di_report_status(args...);
95  }
96  template<typename... A>
97  static int save_numeric(A... args)
98  {
99  return umfpack_di_save_numeric(args...);
100  }
101  template<typename... A>
102  static void solve(A... args)
103  {
104  umfpack_di_solve(args...);
105  }
106  template<typename... A>
107  static void symbolic(A... args)
108  {
109  umfpack_di_symbolic(args...);
110  }
111  };
112 
113  template<>
114  struct UMFPackMethodChooser<std::complex<double> >
115  {
116  template<typename... A>
117  static void defaults(A... args)
118  {
119  umfpack_zi_defaults(args...);
120  }
121  template<typename... A>
122  static void free_numeric(A... args)
123  {
124  umfpack_zi_free_numeric(args...);
125  }
126  template<typename... A>
127  static void free_symbolic(A... args)
128  {
129  umfpack_zi_free_symbolic(args...);
130  }
131  template<typename... A>
132  static int load_numeric(A... args)
133  {
134  return umfpack_zi_load_numeric(args...);
135  }
136  template<typename... A>
137  static void numeric(const int* cs, const int* ri, const double* val, A... args)
138  {
139  umfpack_zi_numeric(cs,ri,val,NULL,args...);
140  }
141  template<typename... A>
142  static void report_info(A... args)
143  {
144  umfpack_zi_report_info(args...);
145  }
146  template<typename... A>
147  static void report_status(A... args)
148  {
149  umfpack_zi_report_status(args...);
150  }
151  template<typename... A>
152  static int save_numeric(A... args)
153  {
154  return umfpack_zi_save_numeric(args...);
155  }
156  template<typename... A>
157  static void solve(int m, const int* cs, const int* ri, std::complex<double>* val, double* x, const double* b,A... args)
158  {
159  const double* cval = reinterpret_cast<const double*>(val);
160  umfpack_zi_solve(m,cs,ri,cval,NULL,x,NULL,b,NULL,args...);
161  }
162  template<typename... A>
163  static void symbolic(int m, int n, const int* cs, const int* ri, const double* val, A... args)
164  {
165  umfpack_zi_symbolic(m,n,cs,ri,val,NULL,args...);
166  }
167  };
168 
182  template<typename T, typename A, int n, int m>
183  class UMFPack<BCRSMatrix<FieldMatrix<T,n,m>,A > >
184  : public InverseOperator<
185  BlockVector<FieldVector<T,m>,
186  typename A::template rebind<FieldVector<T,m> >::other>,
187  BlockVector<FieldVector<T,n>,
188  typename A::template rebind<FieldVector<T,n> >::other> >
189  {
190  public:
193  typedef Dune::BCRSMatrix<FieldMatrix<T,n,m>,A> matrix_type;
199  typedef Dune::BlockVector<
200  FieldVector<T,m>,
201  typename A::template rebind<FieldVector<T,m> >::other> domain_type;
203  typedef Dune::BlockVector<
204  FieldVector<T,n>,
205  typename A::template rebind<FieldVector<T,n> >::other> range_type;
206 
215  UMFPack(const Matrix& matrix, int verbose=0) : matrixIsLoaded_(false)
216  {
217  //check whether T is a supported type
218  static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
219  "Unsupported Type in UMFPack (only double and std::complex<double> supported)");
220  Caller::defaults(UMF_Control);
221  setVerbosity(verbose);
222  setMatrix(matrix);
223  }
224 
233  UMFPack(const Matrix& matrix, int verbose, bool) : matrixIsLoaded_(false)
234  {
235  //check whether T is a supported type
236  static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
237  "Unsupported Type in UMFPack (only double and std::complex<double> supported)");
238  Caller::defaults(UMF_Control);
239  setVerbosity(verbose);
240  setMatrix(matrix);
241  }
242 
245  UMFPack() : matrixIsLoaded_(false), verbose(0)
246  {
247  //check whether T is a supported 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);
251  }
252 
263  UMFPack(const Matrix& mat_, const char* file, int verbose=0)
264  {
265  //check whether T is a supported type
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);
269  setVerbosity(verbose);
270  int errcode = Caller::load_numeric(&UMF_Numeric, const_cast<char*>(file));
271  if ((errcode == UMFPACK_ERROR_out_of_memory) || (errcode == UMFPACK_ERROR_file_IO))
272  {
273  matrixIsLoaded_ = false;
274  setMatrix(mat_);
275  saveDecomposition(file);
276  }
277  else
278  {
279  matrixIsLoaded_ = true;
280  std::cout << "UMFPack decomposition successfully loaded from " << file << std::endl;
281  }
282  }
283 
290  UMFPack(const char* file, int verbose=0)
291  {
292  //check whether T is a supported 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);
296  int errcode = Caller::load_numeric(&UMF_Numeric, const_cast<char*>(file));
297  if (errcode == UMFPACK_ERROR_out_of_memory)
298  DUNE_THROW(Dune::Exception, "ran out of memory while loading UMFPack decomposition");
299  if (errcode == UMFPACK_ERROR_file_IO)
300  DUNE_THROW(Dune::Exception, "IO error while loading UMFPack decomposition");
301  matrixIsLoaded_ = true;
302  std::cout << "UMFPack decomposition successfully loaded from " << file << std::endl;
303  setVerbosity(verbose);
304  }
305 
306  virtual ~UMFPack()
307  {
308  if ((umfpackMatrix_.N() + umfpackMatrix_.M() > 0) || (matrixIsLoaded_))
309  free();
310  }
311 
315  virtual void apply(domain_type& x, range_type& b, InverseOperatorResult& res)
316  {
317  double UMF_Apply_Info[UMFPACK_INFO];
318  Caller::solve(UMFPACK_A,
319  umfpackMatrix_.getColStart(),
320  umfpackMatrix_.getRowIndex(),
321  umfpackMatrix_.getValues(),
322  reinterpret_cast<double*>(&x[0]),
323  reinterpret_cast<double*>(&b[0]),
324  UMF_Numeric,
325  UMF_Control,
326  UMF_Apply_Info);
327 
328  //this is a direct solver
329  res.iterations = 1;
330  res.converged = true;
331  res.elapsed = UMF_Apply_Info[UMFPACK_SOLVE_WALLTIME];
332 
333  printOnApply(UMF_Apply_Info);
334  }
335 
339  virtual void apply (domain_type& x, range_type& b, double reduction, InverseOperatorResult& res)
340  {
341  DUNE_UNUSED_PARAMETER(reduction);
342  apply(x,b,res);
343  }
344 
350  void apply(T* x, T* b)
351  {
352  double UMF_Apply_Info[UMFPACK_INFO];
353  Caller::solve(UMFPACK_A,
354  umfpackMatrix_.getColStart(),
355  umfpackMatrix_.getRowIndex(),
356  umfpackMatrix_.getValues(),
357  x,
358  b,
359  UMF_Numeric,
360  UMF_Control,
361  UMF_Apply_Info);
362  printOnApply(UMF_Apply_Info);
363  }
364 
376  void setOption(unsigned int option, double value)
377  {
378  if (option >= UMFPACK_CONTROL)
379  DUNE_THROW(RangeError, "Requested non-existing UMFPack option");
380 
381  UMF_Control[option] = value;
382  }
383 
387  void saveDecomposition(const char* file)
388  {
389  int errcode = Caller::save_numeric(UMF_Numeric, const_cast<char*>(file));
390  if (errcode != UMFPACK_OK)
391  DUNE_THROW(Dune::Exception,"IO ERROR while trying to save UMFPack decomposition");
392  }
393 
395  void setMatrix(const Matrix& matrix)
396  {
397  if ((umfpackMatrix_.N() + umfpackMatrix_.M() > 0) || (matrixIsLoaded_))
398  free();
399  umfpackMatrix_ = matrix;
400  decompose();
401  }
402 
403  template<class S>
404  void setSubMatrix(const Matrix& _mat, const S& rowIndexSet)
405  {
406  if ((umfpackMatrix_.N() + umfpackMatrix_.M() > 0) || (matrixIsLoaded_))
407  free();
408  umfpackMatrix_.setMatrix(_mat,rowIndexSet);
409  decompose();
410  }
411 
419  void setVerbosity(int v)
420  {
421  verbose = v;
422  // set the verbosity level in UMFPack
423  if (verbose == 0)
424  UMF_Control[UMFPACK_PRL] = 1;
425  if (verbose == 1)
426  UMF_Control[UMFPACK_PRL] = 2;
427  if (verbose == 2)
428  UMF_Control[UMFPACK_PRL] = 4;
429  }
430 
435  void free()
436  {
437  if (!matrixIsLoaded_)
438  {
439  Caller::free_symbolic(&UMF_Symbolic);
440  umfpackMatrix_.free();
441  }
442  Caller::free_numeric(&UMF_Numeric);
443  matrixIsLoaded_ = false;
444  }
445 
446  const char* name() { return "UMFPACK"; }
447 
448  private:
449  typedef typename Dune::UMFPackMethodChooser<T> Caller;
450 
451  template<class M,class X, class TM, class TD, class T1>
452  friend class SeqOverlappingSchwarz;
453  friend struct SeqOverlappingSchwarzAssemblerHelper<UMFPack<Matrix>,true>;
454 
456  void decompose()
457  {
458  double UMF_Decomposition_Info[UMFPACK_INFO];
459  Caller::symbolic(static_cast<int>(umfpackMatrix_.N()),
460  static_cast<int>(umfpackMatrix_.N()),
461  umfpackMatrix_.getColStart(),
462  umfpackMatrix_.getRowIndex(),
463  reinterpret_cast<double*>(umfpackMatrix_.getValues()),
464  &UMF_Symbolic,
465  UMF_Control,
466  UMF_Decomposition_Info);
467  Caller::numeric(umfpackMatrix_.getColStart(),
468  umfpackMatrix_.getRowIndex(),
469  reinterpret_cast<double*>(umfpackMatrix_.getValues()),
470  UMF_Symbolic,
471  &UMF_Numeric,
472  UMF_Control,
473  UMF_Decomposition_Info);
474  Caller::report_status(UMF_Control,UMF_Decomposition_Info[UMFPACK_STATUS]);
475  if (verbose == 1)
476  {
477  std::cout << "[UMFPack Decomposition]" << std::endl;
478  std::cout << "Wallclock Time taken: " << UMF_Decomposition_Info[UMFPACK_NUMERIC_WALLTIME] << " (CPU Time: " << UMF_Decomposition_Info[UMFPACK_NUMERIC_TIME] << ")" << std::endl;
479  std::cout << "Flops taken: " << UMF_Decomposition_Info[UMFPACK_FLOPS] << std::endl;
480  std::cout << "Peak Memory Usage: " << UMF_Decomposition_Info[UMFPACK_PEAK_MEMORY]*UMF_Decomposition_Info[UMFPACK_SIZE_OF_UNIT] << " bytes" << std::endl;
481  std::cout << "Condition number estimate: " << 1./UMF_Decomposition_Info[UMFPACK_RCOND] << std::endl;
482  std::cout << "Numbers of non-zeroes in decomposition: L: " << UMF_Decomposition_Info[UMFPACK_LNZ] << " U: " << UMF_Decomposition_Info[UMFPACK_UNZ] << std::endl;
483  }
484  if (verbose == 2)
485  {
486  Caller::report_info(UMF_Control,UMF_Decomposition_Info);
487  }
488  }
489 
490  void printOnApply(double* UMF_Info)
491  {
492  Caller::report_status(UMF_Control,UMF_Info[UMFPACK_STATUS]);
493  if (verbose > 0)
494  {
495  std::cout << "[UMFPack Solve]" << std::endl;
496  std::cout << "Wallclock Time: " << UMF_Info[UMFPACK_SOLVE_WALLTIME] << " (CPU Time: " << UMF_Info[UMFPACK_SOLVE_TIME] << ")" << std::endl;
497  std::cout << "Flops Taken: " << UMF_Info[UMFPACK_SOLVE_FLOPS] << std::endl;
498  std::cout << "Iterative Refinement steps taken: " << UMF_Info[UMFPACK_IR_TAKEN] << std::endl;
499  std::cout << "Error Estimate: " << UMF_Info[UMFPACK_OMEGA1] << " resp. " << UMF_Info[UMFPACK_OMEGA2] << std::endl;
500  }
501  }
502 
503  UMFPackMatrix& getInternalMatrix() { return umfpackMatrix_; }
504 
505  UMFPackMatrix umfpackMatrix_;
506  bool matrixIsLoaded_;
507  int verbose;
508  void *UMF_Symbolic;
509  void *UMF_Numeric;
510  double UMF_Control[UMFPACK_CONTROL];
511  };
512 
513  template<typename T, typename A, int n, int m>
515  {
516  enum { value=true};
517  };
518 
519  template<typename T, typename A, int n, int m>
521  {
522  enum { value = true };
523  };
524 }
525 
526 #endif //HAVE_UMFPACK
527 
528 #endif //DUNE_ISTL_UMFPACK_HH
whether the solver internally uses column compressed storage
Definition: solvertype.hh:34
STL namespace.
Use the UMFPack package to directly solve linear systems – empty default class.
Definition: umfpack.hh:49
Dune::BCRSMatrix< FieldMatrix< T, n, m >, A > matrix_type
Definition: umfpack.hh:193
Implementation of the BCRSMatrix class.
virtual void apply(domain_type &x, range_type &b, double reduction, InverseOperatorResult &res)
apply inverse operator, with given convergence criteria.
Definition: umfpack.hh:339
void free()
free allocated space.
Definition: umfpack.hh:435
Definition: basearray.hh:19
static void free_numeric(A...args)
Definition: umfpack.hh:122
static void report_info(A...args)
Definition: umfpack.hh:142
ColCompMatrixInitializer< BCRSMatrix< FieldMatrix< T, n, m >, A > > MatrixInitializer
Type of an associated initializer class.
Definition: umfpack.hh:197
static void free_symbolic(A...args)
Definition: umfpack.hh:127
UMFPack(const Matrix &matrix, int verbose, bool)
Constructor for compatibility with SuperLU standard constructor.
Definition: umfpack.hh:233
Inititializer for the ColCompMatrix as needed by OverlappingSchwarz.
Definition: colcompmatrix.hh:153
UMFPack(const char *file, int verbose=0)
try loading a decomposition from file
Definition: umfpack.hh:290
A vector of blocks with memory management.
Definition: bvector.hh:252
double elapsed
Elapsed time in seconds.
Definition: solver.hh:62
int iterations
Number of iterations.
Definition: solver.hh:50
static void report_status(A...args)
Definition: umfpack.hh:92
static void defaults(A...args)
Definition: umfpack.hh:62
A sparse block matrix with compressed row storage.
Definition: bcrsmatrix.hh:412
Definition: overlappingschwarz.hh:690
Definition: umfpack.hh:55
static int save_numeric(A...args)
Definition: umfpack.hh:152
Matrix & A
Definition: matrixmatrix.hh:216
UMFPack(const Matrix &matrix, int verbose=0)
Construct a solver object from a BCRSMatrix.
Definition: umfpack.hh:215
static void numeric(const int *cs, const int *ri, const double *val, A...args)
Definition: umfpack.hh:137
static void report_status(A...args)
Definition: umfpack.hh:147
static void solve(A...args)
Definition: umfpack.hh:102
Whether this is a direct solver.
Definition: solvertype.hh:22
void saveDecomposition(const char *file)
saves a decomposition to a file
Definition: umfpack.hh:387
static void free_symbolic(A...args)
Definition: umfpack.hh:72
void setSubMatrix(const Matrix &_mat, const S &rowIndexSet)
Definition: umfpack.hh:404
virtual void apply(domain_type &x, range_type &b, InverseOperatorResult &res)
Apply inverse operator,.
Definition: umfpack.hh:315
void apply(T *x, T *b)
additional apply method with c-arrays in analogy to superlu
Definition: umfpack.hh:350
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:201
Definition: matrixutils.hh:24
Sequential overlapping Schwarz preconditioner.
Definition: colcompmatrix.hh:157
static int load_numeric(A...args)
Definition: umfpack.hh:77
void setOption(unsigned int option, double value)
Set UMFPack-specific options.
Definition: umfpack.hh:376
Abstract base class for all solvers.
Definition: solver.hh:79
static void solve(int m, const int *cs, const int *ri, std::complex< double > *val, double *x, const double *b, A...args)
Definition: umfpack.hh:157
void setVerbosity(int v)
sets the verbosity level for the UMFPack solver
Definition: umfpack.hh:419
Definition: solvertype.hh:13
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:205
static void report_info(A...args)
Definition: umfpack.hh:87
Dune::BCRSMatrix< FieldMatrix< T, n, m >, A > Matrix
The matrix type.
Definition: umfpack.hh:192
static void symbolic(int m, int n, const int *cs, const int *ri, const double *val, A...args)
Definition: umfpack.hh:163
Dune::ColCompMatrix< Matrix > UMFPackMatrix
The corresponding SuperLU Matrix type.
Definition: umfpack.hh:195
Templates characterizing the type of a solver.
Definition: solvertype.hh:27
void setMatrix(const Matrix &matrix)
Initialize data from given matrix.
Definition: umfpack.hh:395
virtual ~UMFPack()
Definition: umfpack.hh:306
UMFPack()
default constructor
Definition: umfpack.hh:245
static int load_numeric(A...args)
Definition: umfpack.hh:132
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:263
static void numeric(A...args)
Definition: umfpack.hh:82
bool converged
True if convergence criterion has been met.
Definition: solver.hh:56
static void symbolic(A...args)
Definition: umfpack.hh:107
Statistics about the application of an inverse operator.
Definition: solver.hh:31
static void defaults(A...args)
Definition: umfpack.hh:117
static void free_numeric(A...args)
Definition: umfpack.hh:67
static int save_numeric(A...args)
Definition: umfpack.hh:97
const char * name()
Definition: umfpack.hh:446
Implementations of the inverse operator interface.