ginkgo/core/solver/ir.hpp Source File

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

Reference API: ginkgo/core/solver/ir.hpp Source File
Reference API
ir.hpp
1// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#ifndef GKO_PUBLIC_CORE_SOLVER_IR_HPP_
6#define GKO_PUBLIC_CORE_SOLVER_IR_HPP_
7
8
9#include <vector>
10
11#include <ginkgo/core/base/exception_helpers.hpp>
12#include <ginkgo/core/base/lin_op.hpp>
13#include <ginkgo/core/base/types.hpp>
14#include <ginkgo/core/config/config.hpp>
15#include <ginkgo/core/config/registry.hpp>
16#include <ginkgo/core/matrix/dense.hpp>
17#include <ginkgo/core/matrix/identity.hpp>
18#include <ginkgo/core/solver/solver_base.hpp>
19#include <ginkgo/core/stop/combined.hpp>
20#include <ginkgo/core/stop/criterion.hpp>
21#include <ginkgo/core/stop/iteration.hpp>
22
23
24namespace gko {
25namespace solver {
26
27
79template <typename ValueType = default_precision>
80class Ir : public EnableLinOp<Ir<ValueType>>,
81 public EnableSolverBase<Ir<ValueType>>,
82 public EnableIterativeBase<Ir<ValueType>>,
83 public EnableApplyWithInitialGuess<Ir<ValueType>>,
84 public Transposable {
85 friend class EnableLinOp<Ir>;
86 friend class EnablePolymorphicObject<Ir, LinOp>;
87 friend class EnableApplyWithInitialGuess<Ir>;
88
89public:
90 using value_type = ValueType;
92
93 std::unique_ptr<LinOp> transpose() const override;
94
95 std::unique_ptr<LinOp> conj_transpose() const override;
96
102 bool apply_uses_initial_guess() const override
103 {
104 return this->get_default_initial_guess() ==
106 }
107
113 std::shared_ptr<const LinOp> get_solver() const { return solver_; }
114
120 void set_solver(std::shared_ptr<const LinOp> new_solver);
121
128 Ir& operator=(const Ir&);
129
138
143 Ir(const Ir&);
144
150 Ir(Ir&&);
151
152 class Factory;
153
155 : enable_iterative_solver_factory_parameters<parameters_type, Factory> {
159 std::shared_ptr<const LinOpFactory> GKO_DEFERRED_FACTORY_PARAMETER(
161
166 std::shared_ptr<const LinOp> GKO_FACTORY_PARAMETER_SCALAR(
168
173 value_type{1});
174
181 };
184
198 static parameters_type parse(const config::pnode& config,
199 const config::registry& context,
200 const config::type_descriptor& td_for_child =
201 config::make_type_descriptor<ValueType>());
202
203protected:
204 void apply_impl(const LinOp* b, LinOp* x) const override;
205
206 template <typename VectorType>
207 void apply_dense_impl(const VectorType* b, VectorType* x,
208 initial_guess_mode guess) const;
209
210 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
211 LinOp* x) const override;
212
214 initial_guess_mode guess) const override;
215
216 void apply_with_initial_guess_impl(const LinOp* alpha, const LinOp* b,
217 const LinOp* beta, LinOp* x,
218 initial_guess_mode guess) const override;
219
220 void set_relaxation_factor(
221 std::shared_ptr<const matrix::Dense<ValueType>> new_factor);
222
223 explicit Ir(std::shared_ptr<const Executor> exec)
224 : EnableLinOp<Ir>(std::move(exec))
225 {}
226
227 explicit Ir(const Factory* factory,
228 std::shared_ptr<const LinOp> system_matrix)
229 : EnableLinOp<Ir>(factory->get_executor(),
230 gko::transpose(system_matrix->get_size())),
231 EnableSolverBase<Ir>{std::move(system_matrix)},
232 EnableIterativeBase<Ir>{
233 stop::combine(factory->get_parameters().criteria)},
234 parameters_{factory->get_parameters()}
235 {
236 if (parameters_.generated_solver) {
237 this->set_solver(parameters_.generated_solver);
238 } else if (parameters_.solver) {
239 this->set_solver(
240 parameters_.solver->generate(this->get_system_matrix()));
241 } else {
243 this->get_executor(), this->get_size()[0]));
244 }
246 relaxation_factor_ = gko::initialize<matrix::Dense<ValueType>>(
247 {parameters_.relaxation_factor}, this->get_executor());
248 }
249
250private:
251 std::shared_ptr<const LinOp> solver_{};
252 std::shared_ptr<const matrix::Dense<ValueType>> relaxation_factor_{};
253};
254
255
256template <typename ValueType = default_precision>
257using Richardson = Ir<ValueType>;
258
259
260template <typename ValueType>
261struct workspace_traits<Ir<ValueType>> {
262 using Solver = Ir<ValueType>;
263 // number of vectors used by this workspace
264 static int num_vectors(const Solver&);
265 // number of arrays used by this workspace
266 static int num_arrays(const Solver&);
267 // array containing the num_vectors names for the workspace vectors
268 static std::vector<std::string> op_names(const Solver&);
269 // array containing the num_arrays names for the workspace vectors
270 static std::vector<std::string> array_names(const Solver&);
271 // array containing all varying scalar vectors (independent of problem size)
272 static std::vector<int> scalars(const Solver&);
273 // array containing all varying vectors (dependent on problem size)
274 static std::vector<int> vectors(const Solver&);
275
276 // residual vector
277 constexpr static int residual = 0;
278 // inner solution vector
279 constexpr static int inner_solution = 1;
280 // constant 1.0 scalar
281 constexpr static int one = 2;
282 // constant -1.0 scalar
283 constexpr static int minus_one = 3;
284
285 // stopping status array
286 constexpr static int stop = 0;
287};
288
289
300template <typename ValueType>
301auto build_smoother(std::shared_ptr<const LinOpFactory> factory,
302 size_type iteration = 1, ValueType relaxation_factor = 0.9)
303{
304 auto exec = factory->get_executor();
305 return Ir<ValueType>::build()
306 .with_solver(factory)
307 .with_relaxation_factor(relaxation_factor)
308 .with_criteria(gko::stop::Iteration::build().with_max_iters(iteration))
309 .on(exec);
310}
311
324template <typename ValueType>
325auto build_smoother(std::shared_ptr<const LinOp> solver,
326 size_type iteration = 1, ValueType relaxation_factor = 0.9)
327{
328 auto exec = solver->get_executor();
329 return Ir<ValueType>::build()
330 .with_generated_solver(solver)
331 .with_relaxation_factor(relaxation_factor)
332 .with_criteria(gko::stop::Iteration::build().with_max_iters(iteration))
333 .on(exec);
334}
335
336
337} // namespace solver
338} // namespace gko
339
340
341#endif // GKO_PUBLIC_CORE_SOLVER_IR_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 dense.hpp:116
static std::unique_ptr< Identity > create(std::shared_ptr< const Executor > exec, dim< 2 > size)
void set_default_initial_guess(initial_guess_mode guess)
Definition solver_base.hpp:141
initial_guess_mode get_default_initial_guess() const
Definition solver_base.hpp:123
Definition solver_base.hpp:161
Definition solver_base.hpp:699
Definition solver_base.hpp:539
Definition ir.hpp:182
Definition ir.hpp:84
Ir(const Ir &)
std::unique_ptr< LinOp > conj_transpose() const override
Ir & operator=(Ir &&)
void set_solver(std::shared_ptr< const LinOp > new_solver)
std::unique_ptr< LinOp > transpose() const override
void apply_with_initial_guess_impl(const LinOp *b, LinOp *x, initial_guess_mode guess) const override
static parameters_type parse(const config::pnode &config, const config::registry &context, const config::type_descriptor &td_for_child=config::make_type_descriptor< ValueType >())
void apply_with_initial_guess_impl(const LinOp *alpha, const LinOp *b, const LinOp *beta, LinOp *x, initial_guess_mode guess) const override
std::shared_ptr< const LinOp > get_solver() const
Definition ir.hpp:113
bool apply_uses_initial_guess() const override
Definition ir.hpp:102
Ir & operator=(const Ir &)
#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
std::shared_ptr< const CriterionFactory > combine(FactoryContainer &&factories)
Definition combined.hpp:109
initial_guess_mode
Definition solver_base.hpp:33
auto build_smoother(std::shared_ptr< const LinOpFactory > factory, size_type iteration=1, ValueType relaxation_factor=0.9)
Definition ir.hpp:301
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.
initial_guess_mode default_initial_guess
Definition ir.hpp:180
std::shared_ptr< const LinOpFactory > solver
Definition ir.hpp:160
std::shared_ptr< const LinOp > generated_solver
Definition ir.hpp:167
ValueType relaxation_factor
Definition ir.hpp:173
Definition solver_base.hpp:238