Ginkgo: ginkgo/core/solver/multigrid.hpp Source File

| Ginkgo Generated from branch based on main. Ginkgo version 1.11.0 A numerical linear algebra library targeting many-core architectures

Loading...
Searching...
No Matches
multigrid.hpp
1// SPDX-FileCopyrightText: 2017 - 2024 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
33namespace gko {
34namespace solver {
35
36
42 namespace multigrid {
43
44
54 enum class cycle { v, f, w };
55
56
70 enum class mid_smooth_type { both, post_smoother, pre_smoother, standalone };
71
72
73namespace detail {
74
75
76// It should only be used internally
77class MultigridState;
78
79
80} // namespace detail
81} // namespace multigrid
82
83
107 class Multigrid : public EnableLinOp<Multigrid>,
108 public EnableSolverBase<Multigrid>,
109 public EnableIterativeBase<Multigrid>,
110 public EnableApplyWithInitialGuess<Multigrid> {
111 friend class EnableLinOp<Multigrid>;
112 friend class EnablePolymorphicObject<Multigrid, LinOp>;
113 friend class EnableApplyWithInitialGuess<Multigrid>;
114
115public:
122 bool apply_uses_initial_guess() const override
123 {
124 return this->get_default_initial_guess() ==
125 initial_guess_mode::provided;
126 }
127
133 std::vector<std::shared_ptr<const gko::multigrid::MultigridLevel>>
134 get_mg_level_list() const
135 {
136 return mg_level_list_;
137 }
138
144 std::vector<std::shared_ptr<const LinOp>> get_pre_smoother_list() const
145 {
146 return pre_smoother_list_;
147 }
148
154 std::vector<std::shared_ptr<const LinOp>> get_mid_smoother_list() const
155 {
156 return mid_smoother_list_;
157 }
158
164 std::vector<std::shared_ptr<const LinOp>> get_post_smoother_list() const
165 {
166 return post_smoother_list_;
167 }
168
174 std::shared_ptr<const LinOp> get_coarsest_solver() const
175 {
176 return coarsest_solver_;
177 }
178
184 multigrid::cycle get_cycle() const { return parameters_.cycle; }
185
191 void set_cycle(multigrid::cycle cycle) { parameters_.cycle = cycle; }
192
193
194 class Factory;
195
196 struct parameters_type
197 : public enable_iterative_solver_factory_parameters<parameters_type,
198 Factory> {
202 std::vector<std::shared_ptr<const LinOpFactory>>
203 GKO_DEFERRED_FACTORY_VECTOR_PARAMETER(mg_level);
204
231 std::function<size_type(const size_type, const LinOp*)>
232 GKO_FACTORY_PARAMETER_SCALAR(level_selector, nullptr);
233
248 std::vector<std::shared_ptr<const LinOpFactory>>
249 GKO_DEFERRED_FACTORY_VECTOR_PARAMETER(pre_smoother);
250
256 std::vector<std::shared_ptr<const LinOpFactory>>
257 GKO_DEFERRED_FACTORY_VECTOR_PARAMETER(post_smoother);
258
266 std::vector<std::shared_ptr<const LinOpFactory>>
267 GKO_DEFERRED_FACTORY_VECTOR_PARAMETER(mid_smoother);
268
273 bool GKO_FACTORY_PARAMETER_SCALAR(post_uses_pre, true);
274
282 multigrid::mid_smooth_type GKO_FACTORY_PARAMETER_SCALAR(
283 mid_case, multigrid::mid_smooth_type::standalone);
284
292 size_type GKO_FACTORY_PARAMETER_SCALAR(max_levels, 10);
293
299 size_type GKO_FACTORY_PARAMETER_SCALAR(min_coarse_rows, 64);
300
307 std::vector<std::shared_ptr<const LinOpFactory>>
308 GKO_DEFERRED_FACTORY_VECTOR_PARAMETER(coarsest_solver);
309
332 std::function<size_type(const size_type, const LinOp*)>
333 GKO_FACTORY_PARAMETER_SCALAR(solver_selector, nullptr);
334
340 multigrid::cycle GKO_FACTORY_PARAMETER_SCALAR(cycle,
341 multigrid::cycle::v);
342
351 size_type GKO_FACTORY_PARAMETER_SCALAR(kcycle_base, 1);
352
360 double GKO_FACTORY_PARAMETER_SCALAR(kcycle_rel_tol, 0.25);
361
366 std::complex<double> GKO_FACTORY_PARAMETER_SCALAR(smoother_relax, 0.9);
367
372 size_type GKO_FACTORY_PARAMETER_SCALAR(smoother_iters, 1);
373
378 initial_guess_mode GKO_FACTORY_PARAMETER_SCALAR(
379 default_initial_guess, initial_guess_mode::zero);
380 };
381 GKO_ENABLE_LIN_OP_FACTORY(Multigrid, parameters, Factory);
382 GKO_ENABLE_BUILD_METHOD(Factory);
383
398 static parameters_type parse(const config::pnode& config,
399 const config::registry& context,
400 const config::type_descriptor& td_for_child =
401 config::make_type_descriptor<>());
402
403protected:
404 void apply_impl(const LinOp* b, LinOp* x) const override;
405
406 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
407 LinOp* x) const override;
408
409 void apply_with_initial_guess_impl(const LinOp* b, LinOp* x,
410 initial_guess_mode guess) const override;
411
412 void apply_with_initial_guess_impl(const LinOp* alpha, const LinOp* b,
413 const LinOp* beta, LinOp* x,
414 initial_guess_mode guess) const override;
415
416 template <typename VectorType>
417 void apply_dense_impl(const VectorType* b, VectorType* x,
418 initial_guess_mode guess) const;
419
424 void generate();
425
426 explicit Multigrid(std::shared_ptr<const Executor> exec);
427
428 explicit Multigrid(const Factory* factory,
429 std::shared_ptr<const LinOp> system_matrix);
430
434 void validate();
435
445 void verify_legal_length(bool checked, size_type len, size_type ref_len);
446
447 void create_state() const;
448
449private:
450 std::vector<std::shared_ptr<const gko::multigrid::MultigridLevel>>
451 mg_level_list_{};
452 std::vector<std::shared_ptr<const LinOp>> pre_smoother_list_{};
453 std::vector<std::shared_ptr<const LinOp>> mid_smoother_list_{};
454 std::vector<std::shared_ptr<const LinOp>> post_smoother_list_{};
455 std::shared_ptr<const LinOp> coarsest_solver_{};
456 std::function<size_type(const size_type, const LinOp*)> level_selector_;
457 std::function<size_type(const size_type, const LinOp*)> solver_selector_;
458
469 mutable struct cache_struct {
470 cache_struct() = default;
471
472 ~cache_struct() = default;
473
474 cache_struct(const cache_struct&) {}
475
476 cache_struct(cache_struct&&) {}
477
478 cache_struct& operator=(const cache_struct&) { return *this; }
479
480 cache_struct& operator=(cache_struct&&) { return *this; }
481
482 // unique_ptr with default destructor does not work with the incomplete
483 // type.
484 std::shared_ptr<multigrid::detail::MultigridState> state{};
485 } cache_;
486};
487
488template <>
489 struct workspace_traits<Multigrid> {
490 using Solver = Multigrid;
491 // number of vectors used by this workspace
492 static int num_vectors(const Solver&);
493 // number of arrays used by this workspace
494 static int num_arrays(const Solver&);
495 // array containing the num_vectors names for the workspace vectors
496 static std::vector<std::string> op_names(const Solver&);
497 // array containing the num_arrays names for the workspace vectors
498 static std::vector<std::string> array_names(const Solver&);
499 // array containing all varying scalar vectors (independent of problem size)
500 static std::vector<int> scalars(const Solver&);
501 // array containing all varying vectors (dependent on problem size)
502 static std::vector<int> vectors(const Solver&);
503
504 // stopping status array
505 constexpr static int stop = 0;
506};
507
508
509} // namespace solver
510} // namespace gko
511
512
513#endif // GKO_PUBLIC_CORE_SOLVER_MULTIGRID_HPP_
gko::EnableLinOp
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition lin_op.hpp:879
gko::EnablePolymorphicObject
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:668
gko::LinOp
Definition lin_op.hpp:117
gko::config::pnode
pnode describes a tree of properties.
Definition property_tree.hpp:28
gko::config::registry
This class stores additional context for creating Ginkgo objects from configuration files.
Definition registry.hpp:167
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::solver::Factory
Definition ir.hpp:184
gko::solver::Multigrid::Factory
Definition multigrid.hpp:381
gko::solver::Multigrid
Multigrid methods have a hierarchy of many levels, whose corase level is a subset of the fine level,...
Definition multigrid.hpp:110
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:174
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:134
gko::solver::Multigrid::set_cycle
void set_cycle(multigrid::cycle cycle)
Set the cycle of multigrid.
Definition multigrid.hpp:191
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:154
gko::solver::Multigrid::get_cycle
multigrid::cycle get_cycle() const
Get the cycle of multigrid.
Definition multigrid.hpp:184
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:122
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:144
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:164
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_FACTORY_PARAMETER_SCALAR
#define GKO_FACTORY_PARAMETER_SCALAR(_name, _default)
Creates a scalar factory parameter in the factory parameters structure.
Definition abstract_factory.hpp:445
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_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::solver::multigrid
The solver multigrid namespace.
Definition multigrid.hpp:42
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::cycle
cycle
cycle defines which kind of multigrid cycle can be used.
Definition multigrid.hpp:54
gko::solver
The ginkgo Solve namespace.
Definition bicg.hpp:28
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::initial_guess_mode::provided
@ provided
the input is provided
Definition solver_base.hpp:45
gko::solver::initial_guess_mode::zero
@ zero
the input is zero
Definition solver_base.hpp:37
gko
The Ginkgo namespace.
Definition abstract_factory.hpp:20
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:90
gko::solver::Multigrid::parameters_type
Definition multigrid.hpp:198
gko::solver::Multigrid::parameters_type::coarsest_solver
std::vector< std::shared_ptr< const LinOpFactory > > coarsest_solver
Coarsest factory list.
Definition multigrid.hpp:308
gko::solver::Multigrid::parameters_type::post_smoother
std::vector< std::shared_ptr< const LinOpFactory > > post_smoother
Post-smooth Factory list.
Definition multigrid.hpp:257
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:292
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:366
gko::solver::Multigrid::parameters_type::min_coarse_rows
size_type min_coarse_rows
The minimal number of coarse rows.
Definition multigrid.hpp:299
gko::solver::Multigrid::parameters_type::default_initial_guess
initial_guess_mode default_initial_guess
Default initial guess mode.
Definition multigrid.hpp:379
gko::solver::Multigrid::parameters_type::pre_smoother
std::vector< std::shared_ptr< const LinOpFactory > > pre_smoother
Pre-smooth Factory list.
Definition multigrid.hpp:249
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:351
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:372
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:232
gko::solver::Multigrid::parameters_type::cycle
multigrid::cycle cycle
Multigrid cycle type.
Definition multigrid.hpp:341
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:283
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:273
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:333
gko::solver::Multigrid::parameters_type::mid_smoother
std::vector< std::shared_ptr< const LinOpFactory > > mid_smoother
Mid-smooth Factory list.
Definition multigrid.hpp:267
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:360
gko::solver::Multigrid::parameters_type::mg_level
std::vector< std::shared_ptr< const LinOpFactory > > mg_level
MultigridLevel Factory list.
Definition multigrid.hpp:203
gko::solver::enable_iterative_solver_factory_parameters
Definition solver_base.hpp:844
gko::solver::workspace_traits
Traits class providing information on the type and location of workspace vectors inside a solver.
Definition solver_base.hpp:238