ginkgo/core/solver/gcr.hpp Source File

ginkgo/core/solver/gcr.hpp Source File#

Reference API: ginkgo/core/solver/gcr.hpp Source File
Reference API
gcr.hpp
1// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#ifndef GKO_PUBLIC_CORE_SOLVER_GCR_HPP_
6#define GKO_PUBLIC_CORE_SOLVER_GCR_HPP_
7
8
9#include <vector>
10
11#include <ginkgo/core/base/array.hpp>
12#include <ginkgo/core/base/exception_helpers.hpp>
13#include <ginkgo/core/base/lin_op.hpp>
14#include <ginkgo/core/base/math.hpp>
15#include <ginkgo/core/base/types.hpp>
16#include <ginkgo/core/config/config.hpp>
17#include <ginkgo/core/config/registry.hpp>
18#include <ginkgo/core/log/logger.hpp>
19#include <ginkgo/core/matrix/dense.hpp>
20#include <ginkgo/core/matrix/identity.hpp>
21#include <ginkgo/core/solver/solver_base.hpp>
22#include <ginkgo/core/stop/combined.hpp>
23#include <ginkgo/core/stop/criterion.hpp>
24
25
26namespace gko {
27namespace solver {
28
29
30constexpr size_type gcr_default_krylov_dim = 100u;
31
32
46template <typename ValueType = default_precision>
47class Gcr
48 : public EnableLinOp<Gcr<ValueType>>,
49 public EnablePreconditionedIterativeSolver<ValueType, Gcr<ValueType>>,
50 public Transposable {
51 friend class EnableLinOp<Gcr>;
52 friend class EnablePolymorphicObject<Gcr, LinOp>;
53
54public:
55 using value_type = ValueType;
57
58 std::unique_ptr<LinOp> transpose() const override;
59
60 std::unique_ptr<LinOp> conj_transpose() const override;
61
67 bool apply_uses_initial_guess() const override { return true; }
68
74 size_type get_krylov_dim() const { return parameters_.krylov_dim; }
75
81 void set_krylov_dim(size_type other) { parameters_.krylov_dim = other; }
82
83 class Factory;
84
93
107 static parameters_type parse(const config::pnode& config,
108 const config::registry& context,
109 const config::type_descriptor& td_for_child =
110 config::make_type_descriptor<ValueType>());
111
112protected:
113 void apply_impl(const LinOp* b, LinOp* x) const override;
114
115 template <typename VectorType>
116 void apply_dense_impl(const VectorType* b, VectorType* x) const;
117
118 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
119 LinOp* x) const override;
120
121 explicit Gcr(std::shared_ptr<const Executor> exec)
122 : EnableLinOp<Gcr>(std::move(exec))
123 {}
124
125 explicit Gcr(const Factory* factory,
126 std::shared_ptr<const LinOp> system_matrix)
127 : EnableLinOp<Gcr>(factory->get_executor(),
128 gko::transpose(system_matrix->get_size())),
129 EnablePreconditionedIterativeSolver<ValueType, Gcr<ValueType>>{
130 std::move(system_matrix), factory->get_parameters()},
131 parameters_{factory->get_parameters()}
132 {
133 if (!parameters_.krylov_dim) {
134 parameters_.krylov_dim = gcr_default_krylov_dim;
135 }
136 }
137};
138
139
140template <typename ValueType>
141struct workspace_traits<Gcr<ValueType>> {
142 using Solver = Gcr<ValueType>;
143 // number of vectors used by this workspace
144 static int num_vectors(const Solver&);
145 // number of arrays used by this workspace
146 static int num_arrays(const Solver&);
147 // array containing the num_vectors names for the workspace vectors
148 static std::vector<std::string> op_names(const Solver&);
149 // array containing the num_arrays names for the workspace vectors
150 static std::vector<std::string> array_names(const Solver&);
151 // array containing all varying scalar vectors (independent of problem size)
152 static std::vector<int> scalars(const Solver&);
153 // array containing all varying vectors (dependent on problem size)
154 static std::vector<int> vectors(const Solver&);
155
156 // residual vector
157 constexpr static int residual = 0;
158 // preconditioned vector
159 constexpr static int precon_residual = 1;
160 // A* preconditioned vector
161 constexpr static int A_precon_residual = 2;
162 // krylov bases (p in the algorithm)
163 constexpr static int krylov_bases_p = 3;
164 // mapped krylov bases (Ap in the algorithm)
165 constexpr static int mapped_krylov_bases_Ap = 4;
166 // tmp rAp parameter (r dot Ap in the algorithm)
167 constexpr static int tmp_rAp = 5;
168 // tmp minus beta parameter (-beta in the algorithm)
169 constexpr static int tmp_minus_beta = 6;
170 // array of norms of Ap
171 constexpr static int Ap_norms = 7;
172 // residual norm scalar
173 constexpr static int residual_norm = 8;
174 // constant 1.0 scalar
175 constexpr static int one = 9;
176 // constant -1.0 scalar
177 constexpr static int minus_one = 10;
178
179 // stopping status array
180 constexpr static int stop = 0;
181 // reduction tmp array
182 constexpr static int tmp = 1;
183 // final iteration number array
184 constexpr static int final_iter_nums = 2;
185};
186
187
188} // namespace solver
189} // namespace gko
190
191
192#endif // GKO_PUBLIC_CORE_SOLVER_GCR_HPP_
Definition lin_op.hpp:878
Definition polymorphic_object.hpp:668
Definition lin_op.hpp:117
std::shared_ptr< const Executor > get_executor() const noexcept
Definition polymorphic_object.hpp:243
Definition lin_op.hpp:433
Definition property_tree.hpp:28
Definition registry.hpp:167
Definition type_descriptor.hpp:39
Definition gcr.hpp:91
Definition gcr.hpp:50
static parameters_type parse(const config::pnode &config, const config::registry &context, const config::type_descriptor &td_for_child=config::make_type_descriptor< ValueType >())
size_type get_krylov_dim() const
Definition gcr.hpp:74
std::unique_ptr< LinOp > conj_transpose() const override
void set_krylov_dim(size_type other)
Definition gcr.hpp:81
std::unique_ptr< LinOp > transpose() const override
bool apply_uses_initial_guess() const override
Definition gcr.hpp:67
#define GKO_FACTORY_PARAMETER_SCALAR(_name, _default)
Definition abstract_factory.hpp:445
#define GKO_ENABLE_BUILD_METHOD(_factory_name)
Definition abstract_factory.hpp:394
#define GKO_ENABLE_LIN_OP_FACTORY(_lin_op, _parameters_name, _factory_name)
Definition lin_op.hpp:1016
The Ginkgo namespace.
Definition abstract_factory.hpp:20
constexpr T one()
Definition math.hpp:630
std::size_t size_type
Definition types.hpp:89
STL namespace.
size_type krylov_dim
Definition gcr.hpp:89
Definition solver_base.hpp:238