Ginkgo  Generated from tags/v1.0.0^0 branch based on master. Ginkgo version 1.0.0
A numerical linear algebra library targeting many-core architectures
gmres.hpp
1 /*******************************<GINKGO LICENSE>******************************
2 Copyright (c) 2017-2019, 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_CORE_SOLVER_GMRES_HPP_
34 #define GKO_CORE_SOLVER_GMRES_HPP_
35 
36 
37 #include <vector>
38 
39 
40 #include <ginkgo/core/base/array.hpp>
41 #include <ginkgo/core/base/lin_op.hpp>
42 #include <ginkgo/core/base/math.hpp>
43 #include <ginkgo/core/base/types.hpp>
44 #include <ginkgo/core/log/logger.hpp>
45 #include <ginkgo/core/matrix/identity.hpp>
46 #include <ginkgo/core/stop/combined.hpp>
47 #include <ginkgo/core/stop/criterion.hpp>
48 
49 
50 namespace gko {
51 namespace solver {
52 
53 
54 constexpr size_type default_krylov_dim = 100u;
55 
56 
70 template <typename ValueType = default_precision>
71 class Gmres : public EnableLinOp<Gmres<ValueType>>, public Preconditionable {
72  friend class EnableLinOp<Gmres>;
73  friend class EnablePolymorphicObject<Gmres, LinOp>;
74 
75 public:
76  using value_type = ValueType;
77 
83  std::shared_ptr<const LinOp> get_system_matrix() const
84  {
85  return system_matrix_;
86  }
87 
93  std::shared_ptr<const LinOp> get_preconditioner() const override
94  {
95  return preconditioner_;
96  }
97 
103  size_type get_krylov_dim() const { return krylov_dim_; }
104 
106  {
110  std::vector<std::shared_ptr<const stop::CriterionFactory>>
111  GKO_FACTORY_PARAMETER(criteria, nullptr);
112 
116  std::shared_ptr<const LinOpFactory> GKO_FACTORY_PARAMETER(
117  preconditioner, nullptr);
118 
123  };
124  GKO_ENABLE_LIN_OP_FACTORY(Gmres, parameters, Factory);
126 
127 protected:
128  void apply_impl(const LinOp *b, LinOp *x) const override;
129 
130  void apply_impl(const LinOp *alpha, const LinOp *b, const LinOp *beta,
131  LinOp *x) const override;
132 
133  explicit Gmres(std::shared_ptr<const Executor> exec)
134  : EnableLinOp<Gmres>(std::move(exec))
135  {}
136 
137  explicit Gmres(const Factory *factory,
138  std::shared_ptr<const LinOp> system_matrix)
139  : EnableLinOp<Gmres>(factory->get_executor(),
140  transpose(system_matrix->get_size())),
141  parameters_{factory->get_parameters()},
142  system_matrix_{std::move(system_matrix)}
143  {
144  if (parameters_.preconditioner) {
145  preconditioner_ =
146  parameters_.preconditioner->generate(system_matrix_);
147  } else {
148  preconditioner_ = matrix::Identity<ValueType>::create(
149  this->get_executor(), this->get_size()[0]);
150  }
151  if (parameters_.krylov_dim) {
152  krylov_dim_ = parameters_.krylov_dim;
153  } else {
154  krylov_dim_ = default_krylov_dim;
155  }
156  stop_criterion_factory_ =
157  stop::combine(std::move(parameters_.criteria));
158  }
159 
160 private:
161  std::shared_ptr<const LinOp> system_matrix_{};
162  std::shared_ptr<const LinOp> preconditioner_{};
163  std::shared_ptr<const stop::CriterionFactory> stop_criterion_factory_{};
164  size_type krylov_dim_;
165 };
166 
167 
168 } // namespace solver
169 } // namespace gko
170 
171 
172 #endif // GKO_CORE_SOLVER_GMRES_HPP
Definition: gmres.hpp:124
std::shared_ptr< const LinOpFactory > preconditioner
Preconditioner factory.
Definition: gmres.hpp:117
const parameters_type & get_parameters() const noexcept
Returns the parameters of the factory.
Definition: abstract_factory.hpp:175
GMRES or the generalized minimal residual method is an iterative type Krylov subspace method which is...
Definition: gmres.hpp:71
size_type get_krylov_dim() const
Returns the krylov dimension.
Definition: gmres.hpp:103
size_type krylov_dim
krylov dimension factory.
Definition: gmres.hpp:122
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition: polymorphic_object.hpp:505
#define GKO_FACTORY_PARAMETER(_name,...)
Creates a factory parameter in the factory parameters structure.
Definition: lin_op.hpp:751
STL namespace.
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:94
The Ginkgo namespace.
Definition: abstract_factory.hpp:45
std::shared_ptr< const LinOp > get_preconditioner() const override
Returns the preconditioner operator used by the solver.
Definition: gmres.hpp:93
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition: lin_op.hpp:509
const dim< 2 > & get_size() const noexcept
Returns the size of the operator.
Definition: lin_op.hpp:221
A LinOp implementing this interface can be preconditioned.
Definition: lin_op.hpp:465
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition: polymorphic_object.hpp:201
std::vector< std::shared_ptr< const stop::CriterionFactory > > criteria
Criterion factories.
Definition: gmres.hpp:111
#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:611
This class is a utility which efficiently implements the identity matrix (a linear operator which map...
Definition: identity.hpp:63
std::shared_ptr< const CriterionFactory > combine(FactoryContainer &&factories)
Combines multiple criterion factories into a single combined criterion factory.
Definition: combined.hpp:116
#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:691
#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:730
std::shared_ptr< const LinOp > get_system_matrix() const
Gets the system operator (matrix) of the linear system.
Definition: gmres.hpp:83
constexpr dim< 2, DimensionType > transpose(const dim< 2, DimensionType > &dimensions) noexcept
Returns a dim<2> object with its dimensions swapped.
Definition: dim.hpp:234