Ginkgo Generated from branch based on master. Ginkgo version 1.7.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
isai.hpp
1/*******************************<GINKGO LICENSE>******************************
2Copyright (c) 2017-2023, the Ginkgo authors
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
91. Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in the
14documentation and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17contributors may be used to endorse or promote products derived from
18this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31******************************<GINKGO LICENSE>*******************************/
32
33#ifndef GKO_PUBLIC_CORE_PRECONDITIONER_ISAI_HPP_
34#define GKO_PUBLIC_CORE_PRECONDITIONER_ISAI_HPP_
35
36
37#include <memory>
38
39
40#include <ginkgo/core/base/composition.hpp>
41#include <ginkgo/core/base/exception_helpers.hpp>
42#include <ginkgo/core/base/executor.hpp>
43#include <ginkgo/core/base/lin_op.hpp>
44#include <ginkgo/core/matrix/csr.hpp>
45#include <ginkgo/core/matrix/dense.hpp>
46
47
48namespace gko {
54namespace preconditioner {
55
56
63enum struct isai_type { lower, upper, general, spd };
64
105template <isai_type IsaiType, typename ValueType, typename IndexType>
106class Isai : public EnableLinOp<Isai<IsaiType, ValueType, IndexType>>,
107 public Transposable {
108 friend class EnableLinOp<Isai>;
109 friend class EnablePolymorphicObject<Isai, LinOp>;
110 friend class Isai<isai_type::general, ValueType, IndexType>;
111 friend class Isai<isai_type::lower, ValueType, IndexType>;
112 friend class Isai<isai_type::upper, ValueType, IndexType>;
113 friend class Isai<isai_type::spd, ValueType, IndexType>;
114
115public:
116 using value_type = ValueType;
117 using index_type = IndexType;
119 Isai<IsaiType == isai_type::general ? isai_type::general
120 : IsaiType == isai_type::spd ? isai_type::spd
121 : IsaiType == isai_type::lower ? isai_type::upper
122 : isai_type::lower,
123 ValueType, IndexType>;
124 using Comp = Composition<ValueType>;
125 using Csr = matrix::Csr<ValueType, IndexType>;
126 using Dense = matrix::Dense<ValueType>;
128
136 std::shared_ptr<const typename std::conditional<IsaiType == isai_type::spd,
137 Comp, Csr>::type>
139 {
140 return as<typename std::conditional<IsaiType == isai_type::spd, Comp,
141 Csr>::type>(approximate_inverse_);
142 }
143
150
158
163 Isai(const Isai& other);
164
171
173 {
182 bool GKO_FACTORY_PARAMETER_SCALAR(skip_sorting, false);
183
192 int GKO_FACTORY_PARAMETER_SCALAR(sparsity_power, 1);
193
206
213 std::shared_ptr<LinOpFactory> GKO_FACTORY_PARAMETER_SCALAR(
214 excess_solver_factory, nullptr);
215
217 excess_solver_reduction,
218 static_cast<remove_complex<value_type>>(1e-6));
219 };
220
223
224 std::unique_ptr<LinOp> transpose() const override;
225
226 std::unique_ptr<LinOp> conj_transpose() const override;
227
228protected:
229 explicit Isai(std::shared_ptr<const Executor> exec)
230 : EnableLinOp<Isai>(std::move(exec))
231 {}
232
239 explicit Isai(const Factory* factory,
240 std::shared_ptr<const LinOp> system_matrix)
241 : EnableLinOp<Isai>(factory->get_executor(), system_matrix->get_size()),
242 parameters_{factory->get_parameters()}
243 {
244 const auto skip_sorting = parameters_.skip_sorting;
245 const auto power = parameters_.sparsity_power;
246 const auto excess_limit = parameters_.excess_limit;
247 generate_inverse(system_matrix, skip_sorting, power, excess_limit,
248 static_cast<remove_complex<value_type>>(
249 parameters_.excess_solver_reduction));
250 if (IsaiType == isai_type::spd) {
251 auto inv = share(as<Csr>(approximate_inverse_));
252 auto inv_transp = share(inv->conj_transpose());
253 approximate_inverse_ =
255 }
256 }
257
258 void apply_impl(const LinOp* b, LinOp* x) const override
259 {
260 approximate_inverse_->apply(b, x);
261 }
262
263 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
264 LinOp* x) const override
265 {
266 approximate_inverse_->apply(alpha, b, beta, x);
267 }
268
269private:
280 void generate_inverse(std::shared_ptr<const LinOp> to_invert,
281 bool skip_sorting, int power, index_type excess_limit,
282 remove_complex<value_type> excess_solver_reduction);
283
284private:
285 std::shared_ptr<LinOp> approximate_inverse_;
286};
287
288
289template <typename ValueType = default_precision, typename IndexType = int32>
290using LowerIsai = Isai<isai_type::lower, ValueType, IndexType>;
291
292template <typename ValueType = default_precision, typename IndexType = int32>
293using UpperIsai = Isai<isai_type::upper, ValueType, IndexType>;
294
295template <typename ValueType = default_precision, typename IndexType = int32>
296using GeneralIsai = Isai<isai_type::general, ValueType, IndexType>;
297
298template <typename ValueType = default_precision, typename IndexType = int32>
299using SpdIsai = Isai<isai_type::spd, ValueType, IndexType>;
300
301
302} // namespace preconditioner
303} // namespace gko
304
305
306#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:69
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition lin_op.hpp:908
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:691
Definition lin_op.hpp:146
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition polymorphic_object.hpp:263
Linear operators which support transposition should implement the Transposable interface.
Definition lin_op.hpp:462
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition csr.hpp:146
Dense is a matrix format which explicitly stores all values of the matrix.
Definition dense.hpp:136
Definition isai.hpp:221
The Incomplete Sparse Approximate Inverse (ISAI) Preconditioner generates an approximate inverse matr...
Definition isai.hpp:107
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:138
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:308
#define GKO_FACTORY_PARAMETER_SCALAR(_name, _default)
Creates a scalar factory parameter in the factory parameters structure.
Definition abstract_factory.hpp:473
#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:422
#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:1046
isai_type
This enum lists the types of the ISAI preconditioner.
Definition isai.hpp:63
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
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:354
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120
std::decay_t< T > * as(U *obj)
Performs polymorphic type conversion.
Definition utils_helper.hpp:337
detail::shared_type< OwningPointer > share(OwningPointer &&p)
Marks the object pointed to by p as shared.
Definition utils_helper.hpp:254
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:182
int sparsity_power
Which power of the input matrix should be used for the sparsity pattern.
Definition isai.hpp:192
size_type excess_limit
Size limit for the excess system.
Definition isai.hpp:205