Ginkgo  Generated from pipelines/2662685947 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
ir.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 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 
24 namespace gko {
25 namespace solver {
26 
27 
80 template <typename ValueType = default_precision>
81 class Ir : public LinOp,
82  public EnableSolverBase<Ir<ValueType>>,
83  public EnableIterativeBase<Ir<ValueType>>,
84  public EnableApplyWithInitialGuess<Ir<ValueType>>,
85  public Transposable {
86  friend class EnableApplyWithInitialGuess<Ir>;
87  GKO_ASSERT_SUPPORTED_VALUE_TYPE;
88 
89 public:
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 
137  Ir& operator=(Ir&&);
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(
167  generated_solver, nullptr);
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 
203 protected:
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 
213  void apply_with_initial_guess_impl(const LinOp* b, LinOp* x,
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) : LinOp(std::move(exec))
224  {}
225 
226  explicit Ir(const Factory* factory,
227  std::shared_ptr<const LinOp> system_matrix)
228  : LinOp(factory->get_executor(),
229  gko::transpose(system_matrix->get_size())),
230  EnableSolverBase<Ir>{std::move(system_matrix)},
232  stop::combine(factory->get_parameters().criteria)},
233  parameters_{factory->get_parameters()}
234  {
235  if (parameters_.generated_solver) {
236  this->set_solver(parameters_.generated_solver);
237  } else if (parameters_.solver) {
238  this->set_solver(
239  parameters_.solver->generate(this->get_system_matrix()));
240  } else {
242  this->get_executor(), this->get_size()[0]));
243  }
244  this->set_default_initial_guess(parameters_.default_initial_guess);
245  relaxation_factor_ = gko::initialize<matrix::Dense<ValueType>>(
246  {parameters_.relaxation_factor}, this->get_executor());
247  }
248 
249 private:
250  std::shared_ptr<const LinOp> solver_{};
251  std::shared_ptr<const matrix::Dense<ValueType>> relaxation_factor_{};
252 };
253 
254 
255 template <typename ValueType = default_precision>
256 using Richardson = Ir<ValueType>;
257 
258 
259 template <typename ValueType>
260 struct workspace_traits<Ir<ValueType>> {
261  using Solver = Ir<ValueType>;
262  // number of vectors used by this workspace
263  static int num_vectors(const Solver&);
264  // number of arrays used by this workspace
265  static int num_arrays(const Solver&);
266  // array containing the num_vectors names for the workspace vectors
267  static std::vector<std::string> op_names(const Solver&);
268  // array containing the num_arrays names for the workspace vectors
269  static std::vector<std::string> array_names(const Solver&);
270  // array containing all varying scalar vectors (independent of problem size)
271  static std::vector<int> scalars(const Solver&);
272  // array containing all varying vectors (dependent on problem size)
273  static std::vector<int> vectors(const Solver&);
274 
275  // residual vector
276  constexpr static int residual = 0;
277  // inner solution vector
278  constexpr static int inner_solution = 1;
279  // constant 1.0 scalar
280  constexpr static int one = 2;
281  // constant -1.0 scalar
282  constexpr static int minus_one = 3;
283 
284  // stopping status array
285  constexpr static int stop = 0;
286 };
287 
288 
299 template <typename ValueType>
300 auto build_smoother(std::shared_ptr<const LinOpFactory> factory,
301  size_type iteration = 1, ValueType relaxation_factor = 0.9)
302 {
303  auto exec = factory->get_executor();
304  return Ir<ValueType>::build()
305  .with_solver(factory)
306  .with_relaxation_factor(relaxation_factor)
307  .with_criteria(gko::stop::Iteration::build().with_max_iters(iteration))
308  .on(exec);
309 }
310 
323 template <typename ValueType>
324 auto build_smoother(std::shared_ptr<const LinOp> solver,
325  size_type iteration = 1, ValueType relaxation_factor = 0.9)
326 {
327  auto exec = solver->get_executor();
328  return Ir<ValueType>::build()
329  .with_generated_solver(solver)
330  .with_relaxation_factor(relaxation_factor)
331  .with_criteria(gko::stop::Iteration::build().with_max_iters(iteration))
332  .on(exec);
333 }
334 
335 
336 } // namespace solver
337 } // namespace gko
338 
339 
340 #endif // GKO_PUBLIC_CORE_SOLVER_IR_HPP_
gko::solver::Ir::Factory
Definition: ir.hpp:182
gko::solver::EnableApplyWithInitialGuess
EnableApplyWithInitialGuess providing default operation for ApplyWithInitialGuess with correct valida...
Definition: solver_base.hpp:161
gko::solver::Ir::operator=
Ir & operator=(const Ir &)
Copy-assigns an IR solver.
gko::config::pnode
pnode describes a tree of properties.
Definition: property_tree.hpp:28
gko::log::profile_event_category::solver
Solver events.
gko::solver::EnableIterativeBase
A LinOp deriving from this CRTP class stores a stopping criterion factory and allows applying with a ...
Definition: solver_base.hpp:718
gko::LinOp
Definition: lin_op.hpp:117
gko::matrix::Dense
Dense is a matrix format which explicitly stores all values of the matrix.
Definition: dense_cache.hpp:28
gko::solver::Ir::parse
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.
gko::solver::enable_iterative_solver_factory_parameters
Definition: solver_base.hpp:844
gko::solver::Ir
Iterative refinement (IR) is an iterative method that uses another coarse method to approximate the e...
Definition: ir.hpp:81
gko::solver::Ir::parameters_type::generated_solver
std::shared_ptr< const LinOp > generated_solver
Already generated solver.
Definition: ir.hpp:167
gko::Transposable
Linear operators which support transposition should implement the Transposable interface.
Definition: lin_op.hpp:392
gko::solver::Ir::Ir
Ir(const Ir &)
Copy-constructs an IR solver.
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:90
gko::solver::Ir::transpose
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
gko::solver::Ir::get_solver
std::shared_ptr< const LinOp > get_solver() const
Returns the solver operator used as the inner solver.
Definition: ir.hpp:113
GKO_FACTORY_PARAMETER_SCALAR
#define GKO_FACTORY_PARAMETER_SCALAR(_name, _default)
Creates a scalar factory parameter in the factory parameters structure.
Definition: abstract_factory.hpp:437
gko::config::type_descriptor
This class describes the value and index types to be used when building a Ginkgo type from a configur...
Definition: type_descriptor.hpp:39
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::solver::Ir::conj_transpose
std::unique_ptr< LinOp > conj_transpose() const override
Returns a LinOp representing the conjugate transpose of the Transposable object.
GKO_ENABLE_LIN_OP_FACTORY
#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:900
gko::stop::combine
std::shared_ptr< const CriterionFactory > combine(FactoryContainer &&factories)
Combines multiple criterion factories into a single combined criterion factory.
Definition: combined.hpp:105
gko::solver::build_smoother
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:300
gko::matrix::Identity::create
static std::unique_ptr< Identity > create(std::shared_ptr< const Executor > exec, dim< 2 > size)
Creates an Identity matrix of the specified size.
gko::solver::Ir::set_solver
void set_solver(std::shared_ptr< const LinOp > new_solver)
Sets the solver operator used as the inner solver.
gko::solver::EnableSolverBase
A LinOp deriving from this CRTP class stores a system matrix.
Definition: solver_base.hpp:556
gko::solver::Ir::parameters_type::default_initial_guess
initial_guess_mode default_initial_guess
Default initial guess mode.
Definition: ir.hpp:180
gko::config::registry
This class stores additional context for creating Ginkgo objects from configuration files.
Definition: registry.hpp:167
gko::solver::Ir::apply_uses_initial_guess
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
GKO_ENABLE_BUILD_METHOD
#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:386
gko::solver::initial_guess_mode
initial_guess_mode
Give a initial guess mode about the input of the apply method.
Definition: solver_base.hpp:33
gko::solver::Ir::parameters_type
Definition: ir.hpp:154
gko::solver::Ir::parameters_type::relaxation_factor
ValueType relaxation_factor
Relaxation factor for Richardson iteration.
Definition: ir.hpp:173
gko::solver::initial_guess_mode::provided
the input is provided
gko::solver::Ir::parameters_type::solver
std::shared_ptr< const LinOpFactory > solver
Inner solver factory.
Definition: ir.hpp:160
gko::PolymorphicObject::get_executor
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition: polymorphic_object.hpp:62
gko::solver::workspace_traits
Traits class providing information on the type and location of workspace vectors inside a solver.
Definition: solver_base.hpp:238
gko::LinOp::get_size
const dim< 2 > & get_size() const noexcept
Returns the size of the operator.
Definition: lin_op.hpp:169
gko::one
constexpr T one()
Returns the multiplicative identity for T.
Definition: math.hpp:654