Ginkgo Generated from branch based on main. Ginkgo version 1.11.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
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
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 GKO_ASSERT_SUPPORTED_VALUE_TYPE;
90
91public:
92 using value_type = ValueType;
93 using transposed_type = Ir<ValueType>;
94
95 std::unique_ptr<LinOp> transpose() const override;
96
97 std::unique_ptr<LinOp> conj_transpose() const override;
98
104 bool apply_uses_initial_guess() const override
105 {
106 return this->get_default_initial_guess() ==
108 }
109
115 std::shared_ptr<const LinOp> get_solver() const { return solver_; }
116
122 void set_solver(std::shared_ptr<const LinOp> new_solver);
123
130 Ir& operator=(const Ir&);
131
140
145 Ir(const Ir&);
146
152 Ir(Ir&&);
153
154 class Factory;
155
157 : enable_iterative_solver_factory_parameters<parameters_type, Factory> {
161 std::shared_ptr<const LinOpFactory> GKO_DEFERRED_FACTORY_PARAMETER(
163
168 std::shared_ptr<const LinOp> GKO_FACTORY_PARAMETER_SCALAR(
170
175 value_type{1});
176
183 };
184 GKO_ENABLE_LIN_OP_FACTORY(Ir, parameters, Factory);
186
200 static parameters_type parse(const config::pnode& config,
201 const config::registry& context,
202 const config::type_descriptor& td_for_child =
203 config::make_type_descriptor<ValueType>());
204
205protected:
206 void apply_impl(const LinOp* b, LinOp* x) const override;
207
208 template <typename VectorType>
209 void apply_dense_impl(const VectorType* b, VectorType* x,
210 initial_guess_mode guess) const;
211
212 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
213 LinOp* x) const override;
214
215 void apply_with_initial_guess_impl(const LinOp* b, LinOp* x,
216 initial_guess_mode guess) const override;
217
218 void apply_with_initial_guess_impl(const LinOp* alpha, const LinOp* b,
219 const LinOp* beta, LinOp* x,
220 initial_guess_mode guess) const override;
221
222 void set_relaxation_factor(
223 std::shared_ptr<const matrix::Dense<ValueType>> new_factor);
224
225 explicit Ir(std::shared_ptr<const Executor> exec)
226 : EnableLinOp<Ir>(std::move(exec))
227 {}
228
229 explicit Ir(const Factory* factory,
230 std::shared_ptr<const LinOp> system_matrix)
231 : EnableLinOp<Ir>(factory->get_executor(),
232 gko::transpose(system_matrix->get_size())),
233 EnableSolverBase<Ir>{std::move(system_matrix)},
234 EnableIterativeBase<Ir>{
235 stop::combine(factory->get_parameters().criteria)},
236 parameters_{factory->get_parameters()}
237 {
238 if (parameters_.generated_solver) {
239 this->set_solver(parameters_.generated_solver);
240 } else if (parameters_.solver) {
241 this->set_solver(
242 parameters_.solver->generate(this->get_system_matrix()));
243 } else {
245 this->get_executor(), this->get_size()[0]));
246 }
247 this->set_default_initial_guess(parameters_.default_initial_guess);
248 relaxation_factor_ = gko::initialize<matrix::Dense<ValueType>>(
249 {parameters_.relaxation_factor}, this->get_executor());
250 }
251
252private:
253 std::shared_ptr<const LinOp> solver_{};
254 std::shared_ptr<const matrix::Dense<ValueType>> relaxation_factor_{};
255};
256
257
258template <typename ValueType = default_precision>
259using Richardson = Ir<ValueType>;
260
261
262template <typename ValueType>
263struct workspace_traits<Ir<ValueType>> {
264 using Solver = Ir<ValueType>;
265 // number of vectors used by this workspace
266 static int num_vectors(const Solver&);
267 // number of arrays used by this workspace
268 static int num_arrays(const Solver&);
269 // array containing the num_vectors names for the workspace vectors
270 static std::vector<std::string> op_names(const Solver&);
271 // array containing the num_arrays names for the workspace vectors
272 static std::vector<std::string> array_names(const Solver&);
273 // array containing all varying scalar vectors (independent of problem size)
274 static std::vector<int> scalars(const Solver&);
275 // array containing all varying vectors (dependent on problem size)
276 static std::vector<int> vectors(const Solver&);
277
278 // residual vector
279 constexpr static int residual = 0;
280 // inner solution vector
281 constexpr static int inner_solution = 1;
282 // constant 1.0 scalar
283 constexpr static int one = 2;
284 // constant -1.0 scalar
285 constexpr static int minus_one = 3;
286
287 // stopping status array
288 constexpr static int stop = 0;
289};
290
291
302template <typename ValueType>
303auto build_smoother(std::shared_ptr<const LinOpFactory> factory,
304 size_type iteration = 1, ValueType relaxation_factor = 0.9)
305{
306 auto exec = factory->get_executor();
307 return Ir<ValueType>::build()
308 .with_solver(factory)
309 .with_relaxation_factor(relaxation_factor)
310 .with_criteria(gko::stop::Iteration::build().with_max_iters(iteration))
311 .on(exec);
312}
313
326template <typename ValueType>
327auto build_smoother(std::shared_ptr<const LinOp> solver,
328 size_type iteration = 1, ValueType relaxation_factor = 0.9)
329{
330 auto exec = solver->get_executor();
331 return Ir<ValueType>::build()
332 .with_generated_solver(solver)
333 .with_relaxation_factor(relaxation_factor)
334 .with_criteria(gko::stop::Iteration::build().with_max_iters(iteration))
335 .on(exec);
336}
337
338
339} // namespace solver
340} // namespace gko
341
342
343#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
Definition lin_op.hpp:117
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:120
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:161
A LinOp deriving from this CRTP class stores a stopping criterion factory and allows applying with a ...
Definition solver_base.hpp:718
A LinOp deriving from this CRTP class stores a system matrix.
Definition solver_base.hpp:556
Definition ir.hpp:184
#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:1593
#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
bool apply_uses_initial_guess() const override
Iterative refinement (IR) is an iterative method that uses another coarse method to approximate the e...
Definition ir.hpp:104
std::shared_ptr< const CriterionFactory > combine(FactoryContainer &&factories)
Combines multiple criterion factories into a single combined criterion factory.
Definition combined.hpp:109
@ solver
Solver events.
Definition profiler_hook.hpp:34
@ factory
LinOpFactory events.
Definition profiler_hook.hpp:32
The ginkgo Solve namespace.
Definition bicg.hpp:28
Ir(const Ir &)
Copy-constructs an IR solver.
void set_solver(std::shared_ptr< const LinOp > new_solver)
Sets the solver operator used as the inner solver.
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
Ir & operator=(const Ir &)
Copy-assigns an IR solver.
std::shared_ptr< const LinOp > get_solver() const
Returns the solver operator used as the inner solver.
Definition ir.hpp:115
The Ginkgo namespace.
Definition abstract_factory.hpp:20
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:654
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:90
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.
Definition ir.hpp:157
std::shared_ptr< const LinOp > generated_solver
Already generated solver.
Definition ir.hpp:169
initial_guess_mode default_initial_guess
Default initial guess mode.
Definition ir.hpp:182
ValueType relaxation_factor
Relaxation factor for Richardson iteration.
Definition ir.hpp:175
std::shared_ptr< const LinOpFactory > solver
Inner solver factory.
Definition ir.hpp:162
Traits class providing information on the type and location of workspace vectors inside a solver.
Definition solver_base.hpp:238