Debian Codegraph @

dune/istl/spqr.hh

raw
1 // SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 // vi: set et ts=4 sw=2 sts=2:
5 #ifndef DUNE_ISTL_SPQR_HH
6 #define DUNE_ISTL_SPQR_HH
7
8 #if HAVE_SUITESPARSE_SPQR || defined DOXYGEN
9
10 #include <complex>
11 #include <type_traits>
12
13 #include <SuiteSparseQR.hpp>
14
15 #include <dune/common/exceptions.hh>
16
17 #include <dune/istl/bccsmatrixinitializer.hh>
18 #include <dune/istl/solvers.hh>
19 #include <dune/istl/solvertype.hh>
20 #include <dune/istl/solverfactory.hh>
21
22 namespace Dune {
23 /**
24 * @addtogroup ISTL
25 *
26 * @{
27 */
28 /**
29 * @file
30 * @author Marco Agnese, Andrea Sacconi
31 * @brief Class for using SPQR with ISTL matrices.
32 */
33
34 // forward declarations
35 template<class M, class T, class TM, class TD, class TA>
36 class SeqOverlappingSchwarz;
37
38 template<class T, bool tag>
39 struct SeqOverlappingSchwarzAssemblerHelper;
40
41 /** @brief Use the %SPQR package to directly solve linear systems -- empty default class
42 * @tparam Matrix the matrix type defining the system
43 * Details on SPQR can be found on
44 * http://www.cise.ufl.edu/research/sparse/spqr/
45 */
46 template<class Matrix>
47 class SPQR
48 {};
49
50 /** @brief The %SPQR direct sparse solver for matrices of type BCRSMatrix
51 *
52 * Specialization for the Dune::BCRSMatrix. %SPQR will always go double
53 * precision and supports complex numbers
54 * too (use std::complex<double> for that).
55 *
56 * \tparam T Number type. Only double and std::complex<double> is supported
57 * \tparam A STL-compatible allocator type
58 * \tparam n Number of rows in a matrix block
59 * \tparam m Number of columns in a matrix block
60 *
61 * \note This will only work if dune-istl has been configured to use SPQR
62 */
63 template<typename T, typename A, int n, int m>
64 class SPQR<BCRSMatrix<FieldMatrix<T,n,m>,A > >
65 : public InverseOperator<BlockVector<FieldVector<T,m>, typename std::allocator_traits<A>::template rebind_alloc<FieldVector<T,m> > >,
66 BlockVector<FieldVector<T,n>, typename std::allocator_traits<A>::template rebind_alloc<FieldVector<T,n> > > >
67 {
68 public:
69 /** @brief The matrix type. */
70 typedef Dune::BCRSMatrix<FieldMatrix<T,n,m>,A> Matrix;
71 typedef Dune::BCRSMatrix<FieldMatrix<T,n,m>,A> matrix_type;
72 /** @brief The corresponding SuperLU Matrix type.*/
73 typedef ISTL::Impl::BCCSMatrix<T,int> SPQRMatrix;
74 /** @brief Type of an associated initializer class. */
75 typedef ISTL::Impl::BCCSMatrixInitializer<BCRSMatrix<FieldMatrix<T,n,m>,A>, int> MatrixInitializer;
76 /** @brief The type of the domain of the solver. */
77 typedef Dune::BlockVector<FieldVector<T,m>, typename std::allocator_traits<A>::template rebind_alloc<FieldVector<T,m> > > domain_type;
78 /** @brief The type of the range of the solver. */
79 typedef Dune::BlockVector<FieldVector<T,n>, typename std::allocator_traits<A>::template rebind_alloc<FieldVector<T,n> > > range_type;
80
81 //! Category of the solver (see SolverCategory::Category)
82 SolverCategory::Category category() const override
83 {
84 return SolverCategory::Category::sequential;
85 }
86
87 /** @brief Construct a solver object from a BCRSMatrix
88 *
89 * This computes the matrix decomposition, and may take a long time
90 * (and use a lot of memory).
91 *
92 * @param matrix the matrix to solve for
93 * @param verbose 0 or 1, set the verbosity level, defaults to 0
94 */
95 SPQR(const Matrix& matrix, int verbose=0) : matrixIsLoaded_(false), verbose_(verbose)
96 {
97 //check whether T is a supported type
98 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
99 "Unsupported Type in SPQR (only double and std::complex<double> supported)");
100 cc_ = new cholmod_common();
101 cholmod_l_start(cc_);
102 setMatrix(matrix);
103 }
104
105 /** @brief Constructor for compatibility with SuperLU standard constructor
106 *
107 * This computes the matrix decomposition, and may take a long time
108 * (and use a lot of memory).
109 *
110 * @param matrix the matrix to solve for
111 * @param verbose 0 or 1, set the verbosity level, defaults to 0
112 */
113 SPQR(const Matrix& matrix, int verbose, bool) : matrixIsLoaded_(false), verbose_(verbose)
114 {
115 //check whether T is a supported type
116 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
117 "Unsupported Type in SPQR (only double and std::complex<double> supported)");
118 cc_ = new cholmod_common();
119 cholmod_l_start(cc_);
120 setMatrix(matrix);
121 }
122
123 /** @brief Constructs the SPQR solver.
124 *
125 * @param matrix The matrix of the system to solve.
126 * @param config ParameterTree containing solver parameters.
127 *
128 * ParameterTree Key | Meaning
129 * ------------------|------------
130 * verbose | The verbosity level. default=0
131 */
132 SPQR(const Matrix& matrix, const ParameterTree& config)
133 : SPQR(matrix, config.get<int>("verbose", 0))
134 {}
135
136 /** @brief Default constructor. */
137 SPQR() : matrixIsLoaded_(false), verbose_(0)
138 {
139 //check whether T is a supported type
140 static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
141 "Unsupported Type in SPQR (only double and std::complex<double> supported)");
142 cc_ = new cholmod_common();
143 cholmod_l_start(cc_);
144 }
145
146 /** @brief Destructor. */
147 virtual ~SPQR()
148 {
149 if ((spqrMatrix_.N() + spqrMatrix_.M() > 0) || matrixIsLoaded_)
150 free();
151 cholmod_l_finish(cc_);
152 }
153
154 /** \copydoc InverseOperator::apply(X&, Y&, InverseOperatorResult&) */
155 void apply(domain_type& x, range_type& b, InverseOperatorResult& res) override
156 {
157 const std::size_t numRows(spqrMatrix_.N());
158 // fill B
159 for(std::size_t k = 0; k != numRows/n; ++k)
160 for (int l = 0; l < n; ++l)
161 (static_cast<T*>(B_->x))[n*k+l] = b[k][l];
162
163 cholmod_dense* BTemp = B_;
164 B_ = SuiteSparseQR_qmult<T>(0, spqrfactorization_, B_, cc_);
165 cholmod_dense* X = SuiteSparseQR_solve<T>(1, spqrfactorization_, B_, cc_);
166 cholmod_l_free_dense(&BTemp, cc_);
167
168 const std::size_t numCols(spqrMatrix_.M());
169 // fill x
170 for(std::size_t k = 0; k != numCols/m; ++k)
171 for (int l = 0; l < m; ++l)
172 x[k][l] = (static_cast<T*>(X->x))[m*k+l];
173
174 cholmod_l_free_dense(&X, cc_);
175 // this is a direct solver
176 res.iterations = 1;
177 res.converged = true;
178 if(verbose_ > 0)
179 {
180 std::cout<<std::endl<<"Solving with SuiteSparseQR"<<std::endl;
181 std::cout<<"Flops Taken: "<<cc_->SPQR_flopcount<<std::endl;
182 std::cout<<"Analysis Time: "<<cc_->SPQR_analyze_time<<" s"<<std::endl;
183 std::cout<<"Factorize Time: "<<cc_->SPQR_factorize_time<<" s"<<std::endl;
184 std::cout<<"Backsolve Time: "<<cc_->SPQR_solve_time<<" s"<<std::endl;
185 std::cout<<"Peak Memory Usage: "<<cc_->memory_usage<<" bytes"<<std::endl;
186 std::cout<<"Rank Estimate: "<<cc_->SPQR_istat[4]<<std::endl<<std::endl;
187 }
188 }
189
190 /** \copydoc InverseOperator::apply(X&,Y&,double,InverseOperatorResult&) */
191 void apply (domain_type& x, range_type& b, [[maybe_unused]] double reduction, InverseOperatorResult& res) override
192 {
193 apply(x, b, res);
194 }
195
196 void setOption([[maybe_unused]] unsigned int option, [[maybe_unused]] double value)
197 {}
198
199 /** @brief Initialize data from given matrix. */
200 void setMatrix(const Matrix& matrix)
201 {
202 if ((spqrMatrix_.N() + spqrMatrix_.M() > 0) || matrixIsLoaded_)
203 free();
204
205 if (spqrMatrix_.N() + spqrMatrix_.M() + spqrMatrix_.nonzeroes() != 0)
206 spqrMatrix_.free();
207 spqrMatrix_.setSize(MatrixDimension<Matrix>::rowdim(matrix),
208 MatrixDimension<Matrix>::coldim(matrix));
209 ISTL::Impl::BCCSMatrixInitializer<Matrix, int> initializer(spqrMatrix_);
210
211 copyToBCCSMatrix(initializer, matrix);
212
213 decompose();
214 }
215
216 template<class S>
217 void setSubMatrix(const Matrix& matrix, const S& rowIndexSet)
218 {
219 if ((spqrMatrix_.N() + spqrMatrix_.M() > 0) || matrixIsLoaded_)
220 free();
221
222 if (spqrMatrix_.N() + spqrMatrix_.M() + spqrMatrix_.nonzeroes() != 0)
223 spqrMatrix_.free();
224
225 spqrMatrix_.setSize(rowIndexSet.size()*MatrixDimension<Matrix>::rowdim(matrix) / matrix.N(),
226 rowIndexSet.size()*MatrixDimension<Matrix>::coldim(matrix) / matrix.M());
227 ISTL::Impl::BCCSMatrixInitializer<Matrix, int> initializer(spqrMatrix_);
228
229 copyToBCCSMatrix(initializer, ISTL::Impl::MatrixRowSubset<Matrix,std::set<std::size_t> >(matrix,rowIndexSet));
230
231 decompose();
232 }
233
234 /**
235 * @brief Sets the verbosity level for the solver.
236 * @param v verbosity level: 0 only error messages, 1 a bit of statistics.
237 */
238 inline void setVerbosity(int v)
239 {
240 verbose_=v;
241 }
242
243 /**
244 * @brief Return the matrix factorization.
245 * @warning It is up to the user to keep consistency.
246 */
247 inline SuiteSparseQR_factorization<T>* getFactorization()
248 {
249 return spqrfactorization_;
250 }
251
252 /**
253 * @brief Return the column coppressed matrix.
254 * @warning It is up to the user to keep consistency.
255 */
256 inline SPQRMatrix& getInternalMatrix()
257 {
258 return spqrMatrix_;
259 }
260
261 /**
262 * @brief Free allocated space.
263 * @warning Later calling apply will result in an error.
264 */
265 void free()
266 {
267 cholmod_l_free_sparse(&A_, cc_);
268 cholmod_l_free_dense(&B_, cc_);
269 SuiteSparseQR_free<T>(&spqrfactorization_, cc_);
270 spqrMatrix_.free();
271 matrixIsLoaded_ = false;
272 }
273
274 /** @brief Get method name. */
275 inline const char* name()
276 {
277 return "SPQR";
278 }
279
280 private:
281 template<class M,class X, class TM, class TD, class T1>
282 friend class SeqOverlappingSchwarz;
283
284 friend struct SeqOverlappingSchwarzAssemblerHelper<SPQR<Matrix>,true>;
285
286 /** @brief Computes the QR decomposition. */
287 void decompose()
288 {
289 const std::size_t nrows(spqrMatrix_.N());
290 const std::size_t ncols(spqrMatrix_.M());
291 const std::size_t nnz(spqrMatrix_.getColStart()[ncols]);
292
293 // initialise the matrix A (sorted, packed, unsymmetric, real entries)
294 A_ = cholmod_l_allocate_sparse(nrows, ncols, nnz, 1, 1, 0, 1, cc_);
295
296 // copy all the entries of Ap, Ai, Ax
297 for(std::size_t k = 0; k != (ncols+1); ++k)
298 (static_cast<long int *>(A_->p))[k] = spqrMatrix_.getColStart()[k];
299
300 for(std::size_t k = 0; k != nnz; ++k)
301 {
302 (static_cast<long int*>(A_->i))[k] = spqrMatrix_.getRowIndex()[k];
303 (static_cast<T*>(A_->x))[k] = spqrMatrix_.getValues()[k];
304 }
305
306 // initialise the vector B
307 B_ = cholmod_l_allocate_dense(nrows, 1, nrows, A_->xtype, cc_);
308 // compute factorization of A
309 spqrfactorization_=SuiteSparseQR_factorize<T>(SPQR_ORDERING_DEFAULT,SPQR_DEFAULT_TOL,A_,cc_);
310 }
311
312 SPQRMatrix spqrMatrix_;
313 bool matrixIsLoaded_;
314 int verbose_;
315 cholmod_common* cc_;
316 cholmod_sparse* A_;
317 cholmod_dense* B_;
318 SuiteSparseQR_factorization<T>* spqrfactorization_;
319 };
320
321 template<typename T, typename A>
322 struct IsDirectSolver<SPQR<BCRSMatrix<T,A> > >
323 {
324 enum {value = true};
325 };
326
327 template<typename T, typename A>
328 struct StoresColumnCompressed<SPQR<BCRSMatrix<T,A> > >
329 {
330 enum {value = true};
331 };
332
333 DUNE_REGISTER_SOLVER("spqr",
334 [](auto opTraits, const auto& op, const Dune::ParameterTree& config)
335 -> std::shared_ptr<typename decltype(opTraits)::solver_type>
336 {
337 using OpTraits = decltype(opTraits);
338 using M = typename OpTraits::matrix_type;
339 // works only for sequential operators
340 if constexpr (OpTraits::isParallel){
341 if(opTraits.getCommOrThrow(op).communicator().size() > 1)
342 DUNE_THROW(Dune::InvalidStateException, "SPQR works only for sequential operators.");
343 }
344 // check if SPQR<M>* is convertible to
345 // InverseOperator*. This allows only the explicit
346 // specialized variants of SPQR
347 // std::complex is temporary disabled, because it fails if libc++ is used
348 if constexpr (std::is_convertible_v<SPQR<M>*,
349 Dune::InverseOperator<typename OpTraits::domain_type,
350 typename OpTraits::range_type>*> &&
351 std::is_same_v<typename FieldTraits<M>::field_type, double>
352 ){
353 const auto& A = opTraits.getAssembledOpOrThrow(op);
354 const M& mat = A->getmat();
355 int verbose = config.get("verbose", 0);
356 return std::make_shared<Dune::SPQR<M>>(mat,verbose);
357 }
358 DUNE_THROW(UnsupportedType,
359 "Unsupported Type in SPQR (only FieldMatrix<double,...> supported)");
360 });
361
362 } // end namespace Dune
363
364
365 #endif //HAVE_SUITESPARSE_SPQR
366 #endif //DUNE_ISTL_SPQR_HH