Ginkgo  Generated from pipelines/2837190956 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
multigrid.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_SOLVER_MULTIGRID_HPP_
6 #define GKO_PUBLIC_CORE_SOLVER_MULTIGRID_HPP_
7 
8 
9 #include <complex>
10 #include <functional>
11 #include <memory>
12 #include <utility>
13 
14 #include <ginkgo/core/base/abstract_factory.hpp>
15 #include <ginkgo/core/base/array.hpp>
16 #include <ginkgo/core/base/exception_helpers.hpp>
17 #include <ginkgo/core/base/lin_op.hpp>
18 #include <ginkgo/core/base/math.hpp>
19 #include <ginkgo/core/base/precision_dispatch.hpp>
20 #include <ginkgo/core/base/types.hpp>
21 #include <ginkgo/core/config/config.hpp>
22 #include <ginkgo/core/config/registry.hpp>
23 #include <ginkgo/core/config/type_descriptor.hpp>
24 #include <ginkgo/core/log/logger.hpp>
25 #include <ginkgo/core/matrix/dense.hpp>
26 #include <ginkgo/core/matrix/identity.hpp>
27 #include <ginkgo/core/multigrid/multigrid_level.hpp>
28 #include <ginkgo/core/solver/solver_base.hpp>
29 #include <ginkgo/core/stop/combined.hpp>
30 #include <ginkgo/core/stop/criterion.hpp>
31 
32 
33 namespace gko {
34 namespace solver {
35 
36 
42 namespace multigrid {
43 
44 
56 enum class cycle { v, f, w };
57 
58 
72 enum class mid_smooth_type { both, post_smoother, pre_smoother, standalone };
73 
74 
75 namespace detail {
76 
77 
78 // It should only be used internally
79 class MultigridState;
80 
81 
82 } // namespace detail
83 } // namespace multigrid
84 
85 
143 class Multigrid : public LinOp,
144  public EnableSolverBase<Multigrid>,
145  public EnableIterativeBase<Multigrid>,
146  public EnableApplyWithInitialGuess<Multigrid> {
148 
149 public:
156  bool apply_uses_initial_guess() const override
157  {
158  return this->get_default_initial_guess() ==
160  }
161 
167  std::vector<std::shared_ptr<const gko::multigrid::MultigridLevel>>
169  {
170  return mg_level_list_;
171  }
172 
178  std::vector<std::shared_ptr<const LinOp>> get_pre_smoother_list() const
179  {
180  return pre_smoother_list_;
181  }
182 
188  std::vector<std::shared_ptr<const LinOp>> get_mid_smoother_list() const
189  {
190  return mid_smoother_list_;
191  }
192 
198  std::vector<std::shared_ptr<const LinOp>> get_post_smoother_list() const
199  {
200  return post_smoother_list_;
201  }
202 
208  std::shared_ptr<const LinOp> get_coarsest_solver() const
209  {
210  return coarsest_solver_;
211  }
212 
218  multigrid::cycle get_cycle() const { return parameters_.cycle; }
219 
225  void set_cycle(multigrid::cycle cycle) { parameters_.cycle = cycle; }
226 
227 
228  class Factory;
229 
231  : public enable_iterative_solver_factory_parameters<parameters_type,
232  Factory> {
236  std::vector<std::shared_ptr<const LinOpFactory>>
237  GKO_DEFERRED_FACTORY_VECTOR_PARAMETER(mg_level);
238 
265  std::function<size_type(const size_type, const LinOp*)>
267 
282  std::vector<std::shared_ptr<const LinOpFactory>>
283  GKO_DEFERRED_FACTORY_VECTOR_PARAMETER(pre_smoother);
284 
290  std::vector<std::shared_ptr<const LinOpFactory>>
291  GKO_DEFERRED_FACTORY_VECTOR_PARAMETER(post_smoother);
292 
300  std::vector<std::shared_ptr<const LinOpFactory>>
301  GKO_DEFERRED_FACTORY_VECTOR_PARAMETER(mid_smoother);
302 
308 
317  mid_case, multigrid::mid_smooth_type::standalone);
318 
327 
334 
341  std::vector<std::shared_ptr<const LinOpFactory>>
342  GKO_DEFERRED_FACTORY_VECTOR_PARAMETER(coarsest_solver);
343 
366  std::function<size_type(const size_type, const LinOp*)>
368 
375  multigrid::cycle::v);
376 
386 
395 
400  std::complex<double> GKO_FACTORY_PARAMETER_SCALAR(smoother_relax, 0.9);
401 
407 
414  };
417 
432  static parameters_type parse(const config::pnode& config,
433  const config::registry& context,
434  const config::type_descriptor& td_for_child =
435  config::make_type_descriptor<>());
436 
437 protected:
438  void apply_impl(const LinOp* b, LinOp* x) const override;
439 
440  void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
441  LinOp* x) const override;
442 
443  void apply_with_initial_guess_impl(const LinOp* b, LinOp* x,
444  initial_guess_mode guess) const override;
445 
446  void apply_with_initial_guess_impl(const LinOp* alpha, const LinOp* b,
447  const LinOp* beta, LinOp* x,
448  initial_guess_mode guess) const override;
449 
450  template <typename VectorType>
451  void apply_dense_impl(const VectorType* b, VectorType* x,
452  initial_guess_mode guess) const;
453 
458  void generate();
459 
460  explicit Multigrid(std::shared_ptr<const Executor> exec);
461 
462  explicit Multigrid(const Factory* factory,
463  std::shared_ptr<const LinOp> system_matrix);
464 
468  void validate();
469 
479  void verify_legal_length(bool checked, size_type len, size_type ref_len);
480 
481  void create_state() const;
482 
483 private:
484  std::vector<std::shared_ptr<const gko::multigrid::MultigridLevel>>
485  mg_level_list_{};
486  std::vector<std::shared_ptr<const LinOp>> pre_smoother_list_{};
487  std::vector<std::shared_ptr<const LinOp>> mid_smoother_list_{};
488  std::vector<std::shared_ptr<const LinOp>> post_smoother_list_{};
489  std::shared_ptr<const LinOp> coarsest_solver_{};
490  std::function<size_type(const size_type, const LinOp*)> level_selector_;
491  std::function<size_type(const size_type, const LinOp*)> solver_selector_;
492 
503  mutable struct cache_struct {
504  cache_struct() = default;
505 
506  ~cache_struct() = default;
507 
508  cache_struct(const cache_struct&) {}
509 
510  cache_struct(cache_struct&&) {}
511 
512  cache_struct& operator=(const cache_struct&) { return *this; }
513 
514  cache_struct& operator=(cache_struct&&) { return *this; }
515 
516  // unique_ptr with default destructor does not work with the incomplete
517  // type.
518  std::shared_ptr<multigrid::detail::MultigridState> state{};
519  } cache_;
520 };
521 
522 template <>
524  using Solver = Multigrid;
525  // number of vectors used by this workspace
526  static int num_vectors(const Solver&);
527  // number of arrays used by this workspace
528  static int num_arrays(const Solver&);
529  // array containing the num_vectors names for the workspace vectors
530  static std::vector<std::string> op_names(const Solver&);
531  // array containing the num_arrays names for the workspace vectors
532  static std::vector<std::string> array_names(const Solver&);
533  // array containing all varying scalar vectors (independent of problem size)
534  static std::vector<int> scalars(const Solver&);
535  // array containing all varying vectors (dependent on problem size)
536  static std::vector<int> vectors(const Solver&);
537 
538  // stopping status array
539  constexpr static int stop = 0;
540 };
541 
542 
543 } // namespace solver
544 } // namespace gko
545 
546 
547 #endif // GKO_PUBLIC_CORE_SOLVER_MULTIGRID_HPP_
gko::solver::Multigrid::parameters_type
Definition: multigrid.hpp:230
gko::solver::EnableApplyWithInitialGuess
EnableApplyWithInitialGuess providing default operation for ApplyWithInitialGuess with correct valida...
Definition: solver_base.hpp:161
gko::solver::Multigrid::parameters_type::coarsest_solver
std::vector< std::shared_ptr< const LinOpFactory > > coarsest_solver
Coarsest factory list.
Definition: multigrid.hpp:342
gko::config::pnode
pnode describes a tree of properties.
Definition: property_tree.hpp:28
gko::log::profile_event_category::solver
Solver events.
gko::solver::EnableIterativeBase
A LinOp deriving from this CRTP class stores a stopping criterion factory and allows applying with a ...
Definition: solver_base.hpp:718
gko::LinOp
Definition: lin_op.hpp:117
gko::solver::Multigrid::parameters_type::cycle
multigrid::cycle cycle
Multigrid cycle type.
Definition: multigrid.hpp:375
gko::solver::Multigrid::get_pre_smoother_list
std::vector< std::shared_ptr< const LinOp > > get_pre_smoother_list() const
Gets the list of pre-smoother operators.
Definition: multigrid.hpp:178
gko::solver::Multigrid::parameters_type::post_smoother
std::vector< std::shared_ptr< const LinOpFactory > > post_smoother
Post-smooth Factory list.
Definition: multigrid.hpp:291
gko::solver::enable_iterative_solver_factory_parameters
Definition: solver_base.hpp:844
gko::solver::Multigrid::get_mg_level_list
std::vector< std::shared_ptr< const gko::multigrid::MultigridLevel > > get_mg_level_list() const
Gets the list of MultigridLevel operators.
Definition: multigrid.hpp:168
gko::solver::Multigrid::parameters_type::level_selector
std::function< size_type(const size_type, const LinOp *)> level_selector
Custom selector size_type (size_type level, const LinOp* fine_matrix) Selector function returns the e...
Definition: multigrid.hpp:266
gko::solver::Multigrid::parameters_type::pre_smoother
std::vector< std::shared_ptr< const LinOpFactory > > pre_smoother
Pre-smooth Factory list.
Definition: multigrid.hpp:283
gko::solver::Multigrid::parameters_type::kcycle_base
size_type kcycle_base
kcycle_base is a factor to choose how often enable FCG/GCR step.
Definition: multigrid.hpp:385
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:101
gko::solver::Multigrid::get_cycle
multigrid::cycle get_cycle() const
Get the cycle of multigrid.
Definition: multigrid.hpp:218
gko::solver::Multigrid
Multigrid solves a linear system by combining cheap iterative sweeps (smoothers) on a hierarchy of pr...
Definition: multigrid.hpp:143
gko::solver::initial_guess_mode::zero
the input is zero
gko::solver::Multigrid::parameters_type::kcycle_rel_tol
double kcycle_rel_tol
kcycle_rel_tol decides whether run the second iteration of FCG/GCR step.
Definition: multigrid.hpp:394
gko::solver::Multigrid::set_cycle
void set_cycle(multigrid::cycle cycle)
Set the cycle of multigrid.
Definition: multigrid.hpp:225
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:469
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:19
gko::solver::Multigrid::parameters_type::mid_case
multigrid::mid_smooth_type mid_case
Choose the behavior of mid smoother between two cycles close to each other in the same level.
Definition: multigrid.hpp:317
gko::solver::Multigrid::get_mid_smoother_list
std::vector< std::shared_ptr< const LinOp > > get_mid_smoother_list() const
Gets the list of mid-smoother operators.
Definition: multigrid.hpp:188
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:899
gko::solver::Multigrid::Factory
Definition: multigrid.hpp:415
gko::LinOp::operator=
LinOp & operator=(const LinOp &)=default
Copy-assigns a LinOp.
gko::solver::Multigrid::parameters_type::default_initial_guess
initial_guess_mode default_initial_guess
Default initial guess mode.
Definition: multigrid.hpp:413
gko::solver::Multigrid::parameters_type::max_levels
size_type max_levels
The maximum number of mg_level (without coarsest solver level) that can be used.
Definition: multigrid.hpp:326
gko::solver::EnableSolverBase
A LinOp deriving from this CRTP class stores a system matrix.
Definition: solver_base.hpp:556
gko::solver::Multigrid::get_coarsest_solver
std::shared_ptr< const LinOp > get_coarsest_solver() const
Gets the operator at the coarsest level.
Definition: multigrid.hpp:208
gko::solver::Multigrid::parameters_type::mg_level
std::vector< std::shared_ptr< const LinOpFactory > > mg_level
MultigridLevel Factory list.
Definition: multigrid.hpp:237
gko::solver::Multigrid::get_post_smoother_list
std::vector< std::shared_ptr< const LinOp > > get_post_smoother_list() const
Gets the list of post-smoother operators.
Definition: multigrid.hpp:198
gko::solver::Multigrid::parameters_type::min_coarse_rows
size_type min_coarse_rows
The minimal number of coarse rows.
Definition: multigrid.hpp:333
gko::config::registry
This class stores additional context for creating Ginkgo objects from configuration files.
Definition: registry.hpp:167
gko::solver::Multigrid::parameters_type::smoother_iters
size_type smoother_iters
smoother_iters is the number of iteration of default generated smoother.
Definition: multigrid.hpp:406
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:418
gko::solver::initial_guess_mode
initial_guess_mode
Give a initial guess mode about the input of the apply method.
Definition: solver_base.hpp:33
gko::solver::Multigrid::parse
static parameters_type parse(const config::pnode &config, const config::registry &context, const config::type_descriptor &td_for_child=config::make_type_descriptor<>())
Create the parameters from the property_tree.
gko::solver::initial_guess_mode::provided
the input is provided
gko::solver::Multigrid::apply_uses_initial_guess
bool apply_uses_initial_guess() const override
Return true as iterative solvers use the data in x as an initial guess or false if multigrid always s...
Definition: multigrid.hpp:156
gko::solver::workspace_traits
Traits class providing information on the type and location of workspace vectors inside a solver.
Definition: solver_base.hpp:238
gko::solver::multigrid::mid_smooth_type
mid_smooth_type
mid_smooth_type gives the options to handle the middle smoother behavior between the two cycles in th...
Definition: multigrid.hpp:72
gko::solver::Multigrid::parameters_type::mid_smoother
std::vector< std::shared_ptr< const LinOpFactory > > mid_smoother
Mid-smooth Factory list.
Definition: multigrid.hpp:301
gko::solver::Multigrid::parameters_type::solver_selector
std::function< size_type(const size_type, const LinOp *)> solver_selector
Custom coarsest_solver selector size_type (size_type level, const LinOp *coarsest_matrix) Selector fu...
Definition: multigrid.hpp:367
gko::solver::multigrid::cycle
cycle
cycle defines which kind of multigrid cycle can be used.
Definition: multigrid.hpp:56
gko::solver::Multigrid::parameters_type::smoother_relax
std::complex< double > smoother_relax
smoother_relax is the relaxation factor of default generated smoother.
Definition: multigrid.hpp:400
gko::LinOp::LinOp
LinOp(const LinOp &)=default
Copy-constructs a LinOp.
gko::solver::Multigrid::parameters_type::post_uses_pre
bool post_uses_pre
Whether post-smoothing-related calls use corresponding pre-smoothing-related calls.
Definition: multigrid.hpp:307