KASKADE 7 development version
factorization.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2002-2024 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#ifndef FACTORIZATION_HH
14#define FACTORIZATION_HH
15
16#include <sstream>
17#include <tuple>
18#include <variant>
19#include <vector>
20
21#include <dune/grid/config.h>
22
23#include "utilities/enums.hh"
24#include "dune/istl/bcrsmatrix.hh"
25
27#include "linalg/factory.hh"
28#include "linalg/triplet.hh"
29
30namespace Kaskade
31{
32
33//---------------------------------------------------------------------
34
41template <class Scalar>
43{
44public:
48 typedef Scalar field_type;
49
51
55 struct Info
56 {
60 struct Mumps
61 {
63 int zeroPivots = 0;
65 };
66
76 virtual void solve(std::vector<field_type>& b) const
77 {
78 assert(b.size() >= size());
79 solve(&b[0]);
80 }
81
87 virtual void solve(field_type* b) const = 0;
88
94 virtual void solve(std::vector<Scalar> const& b, std::vector<Scalar>& x, bool transposed=false) const
95 {
96 assert(b.size() >= size());
97 if (x.size() < size())
98 x.resize(size());
99 solve(&b[0], &x[0]);
100 }
101
107 virtual void solve(Scalar const* b, Scalar* x, bool transposed=false) const = 0;
108
110
111 Factorization() = default;
112 Factorization(Options options_) : options(options_) {}
113 virtual ~Factorization() {}
114 void setVerbose(int verbose_) { options.verbosity = verbose_; }
115 int getVerbose() const { return options.verbosity; }
116
117 Info const& info() const { return info_; }
118
122 virtual size_t size() const = 0;
123
124protected:
127};
128
129// -------------------------------------------------------------------------------------------------
130
138template <class Scalar, class Index>
139void tripletToCompressedColumn(Index nRows, Index nCols,
140 size_t nNonZeros,
141 std::vector<Index> const& ridx,
142 std::vector<Index> const& cidx,
143 std::vector<Scalar> const& values,
144 std::vector<Index>& Ap, std::vector<Index>& Ai,
145 std::vector<Scalar>& Az);
146
147// -------------------------------------------------------------------------------------------------
148
155template <class Scalar>
156struct Creator<Factorization<Scalar>>
157{
158 // We can provide a matrix either with int or with long row and column indices.
159 // Concrete factorization classes are responsible for conversion, if needed.
160 using Matrices = std::variant<MatrixAsTriplet<Scalar,int> const*,
163 using Argument = std::tuple<Matrices, FactorizationOptions>;
164
170 virtual std::unique_ptr<Factorization<Scalar>> create(Argument a) const = 0;
171
172 virtual ~Creator() {}
173
174 static std::string lookupFailureHint(DirectType direct)
175 {
176 std::ostringstream hint;
177 hint << "Solver key " << direct << " not registered.\n"
178 << "Has the corresponding object file been linked in?\n"
179 << "Did you specify the right index type (int/long/unsigned long) in your matrix?\n";
180 return hint.str();
181 }
182};
183
184
185//---------------------------------------------------------------------
186
191template <class Scalar, class Index>
192std::unique_ptr<Factorization<Scalar>> getFactorization(DirectType directType,
194 FactorizationOptions options)
195{
196 assert(A.nrows()==A.ncols());
197 if (A.nnz()==0)
198 throw SingularMatrixException("No nonzero entries.",__FILE__,__LINE__);
199
200 using Argument = typename Creator<Factorization<Scalar>>::Argument;
201 using Matrices = typename Creator<Factorization<Scalar>>::Matrices;
202
203 Argument args{Matrices(&A),options};
204
205 if (directType == DirectType::ANY)
206 return Factory<DirectType,Factorization<Scalar>>::createAny(args);
207 else
208 return Factory<DirectType,Factorization<Scalar>>::create(directType,args);
209}
210
215template <class Scalar>
216std::unique_ptr<Factorization<Scalar>>
218 Dune::BCRSMatrix<Dune::FieldMatrix<Scalar,1,1>> const& A,
219 FactorizationOptions options)
220{
221 using Index = typename Dune::BCRSMatrix<Dune::FieldMatrix<Scalar,1,1>>::size_type;
222 return getFactorization(directType,MatrixAsTriplet<Scalar,Index>(A),options);
223}
224
229template <class Scalar, int n, int m, class Index>
230std::unique_ptr<Factorization<Scalar>>
233 FactorizationOptions options)
234{
235 return getFactorization(directType,MatrixAsTriplet<Scalar,Index>(A),options);
236}
237
238} // namespace Kaskade
239
240#endif
Abstract base class for matrix factorizations.
Factorization(Options options_)
virtual void solve(std::vector< Scalar > const &b, std::vector< Scalar > &x, bool transposed=false) const
Solves the system for the given right hand side .
Scalar field_type
The type of matrix elements (a field type).
virtual void solve(std::vector< field_type > &b) const
Solves the system for the given right hand side .
virtual void solve(Scalar const *b, Scalar *x, bool transposed=false) const =0
Solves the system for the given right hand side .
void setVerbose(int verbose_)
Info const & info() const
virtual void solve(field_type *b) const =0
Solves the system for the given right hand side .
virtual size_t size() const =0
reports the dimension of the system
A pluggable factory.
Definition: factory.hh:59
size_t nnz() const
Definition: triplet.hh:254
SparseIndexInt nrows() const
Returns number of rows (computes them, if not known)
Definition: triplet.hh:202
SparseIndexInt ncols() const
Returns number of cols (computes them, if not known)
Definition: triplet.hh:215
A NUMA-aware compressed row storage matrix adhering mostly to the Dune ISTL interface (to complete....
To be raised if the matrix is singular.
std::unique_ptr< Factorization< Scalar > > getFactorization(DirectType directType, MatrixAsTriplet< Scalar, Index > const &A, FactorizationOptions options)
Creates a factorization of the given triplet matrix.
DirectType
Available direct solvers for linear equation systems.
Definition: enums.hh:35
void tripletToCompressedColumn(Index nRows, Index nCols, size_t nNonZeros, std::vector< Index > const &ridx, std::vector< Index > const &cidx, std::vector< Scalar > const &values, std::vector< Index > &Ap, std::vector< Index > &Ai, std::vector< Scalar > &Az)
Converts a matrix in triplet format to a compressed column format.
std::tuple< Matrices, FactorizationOptions > Argument
virtual std::unique_ptr< Factorization< Scalar > > create(Argument a) const =0
Creates a factorization for the given pair (A,opt) of matrix A and options opt.
std::variant< MatrixAsTriplet< Scalar, int > const *, MatrixAsTriplet< Scalar, long > const *, MatrixAsTriplet< Scalar, unsigned long > const * > Matrices
static std::string lookupFailureHint(DirectType direct)
Abstract base class of creators that can be registered at the pluggable factory.
Definition: factory.hh:31
The Mumps struct provides output information specific to MUMPS.
int zeroPivots
number of zero eigenvalues (if inertia detection for symmetric matrix was enabled)
int negativePivots
number of negative eigenvalues (if inertia detection for symmetric matrix was enabled)
The Info struct provides output information of direct solvers.
struct Kaskade::Factorization::Info::Mumps mumps
The Options struct allows to specify several options of factorization.
int verbosity
How detailed information should be reported.