Ginkgo  Generated from pipelines/2662685947 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
cg.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_SOLVER_CG_HPP_
6 #define GKO_PUBLIC_CORE_SOLVER_CG_HPP_
7 
8 
9 #include <vector>
10 
11 #include <ginkgo/core/base/array.hpp>
12 #include <ginkgo/core/base/exception_helpers.hpp>
13 #include <ginkgo/core/base/lin_op.hpp>
14 #include <ginkgo/core/base/math.hpp>
15 #include <ginkgo/core/base/types.hpp>
16 #include <ginkgo/core/config/config.hpp>
17 #include <ginkgo/core/config/registry.hpp>
18 #include <ginkgo/core/config/type_descriptor.hpp>
19 #include <ginkgo/core/log/logger.hpp>
20 #include <ginkgo/core/matrix/dense.hpp>
21 #include <ginkgo/core/matrix/identity.hpp>
22 #include <ginkgo/core/solver/solver_base.hpp>
23 #include <ginkgo/core/stop/combined.hpp>
24 #include <ginkgo/core/stop/criterion.hpp>
25 
26 
27 namespace gko {
28 namespace solver {
29 
30 
47 template <typename ValueType = default_precision>
48 class Cg : public LinOp,
49  public EnablePreconditionedIterativeSolver<ValueType, Cg<ValueType>>,
50  public Transposable {
51  GKO_ASSERT_SUPPORTED_VALUE_TYPE;
52 
53 public:
54  using value_type = ValueType;
56 
57  std::unique_ptr<LinOp> transpose() const override;
58 
59  std::unique_ptr<LinOp> conj_transpose() const override;
60 
66  bool apply_uses_initial_guess() const override { return true; }
67 
68  class Factory;
69 
72  parameters_type, Factory> {};
73 
76 
90  static parameters_type parse(const config::pnode& config,
91  const config::registry& context,
92  const config::type_descriptor& td_for_child =
93  config::make_type_descriptor<ValueType>());
94 
95 protected:
96  void apply_impl(const LinOp* b, LinOp* x) const override;
97 
98  template <typename VectorType>
99  void apply_dense_impl(const VectorType* b, VectorType* x) const;
100 
101  void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
102  LinOp* x) const override;
103 
104  explicit Cg(std::shared_ptr<const Executor> exec) : LinOp(std::move(exec))
105  {}
106 
107  explicit Cg(const Factory* factory,
108  std::shared_ptr<const LinOp> system_matrix)
109  : LinOp(factory->get_executor(),
110  gko::transpose(system_matrix->get_size())),
111  EnablePreconditionedIterativeSolver<ValueType, Cg<ValueType>>{
112  std::move(system_matrix), factory->get_parameters()},
113  parameters_{factory->get_parameters()}
114  {}
115 };
116 
117 
118 template <typename ValueType>
119 struct workspace_traits<Cg<ValueType>> {
120  using Solver = Cg<ValueType>;
121  // number of vectors used by this workspace
122  static int num_vectors(const Solver&);
123  // number of arrays used by this workspace
124  static int num_arrays(const Solver&);
125  // array containing the num_vectors names for the workspace vectors
126  static std::vector<std::string> op_names(const Solver&);
127  // array containing the num_arrays names for the workspace vectors
128  static std::vector<std::string> array_names(const Solver&);
129  // array containing all varying scalar vectors (independent of problem size)
130  static std::vector<int> scalars(const Solver&);
131  // array containing all varying vectors (dependent on problem size)
132  static std::vector<int> vectors(const Solver&);
133 
134  // residual vector
135  constexpr static int r = 0;
136  // preconditioned residual vector
137  constexpr static int z = 1;
138  // p vector
139  constexpr static int p = 2;
140  // q vector
141  constexpr static int q = 3;
142  // beta scalar
143  constexpr static int beta = 4;
144  // previous rho scalar
145  constexpr static int prev_rho = 5;
146  // current rho scalar
147  constexpr static int rho = 6;
148  // constant 1.0 scalar
149  constexpr static int one = 7;
150  // constant -1.0 scalar
151  constexpr static int minus_one = 8;
152 
153  // stopping status array
154  constexpr static int stop = 0;
155  // reduction tmp array
156  constexpr static int tmp = 1;
157 };
158 
159 
160 } // namespace solver
161 } // namespace gko
162 
163 
164 #endif // GKO_PUBLIC_CORE_SOLVER_CG_HPP_
gko::solver::Cg::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: cg.hpp:66
gko::config::pnode
pnode describes a tree of properties.
Definition: property_tree.hpp:28
gko::log::profile_event_category::solver
Solver events.
gko::LinOp
Definition: lin_op.hpp:117
gko::Transposable
Linear operators which support transposition should implement the Transposable interface.
Definition: lin_op.hpp:392
gko::solver::Cg::transpose
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
gko::solver::enable_preconditioned_iterative_solver_factory_parameters
Definition: solver_base.hpp:855
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::solver::Cg::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
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::solver::Cg::parameters_type
Definition: cg.hpp:70
gko::solver::Cg::conj_transpose
std::unique_ptr< LinOp > conj_transpose() const override
Returns a LinOp representing the conjugate transpose of the Transposable object.
gko::solver::Cg
CG or the conjugate gradient method is an iterative type Krylov subspace method which is suitable for...
Definition: cg.hpp:48
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::config::registry
This class stores additional context for creating Ginkgo objects from configuration files.
Definition: registry.hpp:167
gko::solver::Cg::Factory
Definition: cg.hpp:74
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::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::solver::EnablePreconditionedIterativeSolver
A LinOp implementing this interface stores a system matrix and stopping criterion factory.
Definition: solver_base.hpp:800
gko::one
constexpr T one()
Returns the multiplicative identity for T.
Definition: math.hpp:654