5 #ifndef GKO_PUBLIC_CORE_SOLVER_SOLVER_BASE_HPP_
6 #define GKO_PUBLIC_CORE_SOLVER_SOLVER_BASE_HPP_
10 #include <type_traits>
13 #include <ginkgo/core/base/lin_op.hpp>
14 #include <ginkgo/core/base/math.hpp>
15 #include <ginkgo/core/log/logger.hpp>
16 #include <ginkgo/core/matrix/dense.hpp>
17 #include <ginkgo/core/matrix/identity.hpp>
18 #include <ginkgo/core/solver/workspace.hpp>
19 #include <ginkgo/core/stop/combined.hpp>
20 #include <ginkgo/core/stop/criterion.hpp>
23 GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS
67 friend class multigrid::detail::MultigridState;
82 virtual void apply_with_initial_guess(
const LinOp* b,
LinOp* x,
88 apply_with_initial_guess(b.
get(), x.
get(), guess);
103 virtual void apply_with_initial_guess(
const LinOp* alpha,
const LinOp* b,
114 apply_with_initial_guess(alpha.
get(), b.
get(), beta.
get(), x.
get(),
160 template <
typename DerivedType>
163 friend class multigrid::detail::MultigridState;
174 void apply_with_initial_guess(
const LinOp* b,
LinOp* x,
177 self()->
template log<log::Logger::linop_apply_started>(
self(), b, x);
178 auto exec =
self()->get_executor();
179 GKO_ASSERT_CONFORMANT(
self(), b);
180 GKO_ASSERT_EQUAL_ROWS(
self(), x);
181 GKO_ASSERT_EQUAL_COLS(b, x);
185 self()->
template log<log::Logger::linop_apply_completed>(
self(), b, x);
192 void apply_with_initial_guess(
const LinOp* alpha,
const LinOp* b,
196 self()->
template log<log::Logger::linop_advanced_apply_started>(
197 self(), alpha, b, beta, x);
198 auto exec =
self()->get_executor();
199 GKO_ASSERT_CONFORMANT(
self(), b);
200 GKO_ASSERT_EQUAL_ROWS(
self(), x);
201 GKO_ASSERT_EQUAL_COLS(b, x);
202 GKO_ASSERT_EQUAL_DIMENSIONS(alpha,
dim<2>(1, 1));
203 GKO_ASSERT_EQUAL_DIMENSIONS(beta,
dim<2>(1, 1));
204 this->apply_with_initial_guess_impl(
209 self()->
template log<log::Logger::linop_advanced_apply_completed>(
210 self(), alpha, b, beta, x);
218 virtual void apply_with_initial_guess_impl(
225 virtual void apply_with_initial_guess_impl(
229 GKO_ENABLE_SELF(DerivedType);
237 template <
typename Solver>
240 static int num_vectors(
const Solver&) {
return 0; }
242 static int num_arrays(
const Solver&) {
return 0; }
244 static std::vector<std::string> op_names(
const Solver&) {
return {}; }
246 static std::vector<std::string> array_names(
const Solver&) {
return {}; }
248 static std::vector<int> scalars(
const Solver&) {
return {}; }
250 static std::vector<int> vectors(
const Solver&) {
return {}; }
269 template <
typename DerivedType>
280 auto exec =
self()->get_executor();
282 GKO_ASSERT_EQUAL_DIMENSIONS(
self(), new_precond);
283 GKO_ASSERT_IS_SQUARE_MATRIX(new_precond);
284 if (new_precond->get_executor() != exec) {
297 if (&other !=
this) {
310 if (&other !=
this) {
312 other.set_preconditioner(
nullptr);
338 *
this = std::move(other);
342 DerivedType*
self() {
return static_cast<DerivedType*>(
this); }
344 const DerivedType*
self()
const
346 return static_cast<const DerivedType*>(
this);
362 class SolverBaseLinOp {
364 SolverBaseLinOp(std::shared_ptr<const Executor> exec)
365 : workspace_{std::move(exec)}
368 virtual ~SolverBaseLinOp() =
default;
375 std::shared_ptr<const LinOp> get_system_matrix()
const
377 return system_matrix_;
380 const LinOp* get_workspace_op(
int vector_id)
const
382 return workspace_.get_op(vector_id);
385 virtual int get_num_workspace_ops()
const {
return 0; }
387 virtual std::vector<std::string> get_workspace_op_names()
const
396 virtual std::vector<int> get_workspace_scalars()
const {
return {}; }
402 virtual std::vector<int> get_workspace_vectors()
const {
return {}; }
405 void set_system_matrix_base(std::shared_ptr<const LinOp> system_matrix)
407 system_matrix_ = std::move(system_matrix);
410 void set_workspace_size(
int num_operators,
int num_arrays)
const
412 workspace_.set_size(num_operators, num_arrays);
415 template <
typename LinOpType>
416 LinOpType* create_workspace_op(
int vector_id,
gko::dim<2> size)
const
418 return workspace_.template create_or_get_op<LinOpType>(
421 return LinOpType::create(this->workspace_.get_executor(), size);
423 typeid(LinOpType), size, size[1]);
426 template <
typename LinOpType>
427 LinOpType* create_workspace_op_with_config_of(
int vector_id,
428 const LinOpType* vec)
const
430 return workspace_.template create_or_get_op<LinOpType>(
431 vector_id, [&] {
return LinOpType::create_with_config_of(vec); },
432 typeid(*vec), vec->get_size(), vec->get_stride());
435 template <
typename LinOpType>
436 LinOpType* create_workspace_op_with_type_of(
int vector_id,
437 const LinOpType* vec,
440 return workspace_.template create_or_get_op<LinOpType>(
443 return LinOpType::create_with_type_of(
444 vec, workspace_.get_executor(), size, size[1]);
446 typeid(*vec), size, size[1]);
449 template <
typename LinOpType>
450 LinOpType* create_workspace_op_with_type_of(
int vector_id,
451 const LinOpType* vec,
453 dim<2> local_size)
const
455 return workspace_.template create_or_get_op<LinOpType>(
458 return LinOpType::create_with_type_of(
459 vec, workspace_.get_executor(), global_size, local_size,
462 typeid(*vec), global_size, local_size[1]);
465 template <
typename ValueType>
466 matrix::Dense<ValueType>* create_workspace_scalar(
int vector_id,
469 return workspace_.template create_or_get_op<matrix::Dense<ValueType>>(
473 workspace_.get_executor(), dim<2>{1, size});
475 typeid(matrix::Dense<ValueType>),
gko::dim<2>{1, size}, size);
478 template <
typename ValueType>
479 array<ValueType>& create_workspace_array(
int array_id,
size_type size)
const
481 return workspace_.template create_or_get_array<ValueType>(array_id,
485 template <
typename ValueType>
486 array<ValueType>& create_workspace_array(
int array_id)
const
488 return workspace_.template init_or_get_array<ValueType>(array_id);
492 mutable detail::workspace workspace_;
494 std::shared_ptr<const LinOp> system_matrix_;
501 template <
typename MatrixType>
504 GKO_DEPRECATED(
"This class will be replaced by the template-less detail::SolverBaseLinOp in a future release")
SolverBase
506 : public detail::SolverBaseLinOp {
508 using detail::SolverBaseLinOp::SolverBaseLinOp;
519 return std::dynamic_pointer_cast<const MatrixType>(
520 SolverBaseLinOp::get_system_matrix());
524 void set_system_matrix_base(std::shared_ptr<const MatrixType> system_matrix)
526 SolverBaseLinOp::set_system_matrix_base(std::move(system_matrix));
540 template <
typename DerivedType,
typename MatrixType = LinOp>
549 if (&other !=
this) {
561 if (&other !=
this) {
562 set_system_matrix(other.get_system_matrix());
563 other.set_system_matrix(
nullptr);
570 EnableSolverBase(std::shared_ptr<const MatrixType> system_matrix)
571 : SolverBase<MatrixType>{
self()->get_executor()}
573 set_system_matrix(std::move(system_matrix));
580 :
SolverBase<MatrixType>{other.self()->get_executor()}
590 :
SolverBase<MatrixType>{other.self()->get_executor()}
592 *
this = std::move(other);
595 int get_num_workspace_ops()
const override
597 using traits = workspace_traits<DerivedType>;
598 return traits::num_vectors(*
self());
601 std::vector<std::string> get_workspace_op_names()
const override
603 using traits = workspace_traits<DerivedType>;
604 return traits::op_names(*
self());
614 return traits::scalars(*
self());
624 return traits::vectors(*
self());
628 void set_system_matrix(std::shared_ptr<const MatrixType> new_system_matrix)
630 auto exec =
self()->get_executor();
631 if (new_system_matrix) {
632 GKO_ASSERT_EQUAL_DIMENSIONS(
self(), new_system_matrix);
633 GKO_ASSERT_IS_SQUARE_MATRIX(new_system_matrix);
634 if (new_system_matrix->get_executor() != exec) {
635 new_system_matrix =
gko::clone(exec, new_system_matrix);
638 this->set_system_matrix_base(new_system_matrix);
641 void setup_workspace()
const
643 using traits = workspace_traits<DerivedType>;
644 this->set_workspace_size(traits::num_vectors(*
self()),
645 traits::num_arrays(*
self()));
649 DerivedType*
self() {
return static_cast<DerivedType*>(
this); }
651 const DerivedType*
self()
const
653 return static_cast<const DerivedType*>(
this);
674 return stop_factory_;
683 std::shared_ptr<const stop::CriterionFactory> new_stop_factory)
685 stop_factory_ = new_stop_factory;
689 std::shared_ptr<const stop::CriterionFactory> stop_factory_;
702 template <
typename DerivedType>
711 if (&other !=
this) {
724 if (&other !=
this) {
726 other.set_stop_criterion_factory(
nullptr);
734 std::shared_ptr<const stop::CriterionFactory> stop_factory)
750 *
this = std::move(other);
754 std::shared_ptr<const stop::CriterionFactory> new_stop_factory)
override
756 auto exec =
self()->get_executor();
757 if (new_stop_factory && new_stop_factory->get_executor() != exec) {
758 new_stop_factory =
gko::clone(exec, new_stop_factory);
764 DerivedType*
self() {
return static_cast<DerivedType*>(
this); }
766 const DerivedType*
self()
const
768 return static_cast<const DerivedType*>(
this);
783 template <
typename ValueType,
typename DerivedType>
792 std::shared_ptr<const LinOp> system_matrix,
793 std::shared_ptr<const stop::CriterionFactory> stop_factory,
794 std::shared_ptr<const LinOp> preconditioner)
800 template <
typename FactoryParameters>
802 std::shared_ptr<const LinOp> system_matrix,
803 const FactoryParameters& params)
806 generate_preconditioner(system_matrix, params)}
810 template <
typename FactoryParameters>
811 static std::shared_ptr<const LinOp> generate_preconditioner(
812 std::shared_ptr<const LinOp> system_matrix,
813 const FactoryParameters& params)
815 if (params.generated_preconditioner) {
816 return params.generated_preconditioner;
817 }
else if (params.preconditioner) {
818 return params.preconditioner->generate(system_matrix);
821 system_matrix->get_executor(), system_matrix->get_size());
827 template <
typename Parameters,
typename Factory>
833 std::vector<std::shared_ptr<const stop::CriterionFactory>>
838 template <
typename Parameters,
typename Factory>
845 std::shared_ptr<const LinOpFactory> GKO_DEFERRED_FACTORY_PARAMETER(
861 GKO_END_DISABLE_DEPRECATION_WARNINGS
864 #endif // GKO_PUBLIC_CORE_SOLVER_SOLVER_BASE_HPP_