Ginkgo Generated from branch based on master. Ginkgo version 1.7.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
multigrid.hpp
1/*******************************<GINKGO LICENSE>******************************
2Copyright (c) 2017-2023, the Ginkgo authors
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
91. Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in the
14documentation and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17contributors may be used to endorse or promote products derived from
18this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31******************************<GINKGO LICENSE>*******************************/
32
33#ifndef GKO_PUBLIC_CORE_SOLVER_MULTIGRID_HPP_
34#define GKO_PUBLIC_CORE_SOLVER_MULTIGRID_HPP_
35
36
37#include <complex>
38#include <functional>
39#include <memory>
40#include <utility>
41
42
43#include <ginkgo/core/base/abstract_factory.hpp>
44#include <ginkgo/core/base/array.hpp>
45#include <ginkgo/core/base/exception_helpers.hpp>
46#include <ginkgo/core/base/lin_op.hpp>
47#include <ginkgo/core/base/math.hpp>
48#include <ginkgo/core/base/precision_dispatch.hpp>
49#include <ginkgo/core/base/types.hpp>
50#include <ginkgo/core/log/logger.hpp>
51#include <ginkgo/core/matrix/dense.hpp>
52#include <ginkgo/core/matrix/identity.hpp>
53#include <ginkgo/core/multigrid/multigrid_level.hpp>
54#include <ginkgo/core/solver/solver_base.hpp>
55#include <ginkgo/core/stop/combined.hpp>
56#include <ginkgo/core/stop/criterion.hpp>
57
58
59namespace gko {
60namespace solver {
61
62
68namespace multigrid {
69
70
80enum class cycle { v, f, w };
81
82
96enum class mid_smooth_type { both, post_smoother, pre_smoother, standalone };
97
98
99namespace detail {
100
101
102// It should only be used internally
103class MultigridState;
104
105
106} // namespace detail
107} // namespace multigrid
108
109
133class Multigrid : public EnableLinOp<Multigrid>,
134 public EnableSolverBase<Multigrid>,
135 public EnableIterativeBase<Multigrid>,
136 public EnableApplyWithInitialGuess<Multigrid> {
137 friend class EnableLinOp<Multigrid>;
140
141public:
148 bool apply_uses_initial_guess() const override
149 {
150 return this->get_default_initial_guess() ==
152 }
153
159 std::vector<std::shared_ptr<const gko::multigrid::MultigridLevel>>
161 {
162 return mg_level_list_;
163 }
164
170 std::vector<std::shared_ptr<const LinOp>> get_pre_smoother_list() const
171 {
172 return pre_smoother_list_;
173 }
174
180 std::vector<std::shared_ptr<const LinOp>> get_mid_smoother_list() const
181 {
182 return mid_smoother_list_;
183 }
184
190 std::vector<std::shared_ptr<const LinOp>> get_post_smoother_list() const
191 {
192 return post_smoother_list_;
193 }
194
200 std::shared_ptr<const LinOp> get_coarsest_solver() const
201 {
202 return coarsest_solver_;
203 }
204
210 multigrid::cycle get_cycle() const { return parameters_.cycle; }
211
217 void set_cycle(multigrid::cycle cycle) { parameters_.cycle = cycle; }
218
219
220 class Factory;
221
223 : public enable_iterative_solver_factory_parameters<parameters_type,
224 Factory> {
228 std::vector<std::shared_ptr<const LinOpFactory>>
229 GKO_DEFERRED_FACTORY_VECTOR_PARAMETER(mg_level);
230
257 std::function<size_type(const size_type, const LinOp*)>
259
274 std::vector<std::shared_ptr<const LinOpFactory>>
275 GKO_DEFERRED_FACTORY_VECTOR_PARAMETER(pre_smoother);
276
282 std::vector<std::shared_ptr<const LinOpFactory>>
283 GKO_DEFERRED_FACTORY_VECTOR_PARAMETER(post_smoother);
284
292 std::vector<std::shared_ptr<const LinOpFactory>>
293 GKO_DEFERRED_FACTORY_VECTOR_PARAMETER(mid_smoother);
294
300
309 mid_case, multigrid::mid_smooth_type::standalone);
310
319
326
333 std::vector<std::shared_ptr<const LinOpFactory>>
334 GKO_DEFERRED_FACTORY_VECTOR_PARAMETER(coarsest_solver);
335
358 std::function<size_type(const size_type, const LinOp*)>
360
367 multigrid::cycle::v);
368
378
387
393
399
406 };
409
410protected:
411 void apply_impl(const LinOp* b, LinOp* x) const override;
412
413 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
414 LinOp* x) const override;
415
416 void apply_with_initial_guess_impl(const LinOp* b, LinOp* x,
417 initial_guess_mode guess) const override;
418
419 void apply_with_initial_guess_impl(const LinOp* alpha, const LinOp* b,
420 const LinOp* beta, LinOp* x,
421 initial_guess_mode guess) const override;
422
423 template <typename VectorType>
424 void apply_dense_impl(const VectorType* b, VectorType* x,
426
431 void generate();
432
433 explicit Multigrid(std::shared_ptr<const Executor> exec);
434
435 explicit Multigrid(const Factory* factory,
436 std::shared_ptr<const LinOp> system_matrix);
437
441 void validate();
442
452 void verify_legal_length(bool checked, size_type len, size_type ref_len);
453
454 void create_state() const;
455
456private:
457 std::vector<std::shared_ptr<const gko::multigrid::MultigridLevel>>
458 mg_level_list_{};
459 std::vector<std::shared_ptr<const LinOp>> pre_smoother_list_{};
460 std::vector<std::shared_ptr<const LinOp>> mid_smoother_list_{};
461 std::vector<std::shared_ptr<const LinOp>> post_smoother_list_{};
462 std::shared_ptr<const LinOp> coarsest_solver_{};
463 std::function<size_type(const size_type, const LinOp*)> level_selector_;
464 std::function<size_type(const size_type, const LinOp*)> solver_selector_;
465
476 mutable struct cache_struct {
477 cache_struct() = default;
478
479 ~cache_struct() = default;
480
481 cache_struct(const cache_struct&) {}
482
483 cache_struct(cache_struct&&) {}
484
485 cache_struct& operator=(const cache_struct&) { return *this; }
486
487 cache_struct& operator=(cache_struct&&) { return *this; }
488
489 // unique_ptr with default destructor does not work with the incomplete
490 // type.
491 std::shared_ptr<multigrid::detail::MultigridState> state{};
492 } cache_;
493};
494
495template <>
497 using Solver = Multigrid;
498 // number of vectors used by this workspace
499 static int num_vectors(const Solver&);
500 // number of arrays used by this workspace
501 static int num_arrays(const Solver&);
502 // array containing the num_vectors names for the workspace vectors
503 static std::vector<std::string> op_names(const Solver&);
504 // array containing the num_arrays names for the workspace vectors
505 static std::vector<std::string> array_names(const Solver&);
506 // array containing all varying scalar vectors (independent of problem size)
507 static std::vector<int> scalars(const Solver&);
508 // array containing all varying vectors (dependent on problem size)
509 static std::vector<int> vectors(const Solver&);
510
511 // stopping status array
512 constexpr static int stop = 0;
513};
514
515
516} // namespace solver
517} // namespace gko
518
519
520#endif // GKO_PUBLIC_CORE_SOLVER_MULTIGRID_HPP_
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition lin_op.hpp:908
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:691
Definition lin_op.hpp:146
EnableApplyWithInitialGuess providing default operation for ApplyWithInitialGuess with correct valida...
Definition solver_base.hpp:190
A LinOp deriving from this CRTP class stores a stopping criterion factory and allows applying with a ...
Definition solver_base.hpp:732
A LinOp deriving from this CRTP class stores a system matrix.
Definition solver_base.hpp:570
Definition multigrid.hpp:407
Multigrid methods have a hierarchy of many levels, whose corase level is a subset of the fine level,...
Definition multigrid.hpp:136
std::shared_ptr< const LinOp > get_coarsest_solver() const
Gets the operator at the coarsest level.
Definition multigrid.hpp:200
std::vector< std::shared_ptr< const gko::multigrid::MultigridLevel > > get_mg_level_list() const
Gets the list of MultigridLevel operators.
Definition multigrid.hpp:160
void set_cycle(multigrid::cycle cycle)
Set the cycle of multigrid.
Definition multigrid.hpp:217
std::vector< std::shared_ptr< const LinOp > > get_mid_smoother_list() const
Gets the list of mid-smoother operators.
Definition multigrid.hpp:180
multigrid::cycle get_cycle() const
Get the cycle of multigrid.
Definition multigrid.hpp:210
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:148
std::vector< std::shared_ptr< const LinOp > > get_pre_smoother_list() const
Gets the list of pre-smoother operators.
Definition multigrid.hpp:170
std::vector< std::shared_ptr< const LinOp > > get_post_smoother_list() const
Gets the list of post-smoother operators.
Definition multigrid.hpp:190
#define GKO_FACTORY_PARAMETER_SCALAR(_name, _default)
Creates a scalar factory parameter in the factory parameters structure.
Definition abstract_factory.hpp:473
#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:422
#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:1046
mid_smooth_type
mid_smooth_type gives the options to handle the middle smoother behavior between the two cycles in th...
Definition multigrid.hpp:96
cycle
cycle defines which kind of multigrid cycle can be used.
Definition multigrid.hpp:80
initial_guess_mode
Give a initial guess mode about the input of the apply method.
Definition solver_base.hpp:62
@ provided
the input is provided
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120
Definition multigrid.hpp:224
std::vector< std::shared_ptr< const LinOpFactory > > coarsest_solver
Coarsest factory list.
Definition multigrid.hpp:334
std::vector< std::shared_ptr< const LinOpFactory > > post_smoother
Post-smooth Factory list.
Definition multigrid.hpp:283
size_type max_levels
The maximum number of mg_level (without coarsest solver level) that can be used.
Definition multigrid.hpp:318
std::complex< double > smoother_relax
smoother_relax is the relaxation factor of default generated smoother.
Definition multigrid.hpp:392
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:359
size_type min_coarse_rows
The minimal number of coarse rows.
Definition multigrid.hpp:325
initial_guess_mode default_initial_guess
Default initial guess mode.
Definition multigrid.hpp:405
std::vector< std::shared_ptr< const LinOpFactory > > pre_smoother
Pre-smooth Factory list.
Definition multigrid.hpp:275
size_type kcycle_base
kcycle_base is a factor to choose how often enable FCG/GCR step.
Definition multigrid.hpp:377
size_type smoother_iters
smoother_iters is the number of iteration of default generated smoother.
Definition multigrid.hpp:398
multigrid::cycle cycle
Multigrid cycle type.
Definition multigrid.hpp:367
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:309
bool post_uses_pre
Whether post-smoothing-related calls use corresponding pre-smoothing-related calls.
Definition multigrid.hpp:299
std::vector< std::shared_ptr< const LinOpFactory > > mid_smoother
Mid-smooth Factory list.
Definition multigrid.hpp:293
double kcycle_rel_tol
kcycle_rel_tol decides whether run the second iteration of FCG/GCR step.
Definition multigrid.hpp:386
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:258
std::vector< std::shared_ptr< const LinOpFactory > > mg_level
MultigridLevel Factory list.
Definition multigrid.hpp:229
Traits class providing information on the type and location of workspace vectors inside a solver.
Definition solver_base.hpp:267