Dune Core Modules (2.6.0)

spqr.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_SPQR_HH
4#define DUNE_ISTL_SPQR_HH
5
6#if HAVE_SUITESPARSE_SPQR || defined DOXYGEN
7
8#include <complex>
9#include <type_traits>
10
11#include <SuiteSparseQR.hpp>
12
14#include <dune/common/unused.hh>
15
16#include <dune/istl/colcompmatrix.hh>
17#include <dune/istl/solvers.hh>
19
20namespace Dune {
32 // forward declarations
33 template<class M, class T, class TM, class TD, class TA>
34 class SeqOverlappingSchwarz;
35
36 template<class T, bool tag>
37 struct SeqOverlappingSchwarzAssemblerHelper;
38
44 template<class Matrix>
45 class SPQR
46 {};
47
61 template<typename T, typename A, int n, int m>
62 class SPQR<BCRSMatrix<FieldMatrix<T,n,m>,A > >
63 : public InverseOperator<BlockVector<FieldVector<T,m>, typename A::template rebind<FieldVector<T,m> >::other>,
64 BlockVector<FieldVector<T,n>, typename A::template rebind<FieldVector<T,n> >::other> >
65 {
66 public:
75 typedef Dune::BlockVector<FieldVector<T,m>, typename A::template rebind<FieldVector<T,m> >::other> domain_type;
77 typedef Dune::BlockVector<FieldVector<T,n>, typename A::template rebind<FieldVector<T,n> >::other> range_type;
78
81 {
82 return SolverCategory::Category::sequential;
83 }
84
93 SPQR(const Matrix& matrix, int verbose=0) : matrixIsLoaded_(false), verbose_(verbose)
94 {
95 //check whether T is a supported type
96 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
97 "Unsupported Type in SPQR (only double and std::complex<double> supported)");
98 cc_ = new cholmod_common();
99 cholmod_l_start(cc_);
100 setMatrix(matrix);
101 }
102
111 SPQR(const Matrix& matrix, int verbose, bool) : matrixIsLoaded_(false), verbose_(verbose)
112 {
113 //check whether T is a supported type
114 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
115 "Unsupported Type in SPQR (only double and std::complex<double> supported)");
116 cc_ = new cholmod_common();
117 cholmod_l_start(cc_);
118 setMatrix(matrix);
119 }
120
122 SPQR() : matrixIsLoaded_(false), verbose_(0)
123 {
124 //check whether T is a supported type
125 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
126 "Unsupported Type in SPQR (only double and std::complex<double> supported)");
127 cc_ = new cholmod_common();
128 cholmod_l_start(cc_);
129 }
130
132 virtual ~SPQR()
133 {
134 if ((spqrMatrix_.N() + spqrMatrix_.M() > 0) || matrixIsLoaded_)
135 free();
136 cholmod_l_finish(cc_);
137 }
138
141 {
142 const std::size_t dimMat(spqrMatrix_.N());
143 // fill B
144 for(std::size_t k = 0; k != dimMat; ++k)
145 (static_cast<T*>(B_->x))[k] = b[k];
146 cholmod_dense* BTemp = B_;
147 B_ = SuiteSparseQR_qmult<T>(0, spqrfactorization_, B_, cc_);
148 cholmod_dense* X = SuiteSparseQR_solve<T>(1, spqrfactorization_, B_, cc_);
149 cholmod_l_free_dense(&BTemp, cc_);
150 // fill x
151 for(std::size_t k = 0; k != dimMat; ++k)
152 x [k] = (static_cast<T*>(X->x))[k];
153 cholmod_l_free_dense(&X, cc_);
154 // this is a direct solver
155 res.iterations = 1;
156 res.converged = true;
157 if(verbose_ > 0)
158 {
159 std::cout<<std::endl<<"Solving with SuiteSparseQR"<<std::endl;
160 std::cout<<"Flops Taken: "<<cc_->SPQR_flopcount<<std::endl;
161 std::cout<<"Analysis Time: "<<cc_->SPQR_analyze_time<<" s"<<std::endl;
162 std::cout<<"Factorize Time: "<<cc_->SPQR_factorize_time<<" s"<<std::endl;
163 std::cout<<"Backsolve Time: "<<cc_->SPQR_solve_time<<" s"<<std::endl;
164 std::cout<<"Peak Memory Usage: "<<cc_->memory_usage<<" bytes"<<std::endl;
165 std::cout<<"Rank Estimate: "<<cc_->SPQR_istat[4]<<std::endl<<std::endl;
166 }
167 }
168
170 virtual void apply (domain_type& x, range_type& b, double reduction, InverseOperatorResult& res)
171 {
172 DUNE_UNUSED_PARAMETER(reduction);
173 apply(x, b, res);
174 }
175
176 void setOption(unsigned int option, double value)
177 {
178 DUNE_UNUSED_PARAMETER(option);
180 }
181
183 void setMatrix(const Matrix& matrix)
184 {
185 if ((spqrMatrix_.N() + spqrMatrix_.M() > 0) || matrixIsLoaded_)
186 free();
187 spqrMatrix_ = matrix;
188 decompose();
189 }
190
191 template<class S>
192 void setSubMatrix(const Matrix& matrix, const S& rowIndexSet)
193 {
194 if ((spqrMatrix_.N() + spqrMatrix_.M() > 0) || matrixIsLoaded_)
195 free();
196 spqrMatrix_.setMatrix(matrix,rowIndexSet);
197 decompose();
198 }
199
204 inline void setVerbosity(int v)
205 {
206 verbose_=v;
207 }
208
213 inline SuiteSparseQR_factorization<T>* getFactorization()
214 {
215 return spqrfactorization_;
216 }
217
223 {
224 return spqrMatrix_;
225 }
226
231 void free()
232 {
233 cholmod_l_free_sparse(&A_, cc_);
234 cholmod_l_free_dense(&B_, cc_);
235 SuiteSparseQR_free<T>(&spqrfactorization_, cc_);
236 spqrMatrix_.free();
237 matrixIsLoaded_ = false;
238 }
239
241 inline const char* name()
242 {
243 return "SPQR";
244 }
245
246 private:
247 template<class M,class X, class TM, class TD, class T1>
248 friend class SeqOverlappingSchwarz;
249
250 friend struct SeqOverlappingSchwarzAssemblerHelper<SPQR<Matrix>,true>;
251
253 void decompose()
254 {
255 const std::size_t dimMat(spqrMatrix_.N());
256 const std::size_t nnz(spqrMatrix_.getColStart()[dimMat]);
257 // initialise the matrix A (sorted, packed, unsymmetric, real entries)
258 A_ = cholmod_l_allocate_sparse(dimMat, dimMat, nnz, 1, 1, 0, 1, cc_);
259 // copy all the entries of Ap, Ai, Ax
260 for(std::size_t k = 0; k != (dimMat+1); ++k)
261 (static_cast<long int *>(A_->p))[k] = spqrMatrix_.getColStart()[k];
262 for(std::size_t k = 0; k != nnz; ++k)
263 {
264 (static_cast<long int*>(A_->i))[k] = spqrMatrix_.getRowIndex()[k];
265 (static_cast<T*>(A_->x))[k] = spqrMatrix_.getValues()[k];
266 }
267 // initialise the vector B
268 B_ = cholmod_l_allocate_dense(dimMat, 1, dimMat, A_->xtype, cc_);
269 // compute factorization of A
270 spqrfactorization_=SuiteSparseQR_factorize<T>(SPQR_ORDERING_DEFAULT,SPQR_DEFAULT_TOL,A_,cc_);
271 }
272
273 SPQRMatrix spqrMatrix_;
274 bool matrixIsLoaded_;
275 int verbose_;
276 cholmod_common* cc_;
277 cholmod_sparse* A_;
278 cholmod_dense* B_;
279 SuiteSparseQR_factorization<T>* spqrfactorization_;
280 };
281
282 template<typename T, typename A, int n, int m>
283 struct IsDirectSolver<SPQR<BCRSMatrix<FieldMatrix<T,n,m>,A> > >
284 {
285 enum {value = true};
286 };
287
288 template<typename T, typename A, int n, int m>
289 struct StoresColumnCompressed<SPQR<BCRSMatrix<FieldMatrix<T,n,m>,A> > >
290 {
291 enum {value = true};
292 };
293
294}
295
296#endif //HAVE_SUITESPARSE_SPQR
297#endif //DUNE_ISTL_SPQR_HH
A sparse block matrix with compressed row storage.
Definition: bcrsmatrix.hh:423
A vector of blocks with memory management.
Definition: bvector.hh:317
A dense n x m matrix.
Definition: fmatrix.hh:68
Abstract base class for all solvers.
Definition: solver.hh:91
A generic dynamic dense matrix.
Definition: matrix.hh:555
Use the SPQR package to directly solve linear systems – empty default class.
Definition: spqr.hh:46
Sequential overlapping Schwarz preconditioner.
Definition: overlappingschwarz.hh:742
A few common exception classes.
decltype(auto) apply(F &&f, ArgTuple &&args)
Apply function with arguments given as tuple.
Definition: apply.hh:58
#define DUNE_UNUSED_PARAMETER(parm)
A macro to mark intentionally unused function parameters with.
Definition: unused.hh:25
virtual ~SPQR()
Destructor.
Definition: spqr.hh:132
SPQR(const Matrix &matrix, int verbose, bool)
Constructor for compatibility with SuperLU standard constructor.
Definition: spqr.hh:111
virtual SolverCategory::Category category() const
Category of the solver (see SolverCategory::Category)
Definition: spqr.hh:80
SPQRMatrix & getInternalMatrix()
Return the column coppressed matrix.
Definition: spqr.hh:222
void setMatrix(const Matrix &matrix)
Initialize data from given matrix.
Definition: spqr.hh:183
SPQR()
Default constructor.
Definition: spqr.hh:122
const char * name()
Get method name.
Definition: spqr.hh:241
SuiteSparseQR_factorization< T > * getFactorization()
Return the matrix factorization.
Definition: spqr.hh:213
void setVerbosity(int v)
Sets the verbosity level for the solver.
Definition: spqr.hh:204
Dune::ColCompMatrix< Matrix > SPQRMatrix
The corresponding SuperLU Matrix type.
Definition: spqr.hh:71
Dune::BlockVector< FieldVector< T, m >, typename A::template rebind< FieldVector< T, m > >::other > domain_type
The type of the domain of the solver.
Definition: spqr.hh:75
virtual void apply(domain_type &x, range_type &b, double reduction, InverseOperatorResult &res)
apply inverse operator, with given convergence criteria.
Definition: spqr.hh:170
void free()
Free allocated space.
Definition: spqr.hh:231
Dune::BlockVector< FieldVector< T, n >, typename A::template rebind< FieldVector< T, n > >::other > range_type
The type of the range of the solver.
Definition: spqr.hh:77
ColCompMatrixInitializer< BCRSMatrix< FieldMatrix< T, n, m >, A > > MatrixInitializer
Type of an associated initializer class.
Definition: spqr.hh:73
virtual void apply(domain_type &x, range_type &b, InverseOperatorResult &res)
Apply inverse operator,.
Definition: spqr.hh:140
SPQR(const Matrix &matrix, int verbose=0)
Construct a solver object from a BCRSMatrix.
Definition: spqr.hh:93
Dune namespace.
Definition: alignedallocator.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:41
int iterations
Number of iterations.
Definition: solver.hh:59
bool converged
True if convergence criterion has been met.
Definition: solver.hh:65
Category
Definition: solvercategory.hh:21
Definition of the DUNE_UNUSED macro for the case that config.h is not available.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 27, 23:30, 2024)