5#ifndef GKO_PUBLIC_CORE_SOLVER_SOLVER_BASE_HPP_
6#define GKO_PUBLIC_CORE_SOLVER_SOLVER_BASE_HPP_
14#include <ginkgo/core/base/lin_op.hpp>
15#include <ginkgo/core/base/math.hpp>
16#include <ginkgo/core/log/logger.hpp>
17#include <ginkgo/core/matrix/dense.hpp>
18#include <ginkgo/core/matrix/identity.hpp>
19#include <ginkgo/core/solver/workspace.hpp>
20#include <ginkgo/core/stop/combined.hpp>
21#include <ginkgo/core/stop/criterion.hpp>
24GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS
68 friend class multigrid::detail::MultigridState;
83 virtual void apply_with_initial_guess(
const LinOp* b,
LinOp* x,
89 apply_with_initial_guess(b.get(), x.get(), guess);
104 virtual void apply_with_initial_guess(
const LinOp* alpha,
const LinOp* b,
115 apply_with_initial_guess(alpha.get(), b.get(), beta.get(), x.get(),
161template <
typename DerivedType>
164 friend class multigrid::detail::MultigridState;
175 void apply_with_initial_guess(
const LinOp* b,
LinOp* x,
178 self()->template log<log::Logger::linop_apply_started>(self(), b, x);
179 auto exec = self()->get_executor();
180 GKO_ASSERT_CONFORMANT(self(), b);
181 GKO_ASSERT_EQUAL_ROWS(self(), x);
182 GKO_ASSERT_EQUAL_COLS(b, x);
186 self()->template log<log::Logger::linop_apply_completed>(self(), b, x);
193 void apply_with_initial_guess(
const LinOp* alpha,
const LinOp* b,
197 self()->template log<log::Logger::linop_advanced_apply_started>(
198 self(), alpha, b, beta, x);
199 auto exec = self()->get_executor();
200 GKO_ASSERT_CONFORMANT(self(), b);
201 GKO_ASSERT_EQUAL_ROWS(self(), x);
202 GKO_ASSERT_EQUAL_COLS(b, x);
203 GKO_ASSERT_EQUAL_DIMENSIONS(alpha,
dim<2>(1, 1));
204 GKO_ASSERT_EQUAL_DIMENSIONS(beta,
dim<2>(1, 1));
205 this->apply_with_initial_guess_impl(
210 self()->template log<log::Logger::linop_advanced_apply_completed>(
211 self(), alpha, b, beta, x);
219 virtual void apply_with_initial_guess_impl(
226 virtual void apply_with_initial_guess_impl(
230 GKO_ENABLE_SELF(DerivedType);
238template <
typename Solver>
241 static int num_vectors(
const Solver&) {
return 0; }
243 static int num_arrays(
const Solver&) {
return 0; }
245 static std::vector<std::string> op_names(
const Solver&) {
return {}; }
247 static std::vector<std::string> array_names(
const Solver&) {
return {}; }
249 static std::vector<int> scalars(
const Solver&) {
return {}; }
251 static std::vector<int> vectors(
const Solver&) {
return {}; }
270template <
typename DerivedType>
281 auto exec = self()->get_executor();
283 GKO_ASSERT_EQUAL_DIMENSIONS(self(), new_precond);
284 GKO_ASSERT_IS_SQUARE_MATRIX(new_precond);
285 if (new_precond->get_executor() != exec) {
298 if (&other !=
this) {
311 if (&other !=
this) {
313 other.set_preconditioner(
nullptr);
339 *
this = std::move(other);
343 DerivedType* self() {
return static_cast<DerivedType*
>(
this); }
345 const DerivedType* self()
const
347 return static_cast<const DerivedType*
>(
this);
363class SolverBaseLinOp {
365 SolverBaseLinOp(std::shared_ptr<const Executor> exec)
366 : workspace_{std::move(exec)}
369 virtual ~SolverBaseLinOp() =
default;
376 std::shared_ptr<const LinOp> get_system_matrix()
const
378 return system_matrix_;
381 const LinOp* get_workspace_op(
int vector_id)
const
383 return workspace_.get_op(vector_id);
386 virtual int get_num_workspace_ops()
const {
return 0; }
388 virtual std::vector<std::string> get_workspace_op_names()
const
397 virtual std::vector<int> get_workspace_scalars()
const {
return {}; }
403 virtual std::vector<int> get_workspace_vectors()
const {
return {}; }
406 void set_system_matrix_base(std::shared_ptr<const LinOp> system_matrix)
408 system_matrix_ = std::move(system_matrix);
411 void set_workspace_size(
int num_operators,
int num_arrays)
const
413 workspace_.set_size(num_operators, num_arrays);
416 template <
typename LinOpType>
417 LinOpType* create_workspace_op(
int vector_id,
gko::dim<2> size)
const
419 return workspace_.template create_or_get_op<LinOpType>(
422 return LinOpType::create(this->workspace_.get_executor(), size);
424 typeid(LinOpType), size, size[1]);
427 template <
typename LinOpType>
428 LinOpType* create_workspace_op_with_config_of(
int vector_id,
429 const LinOpType* vec)
const
431 return workspace_.template create_or_get_op<LinOpType>(
432 vector_id, [&] {
return LinOpType::create_with_config_of(vec); },
433 typeid(*vec), vec->get_size(), vec->get_stride());
436 template <
typename LinOpType>
437 LinOpType* create_workspace_op_with_type_of(
int vector_id,
438 const LinOpType* vec,
441 return workspace_.template create_or_get_op<LinOpType>(
444 return LinOpType::create_with_type_of(
445 vec, workspace_.get_executor(), size, size[1]);
447 typeid(*vec), size, size[1]);
450 template <
typename LinOpType>
451 LinOpType* create_workspace_op_with_type_of(
int vector_id,
452 const LinOpType* vec,
454 dim<2> local_size)
const
456 return workspace_.template create_or_get_op<LinOpType>(
459 return LinOpType::create_with_type_of(
460 vec, workspace_.get_executor(), global_size, local_size,
463 typeid(*vec), global_size, local_size[1]);
466 template <
typename ValueType>
467 matrix::Dense<ValueType>* create_workspace_scalar(
int vector_id,
470 return workspace_.template create_or_get_op<matrix::Dense<ValueType>>(
474 workspace_.get_executor(), dim<2>{1, size});
476 typeid(matrix::Dense<ValueType>),
gko::dim<2>{1, size}, size);
479 template <
typename ValueType>
482 return workspace_.template create_or_get_array<ValueType>(array_id,
486 template <
typename ValueType>
489 return workspace_.template init_or_get_array<ValueType>(array_id);
493 mutable detail::workspace workspace_;
495 std::shared_ptr<const LinOp> system_matrix_;
502template <
typename MatrixType>
505 GKO_DEPRECATED(
"This class will be replaced by the template-less detail::SolverBaseLinOp in a future release")
SolverBase
507 : public detail::SolverBaseLinOp {
509 using detail::SolverBaseLinOp::SolverBaseLinOp;
520 return std::dynamic_pointer_cast<const MatrixType>(
521 SolverBaseLinOp::get_system_matrix());
525 void set_system_matrix_base(std::shared_ptr<const MatrixType> system_matrix)
527 SolverBaseLinOp::set_system_matrix_base(std::move(system_matrix));
541template <
typename DerivedType,
typename MatrixType = LinOp>
550 if (&other !=
this) {
562 if (&other !=
this) {
563 set_system_matrix(other.get_system_matrix());
564 other.set_system_matrix(
nullptr);
571 EnableSolverBase(std::shared_ptr<const MatrixType> system_matrix)
572 : SolverBase<MatrixType>{self()->get_executor()}
574 set_system_matrix(std::move(system_matrix));
581 :
SolverBase<MatrixType>{other.self()->get_executor()}
591 :
SolverBase<MatrixType>{other.self()->get_executor()}
593 *
this = std::move(other);
596 int get_num_workspace_ops()
const override
598 using traits = workspace_traits<DerivedType>;
599 return traits::num_vectors(*self());
602 std::vector<std::string> get_workspace_op_names()
const override
604 using traits = workspace_traits<DerivedType>;
605 return traits::op_names(*self());
615 return traits::scalars(*self());
625 return traits::vectors(*self());
629 void set_system_matrix(std::shared_ptr<const MatrixType> new_system_matrix)
631 auto exec = self()->get_executor();
632 if (new_system_matrix) {
633 GKO_ASSERT_EQUAL_DIMENSIONS(self(), new_system_matrix);
634 GKO_ASSERT_IS_SQUARE_MATRIX(new_system_matrix);
635 if (new_system_matrix->get_executor() != exec) {
636 new_system_matrix =
gko::clone(exec, new_system_matrix);
639 this->set_system_matrix_base(new_system_matrix);
642 void setup_workspace()
const
644 using traits = workspace_traits<DerivedType>;
645 this->set_workspace_size(traits::num_vectors(*self()),
646 traits::num_arrays(*self()));
650 DerivedType* self() {
return static_cast<DerivedType*
>(
this); }
652 const DerivedType* self()
const
654 return static_cast<const DerivedType*
>(
this);
675 return stop_factory_;
684 std::shared_ptr<const stop::CriterionFactory> new_stop_factory)
686 stop_factory_ = new_stop_factory;
690 std::shared_ptr<const stop::CriterionFactory> stop_factory_;
703template <
typename DerivedType>
712 if (&other !=
this) {
725 if (&other !=
this) {
727 other.set_stop_criterion_factory(
nullptr);
735 std::shared_ptr<const stop::CriterionFactory> stop_factory)
751 *
this = std::move(other);
755 std::shared_ptr<const stop::CriterionFactory> new_stop_factory)
override
757 auto exec = self()->get_executor();
758 if (new_stop_factory && new_stop_factory->get_executor() != exec) {
759 new_stop_factory =
gko::clone(exec, new_stop_factory);
765 DerivedType* self() {
return static_cast<DerivedType*
>(
this); }
767 const DerivedType* self()
const
769 return static_cast<const DerivedType*
>(
this);
784template <
typename ValueType,
typename DerivedType>
793 std::shared_ptr<const LinOp> system_matrix,
794 std::shared_ptr<const stop::CriterionFactory> stop_factory,
795 std::shared_ptr<const LinOp> preconditioner)
801 template <
typename FactoryParameters>
803 std::shared_ptr<const LinOp> system_matrix,
804 const FactoryParameters& params)
807 generate_preconditioner(system_matrix, params)}
811 template <
typename FactoryParameters>
812 static std::shared_ptr<const LinOp> generate_preconditioner(
813 std::shared_ptr<const LinOp> system_matrix,
814 const FactoryParameters& params)
816 if (params.generated_preconditioner) {
817 return params.generated_preconditioner;
818 }
else if (params.preconditioner) {
819 return params.preconditioner->generate(system_matrix);
822 system_matrix->get_executor(), system_matrix->get_size());
828template <
typename Parameters,
typename Factory>
834 std::vector<std::shared_ptr<const stop::CriterionFactory>>
839template <
typename Parameters,
typename Factory>
846 std::shared_ptr<const LinOpFactory> GKO_DEFERRED_FACTORY_PARAMETER(
862GKO_END_DISABLE_DEPRECATION_WARNINGS
Definition lin_op.hpp:118
A LinOp implementing this interface can be preconditioned.
Definition lin_op.hpp:683
virtual void set_preconditioner(std::shared_ptr< const LinOp > new_precond)
Sets the preconditioner operator used by the Preconditionable.
Definition lin_op.hpp:703
virtual std::shared_ptr< const LinOp > get_preconditioner() const
Returns the preconditioner operator used by the Preconditionable.
Definition lin_op.hpp:692
The enable_parameters_type mixin is used to create a base implementation of the factory parameters st...
Definition abstract_factory.hpp:211
static std::unique_ptr< Dense > create(std::shared_ptr< const Executor > exec, const dim< 2 > &size={}, size_type stride=0)
Creates an uninitialized Dense matrix of the specified size.
static std::unique_ptr< Identity > create(std::shared_ptr< const Executor > exec, dim< 2 > size)
Creates an Identity matrix of the specified size.
This class is used for function parameters in the place of raw pointers.
Definition utils_helper.hpp:43
ApplyWithInitialGuess provides a way to give the input guess for apply function.
Definition solver_base.hpp:66
EnableApplyWithInitialGuess providing default operation for ApplyWithInitialGuess with correct valida...
Definition solver_base.hpp:162
A LinOp deriving from this CRTP class stores a stopping criterion factory and allows applying with a ...
Definition solver_base.hpp:704
EnableIterativeBase & operator=(EnableIterativeBase &&other)
Moves the provided stopping criterion, clones it onto this executor if executors don't match.
Definition solver_base.hpp:723
void set_stop_criterion_factory(std::shared_ptr< const stop::CriterionFactory > new_stop_factory) override
Sets the stopping criterion of the solver.
Definition solver_base.hpp:754
EnableIterativeBase(EnableIterativeBase &&other)
Moves the provided stopping criterion.
Definition solver_base.hpp:749
EnableIterativeBase(const EnableIterativeBase &other)
Creates a shallow copy of the provided stopping criterion.
Definition solver_base.hpp:743
EnableIterativeBase & operator=(const EnableIterativeBase &other)
Creates a shallow copy of the provided stopping criterion, clones it onto this executor if executors ...
Definition solver_base.hpp:710
Mixin providing default operation for Preconditionable with correct value semantics.
Definition solver_base.hpp:271
EnablePreconditionable(const EnablePreconditionable &other)
Creates a shallow copy of the provided preconditioner.
Definition solver_base.hpp:328
EnablePreconditionable & operator=(EnablePreconditionable &&other)
Moves the provided preconditioner, clones it onto this executor if executors don't match.
Definition solver_base.hpp:309
EnablePreconditionable(EnablePreconditionable &&other)
Moves the provided preconditioner.
Definition solver_base.hpp:337
EnablePreconditionable & operator=(const EnablePreconditionable &other)
Creates a shallow copy of the provided preconditioner, clones it onto this executor if executors don'...
Definition solver_base.hpp:296
void set_preconditioner(std::shared_ptr< const LinOp > new_precond) override
Sets the preconditioner operator used by the Preconditionable.
Definition solver_base.hpp:279
A LinOp implementing this interface stores a system matrix and stopping criterion factory.
Definition solver_base.hpp:788
A LinOp deriving from this CRTP class stores a system matrix.
Definition solver_base.hpp:542
EnableSolverBase(EnableSolverBase &&other)
Moves the provided system matrix.
Definition solver_base.hpp:590
std::vector< int > get_workspace_vectors() const override
Returns the IDs of all vectors (workspace vectors with system dimension-dependent size,...
Definition solver_base.hpp:622
std::vector< int > get_workspace_scalars() const override
Returns the IDs of all scalars (workspace vectors with system dimension-independent size,...
Definition solver_base.hpp:612
EnableSolverBase(const EnableSolverBase &other)
Creates a shallow copy of the provided system matrix.
Definition solver_base.hpp:580
EnableSolverBase & operator=(EnableSolverBase &&other)
Moves the provided system matrix, clones it onto this executor if executors don't match.
Definition solver_base.hpp:560
EnableSolverBase & operator=(const EnableSolverBase &other)
Creates a shallow copy of the provided system matrix, clones it onto this executor if executors don't...
Definition solver_base.hpp:548
A LinOp implementing this interface stores a stopping criterion factory.
Definition solver_base.hpp:665
std::shared_ptr< const stop::CriterionFactory > get_stop_criterion_factory() const
Gets the stopping criterion factory of the solver.
Definition solver_base.hpp:672
virtual void set_stop_criterion_factory(std::shared_ptr< const stop::CriterionFactory > new_stop_factory)
Sets the stopping criterion of the solver.
Definition solver_base.hpp:683
Definition solver_base.hpp:507
std::shared_ptr< const MatrixType > get_system_matrix() const
Returns the system matrix, with its concrete type, used by the solver.
Definition solver_base.hpp:518
#define GKO_FACTORY_PARAMETER_SCALAR(_name, _default)
Creates a scalar factory parameter in the factory parameters structure.
Definition abstract_factory.hpp:445
std::shared_ptr< const CriterionFactory > combine(FactoryContainer &&factories)
Combines multiple criterion factories into a single combined criterion factory.
Definition combined.hpp:110
initial_guess_mode
Give a initial guess mode about the input of the apply method.
Definition solver_base.hpp:34
@ provided
the input is provided
@ rhs
the input is right hand side
The Ginkgo namespace.
Definition abstract_factory.hpp:20
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:86
detail::cloned_type< Pointer > clone(const Pointer &p)
Creates a unique clone of the object pointed to by p.
Definition utils_helper.hpp:175
detail::temporary_clone< detail::pointee< Ptr > > make_temporary_clone(std::shared_ptr< const Executor > exec, Ptr &&ptr)
Creates a temporary_clone.
Definition temporary_clone.hpp:209
@ array
The matrix should be written as dense matrix in column-major order.
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:27
Definition solver_base.hpp:830
std::vector< std::shared_ptr< const stop::CriterionFactory > > criteria
Stopping criteria to be used by the solver.
Definition solver_base.hpp:835
Definition solver_base.hpp:841
std::shared_ptr< const LinOp > generated_preconditioner
Already generated preconditioner.
Definition solver_base.hpp:854
std::shared_ptr< const LinOpFactory > preconditioner
The preconditioner to be used by the iterative solver.
Definition solver_base.hpp:847
Traits class providing information on the type and location of workspace vectors inside a solver.
Definition solver_base.hpp:239