Ginkgo Generated from branch based on main. Ginkgo version 1.9.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#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
80template <typename ValueType = default_precision>
81class Ir : public EnableLinOp<Ir<ValueType>>,
82 public EnableSolverBase<Ir<ValueType>>,
83 public EnableIterativeBase<Ir<ValueType>>,
84 public EnableApplyWithInitialGuess<Ir<ValueType>>,
85 public Transposable {
86 friend class EnableLinOp<Ir>;
87 friend class EnablePolymorphicObject<Ir, LinOp>;
88 friend class EnableApplyWithInitialGuess<Ir>;
89
90public:
91 using value_type = ValueType;
92 using transposed_type = Ir<ValueType>;
93
94 std::unique_ptr<LinOp> transpose() const override;
95
96 std::unique_ptr<LinOp> conj_transpose() const override;
97
103 bool apply_uses_initial_guess() const override
104 {
105 return this->get_default_initial_guess() ==
107 }
108
114 std::shared_ptr<const LinOp> get_solver() const { return solver_; }
115
121 void set_solver(std::shared_ptr<const LinOp> new_solver);
122
129 Ir& operator=(const Ir&);
130
139
144 Ir(const Ir&);
145
151 Ir(Ir&&);
152
153 class Factory;
154
156 : enable_iterative_solver_factory_parameters<parameters_type, Factory> {
160 std::shared_ptr<const LinOpFactory> GKO_DEFERRED_FACTORY_PARAMETER(
162
167 std::shared_ptr<const LinOp> GKO_FACTORY_PARAMETER_SCALAR(
169
174 value_type{1});
175
182 };
183 GKO_ENABLE_LIN_OP_FACTORY(Ir, parameters, Factory);
185
199 static parameters_type parse(const config::pnode& config,
200 const config::registry& context,
201 const config::type_descriptor& td_for_child =
202 config::make_type_descriptor<ValueType>());
203
204protected:
205 void apply_impl(const LinOp* b, LinOp* x) const override;
206
207 template <typename VectorType>
208 void apply_dense_impl(const VectorType* b, VectorType* x,
209 initial_guess_mode guess) const;
210
211 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
212 LinOp* x) const override;
213
214 void apply_with_initial_guess_impl(const LinOp* b, LinOp* x,
215 initial_guess_mode guess) const override;
216
217 void apply_with_initial_guess_impl(const LinOp* alpha, const LinOp* b,
218 const LinOp* beta, LinOp* x,
219 initial_guess_mode guess) const override;
220
221 void set_relaxation_factor(
222 std::shared_ptr<const matrix::Dense<ValueType>> new_factor);
223
224 explicit Ir(std::shared_ptr<const Executor> exec)
225 : EnableLinOp<Ir>(std::move(exec))
226 {}
227
228 explicit Ir(const Factory* factory,
229 std::shared_ptr<const LinOp> system_matrix)
230 : EnableLinOp<Ir>(factory->get_executor(),
231 gko::transpose(system_matrix->get_size())),
232 EnableSolverBase<Ir>{std::move(system_matrix)},
233 EnableIterativeBase<Ir>{
234 stop::combine(factory->get_parameters().criteria)},
235 parameters_{factory->get_parameters()}
236 {
237 if (parameters_.generated_solver) {
238 this->set_solver(parameters_.generated_solver);
239 } else if (parameters_.solver) {
240 this->set_solver(
241 parameters_.solver->generate(this->get_system_matrix()));
242 } else {
244 this->get_executor(), this->get_size()[0]));
245 }
246 this->set_default_initial_guess(parameters_.default_initial_guess);
247 relaxation_factor_ = gko::initialize<matrix::Dense<ValueType>>(
248 {parameters_.relaxation_factor}, this->get_executor());
249 }
250
251private:
252 std::shared_ptr<const LinOp> solver_{};
253 std::shared_ptr<const matrix::Dense<ValueType>> relaxation_factor_{};
254};
255
256
257template <typename ValueType = default_precision>
258using Richardson = Ir<ValueType>;
259
260
261template <typename ValueType>
262struct workspace_traits<Ir<ValueType>> {
263 using Solver = Ir<ValueType>;
264 // number of vectors used by this workspace
265 static int num_vectors(const Solver&);
266 // number of arrays used by this workspace
267 static int num_arrays(const Solver&);
268 // array containing the num_vectors names for the workspace vectors
269 static std::vector<std::string> op_names(const Solver&);
270 // array containing the num_arrays names for the workspace vectors
271 static std::vector<std::string> array_names(const Solver&);
272 // array containing all varying scalar vectors (independent of problem size)
273 static std::vector<int> scalars(const Solver&);
274 // array containing all varying vectors (dependent on problem size)
275 static std::vector<int> vectors(const Solver&);
276
277 // residual vector
278 constexpr static int residual = 0;
279 // inner solution vector
280 constexpr static int inner_solution = 1;
281 // constant 1.0 scalar
282 constexpr static int one = 2;
283 // constant -1.0 scalar
284 constexpr static int minus_one = 3;
285
286 // stopping status array
287 constexpr static int stop = 0;
288};
289
290
301template <typename ValueType>
302auto build_smoother(std::shared_ptr<const LinOpFactory> factory,
303 size_type iteration = 1, ValueType relaxation_factor = 0.9)
304{
305 auto exec = factory->get_executor();
306 return Ir<ValueType>::build()
307 .with_solver(factory)
308 .with_relaxation_factor(relaxation_factor)
309 .with_criteria(gko::stop::Iteration::build().with_max_iters(iteration))
310 .on(exec);
311}
312
325template <typename ValueType>
326auto build_smoother(std::shared_ptr<const LinOp> solver,
327 size_type iteration = 1, ValueType relaxation_factor = 0.9)
328{
329 auto exec = solver->get_executor();
330 return Ir<ValueType>::build()
331 .with_generated_solver(solver)
332 .with_relaxation_factor(relaxation_factor)
333 .with_criteria(gko::stop::Iteration::build().with_max_iters(iteration))
334 .on(exec);
335}
336
337
338} // namespace solver
339} // namespace gko
340
341
342#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:879
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:662
Definition lin_op.hpp:117
const dim< 2 > & get_size() const noexcept
Returns the size of the operator.
Definition lin_op.hpp:210
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition polymorphic_object.hpp:234
Linear operators which support transposition should implement the Transposable interface.
Definition lin_op.hpp:433
pnode describes a tree of properties.
Definition property_tree.hpp:28
This class stores additional context for creating Ginkgo objects from configuration files.
Definition registry.hpp:167
This class describes the value and index types to be used when building a Ginkgo type from a configur...
Definition type_descriptor.hpp:39
Dense is a matrix format which explicitly stores all values of the matrix.
Definition dense.hpp:117
static std::unique_ptr< Identity > create(std::shared_ptr< const Executor > exec, dim< 2 > size)
Creates an Identity matrix of the specified size.
A LinOp deriving from this CRTP class stores a system matrix.
Definition solver_base.hpp:541
Iterative refinement (IR) is an iterative method that uses another coarse method to approximate the e...
Definition ir.hpp:85
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.
static parameters_type parse(const config::pnode &config, const config::registry &context, const config::type_descriptor &td_for_child=config::make_type_descriptor< ValueType >())
Create the parameters from the property_tree.
std::shared_ptr< const LinOp > get_solver() const
Returns the solver operator used as the inner solver.
Definition ir.hpp:114
bool apply_uses_initial_guess() const override
Return true as iterative solvers use the data in x as an initial guess.
Definition ir.hpp:103
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
std::unique_ptr< Matrix > initialize(size_type stride, std::initializer_list< typename Matrix::value_type > vals, std::shared_ptr< const Executor > exec, TArgs &&... create_args)
Creates and initializes a column-vector.
Definition dense.hpp:1565
#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
std::shared_ptr< const CriterionFactory > combine(FactoryContainer &&factories)
Combines multiple criterion factories into a single combined criterion factory.
Definition combined.hpp:109
The ginkgo Solve namespace.
Definition bicg.hpp:28
initial_guess_mode
Give a initial guess mode about the input of the apply method.
Definition solver_base.hpp:33
@ provided
the input is provided
Definition solver_base.hpp:45
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:302
The Ginkgo namespace.
Definition abstract_factory.hpp:20
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:89
batch_dim< 2, DimensionType > transpose(const batch_dim< 2, DimensionType > &input)
Returns a batch_dim object with its dimensions swapped for batched operators.
Definition batch_dim.hpp:119
STL namespace.
initial_guess_mode default_initial_guess
Default initial guess mode.
Definition ir.hpp:181
std::shared_ptr< const LinOpFactory > solver
Inner solver factory.
Definition ir.hpp:161
std::shared_ptr< const LinOp > generated_solver
Already generated solver.
Definition ir.hpp:168
ValueType relaxation_factor
Relaxation factor for Richardson iteration.
Definition ir.hpp:174
Traits class providing information on the type and location of workspace vectors inside a solver.
Definition solver_base.hpp:238