5#ifndef GKO_PUBLIC_CORE_SOLVER_BATCH_SOLVER_BASE_HPP_
6#define GKO_PUBLIC_CORE_SOLVER_BATCH_SOLVER_BASE_HPP_
9#include <ginkgo/core/base/abstract_factory.hpp>
10#include <ginkgo/core/base/batch_lin_op.hpp>
11#include <ginkgo/core/base/batch_multi_vector.hpp>
12#include <ginkgo/core/base/utils_helper.hpp>
13#include <ginkgo/core/log/batch_logger.hpp>
14#include <ginkgo/core/matrix/batch_identity.hpp>
15#include <ginkgo/core/stop/batch_stop_enum.hpp>
38 return this->system_matrix_;
48 return this->preconditioner_;
67 GKO_INVALID_STATE(
"Tolerance cannot be negative!");
87 if (max_iterations < 0) {
88 GKO_INVALID_STATE(
"Max iterations cannot be negative!");
90 this->max_iterations_ = max_iterations;
100 return this->tol_type_;
110 if (
tol_type == ::gko::batch::stop::tolerance_type::absolute ||
111 tol_type == ::gko::batch::stop::tolerance_type::relative) {
114 GKO_INVALID_STATE(
"Invalid tolerance type specified!");
121 BatchSolver(std::shared_ptr<const BatchLinOp> system_matrix,
123 const double res_tol,
const int max_iterations,
124 const ::gko::batch::stop::tolerance_type
tol_type)
125 : system_matrix_{std::
move(system_matrix)},
128 max_iterations_{max_iterations},
133 void set_system_matrix_base(std::shared_ptr<const BatchLinOp> system_matrix)
135 this->system_matrix_ = std::move(system_matrix);
138 void set_preconditioner_base(std::shared_ptr<const BatchLinOp> precond)
140 this->preconditioner_ = std::move(precond);
143 std::shared_ptr<const BatchLinOp> system_matrix_{};
144 std::shared_ptr<const BatchLinOp> preconditioner_{};
145 double residual_tol_{};
146 int max_iterations_{};
147 ::gko::batch::stop::tolerance_type tol_type_{};
148 mutable array<unsigned char> workspace_{};
152template <
typename Parameters,
typename Factory>
182 std::shared_ptr<const BatchLinOpFactory> GKO_DEFERRED_FACTORY_PARAMETER(
213 this->validate_application_parameters(b.get(), x.get());
225 this->validate_application_parameters(alpha.get(), b.get(), beta.get(),
238 this->validate_application_parameters(b.get(), x.get());
250 this->validate_application_parameters(alpha.get(), b.get(), beta.get(),
267 template <
typename FactoryParameters>
269 std::shared_ptr<const BatchLinOp> system_matrix,
276 GKO_ASSERT_BATCH_HAS_SQUARE_DIMENSIONS(system_matrix_);
278 using value_type =
typename ConcreteSolver::value_type;
282 if (
params.generated_preconditioner) {
283 GKO_ASSERT_BATCH_EQUAL_DIMENSIONS(
params.generated_preconditioner,
285 preconditioner_ = std::move(
params.generated_preconditioner);
286 }
else if (
params.preconditioner) {
287 preconditioner_ =
params.preconditioner->generate(system_matrix_);
289 auto id = Identity::create(exec, system_matrix->get_size());
290 preconditioner_ = std::move(
id);
293 (
sizeof(real_type) +
sizeof(
int));
294 workspace_.set_executor(exec);
300 auto exec = self()->get_executor();
311 void set_preconditioner(std::shared_ptr<const BatchLinOp>
new_precond)
313 auto exec = self()->get_executor();
315 GKO_ASSERT_BATCH_EQUAL_DIMENSIONS(self(),
new_precond);
316 GKO_ASSERT_BATCH_HAS_SQUARE_DIMENSIONS(
new_precond);
326 if (&
other !=
this) {
327 this->set_size(
other.get_size());
328 this->set_system_matrix(
other.get_system_matrix());
329 this->set_preconditioner(
other.get_preconditioner());
340 if (&
other !=
this) {
341 this->set_size(
other.get_size());
342 this->set_system_matrix(
other.get_system_matrix());
343 this->set_preconditioner(
other.get_preconditioner());
347 other.set_system_matrix(
nullptr);
348 other.set_preconditioner(
nullptr);
355 other.self()->get_executor(),
other.self()->get_size())
362 other.self()->get_executor(),
other.self()->get_size())
364 *
this = std::move(
other);
375 auto log_data_ = std::make_unique<log::detail::log_data<real_type>>(
378 this->solver_apply(b, x,
log_data_.get());
397 log::detail::log_data<real_type>*
info)
const = 0;
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition polymorphic_object.hpp:235
Definition batch_lin_op.hpp:60
The EnableBatchLinOp mixin can be used to provide sensible default implementations of the majority of...
Definition batch_lin_op.hpp:253
MultiVector stores multiple vectors in a batched fashion and is useful for batched operations.
Definition batch_multi_vector.hpp:56
void scale(ptr_param< const MultiVector< ValueType > > alpha)
Scales the vector with a scalar (aka: BLAS scal).
dim< 2 > get_common_size() const
Returns the common size of the batch items.
Definition batch_multi_vector.hpp:127
size_type get_num_batch_items() const
Returns the number of batch items.
Definition batch_multi_vector.hpp:117
void add_scaled(ptr_param< const MultiVector< ValueType > > alpha, ptr_param< const MultiVector< ValueType > > b)
Adds b scaled by alpha to the vector (aka: BLAS axpy).
The batch Identity matrix, which represents a batch of Identity matrices.
Definition batch_identity.hpp:32
The BatchSolver is a base class for all batched solvers and provides the common getters and setter fo...
Definition batch_solver_base.hpp:29
std::shared_ptr< const BatchLinOp > get_system_matrix() const
Returns the system operator (matrix) of the linear system.
Definition batch_solver_base.hpp:36
void reset_max_iterations(int max_iterations)
Set the maximum number of iterations for the solver to use, independent of the factory that created i...
Definition batch_solver_base.hpp:85
double get_tolerance() const
Get the residual tolerance used by the solver.
Definition batch_solver_base.hpp:56
int get_max_iterations() const
Get the maximum number of iterations set on the solver.
Definition batch_solver_base.hpp:77
void reset_tolerance(double res_tol)
Update the residual tolerance to be used by the solver.
Definition batch_solver_base.hpp:64
::gko::batch::stop::tolerance_type get_tolerance_type() const
Get the tolerance type.
Definition batch_solver_base.hpp:98
void reset_tolerance_type(::gko::batch::stop::tolerance_type tol_type)
Set the type of tolerance check to use inside the solver.
Definition batch_solver_base.hpp:108
std::shared_ptr< const BatchLinOp > get_preconditioner() const
Returns the generated preconditioner.
Definition batch_solver_base.hpp:46
This mixin provides apply and common iterative solver functionality to all the batched solvers.
Definition batch_solver_base.hpp:206
The enable_parameters_type mixin is used to create a base implementation of the factory parameters st...
Definition abstract_factory.hpp:211
This class is used for function parameters in the place of raw pointers.
Definition utils_helper.hpp:43
#define GKO_FACTORY_PARAMETER_SCALAR(_name, _default)
Creates a scalar factory parameter in the factory parameters structure.
Definition abstract_factory.hpp:445
The Ginkgo namespace.
Definition abstract_factory.hpp:20
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:775
typename detail::remove_complex_s< T >::type remove_complex
Obtain the type which removed the complex of complex/scalar type or the template parameter of class b...
Definition math.hpp:326
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:92
detail::cloned_type< Pointer > clone(const Pointer &p)
Creates a unique clone of the object pointed to by p.
Definition utils_helper.hpp:175
batch_dim< 2, DimensionType > transpose(const batch_dim< 2, DimensionType > &input)
Returns a batch_dim object with its dimensions swapped for batched operators.
Definition batch_dim.hpp:120
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:179
Definition batch_solver_base.hpp:154
int max_iterations
Default maximum number iterations allowed.
Definition batch_solver_base.hpp:161
double tolerance
Default residual tolerance.
Definition batch_solver_base.hpp:169
std::shared_ptr< const BatchLinOpFactory > preconditioner
The preconditioner to be used by the iterative solver.
Definition batch_solver_base.hpp:183
::gko::batch::stop::tolerance_type tolerance_type
To specify which type of tolerance check is to be considered, absolute or relative (to the rhs l2 nor...
Definition batch_solver_base.hpp:176
std::shared_ptr< const BatchLinOp > generated_preconditioner
Already generated preconditioner.
Definition batch_solver_base.hpp:190