Ginkgo  Generated from pipelines/1478841010 branch based on develop. Ginkgo version 1.9.0
A numerical linear algebra library targeting many-core architectures
ic.hpp
1 // SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_PRECONDITIONER_IC_HPP_
6 #define GKO_PUBLIC_CORE_PRECONDITIONER_IC_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_ic.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 Type>
39 constexpr bool support_ic_parse =
40  is_instantiation_of<Type, solver::LowerTrs>::value ||
41  is_instantiation_of<Type, solver::Ir>::value ||
42  is_instantiation_of<Type, solver::Gmres>::value ||
43  is_instantiation_of<Type, preconditioner::LowerIsai>::value;
44 
45 
46 template <
47  typename Ic,
48  std::enable_if_t<!support_ic_parse<typename Ic::l_solver_type>>* = nullptr>
49 typename Ic::parameters_type ic_parse(
50  const config::pnode& config, const config::registry& context,
51  const config::type_descriptor& td_for_child)
52 {
53  GKO_INVALID_STATE(
54  "preconditioner::Ic only supports limited type for parse.");
55 }
56 
57 template <
58  typename Ic,
59  std::enable_if_t<support_ic_parse<typename Ic::l_solver_type>>* = nullptr>
60 typename Ic::parameters_type ic_parse(
61  const config::pnode& config, const config::registry& context,
62  const config::type_descriptor& td_for_child);
63 
64 
65 } // namespace detail
66 
112 template <typename LSolverType = solver::LowerTrs<>, typename IndexType = int32>
113 class Ic : public EnableLinOp<Ic<LSolverType, IndexType>>, public Transposable {
114  friend class EnableLinOp<Ic>;
115  friend class EnablePolymorphicObject<Ic, LinOp>;
116 
117 public:
118  static_assert(
119  std::is_same<typename LSolverType::transposed_type::transposed_type,
120  LSolverType>::value,
121  "LSolverType::transposed_type must be symmetric");
122  using value_type = typename LSolverType::value_type;
123  using l_solver_type = LSolverType;
124  using lh_solver_type = typename LSolverType::transposed_type;
125  using index_type = IndexType;
127 
128  class Factory;
129 
131  : public enable_parameters_type<parameters_type, Factory> {
135  std::shared_ptr<const typename l_solver_type::Factory>
137 
141  std::shared_ptr<const LinOpFactory> factorization_factory{};
142 
143  GKO_DEPRECATED("use with_l_solver instead")
144  parameters_type& with_l_solver_factory(
145  deferred_factory_parameter<const typename l_solver_type::Factory>
146  solver)
147  {
148  return with_l_solver(std::move(solver));
149  }
150 
151  parameters_type& with_l_solver(
153  solver)
154  {
155  this->l_solver_generator = std::move(solver);
156  this->deferred_factories["l_solver"] = [](const auto& exec,
157  auto& params) {
158  if (!params.l_solver_generator.is_empty()) {
159  params.l_solver_factory =
160  params.l_solver_generator.on(exec);
161  }
162  };
163  return *this;
164  }
165 
166  GKO_DEPRECATED("use with_factorization instead")
167  parameters_type& with_factorization_factory(
168  deferred_factory_parameter<const LinOpFactory> factorization)
169  {
170  return with_factorization(std::move(factorization));
171  }
172 
173  parameters_type& with_factorization(
174  deferred_factory_parameter<const LinOpFactory> factorization)
175  {
176  this->factorization_generator = std::move(factorization);
177  this->deferred_factories["factorization"] = [](const auto& exec,
178  auto& params) {
179  if (!params.factorization_generator.is_empty()) {
180  params.factorization_factory =
181  params.factorization_generator.on(exec);
182  }
183  };
184  return *this;
185  }
186 
187  private:
188  deferred_factory_parameter<const typename l_solver_type::Factory>
189  l_solver_generator;
190 
191  deferred_factory_parameter<const LinOpFactory> factorization_generator;
192  };
193 
196 
214  const config::pnode& config, const config::registry& context,
215  const config::type_descriptor& td_for_child =
216  config::make_type_descriptor<value_type, index_type>())
217  {
218  return detail::ic_parse<Ic>(config, context, td_for_child);
219  }
220 
226  std::shared_ptr<const l_solver_type> get_l_solver() const
227  {
228  return l_solver_;
229  }
230 
236  std::shared_ptr<const lh_solver_type> get_lh_solver() const
237  {
238  return lh_solver_;
239  }
240 
241  std::unique_ptr<LinOp> transpose() const override
242  {
243  std::unique_ptr<transposed_type> transposed{
244  new transposed_type{this->get_executor()}};
245  transposed->set_size(gko::transpose(this->get_size()));
246  transposed->l_solver_ =
247  share(as<typename lh_solver_type::transposed_type>(
248  this->get_lh_solver()->transpose()));
249  transposed->lh_solver_ =
250  share(as<typename l_solver_type::transposed_type>(
251  this->get_l_solver()->transpose()));
252 
253  return std::move(transposed);
254  }
255 
256  std::unique_ptr<LinOp> conj_transpose() const override
257  {
258  std::unique_ptr<transposed_type> transposed{
259  new transposed_type{this->get_executor()}};
260  transposed->set_size(gko::transpose(this->get_size()));
261  transposed->l_solver_ =
262  share(as<typename lh_solver_type::transposed_type>(
263  this->get_lh_solver()->conj_transpose()));
264  transposed->lh_solver_ =
265  share(as<typename l_solver_type::transposed_type>(
266  this->get_l_solver()->conj_transpose()));
267 
268  return std::move(transposed);
269  }
270 
276  Ic& operator=(const Ic& other)
277  {
278  if (&other != this) {
280  auto exec = this->get_executor();
281  l_solver_ = other.l_solver_;
282  lh_solver_ = other.lh_solver_;
283  parameters_ = other.parameters_;
284  if (other.get_executor() != exec) {
285  l_solver_ = gko::clone(exec, l_solver_);
286  lh_solver_ = gko::clone(exec, lh_solver_);
287  }
288  }
289  return *this;
290  }
291 
298  Ic& operator=(Ic&& other)
299  {
300  if (&other != this) {
302  auto exec = this->get_executor();
303  l_solver_ = std::move(other.l_solver_);
304  lh_solver_ = std::move(other.lh_solver_);
305  parameters_ = std::exchange(other.parameters_, parameters_type{});
306  if (other.get_executor() != exec) {
307  l_solver_ = gko::clone(exec, l_solver_);
308  lh_solver_ = gko::clone(exec, lh_solver_);
309  }
310  }
311  return *this;
312  }
313 
318  Ic(const Ic& other) : Ic{other.get_executor()} { *this = other; }
319 
325  Ic(Ic&& other) : Ic{other.get_executor()} { *this = std::move(other); }
326 
327 protected:
328  void apply_impl(const LinOp* b, LinOp* x) const override
329  {
330  // take care of real-to-complex apply
331  precision_dispatch_real_complex<value_type>(
332  [&](auto dense_b, auto dense_x) {
333  this->set_cache_to(dense_b);
334  l_solver_->apply(dense_b, cache_.intermediate);
335  if (lh_solver_->apply_uses_initial_guess()) {
336  dense_x->copy_from(cache_.intermediate);
337  }
338  lh_solver_->apply(cache_.intermediate, dense_x);
339  },
340  b, x);
341  }
342 
343  void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
344  LinOp* x) const override
345  {
346  precision_dispatch_real_complex<value_type>(
347  [&](auto dense_alpha, auto dense_b, auto dense_beta, auto dense_x) {
348  this->set_cache_to(dense_b);
349  l_solver_->apply(dense_b, cache_.intermediate);
350  lh_solver_->apply(dense_alpha, cache_.intermediate, dense_beta,
351  dense_x);
352  },
353  alpha, b, beta, x);
354  }
355 
356  explicit Ic(std::shared_ptr<const Executor> exec)
357  : EnableLinOp<Ic>(std::move(exec))
358  {}
359 
360  explicit Ic(const Factory* factory, std::shared_ptr<const LinOp> lin_op)
361  : EnableLinOp<Ic>(factory->get_executor(), lin_op->get_size()),
362  parameters_{factory->get_parameters()}
363  {
364  auto comp =
365  std::dynamic_pointer_cast<const Composition<value_type>>(lin_op);
366  std::shared_ptr<const LinOp> l_factor;
367 
368  // build factorization if we weren't passed a composition
369  if (!comp) {
370  auto exec = lin_op->get_executor();
371  if (!parameters_.factorization_factory) {
372  parameters_.factorization_factory =
373  factorization::ParIc<value_type, index_type>::build()
374  .with_both_factors(false)
375  .on(exec);
376  }
377  auto fact = std::shared_ptr<const LinOp>(
378  parameters_.factorization_factory->generate(lin_op));
379  // ensure that the result is a composition
380  comp =
381  std::dynamic_pointer_cast<const Composition<value_type>>(fact);
382  if (!comp) {
383  GKO_NOT_SUPPORTED(comp);
384  }
385  }
386  // comp must contain one or two factors
387  if (comp->get_operators().size() > 2 || comp->get_operators().empty()) {
388  GKO_NOT_SUPPORTED(comp);
389  }
390  l_factor = comp->get_operators()[0];
391  GKO_ASSERT_IS_SQUARE_MATRIX(l_factor);
392 
393  auto exec = this->get_executor();
394 
395  // If no factories are provided, generate default ones
396  if (!parameters_.l_solver_factory) {
397  l_solver_ = generate_default_solver<l_solver_type>(exec, l_factor);
398  // If comp contains both factors: use the transposed factor to avoid
399  // transposing twice
400  if (comp->get_operators().size() == 2) {
401  auto lh_factor = comp->get_operators()[1];
402  GKO_ASSERT_EQUAL_DIMENSIONS(l_factor, lh_factor);
403  lh_solver_ = as<lh_solver_type>(l_solver_->conj_transpose());
404  } else {
405  lh_solver_ = as<lh_solver_type>(l_solver_->conj_transpose());
406  }
407  } else {
408  l_solver_ = parameters_.l_solver_factory->generate(l_factor);
409  lh_solver_ = as<lh_solver_type>(l_solver_->conj_transpose());
410  }
411  }
412 
420  void set_cache_to(const LinOp* b) const
421  {
422  if (cache_.intermediate == nullptr) {
423  cache_.intermediate =
425  }
426  // Use b as the initial guess for the first triangular solve
427  cache_.intermediate->copy_from(b);
428  }
429 
430 
438  template <typename SolverType>
439  static std::enable_if_t<solver::has_with_criteria<SolverType>::value,
440  std::unique_ptr<SolverType>>
441  generate_default_solver(const std::shared_ptr<const Executor>& exec,
442  const std::shared_ptr<const LinOp>& mtx)
443  {
444  constexpr gko::remove_complex<value_type> default_reduce_residual{1e-4};
445  const unsigned int default_max_iters{
446  static_cast<unsigned int>(mtx->get_size()[0])};
447 
448  return SolverType::build()
449  .with_criteria(
450  gko::stop::Iteration::build().with_max_iters(default_max_iters),
452  .with_reduction_factor(default_reduce_residual))
453  .on(exec)
454  ->generate(mtx);
455  }
456 
460  template <typename SolverType>
461  static std::enable_if_t<!solver::has_with_criteria<SolverType>::value,
462  std::unique_ptr<SolverType>>
463  generate_default_solver(const std::shared_ptr<const Executor>& exec,
464  const std::shared_ptr<const LinOp>& mtx)
465  {
466  return SolverType::build().on(exec)->generate(mtx);
467  }
468 
469 private:
470  std::shared_ptr<const l_solver_type> l_solver_{};
471  std::shared_ptr<const lh_solver_type> lh_solver_{};
482  mutable struct cache_struct {
483  cache_struct() = default;
484  ~cache_struct() = default;
485  cache_struct(const cache_struct&) {}
486  cache_struct(cache_struct&&) {}
487  cache_struct& operator=(const cache_struct&) { return *this; }
488  cache_struct& operator=(cache_struct&&) { return *this; }
489  std::unique_ptr<LinOp> intermediate{};
490  } cache_;
491 };
492 
493 
494 } // namespace preconditioner
495 } // namespace gko
496 
497 
498 #endif // GKO_PUBLIC_CORE_PRECONDITIONER_IC_HPP_
gko::config::pnode
pnode describes a tree of properties.
Definition: property_tree.hpp:28
gko::LinOp
Definition: lin_op.hpp:117
gko::preconditioner::Ic::operator=
Ic & operator=(const Ic &other)
Copy-assigns an IC preconditioner.
Definition: ic.hpp:276
gko::preconditioner::Ic::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: ic.hpp:226
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::Ic::Ic
Ic(Ic &&other)
Move-constructs an IC preconditioner.
Definition: ic.hpp:325
gko::preconditioner::Ic
The Incomplete Cholesky (IC) preconditioner solves the equation for a given lower triangular matrix ...
Definition: ic.hpp:113
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::preconditioner::Ic::get_lh_solver
std::shared_ptr< const lh_solver_type > get_lh_solver() const
Returns the solver which is used for the L^H matrix.
Definition: ic.hpp:236
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::Ic::Ic
Ic(const Ic &other)
Copy-constructs an IC preconditioner.
Definition: ic.hpp:318
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::Ic::conj_transpose
std::unique_ptr< LinOp > conj_transpose() const override
Returns a LinOp representing the conjugate transpose of the Transposable object.
Definition: ic.hpp:256
gko::preconditioner::Ic::parameters_type::factorization_factory
std::shared_ptr< const LinOpFactory > factorization_factory
Factory for the factorization.
Definition: ic.hpp:141
gko::preconditioner::Ic::parameters_type::l_solver_factory
std::shared_ptr< const typename l_solver_type::Factory > l_solver_factory
Factory for the L solver.
Definition: ic.hpp:136
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::share
detail::shared_type< OwningPointer > share(OwningPointer &&p)
Marks the object pointed to by p as shared.
Definition: utils_helper.hpp:224
gko::preconditioner::Ic::transpose
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
Definition: ic.hpp:241
gko::preconditioner::Ic::parameters_type
Definition: ic.hpp:130
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::Ic::Factory
Definition: ic.hpp:194
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::Ic::operator=
Ic & operator=(Ic &&other)
Move-assigns an IC preconditioner.
Definition: ic.hpp:298
gko::preconditioner::Ic::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: ic.hpp:213
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::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::EnablePolymorphicObject
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition: polymorphic_object.hpp:661