Ginkgo  Generated from pipelines/2662685947 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
isai.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 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 #include <ginkgo/core/base/composition.hpp>
12 #include <ginkgo/core/base/exception_helpers.hpp>
13 #include <ginkgo/core/base/executor.hpp>
14 #include <ginkgo/core/base/lin_op.hpp>
15 #include <ginkgo/core/config/config.hpp>
16 #include <ginkgo/core/config/registry.hpp>
17 #include <ginkgo/core/matrix/csr.hpp>
18 #include <ginkgo/core/matrix/dense.hpp>
19 
20 
21 namespace gko {
27 namespace preconditioner {
28 
29 
36 enum struct isai_type { lower, upper, general, spd };
37 
78 template <isai_type IsaiType, typename ValueType, typename IndexType>
79 class Isai : public LinOp, public Transposable {
80  friend class Isai<isai_type::general, ValueType, IndexType>;
81  friend class Isai<isai_type::lower, ValueType, IndexType>;
82  friend class Isai<isai_type::upper, ValueType, IndexType>;
83  friend class Isai<isai_type::spd, ValueType, IndexType>;
84  GKO_ASSERT_SUPPORTED_VALUE_AND_INDEX_TYPE;
85 
86 public:
87  using value_type = ValueType;
88  using index_type = IndexType;
89  using transposed_type =
90  Isai<IsaiType == isai_type::general ? isai_type::general
91  : IsaiType == isai_type::spd ? isai_type::spd
92  : IsaiType == isai_type::lower ? isai_type::upper
93  : isai_type::lower,
94  ValueType, IndexType>;
95  using Comp = Composition<ValueType>;
96  using Csr = matrix::Csr<ValueType, IndexType>;
97  using Dense = matrix::Dense<ValueType>;
98  static constexpr isai_type type{IsaiType};
99 
107  std::shared_ptr<const typename std::conditional<IsaiType == isai_type::spd,
108  Comp, Csr>::type>
110  {
111  return as<typename std::conditional<IsaiType == isai_type::spd, Comp,
112  Csr>::type>(approximate_inverse_);
113  }
114 
120  Isai& operator=(const Isai& other);
121 
128  Isai& operator=(Isai&& other);
129 
134  Isai(const Isai& other);
135 
141  Isai(Isai&& other);
142 
144  {
153  bool GKO_FACTORY_PARAMETER_SCALAR(skip_sorting, false);
154 
163  int GKO_FACTORY_PARAMETER_SCALAR(sparsity_power, 1);
164 
177 
184  std::shared_ptr<const LinOpFactory> GKO_DEFERRED_FACTORY_PARAMETER(
185  excess_solver_factory);
186 
188  excess_solver_reduction,
189  static_cast<remove_complex<value_type>>(1e-6));
190  };
191 
194 
208  static parameters_type parse(
209  const config::pnode& config, const config::registry& context,
210  const config::type_descriptor& td_for_child =
211  config::make_type_descriptor<ValueType, IndexType>());
212 
213  std::unique_ptr<LinOp> transpose() const override;
214 
215  std::unique_ptr<LinOp> conj_transpose() const override;
216 
217 protected:
218  explicit Isai(std::shared_ptr<const Executor> exec) : LinOp(std::move(exec))
219  {}
220 
227  explicit Isai(const Factory* factory,
228  std::shared_ptr<const LinOp> system_matrix)
229  : LinOp(factory->get_executor(), system_matrix->get_size()),
230  parameters_{factory->get_parameters()}
231  {
232  const auto skip_sorting = parameters_.skip_sorting;
233  const auto power = parameters_.sparsity_power;
234  const auto excess_limit = parameters_.excess_limit;
235  generate_inverse(system_matrix, skip_sorting, power, excess_limit,
236  static_cast<remove_complex<value_type>>(
237  parameters_.excess_solver_reduction));
238  if (IsaiType == isai_type::spd) {
239  auto inv = share(as<Csr>(approximate_inverse_));
240  auto inv_transp = share(inv->conj_transpose());
241  approximate_inverse_ =
242  Composition<ValueType>::create(inv_transp, inv);
243  }
244  }
245 
246  void apply_impl(const LinOp* b, LinOp* x) const override
247  {
248  approximate_inverse_->apply(b, x);
249  }
250 
251  void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
252  LinOp* x) const override
253  {
254  approximate_inverse_->apply(alpha, b, beta, x);
255  }
256 
257 private:
268  void generate_inverse(std::shared_ptr<const LinOp> to_invert,
269  bool skip_sorting, int power, index_type excess_limit,
270  remove_complex<value_type> excess_solver_reduction);
271 
272 private:
273  std::shared_ptr<LinOp> approximate_inverse_;
274 };
275 
276 
277 template <typename ValueType = default_precision, typename IndexType = int32>
278 using LowerIsai = Isai<isai_type::lower, ValueType, IndexType>;
279 
280 template <typename ValueType = default_precision, typename IndexType = int32>
281 using UpperIsai = Isai<isai_type::upper, ValueType, IndexType>;
282 
283 template <typename ValueType = default_precision, typename IndexType = int32>
284 using GeneralIsai = Isai<isai_type::general, ValueType, IndexType>;
285 
286 template <typename ValueType = default_precision, typename IndexType = int32>
287 using SpdIsai = Isai<isai_type::spd, ValueType, IndexType>;
288 
289 
290 } // namespace preconditioner
291 } // namespace gko
292 
293 
294 #endif // GKO_PUBLIC_CORE_PRECONDITIONER_ISAI_HPP_
gko::preconditioner::Isai::Isai
Isai(const Isai &other)
Copy-constructs an ISAI preconditioner.
gko::matrix::Csr
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition: matrix.hpp:30
gko::config::pnode
pnode describes a tree of properties.
Definition: property_tree.hpp:28
gko::LinOp
Definition: lin_op.hpp:117
gko::matrix::Dense
Dense is a matrix format which explicitly stores all values of the matrix.
Definition: dense_cache.hpp:28
gko::preconditioner::Isai::parse
static parameters_type parse(const config::pnode &config, const config::registry &context, const config::type_descriptor &td_for_child=config::make_type_descriptor< ValueType, IndexType >())
Create the parameters from the property_tree.
gko::Transposable
Linear operators which support transposition should implement the Transposable interface.
Definition: lin_op.hpp:392
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:90
gko::preconditioner::Isai::conj_transpose
std::unique_ptr< LinOp > conj_transpose() const override
Returns a LinOp representing the conjugate transpose of the Transposable object.
gko::preconditioner::Isai::parameters_type::excess_limit
size_type excess_limit
Size limit for the excess system.
Definition: isai.hpp:176
gko::preconditioner::isai_type
isai_type
This enum lists the types of the ISAI preconditioner.
Definition: isai.hpp:36
GKO_FACTORY_PARAMETER_SCALAR
#define GKO_FACTORY_PARAMETER_SCALAR(_name, _default)
Creates a scalar factory parameter in the factory parameters structure.
Definition: abstract_factory.hpp:437
gko::preconditioner::Isai
The Incomplete Sparse Approximate Inverse (ISAI) Preconditioner generates an approximate inverse matr...
Definition: isai.hpp:79
gko::config::type_descriptor
This class describes the value and index types to be used when building a Ginkgo type from a configur...
Definition: type_descriptor.hpp:39
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::Composition
The Composition class can be used to compose linear operators op1, op2, ..., opn and obtain the opera...
Definition: composition.hpp:39
gko::as
std::decay_t< T > * as(U *obj)
Performs polymorphic type conversion.
Definition: utils_helper.hpp:257
GKO_ENABLE_LIN_OP_FACTORY
#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:900
gko::preconditioner::Isai::parameters_type::skip_sorting
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:153
gko::share
detail::shared_type< OwningPointer > share(OwningPointer &&p)
Marks the object pointed to by p as shared.
Definition: utils_helper.hpp:174
gko::preconditioner::Isai::get_approximate_inverse
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:109
gko::preconditioner::Isai::parameters_type::sparsity_power
int sparsity_power
Which power of the input matrix should be used for the sparsity pattern.
Definition: isai.hpp:163
gko::preconditioner::Isai::transpose
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
gko::preconditioner::Isai::Factory
Definition: isai.hpp:192
gko::config::registry
This class stores additional context for creating Ginkgo objects from configuration files.
Definition: registry.hpp:167
GKO_ENABLE_BUILD_METHOD
#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:386
GKO_CREATE_FACTORY_PARAMETERS
#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:272
gko::PolymorphicObject::get_executor
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition: polymorphic_object.hpp:62
gko::LinOp::get_size
const dim< 2 > & get_size() const noexcept
Returns the size of the operator.
Definition: lin_op.hpp:169
gko::remove_complex
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:264
gko::preconditioner::Isai::operator=
Isai & operator=(const Isai &other)
Copy-assigns an ISAI preconditioner.
gko::LinOp::LinOp
LinOp(const LinOp &)=default
Copy-constructs a LinOp.