Ginkgo  Generated from pipelines/2457497073 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
ilu.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_PRECONDITIONER_ILU_HPP_
6 #define GKO_PUBLIC_CORE_PRECONDITIONER_ILU_HPP_
7 
8 
9 #include <memory>
10 #include <type_traits>
11 
12 #include <ginkgo/core/base/abstract_factory.hpp>
13 #include <ginkgo/core/base/composition.hpp>
14 #include <ginkgo/core/base/exception.hpp>
15 #include <ginkgo/core/base/exception_helpers.hpp>
16 #include <ginkgo/core/base/lin_op.hpp>
17 #include <ginkgo/core/base/precision_dispatch.hpp>
18 #include <ginkgo/core/base/type_traits.hpp>
19 #include <ginkgo/core/config/config.hpp>
20 #include <ginkgo/core/config/registry.hpp>
21 #include <ginkgo/core/factorization/par_ilu.hpp>
22 #include <ginkgo/core/matrix/dense.hpp>
23 #include <ginkgo/core/solver/solver_traits.hpp>
24 #include <ginkgo/core/solver/triangular.hpp>
25 #include <ginkgo/core/stop/combined.hpp>
26 #include <ginkgo/core/stop/iteration.hpp>
27 #include <ginkgo/core/stop/residual_norm.hpp>
28 
29 
30 namespace gko {
31 namespace preconditioner {
32 
33 
79 template <typename ValueType = default_precision, bool ReverseApply = false,
80  typename IndexType = int32>
81 class Ilu : public EnableLinOp<Ilu<ValueType, ReverseApply, IndexType>>,
82  public Transposable {
83  friend class EnableLinOp<Ilu>;
84  friend class EnablePolymorphicObject<Ilu, LinOp>;
85 
86 public:
87  using value_type = ValueType;
88  static constexpr bool performs_reverse_apply = ReverseApply;
89  using index_type = IndexType;
90  using transposed_type = Ilu;
91 
92  class Factory;
93 
95  : public enable_parameters_type<parameters_type, Factory> {
99  std::shared_ptr<const LinOpFactory> l_solver_factory{};
100 
104  std::shared_ptr<const LinOpFactory> u_solver_factory{};
105 
109  std::shared_ptr<const LinOpFactory> factorization_factory{};
110 
118 
126  {
127  this->u_solver_generator = std::move(solver);
128  this->deferred_factories["u_solver"] = [](const auto& exec,
129  auto& params) {
130  if (!params.u_solver_generator.is_empty()) {
131  params.u_solver_factory =
132  params.u_solver_generator.on(exec);
133  }
134  };
135  return *this;
136  }
137 
138  parameters_type& with_factorization(
140  {
141  this->factorization_generator = std::move(factorization);
142  this->deferred_factories["factorization"] = [](const auto& exec,
143  auto& params) {
144  if (!params.factorization_generator.is_empty()) {
145  params.factorization_factory =
146  params.factorization_generator.on(exec);
147  }
148  };
149  return *this;
150  }
151 
152  private:
153  deferred_factory_parameter<const LinOpFactory> l_solver_generator;
154  deferred_factory_parameter<const LinOpFactory> u_solver_generator;
155  deferred_factory_parameter<const LinOpFactory> factorization_generator;
156  };
157 
160 
178  static parameters_type parse(
179  const config::pnode& config, const config::registry& context,
180  const config::type_descriptor& td_for_child =
181  config::make_type_descriptor<value_type, index_type>());
182 
188  std::shared_ptr<const LinOp> get_l_solver() const { return l_solver_; }
189 
195  std::shared_ptr<const LinOp> get_u_solver() const { return u_solver_; }
196 
197  std::unique_ptr<LinOp> transpose() const override;
198 
199  std::unique_ptr<LinOp> conj_transpose() const override;
200 
206  Ilu& operator=(const Ilu& other);
207 
214  Ilu& operator=(Ilu&& other);
215 
220  Ilu(const Ilu& other);
221 
227  Ilu(Ilu&& other);
228 
229 protected:
230  void apply_impl(const LinOp* b, LinOp* x) const override;
231 
232  void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
233  LinOp* x) const override;
234 
235  explicit Ilu(std::shared_ptr<const Executor> exec);
236 
237  explicit Ilu(const Factory* factory, std::shared_ptr<const LinOp> lin_op);
238 
246  void set_cache_to(const LinOp* b) const;
247 
248 private:
249  std::shared_ptr<const LinOp> l_solver_{};
250  std::shared_ptr<const LinOp> u_solver_{};
261  mutable struct cache_struct {
262  cache_struct() = default;
263  ~cache_struct() = default;
264  cache_struct(const cache_struct&) {}
265  cache_struct(cache_struct&&) {}
266  cache_struct& operator=(const cache_struct&) { return *this; }
267  cache_struct& operator=(cache_struct&&) { return *this; }
268  std::unique_ptr<LinOp> intermediate{};
269  } cache_;
270 };
271 
272 
273 } // namespace preconditioner
274 } // namespace gko
275 
276 
277 #endif // GKO_PUBLIC_CORE_PRECONDITIONER_ILU_HPP_
gko::preconditioner::Ilu::Factory
Definition: ilu.hpp:158
gko::preconditioner::Ilu::Ilu
Ilu(const Ilu &other)
Copy-constructs an ILU preconditioner.
gko::config::pnode
pnode describes a tree of properties.
Definition: property_tree.hpp:28
gko::preconditioner::Ilu::parse
static parameters_type parse(const config::pnode &config, const config::registry &context, const config::type_descriptor &td_for_child=config::make_type_descriptor< value_type, index_type >())
Create the parameters from the property_tree.
gko::LinOp
Definition: lin_op.hpp:117
gko::preconditioner::Ilu::operator=
Ilu & operator=(const Ilu &other)
Copy-assigns an ILU preconditioner.
gko::preconditioner::Ilu::get_u_solver
std::shared_ptr< const LinOp > get_u_solver() const
Returns the solver which is used for the provided U matrix.
Definition: ilu.hpp:195
gko::preconditioner::Ilu::parameters_type::l_solver_factory
std::shared_ptr< const LinOpFactory > l_solver_factory
Factory for the L solver.
Definition: ilu.hpp:99
gko::Transposable
Linear operators which support transposition should implement the Transposable interface.
Definition: lin_op.hpp:392
gko::preconditioner::Ilu::parameters_type::u_solver_factory
std::shared_ptr< const LinOpFactory > u_solver_factory
Factory for the U solver.
Definition: ilu.hpp:104
gko::preconditioner::Ilu::transpose
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
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::preconditioner::Ilu::parameters_type::factorization_factory
std::shared_ptr< const LinOpFactory > factorization_factory
Factory for the factorization.
Definition: ilu.hpp:109
gko::preconditioner::Ilu::conj_transpose
std::unique_ptr< LinOp > conj_transpose() const override
Returns a LinOp representing the conjugate transpose of the Transposable object.
gko::preconditioner::Ilu::get_l_solver
std::shared_ptr< const LinOp > get_l_solver() const
Returns the solver which is used for the provided L matrix.
Definition: ilu.hpp:188
gko::preconditioner::Ilu::parameters_type::with_l_solver
parameters_type & with_l_solver(deferred_factory_parameter< const LinOpFactory > solver)
When LSolverTypeOrValueType is a concrete solver type, this only accepts the factory from the same co...
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:948
gko::preconditioner::Ilu::parameters_type
Definition: ilu.hpp:94
gko::default_precision
double default_precision
Precision used if no precision is explicitly specified.
Definition: types.hpp:172
gko::int32
std::int32_t int32
32-bit signed integral type.
Definition: types.hpp:107
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:394
gko::enable_parameters_type
The enable_parameters_type mixin is used to create a base implementation of the factory parameters st...
Definition: abstract_factory.hpp:211
gko::deferred_factory_parameter
Represents a factory parameter of factory type that can either initialized by a pre-existing factory ...
Definition: abstract_factory.hpp:309
gko::EnableLinOp
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition: lin_op.hpp:836
gko::preconditioner::Ilu::parameters_type::with_u_solver
parameters_type & with_u_solver(deferred_factory_parameter< const LinOpFactory > solver)
When USolverTypeOrValueType is a concrete solver type, this only accepts the factory from the same co...
Definition: ilu.hpp:124
gko::preconditioner::Ilu
The Incomplete LU (ILU) preconditioner solves the equation for a given lower triangular matrix L,...
Definition: ilu.hpp:81
gko::EnablePolymorphicObject
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition: polymorphic_object.hpp:667