Ginkgo  Generated from pipelines/238965899 branch based on develop. Ginkgo version 1.3.0
A numerical linear algebra library targeting many-core architectures
ir.hpp
1 /*******************************<GINKGO LICENSE>******************************
2 Copyright (c) 2017-2021, the Ginkgo authors
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 ******************************<GINKGO LICENSE>*******************************/
32 
33 #ifndef GKO_PUBLIC_CORE_SOLVER_IR_HPP_
34 #define GKO_PUBLIC_CORE_SOLVER_IR_HPP_
35 
36 
37 #include <vector>
38 
39 
40 #include <ginkgo/core/base/exception_helpers.hpp>
41 #include <ginkgo/core/base/lin_op.hpp>
42 #include <ginkgo/core/base/types.hpp>
43 #include <ginkgo/core/matrix/dense.hpp>
44 #include <ginkgo/core/matrix/identity.hpp>
45 #include <ginkgo/core/stop/combined.hpp>
46 #include <ginkgo/core/stop/criterion.hpp>
47 
48 
49 namespace gko {
50 namespace solver {
51 
52 
105 template <typename ValueType = default_precision>
106 class Ir : public EnableLinOp<Ir<ValueType>>, public Transposable {
107  friend class EnableLinOp<Ir>;
108  friend class EnablePolymorphicObject<Ir, LinOp>;
109 
110 public:
111  using value_type = ValueType;
113 
119  std::shared_ptr<const LinOp> get_system_matrix() const
120  {
121  return system_matrix_;
122  }
123 
124  std::unique_ptr<LinOp> transpose() const override;
125 
126  std::unique_ptr<LinOp> conj_transpose() const override;
127 
133  bool apply_uses_initial_guess() const override { return true; }
134 
140  std::shared_ptr<const LinOp> get_solver() const { return solver_; }
141 
147  void set_solver(std::shared_ptr<const LinOp> new_solver)
148  {
149  GKO_ASSERT_EQUAL_DIMENSIONS(new_solver, this);
150  solver_ = new_solver;
151  }
152 
158  std::shared_ptr<const stop::CriterionFactory> get_stop_criterion_factory()
159  const
160  {
161  return stop_criterion_factory_;
162  }
163 
170  std::shared_ptr<const stop::CriterionFactory> other)
171  {
172  stop_criterion_factory_ = std::move(other);
173  }
174 
176  {
180  std::vector<std::shared_ptr<const stop::CriterionFactory>>
181  GKO_FACTORY_PARAMETER_VECTOR(criteria, nullptr);
182 
186  std::shared_ptr<const LinOpFactory> GKO_FACTORY_PARAMETER_SCALAR(
187  solver, nullptr);
188 
193  std::shared_ptr<const LinOp> GKO_FACTORY_PARAMETER_SCALAR(
194  generated_solver, nullptr);
195 
199  ValueType GKO_FACTORY_PARAMETER_SCALAR(relaxation_factor,
200  value_type{1});
201  };
204 
205 protected:
206  void apply_impl(const LinOp *b, LinOp *x) const override;
207 
208  void apply_impl(const LinOp *alpha, const LinOp *b, const LinOp *beta,
209  LinOp *x) const override;
210 
211  explicit Ir(std::shared_ptr<const Executor> exec)
212  : EnableLinOp<Ir>(std::move(exec))
213  {}
214 
215  explicit Ir(const Factory *factory,
216  std::shared_ptr<const LinOp> system_matrix)
217  : EnableLinOp<Ir>(factory->get_executor(),
218  gko::transpose(system_matrix->get_size())),
219  parameters_{factory->get_parameters()},
220  system_matrix_{std::move(system_matrix)}
221  {
222  GKO_ASSERT_IS_SQUARE_MATRIX(system_matrix_);
223  if (parameters_.generated_solver) {
224  solver_ = parameters_.generated_solver;
225  GKO_ASSERT_EQUAL_DIMENSIONS(solver_, this);
226  } else if (parameters_.solver) {
227  solver_ = parameters_.solver->generate(system_matrix_);
228  } else {
229  solver_ = matrix::Identity<ValueType>::create(this->get_executor(),
230  this->get_size());
231  }
232  relaxation_factor_ = gko::initialize<matrix::Dense<ValueType>>(
233  {parameters_.relaxation_factor}, this->get_executor());
234  stop_criterion_factory_ =
235  stop::combine(std::move(parameters_.criteria));
236  }
237 
238 private:
239  std::shared_ptr<const LinOp> system_matrix_{};
240  std::shared_ptr<const LinOp> solver_{};
241  std::shared_ptr<const stop::CriterionFactory> stop_criterion_factory_{};
242  std::shared_ptr<const matrix::Dense<ValueType>> relaxation_factor_{};
243 };
244 
245 
246 template <typename ValueType = default_precision>
247 using Richardson = Ir<ValueType>;
248 
249 
250 } // namespace solver
251 } // namespace gko
252 
253 
254 #endif // GKO_PUBLIC_CORE_SOLVER_IR_HPP_
gko::solver::Ir::Factory
Definition: ir.hpp:202
gko::LinOp
Definition: lin_op.hpp:145
gko::solver::Ir
Iterative refinement (IR) is an iterative method that uses another coarse method to approximate the e...
Definition: ir.hpp:106
gko::solver::Ir::parameters_type::generated_solver
std::shared_ptr< const LinOp > generated_solver
Already generated solver.
Definition: ir.hpp:194
gko::Transposable
Linear operators which support transposition should implement the Transposable interface.
Definition: lin_op.hpp:418
gko::solver::Ir::transpose
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
gko::EnableDefaultFactory::get_parameters
const parameters_type & get_parameters() const noexcept
Returns the parameters of the factory.
Definition: abstract_factory.hpp:175
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:140
GKO_FACTORY_PARAMETER_SCALAR
#define GKO_FACTORY_PARAMETER_SCALAR(_name, _default)
Creates a scalar factory parameter in the factory parameters structure.
Definition: lin_op.hpp:1048
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:45
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:950
gko::stop::combine
std::shared_ptr< const CriterionFactory > combine(FactoryContainer &&factories)
Combines multiple criterion factories into a single combined criterion factory.
Definition: combined.hpp:123
gko::solver::Ir::get_stop_criterion_factory
std::shared_ptr< const stop::CriterionFactory > get_stop_criterion_factory() const
Gets the stopping criterion factory of the solver.
Definition: ir.hpp:158
gko::solver::Ir::set_solver
void set_solver(std::shared_ptr< const LinOp > new_solver)
Sets the solver operator used as the inner solver.
Definition: ir.hpp:147
gko::solver::Ir::parameters_type::criteria
std::vector< std::shared_ptr< const stop::CriterionFactory > > criteria
Criterion factories.
Definition: ir.hpp:181
gko::solver::Ir::set_stop_criterion_factory
void set_stop_criterion_factory(std::shared_ptr< const stop::CriterionFactory > other)
Sets the stopping criterion of the solver.
Definition: ir.hpp:169
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:133
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: lin_op.hpp:997
GKO_CREATE_FACTORY_PARAMETERS
#define GKO_CREATE_FACTORY_PARAMETERS(_parameters_name, _factory_name)
This Macro will generate a new type containing the parameters for the factory _factory_name.
Definition: lin_op.hpp:867
GKO_FACTORY_PARAMETER_VECTOR
#define GKO_FACTORY_PARAMETER_VECTOR(_name,...)
Creates a vector factory parameter in the factory parameters structure.
Definition: lin_op.hpp:1064
gko::solver::Ir::parameters_type::relaxation_factor
ValueType relaxation_factor
Relaxation factor for Richardson iteration.
Definition: ir.hpp:200
gko::solver::Ir::get_system_matrix
std::shared_ptr< const LinOp > get_system_matrix() const
Returns the system operator (matrix) of the linear system.
Definition: ir.hpp:119
gko::solver::Ir::parameters_type::solver
std::shared_ptr< const LinOpFactory > solver
Inner solver factory.
Definition: ir.hpp:187
gko::PolymorphicObject::get_executor
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition: polymorphic_object.hpp:205
gko::LinOp::get_size
const dim< 2 > & get_size() const noexcept
Returns the size of the operator.
Definition: lin_op.hpp:232
gko::EnableLinOp
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition: lin_op.hpp:765
gko::EnablePolymorphicObject
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition: polymorphic_object.hpp:570