Ginkgo Generated from develop branch based on develop. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
ir.hpp
1// SPDX-FileCopyrightText: 2017 - 2024 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
12#include <ginkgo/core/base/exception_helpers.hpp>
13#include <ginkgo/core/base/lin_op.hpp>
14#include <ginkgo/core/base/types.hpp>
15#include <ginkgo/core/matrix/dense.hpp>
16#include <ginkgo/core/matrix/identity.hpp>
17#include <ginkgo/core/solver/solver_base.hpp>
18#include <ginkgo/core/stop/combined.hpp>
19#include <ginkgo/core/stop/criterion.hpp>
20#include <ginkgo/core/stop/iteration.hpp>
21
22
23namespace gko {
24namespace solver {
25
26
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
185protected:
186 void apply_impl(const LinOp* b, LinOp* x) const override;
187
188 template <typename VectorType>
189 void apply_dense_impl(const VectorType* b, VectorType* x,
191
192 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
193 LinOp* x) const override;
194
195 void apply_with_initial_guess_impl(const LinOp* b, LinOp* x,
196 initial_guess_mode guess) const override;
197
198 void apply_with_initial_guess_impl(const LinOp* alpha, const LinOp* b,
199 const LinOp* beta, LinOp* x,
200 initial_guess_mode guess) const override;
201
202 void set_relaxation_factor(
203 std::shared_ptr<const matrix::Dense<ValueType>> new_factor);
204
205 explicit Ir(std::shared_ptr<const Executor> exec)
206 : EnableLinOp<Ir>(std::move(exec))
207 {}
208
209 explicit Ir(const Factory* factory,
210 std::shared_ptr<const LinOp> system_matrix)
211 : EnableLinOp<Ir>(factory->get_executor(),
212 gko::transpose(system_matrix->get_size())),
213 EnableSolverBase<Ir>{std::move(system_matrix)},
215 stop::combine(factory->get_parameters().criteria)},
216 parameters_{factory->get_parameters()}
217 {
218 if (parameters_.generated_solver) {
219 this->set_solver(parameters_.generated_solver);
220 } else if (parameters_.solver) {
221 this->set_solver(
222 parameters_.solver->generate(this->get_system_matrix()));
223 } else {
225 this->get_executor(), this->get_size()[0]));
226 }
227 this->set_default_initial_guess(parameters_.default_initial_guess);
228 relaxation_factor_ = gko::initialize<matrix::Dense<ValueType>>(
229 {parameters_.relaxation_factor}, this->get_executor());
230 }
231
232private:
233 std::shared_ptr<const LinOp> solver_{};
234 std::shared_ptr<const matrix::Dense<ValueType>> relaxation_factor_{};
235};
236
237
238template <typename ValueType = default_precision>
239using Richardson = Ir<ValueType>;
240
241
242template <typename ValueType>
243struct workspace_traits<Ir<ValueType>> {
244 using Solver = Ir<ValueType>;
245 // number of vectors used by this workspace
246 static int num_vectors(const Solver&);
247 // number of arrays used by this workspace
248 static int num_arrays(const Solver&);
249 // array containing the num_vectors names for the workspace vectors
250 static std::vector<std::string> op_names(const Solver&);
251 // array containing the num_arrays names for the workspace vectors
252 static std::vector<std::string> array_names(const Solver&);
253 // array containing all varying scalar vectors (independent of problem size)
254 static std::vector<int> scalars(const Solver&);
255 // array containing all varying vectors (dependent on problem size)
256 static std::vector<int> vectors(const Solver&);
257
258 // residual vector
259 constexpr static int residual = 0;
260 // inner solution vector
261 constexpr static int inner_solution = 1;
262 // constant 1.0 scalar
263 constexpr static int one = 2;
264 // constant -1.0 scalar
265 constexpr static int minus_one = 3;
266
267 // stopping status array
268 constexpr static int stop = 0;
269};
270
271
282template <typename ValueType>
283auto build_smoother(std::shared_ptr<const LinOpFactory> factory,
284 size_type iteration = 1, ValueType relaxation_factor = 0.9)
285{
286 auto exec = factory->get_executor();
287 return Ir<ValueType>::build()
288 .with_solver(factory)
289 .with_relaxation_factor(relaxation_factor)
290 .with_criteria(gko::stop::Iteration::build().with_max_iters(iteration))
291 .on(exec);
292}
293
306template <typename ValueType>
307auto build_smoother(std::shared_ptr<const LinOp> solver,
308 size_type iteration = 1, ValueType relaxation_factor = 0.9)
309{
310 auto exec = solver->get_executor();
311 return Ir<ValueType>::build()
312 .with_generated_solver(solver)
313 .with_relaxation_factor(relaxation_factor)
314 .with_criteria(gko::stop::Iteration::build().with_max_iters(iteration))
315 .on(exec);
316}
317
318
319} // namespace solver
320} // namespace gko
321
322
323#endif // GKO_PUBLIC_CORE_SOLVER_IR_HPP_
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition lin_op.hpp:880
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:663
Definition lin_op.hpp:118
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition polymorphic_object.hpp:235
Linear operators which support transposition should implement the Transposable interface.
Definition lin_op.hpp:434
Dense is a matrix format which explicitly stores all values of the matrix.
Definition dense.hpp:107
static std::unique_ptr< Identity > create(std::shared_ptr< const Executor > exec, dim< 2 > size)
Creates an Identity matrix of the specified size.
EnableApplyWithInitialGuess providing default operation for ApplyWithInitialGuess with correct valida...
Definition solver_base.hpp:162
A LinOp deriving from this CRTP class stores a stopping criterion factory and allows applying with a ...
Definition solver_base.hpp:704
A LinOp deriving from this CRTP class stores a system matrix.
Definition solver_base.hpp:542
Definition ir.hpp:182
Iterative refinement (IR) is an iterative method that uses another coarse method to approximate the e...
Definition ir.hpp:84
Ir(const Ir &)
Copy-constructs an IR solver.
std::unique_ptr< LinOp > conj_transpose() const override
Returns a LinOp representing the conjugate transpose of the Transposable object.
Ir & operator=(Ir &&)
Move-assigns an IR solver.
Ir(Ir &&)
Move-constructs an IR solver.
void set_solver(std::shared_ptr< const LinOp > new_solver)
Sets the solver operator used as the inner solver.
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
std::shared_ptr< const LinOp > get_solver() const
Returns the solver operator used as the inner solver.
Definition ir.hpp:113
bool apply_uses_initial_guess() const override
Return true as iterative solvers use the data in x as an initial guess.
Definition ir.hpp:102
Ir & operator=(const Ir &)
Copy-assigns an IR solver.
#define GKO_FACTORY_PARAMETER_SCALAR(_name, _default)
Creates a scalar factory parameter in the factory parameters structure.
Definition abstract_factory.hpp:445
#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
#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:1018
std::shared_ptr< const CriterionFactory > combine(FactoryContainer &&factories)
Combines multiple criterion factories into a single combined criterion factory.
Definition combined.hpp:110
initial_guess_mode
Give a initial guess mode about the input of the apply method.
Definition solver_base.hpp:34
@ provided
the input is provided
auto build_smoother(std::shared_ptr< const LinOpFactory > factory, size_type iteration=1, ValueType relaxation_factor=0.9)
build_smoother gives a shortcut to build a smoother by IR(Richardson) with limited stop criterion(ite...
Definition ir.hpp:283
The Ginkgo namespace.
Definition abstract_factory.hpp:20
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:775
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:92
initial_guess_mode default_initial_guess
Default initial guess mode.
Definition ir.hpp:180
std::shared_ptr< const LinOpFactory > solver
Inner solver factory.
Definition ir.hpp:160
std::shared_ptr< const LinOp > generated_solver
Already generated solver.
Definition ir.hpp:167
ValueType relaxation_factor
Relaxation factor for Richardson iteration.
Definition ir.hpp:173
Traits class providing information on the type and location of workspace vectors inside a solver.
Definition solver_base.hpp:239