Loading...
Searching...
No Matches
spqr.hh
Go to the documentation of this file.
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_ISTL_SPQR_HH
#define DUNE_ISTL_SPQR_HH
#if HAVE_SUITESPARSE_SPQR || defined DOXYGEN
#include <complex>
#include <type_traits>
#include <SuiteSparseQR.hpp>
#include <dune/common/exceptions.hh>
#include <dune/istl/bccsmatrixinitializer.hh>
#include <dune/istl/solvers.hh>
#include <dune/istl/solvertype.hh>
#include <dune/istl/solverfactory.hh>
namespace Dune {
// forward declarations
template<class M, class T, class TM, class TD, class TA>
class SeqOverlappingSchwarz;
template<class T, bool tag>
struct SeqOverlappingSchwarzAssemblerHelper;
template<class Matrix>
class SPQR
{};
template<typename T, typename A, int n, int m>
class SPQR<BCRSMatrix<FieldMatrix<T,n,m>,A > >
: public InverseOperator<BlockVector<FieldVector<T,m>, typename std::allocator_traits<A>::template rebind_alloc<FieldVector<T,m> > >,
BlockVector<FieldVector<T,n>, typename std::allocator_traits<A>::template rebind_alloc<FieldVector<T,n> > > >
{
public:
typedef Dune::BCRSMatrix<FieldMatrix<T,n,m>,A> Matrix;
typedef Dune::BCRSMatrix<FieldMatrix<T,n,m>,A> matrix_type;
typedef ISTL::Impl::BCCSMatrix<T,int> SPQRMatrix;
typedef ISTL::Impl::BCCSMatrixInitializer<BCRSMatrix<FieldMatrix<T,n,m>,A>, int> MatrixInitializer;
typedef Dune::BlockVector<FieldVector<T,m>, typename std::allocator_traits<A>::template rebind_alloc<FieldVector<T,m> > > domain_type;
typedef Dune::BlockVector<FieldVector<T,n>, typename std::allocator_traits<A>::template rebind_alloc<FieldVector<T,n> > > range_type;
SolverCategory::Category category() const override
{
return SolverCategory::Category::sequential;
}
SPQR(const Matrix& matrix, int verbose=0) : matrixIsLoaded_(false), verbose_(verbose)
{
//check whether T is a supported type
static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
"Unsupported Type in SPQR (only double and std::complex<double> supported)");
cc_ = new cholmod_common();
cholmod_l_start(cc_);
setMatrix(matrix);
}
SPQR(const Matrix& matrix, int verbose, bool) : matrixIsLoaded_(false), verbose_(verbose)
{
//check whether T is a supported type
static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
"Unsupported Type in SPQR (only double and std::complex<double> supported)");
cc_ = new cholmod_common();
cholmod_l_start(cc_);
setMatrix(matrix);
}
SPQR(const Matrix& matrix, const ParameterTree& config)
: SPQR(matrix, config.get<int>("verbose", 0))
{}
SPQR() : matrixIsLoaded_(false), verbose_(0)
{
//check whether T is a supported type
static_assert((std::is_same<T,double>::value) || (std::is_same<T,std::complex<double> >::value),
"Unsupported Type in SPQR (only double and std::complex<double> supported)");
cc_ = new cholmod_common();
cholmod_l_start(cc_);
}
virtual ~SPQR()
{
if ((spqrMatrix_.N() + spqrMatrix_.M() > 0) || matrixIsLoaded_)
free();
cholmod_l_finish(cc_);
}
void apply(domain_type& x, range_type& b, InverseOperatorResult& res) override
{
const std::size_t numRows(spqrMatrix_.N());
// fill B
for(std::size_t k = 0; k != numRows/n; ++k)
for (int l = 0; l < n; ++l)
(static_cast<T*>(B_->x))[n*k+l] = b[k][l];
cholmod_dense* BTemp = B_;
B_ = SuiteSparseQR_qmult<T>(0, spqrfactorization_, B_, cc_);
cholmod_dense* X = SuiteSparseQR_solve<T>(1, spqrfactorization_, B_, cc_);
cholmod_l_free_dense(&BTemp, cc_);
const std::size_t numCols(spqrMatrix_.M());
// fill x
for(std::size_t k = 0; k != numCols/m; ++k)
for (int l = 0; l < m; ++l)
x[k][l] = (static_cast<T*>(X->x))[m*k+l];
cholmod_l_free_dense(&X, cc_);
// this is a direct solver
res.iterations = 1;
res.converged = true;
if(verbose_ > 0)
{
std::cout<<std::endl<<"Solving with SuiteSparseQR"<<std::endl;
std::cout<<"Flops Taken: "<<cc_->SPQR_flopcount<<std::endl;
std::cout<<"Analysis Time: "<<cc_->SPQR_analyze_time<<" s"<<std::endl;
std::cout<<"Factorize Time: "<<cc_->SPQR_factorize_time<<" s"<<std::endl;
std::cout<<"Backsolve Time: "<<cc_->SPQR_solve_time<<" s"<<std::endl;
std::cout<<"Peak Memory Usage: "<<cc_->memory_usage<<" bytes"<<std::endl;
std::cout<<"Rank Estimate: "<<cc_->SPQR_istat[4]<<std::endl<<std::endl;
}
}
void apply (domain_type& x, range_type& b, [[maybe_unused]] double reduction, InverseOperatorResult& res) override
{
apply(x, b, res);
}
void setOption([[maybe_unused]] unsigned int option, [[maybe_unused]] double value)
{}
void setMatrix(const Matrix& matrix)
{
if ((spqrMatrix_.N() + spqrMatrix_.M() > 0) || matrixIsLoaded_)
free();
if (spqrMatrix_.N() + spqrMatrix_.M() + spqrMatrix_.nonzeroes() != 0)
spqrMatrix_.free();
spqrMatrix_.setSize(MatrixDimension<Matrix>::rowdim(matrix),
MatrixDimension<Matrix>::coldim(matrix));
ISTL::Impl::BCCSMatrixInitializer<Matrix, int> initializer(spqrMatrix_);
copyToBCCSMatrix(initializer, matrix);
decompose();
}
template<class S>
void setSubMatrix(const Matrix& matrix, const S& rowIndexSet)
{
if ((spqrMatrix_.N() + spqrMatrix_.M() > 0) || matrixIsLoaded_)
free();
if (spqrMatrix_.N() + spqrMatrix_.M() + spqrMatrix_.nonzeroes() != 0)
spqrMatrix_.free();
spqrMatrix_.setSize(rowIndexSet.size()*MatrixDimension<Matrix>::rowdim(matrix) / matrix.N(),
rowIndexSet.size()*MatrixDimension<Matrix>::coldim(matrix) / matrix.M());
ISTL::Impl::BCCSMatrixInitializer<Matrix, int> initializer(spqrMatrix_);
copyToBCCSMatrix(initializer, ISTL::Impl::MatrixRowSubset<Matrix,std::set<std::size_t> >(matrix,rowIndexSet));
decompose();
}
inline void setVerbosity(int v)
{
verbose_=v;
}
inline SuiteSparseQR_factorization<T>* getFactorization()
{
return spqrfactorization_;
}
inline SPQRMatrix& getInternalMatrix()
{
return spqrMatrix_;
}
void free()
{
cholmod_l_free_sparse(&A_, cc_);
cholmod_l_free_dense(&B_, cc_);
SuiteSparseQR_free<T>(&spqrfactorization_, cc_);
spqrMatrix_.free();
matrixIsLoaded_ = false;
}
inline const char* name()
{
return "SPQR";
}
private:
template<class M,class X, class TM, class TD, class T1>
friend class SeqOverlappingSchwarz;
friend struct SeqOverlappingSchwarzAssemblerHelper<SPQR<Matrix>,true>;
void decompose()
{
const std::size_t nrows(spqrMatrix_.N());
const std::size_t ncols(spqrMatrix_.M());
const std::size_t nnz(spqrMatrix_.getColStart()[ncols]);
// initialise the matrix A (sorted, packed, unsymmetric, real entries)
A_ = cholmod_l_allocate_sparse(nrows, ncols, nnz, 1, 1, 0, 1, cc_);
// copy all the entries of Ap, Ai, Ax
for(std::size_t k = 0; k != (ncols+1); ++k)
(static_cast<long int *>(A_->p))[k] = spqrMatrix_.getColStart()[k];
for(std::size_t k = 0; k != nnz; ++k)
{
(static_cast<long int*>(A_->i))[k] = spqrMatrix_.getRowIndex()[k];
(static_cast<T*>(A_->x))[k] = spqrMatrix_.getValues()[k];
}
// initialise the vector B
B_ = cholmod_l_allocate_dense(nrows, 1, nrows, A_->xtype, cc_);
// compute factorization of A
spqrfactorization_=SuiteSparseQR_factorize<T>(SPQR_ORDERING_DEFAULT,SPQR_DEFAULT_TOL,A_,cc_);
}
SPQRMatrix spqrMatrix_;
bool matrixIsLoaded_;
int verbose_;
cholmod_common* cc_;
cholmod_sparse* A_;
cholmod_dense* B_;
SuiteSparseQR_factorization<T>* spqrfactorization_;
};
template<typename T, typename A>
struct IsDirectSolver<SPQR<BCRSMatrix<T,A> > >
{
enum {value = true};
};
template<typename T, typename A>
struct StoresColumnCompressed<SPQR<BCRSMatrix<T,A> > >
{
enum {value = true};
};
DUNE_REGISTER_SOLVER("spqr",
[](auto opTraits, const auto& op, const Dune::ParameterTree& config)
-> std::shared_ptr<typename decltype(opTraits)::solver_type>
{
using OpTraits = decltype(opTraits);
using M = typename OpTraits::matrix_type;
// works only for sequential operators
if constexpr (OpTraits::isParallel){
if(opTraits.getCommOrThrow(op).communicator().size() > 1)
DUNE_THROW(Dune::InvalidStateException, "SPQR works only for sequential operators.");
}
// check if SPQR<M>* is convertible to
// InverseOperator*. This allows only the explicit
// specialized variants of SPQR
// std::complex is temporary disabled, because it fails if libc++ is used
if constexpr (std::is_convertible_v<SPQR<M>*,
Dune::InverseOperator<typename OpTraits::domain_type,
typename OpTraits::range_type>*> &&
std::is_same_v<typename FieldTraits<M>::field_type, double>
){
const auto& A = opTraits.getAssembledOpOrThrow(op);
const M& mat = A->getmat();
int verbose = config.get("verbose", 0);
return std::make_shared<Dune::SPQR<M>>(mat,verbose);
}
DUNE_THROW(UnsupportedType,
"Unsupported Type in SPQR (only FieldMatrix<double,...> supported)");
});
} // end namespace Dune
#endif //HAVE_SUITESPARSE_SPQR
#endif //DUNE_ISTL_SPQR_HH
solverfactory.hh
solvers.hh
Implementations of the inverse operator interface.
solvertype.hh
Templates characterizing the type of a solver.
bccsmatrixinitializer.hh
DUNE_REGISTER_SOLVER
#define DUNE_REGISTER_SOLVER(name,...)
Definition solverregistry.hh:16
Dune::SPQR< BCRSMatrix< FieldMatrix< T, n, m >, A > >::matrix_type
Dune::BCRSMatrix< FieldMatrix< T, n, m >, A > matrix_type
Definition spqr.hh:71
Dune::SPQR< BCRSMatrix< FieldMatrix< T, n, m >, A > >::SeqOverlappingSchwarz
friend class SeqOverlappingSchwarz
Definition spqr.hh:282
Dune::SPQR< BCRSMatrix< FieldMatrix< T, n, m >, A > >::~SPQR
virtual ~SPQR()
Destructor.
Definition spqr.hh:147
Dune::SPQR< BCRSMatrix< FieldMatrix< T, n, m >, A > >::SPQR
SPQR(const Matrix &matrix, int verbose, bool)
Constructor for compatibility with SuperLU standard constructor.
Definition spqr.hh:113
Dune::SPQR< BCRSMatrix< FieldMatrix< T, n, m >, A > >::getInternalMatrix
SPQRMatrix & getInternalMatrix()
Return the column coppressed matrix.
Definition spqr.hh:256
Dune::SPQR< BCRSMatrix< FieldMatrix< T, n, m >, A > >::setMatrix
void setMatrix(const Matrix &matrix)
Initialize data from given matrix.
Definition spqr.hh:200
Dune::SPQR< BCRSMatrix< FieldMatrix< T, n, m >, A > >::apply
void apply(domain_type &x, range_type &b, InverseOperatorResult &res) override
Apply inverse operator,.
Definition spqr.hh:155
Dune::SPQR< BCRSMatrix< FieldMatrix< T, n, m >, A > >::MatrixInitializer
ISTL::Impl::BCCSMatrixInitializer< BCRSMatrix< FieldMatrix< T, n, m >, A >, int > MatrixInitializer
Type of an associated initializer class.
Definition spqr.hh:75
Dune::SPQR< BCRSMatrix< FieldMatrix< T, n, m >, A > >::Matrix
Dune::BCRSMatrix< FieldMatrix< T, n, m >, A > Matrix
The matrix type.
Definition spqr.hh:70
Dune::SPQR< BCRSMatrix< FieldMatrix< T, n, m >, A > >::category
SolverCategory::Category category() const override
Category of the solver (see SolverCategory::Category).
Definition spqr.hh:82
Dune::SPQR< BCRSMatrix< FieldMatrix< T, n, m >, A > >::SPQR
SPQR()
Default constructor.
Definition spqr.hh:137
Dune::SPQR< BCRSMatrix< FieldMatrix< T, n, m >, A > >::name
const char * name()
Get method name.
Definition spqr.hh:275
Dune::SPQR< BCRSMatrix< FieldMatrix< T, n, m >, A > >::getFactorization
SuiteSparseQR_factorization< T > * getFactorization()
Return the matrix factorization.
Definition spqr.hh:247
Dune::SPQR< BCRSMatrix< FieldMatrix< T, n, m >, A > >::setVerbosity
void setVerbosity(int v)
Sets the verbosity level for the solver.
Definition spqr.hh:238
Dune::SPQR< BCRSMatrix< FieldMatrix< T, n, m >, A > >::SPQRMatrix
ISTL::Impl::BCCSMatrix< T, int > SPQRMatrix
The corresponding SuperLU Matrix type.
Definition spqr.hh:73
Dune::SPQR< BCRSMatrix< FieldMatrix< T, n, m >, A > >::free
void free()
Free allocated space.
Definition spqr.hh:265
Dune::SPQR< BCRSMatrix< FieldMatrix< T, n, m >, A > >::SPQR
SPQR(const Matrix &matrix, const ParameterTree &config)
Constructs the SPQR solver.
Definition spqr.hh:132
Dune::SPQR< BCRSMatrix< FieldMatrix< T, n, m >, A > >::range_type
Dune::BlockVector< FieldVector< T, n >, typename std::allocator_traits< A >::template rebind_alloc< FieldVector< T, n > > > range_type
The type of the range of the solver.
Definition spqr.hh:79
Dune::SPQR< BCRSMatrix< FieldMatrix< T, n, m >, A > >::setSubMatrix
void setSubMatrix(const Matrix &matrix, const S &rowIndexSet)
Definition spqr.hh:217
Dune::SPQR< BCRSMatrix< FieldMatrix< T, n, m >, A > >::domain_type
Dune::BlockVector< FieldVector< T, m >, typename std::allocator_traits< A >::template rebind_alloc< FieldVector< T, m > > > domain_type
The type of the domain of the solver.
Definition spqr.hh:77
Dune::SPQR< BCRSMatrix< FieldMatrix< T, n, m >, A > >::setOption
void setOption(unsigned int option, double value)
Definition spqr.hh:196
Dune::SPQR< BCRSMatrix< FieldMatrix< T, n, m >, A > >::SPQR
SPQR(const Matrix &matrix, int verbose=0)
Construct a solver object from a BCRSMatrix.
Definition spqr.hh:95
Dune::SPQR< BCRSMatrix< FieldMatrix< T, n, m >, A > >::apply
void apply(domain_type &x, range_type &b, double reduction, InverseOperatorResult &res) override
apply inverse operator, with given convergence criteria.
Definition spqr.hh:191
Dune::IsDirectSolver< SPQR< BCRSMatrix< T, A > > >::value
@ value
Definition spqr.hh:324
Dune::StoresColumnCompressed< SPQR< BCRSMatrix< T, A > > >::value
@ value
Definition spqr.hh:330
Dune
Definition allocator.hh:11
Dune::get
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
Dune::MatrixDimension::coldim
static auto coldim(const M &)
Definition matrixutils.hh:219
Dune::MatrixDimension::rowdim
static auto rowdim(const M &)
Definition matrixutils.hh:214
Dune::BCRSMatrix
A sparse block matrix with compressed row storage.
Definition bcrsmatrix.hh:467
Dune::BCRSMatrix::M
size_type M() const
number of columns (counted in blocks)
Definition bcrsmatrix.hh:2010
Dune::BCRSMatrix::N
size_type N() const
number of rows (counted in blocks)
Definition bcrsmatrix.hh:2004
Dune::BlockVector
A vector of blocks with memory management.
Definition bvector.hh:392
Dune::SeqOverlappingSchwarz
Sequential overlapping Schwarz preconditioner.
Definition overlappingschwarz.hh:755
Dune::SeqOverlappingSchwarzAssemblerHelper
Definition overlappingschwarz.hh:694
Dune::FieldMatrix
Definition matrixutils.hh:27
Dune::InverseOperatorResult
Statistics about the application of an inverse operator.
Definition solver.hh:50
Dune::InverseOperatorResult::iterations
int iterations
Number of iterations.
Definition solver.hh:69
Dune::InverseOperatorResult::converged
bool converged
True if convergence criterion has been met.
Definition solver.hh:75
Dune::InverseOperator
Abstract base class for all solvers.
Definition solver.hh:101
Dune::SolverCategory::Category
Category
Definition solvercategory.hh:23
Dune::SolverCategory::sequential
@ sequential
Category for sequential solvers.
Definition solvercategory.hh:25
Dune::UnsupportedType
Definition solverregistry.hh:97
Dune::IsDirectSolver
Definition solvertype.hh:16
Dune::StoresColumnCompressed
Definition solvertype.hh:30
Dune::SPQR
Use the SPQR package to directly solve linear systems – empty default class.
Definition spqr.hh:48