Ginkgo Generated from branch based on master. Ginkgo version 1.7.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
ir.hpp
1/*******************************<GINKGO LICENSE>******************************
2Copyright (c) 2017-2023, the Ginkgo authors
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
91. Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in the
14documentation and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17contributors may be used to endorse or promote products derived from
18this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF 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/solver/solver_base.hpp>
46#include <ginkgo/core/stop/combined.hpp>
47#include <ginkgo/core/stop/criterion.hpp>
48#include <ginkgo/core/stop/iteration.hpp>
49
50
51namespace gko {
52namespace solver {
53
54
107template <typename ValueType = default_precision>
108class Ir : public EnableLinOp<Ir<ValueType>>,
109 public EnableSolverBase<Ir<ValueType>>,
110 public EnableIterativeBase<Ir<ValueType>>,
111 public EnableApplyWithInitialGuess<Ir<ValueType>>,
112 public Transposable {
113 friend class EnableLinOp<Ir>;
114 friend class EnablePolymorphicObject<Ir, LinOp>;
115 friend class EnableApplyWithInitialGuess<Ir>;
116
117public:
118 using value_type = ValueType;
120
121 std::unique_ptr<LinOp> transpose() const override;
122
123 std::unique_ptr<LinOp> conj_transpose() const override;
124
130 bool apply_uses_initial_guess() const override
131 {
132 return this->get_default_initial_guess() ==
134 }
135
141 std::shared_ptr<const LinOp> get_solver() const { return solver_; }
142
148 void set_solver(std::shared_ptr<const LinOp> new_solver);
149
156 Ir& operator=(const Ir&);
157
166
171 Ir(const Ir&);
172
178 Ir(Ir&&);
179
180 class Factory;
181
183 : enable_iterative_solver_factory_parameters<parameters_type, Factory> {
187 std::shared_ptr<const LinOpFactory> GKO_DEFERRED_FACTORY_PARAMETER(
189
194 std::shared_ptr<const LinOp> GKO_FACTORY_PARAMETER_SCALAR(
196
201 value_type{1});
202
209 };
212
213protected:
214 void apply_impl(const LinOp* b, LinOp* x) const override;
215
216 template <typename VectorType>
217 void apply_dense_impl(const VectorType* b, VectorType* x,
219
220 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
221 LinOp* x) const override;
222
223 void apply_with_initial_guess_impl(const LinOp* b, LinOp* x,
224 initial_guess_mode guess) const override;
225
226 void apply_with_initial_guess_impl(const LinOp* alpha, const LinOp* b,
227 const LinOp* beta, LinOp* x,
228 initial_guess_mode guess) const override;
229
230 void set_relaxation_factor(
231 std::shared_ptr<const matrix::Dense<ValueType>> new_factor);
232
233 explicit Ir(std::shared_ptr<const Executor> exec)
234 : EnableLinOp<Ir>(std::move(exec))
235 {}
236
237 explicit Ir(const Factory* factory,
238 std::shared_ptr<const LinOp> system_matrix)
239 : EnableLinOp<Ir>(factory->get_executor(),
240 gko::transpose(system_matrix->get_size())),
241 EnableSolverBase<Ir>{std::move(system_matrix)},
243 stop::combine(factory->get_parameters().criteria)},
244 parameters_{factory->get_parameters()}
245 {
246 if (parameters_.generated_solver) {
247 this->set_solver(parameters_.generated_solver);
248 } else if (parameters_.solver) {
249 this->set_solver(
250 parameters_.solver->generate(this->get_system_matrix()));
251 } else {
252 this->set_solver(matrix::Identity<ValueType>::create(
253 this->get_executor(), this->get_size()));
254 }
255 this->set_default_initial_guess(parameters_.default_initial_guess);
256 relaxation_factor_ = gko::initialize<matrix::Dense<ValueType>>(
257 {parameters_.relaxation_factor}, this->get_executor());
258 }
259
260private:
261 std::shared_ptr<const LinOp> solver_{};
262 std::shared_ptr<const matrix::Dense<ValueType>> relaxation_factor_{};
263};
264
265
266template <typename ValueType = default_precision>
267using Richardson = Ir<ValueType>;
268
269
270template <typename ValueType>
271struct workspace_traits<Ir<ValueType>> {
272 using Solver = Ir<ValueType>;
273 // number of vectors used by this workspace
274 static int num_vectors(const Solver&);
275 // number of arrays used by this workspace
276 static int num_arrays(const Solver&);
277 // array containing the num_vectors names for the workspace vectors
278 static std::vector<std::string> op_names(const Solver&);
279 // array containing the num_arrays names for the workspace vectors
280 static std::vector<std::string> array_names(const Solver&);
281 // array containing all varying scalar vectors (independent of problem size)
282 static std::vector<int> scalars(const Solver&);
283 // array containing all varying vectors (dependent on problem size)
284 static std::vector<int> vectors(const Solver&);
285
286 // residual vector
287 constexpr static int residual = 0;
288 // inner solution vector
289 constexpr static int inner_solution = 1;
290 // constant 1.0 scalar
291 constexpr static int one = 2;
292 // constant -1.0 scalar
293 constexpr static int minus_one = 3;
294
295 // stopping status array
296 constexpr static int stop = 0;
297};
298
299
310template <typename ValueType>
311auto build_smoother(std::shared_ptr<const LinOpFactory> factory,
312 size_type iteration = 1, ValueType relaxation_factor = 0.9)
313{
314 auto exec = factory->get_executor();
315 return Ir<ValueType>::build()
316 .with_solver(factory)
317 .with_relaxation_factor(relaxation_factor)
318 .with_criteria(gko::stop::Iteration::build().with_max_iters(iteration))
319 .on(exec);
320}
321
334template <typename ValueType>
335auto build_smoother(std::shared_ptr<const LinOp> solver,
336 size_type iteration = 1, ValueType relaxation_factor = 0.9)
337{
338 auto exec = solver->get_executor();
339 return Ir<ValueType>::build()
340 .with_generated_solver(solver)
341 .with_relaxation_factor(relaxation_factor)
342 .with_criteria(gko::stop::Iteration::build().with_max_iters(iteration))
343 .on(exec);
344}
345
346
347} // namespace solver
348} // namespace gko
349
350
351#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:908
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:691
Definition lin_op.hpp:146
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition polymorphic_object.hpp:263
Linear operators which support transposition should implement the Transposable interface.
Definition lin_op.hpp:462
Dense is a matrix format which explicitly stores all values of the matrix.
Definition dense.hpp:136
EnableApplyWithInitialGuess providing default operation for ApplyWithInitialGuess with correct valida...
Definition solver_base.hpp:190
A LinOp deriving from this CRTP class stores a stopping criterion factory and allows applying with a ...
Definition solver_base.hpp:732
A LinOp deriving from this CRTP class stores a system matrix.
Definition solver_base.hpp:570
Definition ir.hpp:210
Iterative refinement (IR) is an iterative method that uses another coarse method to approximate the e...
Definition ir.hpp:112
Ir(const Ir &)
Copy-constructs an IR solver.
std::unique_ptr< LinOp > conj_transpose() const override
Returns a LinOp representing the conjugate transpose of the Transposable object.
Ir & operator=(Ir &&)
Move-assigns an IR solver.
Ir(Ir &&)
Move-constructs an IR solver.
void set_solver(std::shared_ptr< const LinOp > new_solver)
Sets the solver operator used as the inner solver.
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
std::shared_ptr< const LinOp > get_solver() const
Returns the solver operator used as the inner solver.
Definition ir.hpp:141
bool apply_uses_initial_guess() const override
Return true as iterative solvers use the data in x as an initial guess.
Definition ir.hpp:130
Ir & operator=(const Ir &)
Copy-assigns an IR solver.
#define GKO_FACTORY_PARAMETER_SCALAR(_name, _default)
Creates a scalar factory parameter in the factory parameters structure.
Definition abstract_factory.hpp:473
#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:422
#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:1046
std::shared_ptr< const CriterionFactory > combine(FactoryContainer &&factories)
Combines multiple criterion factories into a single combined criterion factory.
Definition combined.hpp:138
initial_guess_mode
Give a initial guess mode about the input of the apply method.
Definition solver_base.hpp:62
@ provided
the input is provided
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:311
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120
initial_guess_mode default_initial_guess
Default initial guess mode.
Definition ir.hpp:208
std::shared_ptr< const LinOpFactory > solver
Inner solver factory.
Definition ir.hpp:188
std::shared_ptr< const LinOp > generated_solver
Already generated solver.
Definition ir.hpp:195
ValueType relaxation_factor
Relaxation factor for Richardson iteration.
Definition ir.hpp:201
Traits class providing information on the type and location of workspace vectors inside a solver.
Definition solver_base.hpp:267