Ginkgo  Generated from pipelines/3133041809 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/types.hpp>
20 #include <ginkgo/core/config/config.hpp>
21 #include <ginkgo/core/config/registry.hpp>
22 #include <ginkgo/core/config/type_descriptor.hpp>
23 #include <ginkgo/core/log/logger.hpp>
24 #include <ginkgo/core/matrix/identity.hpp>
25 #include <ginkgo/core/multigrid/multigrid_level.hpp>
26 #include <ginkgo/core/solver/solver_base.hpp>
27 #include <ginkgo/core/stop/combined.hpp>
28 #include <ginkgo/core/stop/criterion.hpp>
29 
30 
31 namespace gko {
32 namespace solver {
33 
34 
40 namespace multigrid {
41 
42 
54 enum class cycle { v, f, w };
55 
56 
70 enum class mid_smooth_type { both, post_smoother, pre_smoother, standalone };
71 
72 
73 namespace detail {
74 
75 
76 // It should only be used internally
77 class MultigridState;
78 
79 
80 } // namespace detail
81 } // namespace multigrid
82 
83 
149 class Multigrid : public LinOp,
150  public EnableSolverBase<Multigrid>,
151  public EnableIterativeBase<Multigrid>,
152  public EnableApplyWithInitialGuess<Multigrid> {
154 
155 public:
162  bool apply_uses_initial_guess() const override
163  {
164  return this->get_default_initial_guess() ==
166  }
167 
173  std::vector<std::shared_ptr<const gko::multigrid::MultigridLevel>>
175  {
176  return mg_level_list_;
177  }
178 
184  std::vector<std::shared_ptr<const LinOp>> get_pre_smoother_list() const
185  {
186  return pre_smoother_list_;
187  }
188 
194  std::vector<std::shared_ptr<const LinOp>> get_mid_smoother_list() const
195  {
196  return mid_smoother_list_;
197  }
198 
204  std::vector<std::shared_ptr<const LinOp>> get_post_smoother_list() const
205  {
206  return post_smoother_list_;
207  }
208 
214  std::shared_ptr<const LinOp> get_coarsest_solver() const
215  {
216  return coarsest_solver_;
217  }
218 
224  multigrid::cycle get_cycle() const { return parameters_.cycle; }
225 
231  void set_cycle(multigrid::cycle cycle) { parameters_.cycle = cycle; }
232 
233 
234  class Factory;
235 
237  : public enable_iterative_solver_factory_parameters<parameters_type,
238  Factory> {
242  std::vector<std::shared_ptr<const LinOpFactory>>
243  GKO_DEFERRED_FACTORY_VECTOR_PARAMETER(mg_level);
244 
271  std::function<size_type(const size_type, const LinOp*)>
273 
288  std::vector<std::shared_ptr<const LinOpFactory>>
289  GKO_DEFERRED_FACTORY_VECTOR_PARAMETER(pre_smoother);
290 
296  std::vector<std::shared_ptr<const LinOpFactory>>
297  GKO_DEFERRED_FACTORY_VECTOR_PARAMETER(post_smoother);
298 
306  std::vector<std::shared_ptr<const LinOpFactory>>
307  GKO_DEFERRED_FACTORY_VECTOR_PARAMETER(mid_smoother);
308 
314 
323  mid_case, multigrid::mid_smooth_type::standalone);
324 
333 
340 
347  std::vector<std::shared_ptr<const LinOpFactory>>
348  GKO_DEFERRED_FACTORY_VECTOR_PARAMETER(coarsest_solver);
349 
372  std::function<size_type(const size_type, const LinOp*)>
374 
381  multigrid::cycle::v);
382 
392 
401 
406  std::complex<double> GKO_FACTORY_PARAMETER_SCALAR(smoother_relax, 0.9);
407 
413 
420  };
421  class reuse_data_type;
425 
431  friend class Multigrid;
432 
433  struct level_data {
434  size_type index{};
435  dim<2> coarse_size{};
436  std::unique_ptr<LinOpFactory::ReuseData> level;
437  std::unique_ptr<LinOpFactory::ReuseData> pre_smoother;
438  std::unique_ptr<LinOpFactory::ReuseData> mid_smoother;
439  std::unique_ptr<LinOpFactory::ReuseData> post_smoother;
440  };
441 
442  bool initialized_ = false;
443  dim<2> size_{};
444  std::vector<level_data> levels_;
445  size_type coarsest_index_{};
446  std::unique_ptr<LinOpFactory::ReuseData> coarsest_solver_;
447  };
448 
463  static parameters_type parse(const config::pnode& config,
464  const config::registry& context,
465  const config::type_descriptor& td_for_child =
466  config::make_type_descriptor<>());
467 
468 protected:
469  void apply_impl(const AbstractMultiVector* b,
470  AbstractMultiVector* x) const override;
471 
472  void apply_impl(const AbstractMultiVector* alpha,
473  const AbstractMultiVector* b,
474  const AbstractMultiVector* beta,
475  AbstractMultiVector* x) const override;
476 
477  void apply_with_initial_guess_impl(const AbstractMultiVector* b,
479  initial_guess_mode guess) const override;
480 
481  void apply_with_initial_guess_impl(const AbstractMultiVector* alpha,
482  const AbstractMultiVector* b,
483  const AbstractMultiVector* beta,
485  initial_guess_mode guess) const override;
486 
487  void apply_with_initial_guess_prepared_impl(const AbstractMultiVector* b,
489  initial_guess_mode guess) const;
490 
499  void generate(reuse_data_type* reuse_data);
500 
515  void generate_smoothers(
516  std::shared_ptr<const gko::multigrid::MultigridLevel> mg_level,
517  size_type index,
518  std::vector<std::shared_ptr<const LinOp>>& pre_smoother_list,
519  std::vector<std::shared_ptr<const LinOp>>& mid_smoother_list,
520  std::vector<std::shared_ptr<const LinOp>>& post_smoother_list,
521  reuse_data_type::level_data* level_reuse);
522 
538  std::unique_ptr<LinOp> generate_coarsest_solver(
539  std::shared_ptr<const gko::multigrid::MultigridLevel> last_mg_level,
540  size_type level, std::shared_ptr<const LinOp> matrix,
541  std::unique_ptr<LinOpFactory::ReuseData>* reuse_data,
542  const size_type* recorded_index, size_type* selected_index);
543 
544  explicit Multigrid(std::shared_ptr<const Executor> exec);
545 
546  explicit Multigrid(const Factory* factory,
547  std::shared_ptr<const LinOp> system_matrix);
548 
549  Multigrid(const Factory* factory,
550  std::shared_ptr<const LinOp> system_matrix,
551  reuse_data_type& reuse_data);
552 
553  Multigrid(const Factory* factory,
554  std::shared_ptr<const LinOp> system_matrix,
555  reuse_data_type* reuse_data);
556 
558  static void check_reuse_consistent(const Factory* factory,
559  const LinOp* input,
560  const reuse_data_type& reuse_data);
561 
565  void validate();
566 
576  void verify_legal_length(bool checked, size_type len, size_type ref_len);
577 
578  void create_state() const;
579 
580 private:
581  std::vector<std::shared_ptr<const gko::multigrid::MultigridLevel>>
582  mg_level_list_{};
583  std::vector<std::shared_ptr<const LinOp>> pre_smoother_list_{};
584  std::vector<std::shared_ptr<const LinOp>> mid_smoother_list_{};
585  std::vector<std::shared_ptr<const LinOp>> post_smoother_list_{};
586  std::shared_ptr<const LinOp> coarsest_solver_{};
587  std::function<size_type(const size_type, const LinOp*)> level_selector_;
588  std::function<size_type(const size_type, const LinOp*)> solver_selector_;
589 
600  mutable struct cache_struct {
601  cache_struct() = default;
602 
603  ~cache_struct() = default;
604 
605  cache_struct(const cache_struct&) {}
606 
607  cache_struct(cache_struct&&) noexcept {}
608 
609  cache_struct& operator=(const cache_struct&) { return *this; }
610 
611  cache_struct& operator=(cache_struct&&) noexcept { return *this; }
612 
613  // unique_ptr with default destructor does not work with the incomplete
614  // type.
615  std::shared_ptr<multigrid::detail::MultigridState> state{};
616  } cache_;
617 };
618 
619 template <>
621  using Solver = Multigrid;
622  // number of vectors used by this workspace
623  static int num_vectors(const Solver&);
624  // number of arrays used by this workspace
625  static int num_arrays(const Solver&);
626  // array containing the num_vectors names for the workspace vectors
627  static std::vector<std::string> op_names(const Solver&);
628  // array containing the num_arrays names for the workspace vectors
629  static std::vector<std::string> array_names(const Solver&);
630  // array containing all varying scalar vectors (independent of problem size)
631  static std::vector<int> scalars(const Solver&);
632  // array containing all varying vectors (dependent on problem size)
633  static std::vector<int> vectors(const Solver&);
634 
635  // stopping status array
636  constexpr static int stop = 0;
637 };
638 
639 
640 } // namespace solver
641 } // namespace gko
642 
643 
644 #endif // GKO_PUBLIC_CORE_SOLVER_MULTIGRID_HPP_
gko::solver::Multigrid::parameters_type
Definition: multigrid.hpp:236
gko::solver::EnableApplyWithInitialGuess
EnableApplyWithInitialGuess providing default operation for ApplyWithInitialGuess with correct valida...
Definition: solver_base.hpp:174
gko::solver::Multigrid::parameters_type::coarsest_solver
std::vector< std::shared_ptr< const LinOpFactory > > coarsest_solver
Coarsest factory list.
Definition: multigrid.hpp:348
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:728
gko::LinOp
Definition: lin_op.hpp:163
gko::solver::Multigrid::parameters_type::cycle
multigrid::cycle cycle
Multigrid cycle type.
Definition: multigrid.hpp:381
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:184
gko::solver::Multigrid::parameters_type::post_smoother
std::vector< std::shared_ptr< const LinOpFactory > > post_smoother
Post-smooth Factory list.
Definition: multigrid.hpp:297
gko::solver::Multigrid::reuse_data_type
Records the hierarchy of generate_reuse() and the reuse data of its levels, smoothers and coarsest so...
Definition: multigrid.hpp:430
gko::solver::enable_iterative_solver_factory_parameters
Definition: solver_base.hpp:1123
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:174
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:272
gko::solver::Multigrid::parameters_type::pre_smoother
std::vector< std::shared_ptr< const LinOpFactory > > pre_smoother
Pre-smooth Factory list.
Definition: multigrid.hpp:289
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:391
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:102
gko::solver::Multigrid::get_cycle
multigrid::cycle get_cycle() const
Get the cycle of multigrid.
Definition: multigrid.hpp:224
gko::solver::Multigrid
Multigrid solves a linear system by combining cheap iterative sweeps (smoothers) on a hierarchy of pr...
Definition: multigrid.hpp:149
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:400
gko::solver::Multigrid::set_cycle
void set_cycle(multigrid::cycle cycle)
Set the cycle of multigrid.
Definition: multigrid.hpp:231
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::dim< 2 >
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:323
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:194
gko::solver::Multigrid::Factory
Definition: multigrid.hpp:423
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:419
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:332
gko::solver::EnableSolverBase
A LinOp deriving from this CRTP class stores a system matrix.
Definition: solver_base.hpp:564
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:214
gko::solver::Multigrid::parameters_type::mg_level
std::vector< std::shared_ptr< const LinOpFactory > > mg_level
MultigridLevel Factory list.
Definition: multigrid.hpp:243
gko::LinOpFactory::ReuseData
Opaque base for the data a factory records in generate_reuse() to speed up later generations.
Definition: lin_op.hpp:475
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:204
gko::solver::Multigrid::parameters_type::min_coarse_rows
size_type min_coarse_rows
The minimal number of coarse rows.
Definition: multigrid.hpp:339
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:412
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:39
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::AbstractMultiVector
Abstract interface class for (multi-)vector types.
Definition: multivector.hpp:81
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:162
gko::solver::workspace_traits
Traits class providing information on the type and location of workspace vectors inside a solver.
Definition: solver_base.hpp:258
GKO_ENABLE_LIN_OP_FACTORY_WITH_REUSE
#define GKO_ENABLE_LIN_OP_FACTORY_WITH_REUSE(_lin_op, _parameters_name, _factory_name, _reuse_data_type)
Like GKO_ENABLE_LIN_OP_FACTORY, but the factory also supports LinOpFactory::generate_reuse(),...
Definition: lin_op.hpp:1265
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:70
gko::solver::Multigrid::parameters_type::mid_smoother
std::vector< std::shared_ptr< const LinOpFactory > > mid_smoother
Mid-smooth Factory list.
Definition: multigrid.hpp:307
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:373
gko::solver::multigrid::cycle
cycle
cycle defines which kind of multigrid cycle can be used.
Definition: multigrid.hpp:54
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:406
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:313