Ginkgo  Generated from pipelines/1478841010 branch based on develop. Ginkgo version 1.9.0
A numerical linear algebra library targeting many-core architectures
ilu.hpp
1 // SPDX-FileCopyrightText: 2017 - 2024 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/config/config.hpp>
19 #include <ginkgo/core/config/registry.hpp>
20 #include <ginkgo/core/factorization/par_ilu.hpp>
21 #include <ginkgo/core/matrix/dense.hpp>
22 #include <ginkgo/core/preconditioner/isai.hpp>
23 #include <ginkgo/core/preconditioner/utils.hpp>
24 #include <ginkgo/core/solver/gmres.hpp>
25 #include <ginkgo/core/solver/ir.hpp>
26 #include <ginkgo/core/solver/solver_traits.hpp>
27 #include <ginkgo/core/solver/triangular.hpp>
28 #include <ginkgo/core/stop/combined.hpp>
29 #include <ginkgo/core/stop/iteration.hpp>
30 #include <ginkgo/core/stop/residual_norm.hpp>
31 
32 
33 namespace gko {
34 namespace preconditioner {
35 namespace detail {
36 
37 
38 template <typename LSolverType, typename USolverType>
39 constexpr bool support_ilu_parse =
40  std::is_same<typename USolverType::transposed_type, LSolverType>::value &&
41  (is_instantiation_of<LSolverType, solver::LowerTrs>::value ||
42  is_instantiation_of<LSolverType, solver::Ir>::value ||
43  is_instantiation_of<LSolverType, solver::Gmres>::value ||
44  is_instantiation_of<LSolverType, preconditioner::LowerIsai>::value);
45 
46 
47 template <typename Ilu,
48  std::enable_if_t<!support_ilu_parse<typename Ilu::l_solver_type,
49  typename Ilu::u_solver_type>>* =
50  nullptr>
51 typename Ilu::parameters_type ilu_parse(
52  const config::pnode& config, const config::registry& context,
53  const config::type_descriptor& td_for_child)
54 {
55  GKO_INVALID_STATE(
56  "preconditioner::Ilu only supports limited type for parse.");
57 }
58 
59 template <
60  typename Ilu,
61  std::enable_if_t<support_ilu_parse<typename Ilu::l_solver_type,
62  typename Ilu::u_solver_type>>* = nullptr>
63 typename Ilu::parameters_type ilu_parse(
64  const config::pnode& config, const config::registry& context,
65  const config::type_descriptor& td_for_child);
66 
67 } // namespace detail
68 
69 
119 template <typename LSolverType = solver::LowerTrs<>,
120  typename USolverType = solver::UpperTrs<>, bool ReverseApply = false,
121  typename IndexType = int32>
122 class Ilu : public EnableLinOp<
123  Ilu<LSolverType, USolverType, ReverseApply, IndexType>>,
124  public Transposable {
125  friend class EnableLinOp<Ilu>;
126  friend class EnablePolymorphicObject<Ilu, LinOp>;
127 
128 public:
129  static_assert(
130  std::is_same<typename LSolverType::value_type,
131  typename USolverType::value_type>::value,
132  "Both the L- and the U-solver must use the same `value_type`!");
133  using value_type = typename LSolverType::value_type;
134  using l_solver_type = LSolverType;
135  using u_solver_type = USolverType;
136  static constexpr bool performs_reverse_apply = ReverseApply;
137  using index_type = IndexType;
138  using transposed_type =
139  Ilu<typename USolverType::transposed_type,
140  typename LSolverType::transposed_type, ReverseApply, IndexType>;
141 
142  class Factory;
143 
145  : public enable_parameters_type<parameters_type, Factory> {
149  std::shared_ptr<const typename l_solver_type::Factory>
151 
155  std::shared_ptr<const typename u_solver_type::Factory>
157 
161  std::shared_ptr<const LinOpFactory> factorization_factory{};
162 
163  GKO_DEPRECATED("use with_l_solver instead")
164  parameters_type& with_l_solver_factory(
165  deferred_factory_parameter<const typename l_solver_type::Factory>
166  solver)
167  {
168  return with_l_solver(std::move(solver));
169  }
170 
171  parameters_type& with_l_solver(
173  solver)
174  {
175  this->l_solver_generator = std::move(solver);
176  this->deferred_factories["l_solver"] = [](const auto& exec,
177  auto& params) {
178  if (!params.l_solver_generator.is_empty()) {
179  params.l_solver_factory =
180  params.l_solver_generator.on(exec);
181  }
182  };
183  return *this;
184  }
185 
186  GKO_DEPRECATED("use with_u_solver instead")
187  parameters_type& with_u_solver_factory(
188  deferred_factory_parameter<const typename u_solver_type::Factory>
189  solver)
190  {
191  return with_u_solver(std::move(solver));
192  }
193 
194  parameters_type& with_u_solver(
195  deferred_factory_parameter<const typename u_solver_type::Factory>
196  solver)
197  {
198  this->u_solver_generator = std::move(solver);
199  this->deferred_factories["u_solver"] = [](const auto& exec,
200  auto& params) {
201  if (!params.u_solver_generator.is_empty()) {
202  params.u_solver_factory =
203  params.u_solver_generator.on(exec);
204  }
205  };
206  return *this;
207  }
208 
209  GKO_DEPRECATED("use with_factorization instead")
210  parameters_type& with_factorization_factory(
211  deferred_factory_parameter<const LinOpFactory> factorization)
212  {
213  return with_factorization(std::move(factorization));
214  }
215 
216  parameters_type& with_factorization(
217  deferred_factory_parameter<const LinOpFactory> factorization)
218  {
219  this->factorization_generator = std::move(factorization);
220  this->deferred_factories["factorization"] = [](const auto& exec,
221  auto& params) {
222  if (!params.factorization_generator.is_empty()) {
223  params.factorization_factory =
224  params.factorization_generator.on(exec);
225  }
226  };
227  return *this;
228  }
229 
230  private:
231  deferred_factory_parameter<const typename l_solver_type::Factory>
232  l_solver_generator;
233 
234  deferred_factory_parameter<const typename u_solver_type::Factory>
235  u_solver_generator;
236 
237  deferred_factory_parameter<const LinOpFactory> factorization_generator;
238  };
239 
242 
261  const config::pnode& config, const config::registry& context,
262  const config::type_descriptor& td_for_child =
263  config::make_type_descriptor<value_type, index_type>())
264  {
265  return detail::ilu_parse<Ilu>(config, context, td_for_child);
266  }
267 
273  std::shared_ptr<const l_solver_type> get_l_solver() const
274  {
275  return l_solver_;
276  }
277 
283  std::shared_ptr<const u_solver_type> get_u_solver() const
284  {
285  return u_solver_;
286  }
287 
288  std::unique_ptr<LinOp> transpose() const override
289  {
290  std::unique_ptr<transposed_type> transposed{
291  new transposed_type{this->get_executor()}};
292  transposed->set_size(gko::transpose(this->get_size()));
293  transposed->l_solver_ =
294  share(as<typename u_solver_type::transposed_type>(
295  this->get_u_solver()->transpose()));
296  transposed->u_solver_ =
297  share(as<typename l_solver_type::transposed_type>(
298  this->get_l_solver()->transpose()));
299 
300  return std::move(transposed);
301  }
302 
303  std::unique_ptr<LinOp> conj_transpose() const override
304  {
305  std::unique_ptr<transposed_type> transposed{
306  new transposed_type{this->get_executor()}};
307  transposed->set_size(gko::transpose(this->get_size()));
308  transposed->l_solver_ =
309  share(as<typename u_solver_type::transposed_type>(
310  this->get_u_solver()->conj_transpose()));
311  transposed->u_solver_ =
312  share(as<typename l_solver_type::transposed_type>(
313  this->get_l_solver()->conj_transpose()));
314 
315  return std::move(transposed);
316  }
317 
323  Ilu& operator=(const Ilu& other)
324  {
325  if (&other != this) {
327  auto exec = this->get_executor();
328  l_solver_ = other.l_solver_;
329  u_solver_ = other.u_solver_;
330  parameters_ = other.parameters_;
331  if (other.get_executor() != exec) {
332  l_solver_ = gko::clone(exec, l_solver_);
333  u_solver_ = gko::clone(exec, u_solver_);
334  }
335  }
336  return *this;
337  }
338 
345  Ilu& operator=(Ilu&& other)
346  {
347  if (&other != this) {
349  auto exec = this->get_executor();
350  l_solver_ = std::move(other.l_solver_);
351  u_solver_ = std::move(other.u_solver_);
352  parameters_ = std::exchange(other.parameters_, parameters_type{});
353  if (other.get_executor() != exec) {
354  l_solver_ = gko::clone(exec, l_solver_);
355  u_solver_ = gko::clone(exec, u_solver_);
356  }
357  }
358  return *this;
359  }
360 
365  Ilu(const Ilu& other) : Ilu{other.get_executor()} { *this = other; }
366 
372  Ilu(Ilu&& other) : Ilu{other.get_executor()} { *this = std::move(other); }
373 
374 protected:
375  void apply_impl(const LinOp* b, LinOp* x) const override
376  {
377  // take care of real-to-complex apply
378  precision_dispatch_real_complex<value_type>(
379  [&](auto dense_b, auto dense_x) {
380  this->set_cache_to(dense_b);
381  if (!ReverseApply) {
382  l_solver_->apply(dense_b, cache_.intermediate);
383  if (u_solver_->apply_uses_initial_guess()) {
384  dense_x->copy_from(cache_.intermediate);
385  }
386  u_solver_->apply(cache_.intermediate, dense_x);
387  } else {
388  u_solver_->apply(dense_b, cache_.intermediate);
389  if (l_solver_->apply_uses_initial_guess()) {
390  dense_x->copy_from(cache_.intermediate);
391  }
392  l_solver_->apply(cache_.intermediate, dense_x);
393  }
394  },
395  b, x);
396  }
397 
398  void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
399  LinOp* x) const override
400  {
401  precision_dispatch_real_complex<value_type>(
402  [&](auto dense_alpha, auto dense_b, auto dense_beta, auto dense_x) {
403  this->set_cache_to(dense_b);
404  if (!ReverseApply) {
405  l_solver_->apply(dense_b, cache_.intermediate);
406  u_solver_->apply(dense_alpha, cache_.intermediate,
407  dense_beta, dense_x);
408  } else {
409  u_solver_->apply(dense_b, cache_.intermediate);
410  l_solver_->apply(dense_alpha, cache_.intermediate,
411  dense_beta, dense_x);
412  }
413  },
414  alpha, b, beta, x);
415  }
416 
417  explicit Ilu(std::shared_ptr<const Executor> exec)
418  : EnableLinOp<Ilu>(std::move(exec))
419  {}
420 
421  explicit Ilu(const Factory* factory, std::shared_ptr<const LinOp> lin_op)
422  : EnableLinOp<Ilu>(factory->get_executor(), lin_op->get_size()),
423  parameters_{factory->get_parameters()}
424  {
425  auto comp =
426  std::dynamic_pointer_cast<const Composition<value_type>>(lin_op);
427  std::shared_ptr<const LinOp> l_factor;
428  std::shared_ptr<const LinOp> u_factor;
429 
430  // build factorization if we weren't passed a composition
431  if (!comp) {
432  auto exec = lin_op->get_executor();
433  if (!parameters_.factorization_factory) {
434  parameters_.factorization_factory =
435  factorization::ParIlu<value_type, index_type>::build().on(
436  exec);
437  }
438  auto fact = std::shared_ptr<const LinOp>(
439  parameters_.factorization_factory->generate(lin_op));
440  // ensure that the result is a composition
441  comp =
442  std::dynamic_pointer_cast<const Composition<value_type>>(fact);
443  if (!comp) {
444  GKO_NOT_SUPPORTED(comp);
445  }
446  }
447  if (comp->get_operators().size() == 2) {
448  l_factor = comp->get_operators()[0];
449  u_factor = comp->get_operators()[1];
450  } else {
451  GKO_NOT_SUPPORTED(comp);
452  }
453  GKO_ASSERT_EQUAL_DIMENSIONS(l_factor, u_factor);
454 
455  auto exec = this->get_executor();
456 
457  // If no factories are provided, generate default ones
458  if (!parameters_.l_solver_factory) {
459  l_solver_ = generate_default_solver<l_solver_type>(exec, l_factor);
460  } else {
461  l_solver_ = parameters_.l_solver_factory->generate(l_factor);
462  }
463  if (!parameters_.u_solver_factory) {
464  u_solver_ = generate_default_solver<u_solver_type>(exec, u_factor);
465  } else {
466  u_solver_ = parameters_.u_solver_factory->generate(u_factor);
467  }
468  }
469 
477  void set_cache_to(const LinOp* b) const
478  {
479  if (cache_.intermediate == nullptr) {
480  cache_.intermediate =
482  }
483  // Use b as the initial guess for the first triangular solve
484  cache_.intermediate->copy_from(b);
485  }
486 
487 
495  template <typename SolverType>
496  static std::enable_if_t<solver::has_with_criteria<SolverType>::value,
497  std::unique_ptr<SolverType>>
498  generate_default_solver(const std::shared_ptr<const Executor>& exec,
499  const std::shared_ptr<const LinOp>& mtx)
500  {
501  constexpr gko::remove_complex<value_type> default_reduce_residual{1e-4};
502  const unsigned int default_max_iters{
503  static_cast<unsigned int>(mtx->get_size()[0])};
504 
505  return SolverType::build()
506  .with_criteria(
507  gko::stop::Iteration::build().with_max_iters(default_max_iters),
509  .with_reduction_factor(default_reduce_residual))
510  .on(exec)
511  ->generate(mtx);
512  }
513 
517  template <typename SolverType>
518  static std::enable_if_t<!solver::has_with_criteria<SolverType>::value,
519  std::unique_ptr<SolverType>>
520  generate_default_solver(const std::shared_ptr<const Executor>& exec,
521  const std::shared_ptr<const LinOp>& mtx)
522  {
523  return SolverType::build().on(exec)->generate(mtx);
524  }
525 
526 private:
527  std::shared_ptr<const l_solver_type> l_solver_{};
528  std::shared_ptr<const u_solver_type> u_solver_{};
539  mutable struct cache_struct {
540  cache_struct() = default;
541  ~cache_struct() = default;
542  cache_struct(const cache_struct&) {}
543  cache_struct(cache_struct&&) {}
544  cache_struct& operator=(const cache_struct&) { return *this; }
545  cache_struct& operator=(cache_struct&&) { return *this; }
546  std::unique_ptr<LinOp> intermediate{};
547  } cache_;
548 };
549 
550 
551 } // namespace preconditioner
552 } // namespace gko
553 
554 
555 #endif // GKO_PUBLIC_CORE_PRECONDITIONER_ILU_HPP_
gko::preconditioner::Ilu::Factory
Definition: ilu.hpp:240
gko::config::pnode
pnode describes a tree of properties.
Definition: property_tree.hpp:28
gko::preconditioner::Ilu::parameters_type::u_solver_factory
std::shared_ptr< const typename u_solver_type::Factory > u_solver_factory
Factory for the U solver.
Definition: ilu.hpp:156
gko::LinOp
Definition: lin_op.hpp:117
gko::preconditioner::Ilu::operator=
Ilu & operator=(Ilu &&other)
Move-assigns an ILU preconditioner.
Definition: ilu.hpp:345
gko::matrix::Dense::create
static std::unique_ptr< Dense > create(std::shared_ptr< const Executor > exec, const dim< 2 > &size={}, size_type stride=0)
Creates an uninitialized Dense matrix of the specified size.
gko::log::profile_event_category::factory
LinOpFactory events.
gko::Transposable
Linear operators which support transposition should implement the Transposable interface.
Definition: lin_op.hpp:433
gko::preconditioner::Ilu::Ilu
Ilu(const Ilu &other)
Copy-constructs an ILU preconditioner.
Definition: ilu.hpp:365
gko::preconditioner::Ilu::operator=
Ilu & operator=(const Ilu &other)
Copy-assigns an ILU preconditioner.
Definition: ilu.hpp:323
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::clone
detail::cloned_type< Pointer > clone(const Pointer &p)
Creates a unique clone of the object pointed to by p.
Definition: utils_helper.hpp:173
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::preconditioner::Ilu::get_u_solver
std::shared_ptr< const u_solver_type > get_u_solver() const
Returns the solver which is used for the provided U matrix.
Definition: ilu.hpp:283
gko::stop::ResidualNorm
The ResidualNorm class is a stopping criterion which stops the iteration process when the actual resi...
Definition: residual_norm.hpp:109
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.
Definition: ilu.hpp:260
gko::preconditioner::Ilu::parameters_type::factorization_factory
std::shared_ptr< const LinOpFactory > factorization_factory
Factory for the factorization.
Definition: ilu.hpp:161
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:1017
gko::preconditioner::Ilu::transpose
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
Definition: ilu.hpp:288
gko::preconditioner::Ilu::parameters_type::l_solver_factory
std::shared_ptr< const typename l_solver_type::Factory > l_solver_factory
Factory for the L solver.
Definition: ilu.hpp:150
gko::share
detail::shared_type< OwningPointer > share(OwningPointer &&p)
Marks the object pointed to by p as shared.
Definition: utils_helper.hpp:224
gko::transpose
batch_dim< 2, DimensionType > transpose(const batch_dim< 2, DimensionType > &input)
Returns a batch_dim object with its dimensions swapped for batched operators.
Definition: batch_dim.hpp:119
gko::preconditioner::Ilu::parameters_type
Definition: ilu.hpp:144
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::preconditioner::Ilu::Ilu
Ilu(Ilu &&other)
Move-constructs an ILU preconditioner.
Definition: ilu.hpp:372
gko::preconditioner::Ilu::get_l_solver
std::shared_ptr< const l_solver_type > get_l_solver() const
Returns the solver which is used for the provided L matrix.
Definition: ilu.hpp:273
gko::PolymorphicObject::get_executor
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition: polymorphic_object.hpp:234
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::LinOp::get_size
const dim< 2 > & get_size() const noexcept
Returns the size of the operator.
Definition: lin_op.hpp:210
gko::preconditioner::Ilu::conj_transpose
std::unique_ptr< LinOp > conj_transpose() const override
Returns a LinOp representing the conjugate transpose of the Transposable object.
Definition: ilu.hpp:303
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:325
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:877
gko::LinOp::LinOp
LinOp(const LinOp &)=default
Copy-constructs a LinOp.
gko::preconditioner::Ilu
The Incomplete LU (ILU) preconditioner solves the equation for a given lower triangular matrix L,...
Definition: ilu.hpp:122
gko::EnablePolymorphicObject
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition: polymorphic_object.hpp:661