Ginkgo Generated from develop branch based on develop. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
isai.hpp
1// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#ifndef GKO_PUBLIC_CORE_PRECONDITIONER_ISAI_HPP_
6#define GKO_PUBLIC_CORE_PRECONDITIONER_ISAI_HPP_
7
8
9#include <memory>
10
11
12#include <ginkgo/core/base/composition.hpp>
13#include <ginkgo/core/base/exception_helpers.hpp>
14#include <ginkgo/core/base/executor.hpp>
15#include <ginkgo/core/base/lin_op.hpp>
16#include <ginkgo/core/matrix/csr.hpp>
17#include <ginkgo/core/matrix/dense.hpp>
18
19
20namespace gko {
26namespace preconditioner {
27
28
35enum struct isai_type { lower, upper, general, spd };
36
77template <isai_type IsaiType, typename ValueType, typename IndexType>
78class Isai : public EnableLinOp<Isai<IsaiType, ValueType, IndexType>>,
79 public Transposable {
80 friend class EnableLinOp<Isai>;
81 friend class EnablePolymorphicObject<Isai, LinOp>;
82 friend class Isai<isai_type::general, ValueType, IndexType>;
83 friend class Isai<isai_type::lower, ValueType, IndexType>;
84 friend class Isai<isai_type::upper, ValueType, IndexType>;
85 friend class Isai<isai_type::spd, ValueType, IndexType>;
86
87public:
88 using value_type = ValueType;
89 using index_type = IndexType;
91 Isai<IsaiType == isai_type::general ? isai_type::general
92 : IsaiType == isai_type::spd ? isai_type::spd
93 : IsaiType == isai_type::lower ? isai_type::upper
94 : isai_type::lower,
95 ValueType, IndexType>;
96 using Comp = Composition<ValueType>;
97 using Csr = matrix::Csr<ValueType, IndexType>;
98 using Dense = matrix::Dense<ValueType>;
100
108 std::shared_ptr<const typename std::conditional<IsaiType == isai_type::spd,
109 Comp, Csr>::type>
111 {
112 return as<typename std::conditional<IsaiType == isai_type::spd, Comp,
113 Csr>::type>(approximate_inverse_);
114 }
115
122
130
135 Isai(const Isai& other);
136
143
145 {
154 bool GKO_FACTORY_PARAMETER_SCALAR(skip_sorting, false);
155
164 int GKO_FACTORY_PARAMETER_SCALAR(sparsity_power, 1);
165
178
185 std::shared_ptr<LinOpFactory> GKO_FACTORY_PARAMETER_SCALAR(
186 excess_solver_factory, nullptr);
187
189 excess_solver_reduction,
190 static_cast<remove_complex<value_type>>(1e-6));
191 };
192
195
196 std::unique_ptr<LinOp> transpose() const override;
197
198 std::unique_ptr<LinOp> conj_transpose() const override;
199
200protected:
201 explicit Isai(std::shared_ptr<const Executor> exec)
202 : EnableLinOp<Isai>(std::move(exec))
203 {}
204
211 explicit Isai(const Factory* factory,
212 std::shared_ptr<const LinOp> system_matrix)
213 : EnableLinOp<Isai>(factory->get_executor(), system_matrix->get_size()),
214 parameters_{factory->get_parameters()}
215 {
216 const auto skip_sorting = parameters_.skip_sorting;
217 const auto power = parameters_.sparsity_power;
218 const auto excess_limit = parameters_.excess_limit;
219 generate_inverse(system_matrix, skip_sorting, power, excess_limit,
220 static_cast<remove_complex<value_type>>(
221 parameters_.excess_solver_reduction));
222 if (IsaiType == isai_type::spd) {
223 auto inv = share(as<Csr>(approximate_inverse_));
224 auto inv_transp = share(inv->conj_transpose());
225 approximate_inverse_ =
227 }
228 }
229
230 void apply_impl(const LinOp* b, LinOp* x) const override
231 {
232 approximate_inverse_->apply(b, x);
233 }
234
235 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
236 LinOp* x) const override
237 {
238 approximate_inverse_->apply(alpha, b, beta, x);
239 }
240
241private:
252 void generate_inverse(std::shared_ptr<const LinOp> to_invert,
253 bool skip_sorting, int power, index_type excess_limit,
254 remove_complex<value_type> excess_solver_reduction);
255
256private:
257 std::shared_ptr<LinOp> approximate_inverse_;
258};
259
260
261template <typename ValueType = default_precision, typename IndexType = int32>
262using LowerIsai = Isai<isai_type::lower, ValueType, IndexType>;
263
264template <typename ValueType = default_precision, typename IndexType = int32>
265using UpperIsai = Isai<isai_type::upper, ValueType, IndexType>;
266
267template <typename ValueType = default_precision, typename IndexType = int32>
268using GeneralIsai = Isai<isai_type::general, ValueType, IndexType>;
269
270template <typename ValueType = default_precision, typename IndexType = int32>
271using SpdIsai = Isai<isai_type::spd, ValueType, IndexType>;
272
273
274} // namespace preconditioner
275} // namespace gko
276
277
278#endif // GKO_PUBLIC_CORE_PRECONDITIONER_ISAI_HPP_
The Composition class can be used to compose linear operators op1, op2, ..., opn and obtain the opera...
Definition composition.hpp:41
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition lin_op.hpp:880
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:663
Definition lin_op.hpp:118
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition polymorphic_object.hpp:235
Linear operators which support transposition should implement the Transposable interface.
Definition lin_op.hpp:434
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition csr.hpp:117
Dense is a matrix format which explicitly stores all values of the matrix.
Definition dense.hpp:107
Definition isai.hpp:193
The Incomplete Sparse Approximate Inverse (ISAI) Preconditioner generates an approximate inverse matr...
Definition isai.hpp:79
Isai(const Isai &other)
Copy-constructs an ISAI preconditioner.
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
Isai & operator=(Isai &&other)
Move-assigns an ISAI preconditioner.
Isai(Isai &&other)
Move-constructs an ISAI preconditioner.
std::unique_ptr< LinOp > conj_transpose() const override
Returns a LinOp representing the conjugate transpose of the Transposable object.
std::shared_ptr< const typename std::conditional< IsaiType==isai_type::spd, Comp, Csr >::type > get_approximate_inverse() const
Returns the approximate inverse of the given matrix (either a CSR matrix for IsaiType general,...
Definition isai.hpp:110
Isai & operator=(const Isai &other)
Copy-assigns an ISAI preconditioner.
#define GKO_CREATE_FACTORY_PARAMETERS(_parameters_name, _factory_name)
This Macro will generate a new type containing the parameters for the factory _factory_name.
Definition abstract_factory.hpp:280
#define GKO_FACTORY_PARAMETER_SCALAR(_name, _default)
Creates a scalar factory parameter in the factory parameters structure.
Definition abstract_factory.hpp:445
#define GKO_ENABLE_BUILD_METHOD(_factory_name)
Defines a build method for the factory, simplifying its construction by removing the repetitive typin...
Definition abstract_factory.hpp:394
#define GKO_ENABLE_LIN_OP_FACTORY(_lin_op, _parameters_name, _factory_name)
This macro will generate a default implementation of a LinOpFactory for the LinOp subclass it is defi...
Definition lin_op.hpp:1018
isai_type
This enum lists the types of the ISAI preconditioner.
Definition isai.hpp:35
The Ginkgo namespace.
Definition abstract_factory.hpp:20
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:775
typename detail::remove_complex_s< T >::type remove_complex
Obtain the type which removed the complex of complex/scalar type or the template parameter of class b...
Definition math.hpp:326
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:92
std::decay_t< T > * as(U *obj)
Performs polymorphic type conversion.
Definition utils_helper.hpp:309
detail::shared_type< OwningPointer > share(OwningPointer &&p)
Marks the object pointed to by p as shared.
Definition utils_helper.hpp:226
bool skip_sorting
Optimization parameter that skips the sorting of the input matrix (only skip if it is known that it i...
Definition isai.hpp:154
int sparsity_power
Which power of the input matrix should be used for the sparsity pattern.
Definition isai.hpp:164
size_type excess_limit
Size limit for the excess system.
Definition isai.hpp:177