5 #ifndef GKO_PUBLIC_CORE_SOLVER_SOLVER_BASE_HPP_
6 #define GKO_PUBLIC_CORE_SOLVER_SOLVER_BASE_HPP_
11 #include <type_traits>
15 #include <ginkgo/core/base/exception.hpp>
16 #include <ginkgo/core/base/lin_op.hpp>
17 #include <ginkgo/core/base/math.hpp>
18 #include <ginkgo/core/base/name_demangling.hpp>
19 #include <ginkgo/core/log/logger.hpp>
20 #include <ginkgo/core/matrix/identity.hpp>
21 #include <ginkgo/core/matrix/multivector.hpp>
22 #include <ginkgo/core/preconditioner/nullspace_projection.hpp>
23 #include <ginkgo/core/solver/nullspace.hpp>
24 #include <ginkgo/core/solver/workspace.hpp>
25 #include <ginkgo/core/stop/combined.hpp>
26 #include <ginkgo/core/stop/criterion.hpp>
29 GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS
76 friend class multigrid::detail::MultigridState;
99 apply_with_initial_guess(b.
get(), x.
get(), guess);
127 apply_with_initial_guess(alpha.
get(), b.
get(), beta.
get(), x.
get(),
173 template <
typename DerivedType>
176 friend class multigrid::detail::MultigridState;
192 self()->
template log<log::Logger::linop_apply_started>(
self(), b, x);
193 auto exec =
self()->get_executor();
194 GKO_ASSERT_CONFORMANT(
self(), b);
195 GKO_ASSERT_EQUAL_ROWS(
self(), x);
196 GKO_ASSERT_EQUAL_COLS(b, x);
200 self()->
template log<log::Logger::linop_apply_completed>(
self(), b, x);
214 self()->
template log<log::Logger::linop_advanced_apply_started>(
215 self(), alpha, b, beta, x);
216 auto exec =
self()->get_executor();
217 GKO_ASSERT_CONFORMANT(
self(), b);
218 GKO_ASSERT_EQUAL_ROWS(
self(), x);
219 GKO_ASSERT_EQUAL_COLS(b, x);
220 GKO_ASSERT_EQUAL_DIMENSIONS(alpha,
dim<2>(1, 1));
221 GKO_ASSERT_EQUAL_DIMENSIONS(beta,
dim<2>(1, 1));
222 this->apply_with_initial_guess_impl(
227 self()->
template log<log::Logger::linop_advanced_apply_completed>(
228 self(), alpha, b, beta, x);
236 virtual void apply_with_initial_guess_impl(
244 virtual void apply_with_initial_guess_impl(
249 GKO_ENABLE_SELF(DerivedType);
257 template <
typename Solver>
260 static int num_vectors(
const Solver&) {
return 0; }
262 static int num_arrays(
const Solver&) {
return 0; }
264 static std::vector<std::string> op_names(
const Solver&) {
return {}; }
266 static std::vector<std::string> array_names(
const Solver&) {
return {}; }
268 static std::vector<int> scalars(
const Solver&) {
return {}; }
270 static std::vector<int> vectors(
const Solver&) {
return {}; }
289 template <
typename DerivedType>
300 auto exec =
self()->get_executor();
302 GKO_ASSERT_EQUAL_DIMENSIONS(
self(), new_precond);
303 GKO_ASSERT_IS_SQUARE_MATRIX(new_precond);
304 if (new_precond->get_executor() != exec) {
317 if (&other !=
this) {
330 if (&other !=
this) {
332 other.set_preconditioner(
nullptr);
358 *
this = std::move(other);
362 DerivedType*
self() {
return static_cast<DerivedType*>(
this); }
364 const DerivedType*
self()
const
366 return static_cast<const DerivedType*>(
this);
382 class SolverBaseLinOp {
384 SolverBaseLinOp(std::shared_ptr<const Executor> exec)
385 : workspace_{std::move(exec)}
388 virtual ~SolverBaseLinOp() =
default;
395 std::shared_ptr<const LinOp> get_system_matrix()
const
397 return system_matrix_;
400 const AbstractMultiVector* get_workspace_op(
int vector_id)
const
402 return workspace_.get_vector(vector_id);
405 virtual int get_num_workspace_ops()
const {
return 0; }
407 virtual std::vector<std::string> get_workspace_op_names()
const
416 virtual std::vector<int> get_workspace_scalars()
const {
return {}; }
422 virtual std::vector<int> get_workspace_vectors()
const {
return {}; }
425 void set_system_matrix_base(std::shared_ptr<const LinOp> system_matrix)
427 system_matrix_ = std::move(system_matrix);
430 void set_workspace_size(
int num_operators,
int num_arrays)
const
432 workspace_.set_size(num_operators, num_arrays);
435 template <
typename VectorType,
436 typename = std::enable_if_t<
437 std::is_base_of_v<AbstractMultiVector, VectorType>>>
438 VectorType* create_workspace_op(
int vector_id, dim<2> size)
const
440 return as<VectorType>(workspace_.create_or_get_vector(
443 return VectorType::create(this->workspace_.get_executor(),
446 typeid(VectorType), size));
449 AbstractMultiVector* create_workspace_op_with_config_of(
450 int vector_id,
const AbstractMultiVector* vec)
const
452 return workspace_.create_or_get_vector(
455 typeid(*vec), vec->get_size());
458 AbstractMultiVector* create_workspace_op_with_type_of(
459 int vector_id,
const AbstractMultiVector* vec, dim<2> global_size,
460 dim<2> local_size)
const
462 return workspace_.create_or_get_vector(
466 vec, workspace_.get_executor(), global_size, local_size);
468 typeid(*vec), global_size);
471 template <
typename ValueType>
472 matrix::MultiVector<ValueType>* create_workspace_scalar(
475 return as<matrix::MultiVector<ValueType>>(
476 workspace_.create_or_get_vector(
480 workspace_.get_executor(), dim<2>{1, size});
482 typeid(matrix::MultiVector<ValueType>),
gko::dim<2>{1, size}));
485 template <
typename ValueType>
486 const matrix::MultiVector<ValueType>* create_workspace_fixed_scalar(
487 int vector_id,
size_type size, ValueType val)
const
489 return as<matrix::MultiVector<ValueType>>(
490 workspace_.create_or_get_vector(
494 workspace_.get_executor(), dim<2>{1, size});
498 typeid(matrix::MultiVector<ValueType>),
gko::dim<2>{1, size}));
501 template <
typename ValueType>
502 array<ValueType>& create_workspace_array(
int array_id,
size_type size)
const
504 return workspace_.template create_or_get_array<ValueType>(array_id,
508 template <
typename ValueType>
509 array<ValueType>& create_workspace_array(
int array_id)
const
511 return workspace_.template init_or_get_array<ValueType>(array_id);
515 mutable detail::workspace workspace_;
517 std::shared_ptr<const LinOp> system_matrix_;
524 template <
typename MatrixType>
527 GKO_DEPRECATED(
"This class will be replaced by the template-less detail::SolverBaseLinOp in a future release")
SolverBase
529 : public detail::SolverBaseLinOp {
531 using detail::SolverBaseLinOp::SolverBaseLinOp;
542 return std::dynamic_pointer_cast<const MatrixType>(
543 SolverBaseLinOp::get_system_matrix());
547 void set_system_matrix_base(std::shared_ptr<const MatrixType> system_matrix)
549 SolverBaseLinOp::set_system_matrix_base(std::move(system_matrix));
563 template <
typename DerivedType,
typename MatrixType = LinOp>
572 if (&other !=
this) {
584 if (&other !=
this) {
585 set_system_matrix(other.get_system_matrix());
586 other.set_system_matrix(
nullptr);
593 EnableSolverBase(std::shared_ptr<const MatrixType> system_matrix)
594 : SolverBase<MatrixType>{
self()->get_executor()}
596 set_system_matrix(std::move(system_matrix));
603 :
SolverBase<MatrixType>{other.self()->get_executor()}
613 :
SolverBase<MatrixType>{other.self()->get_executor()}
615 *
this = std::move(other);
618 int get_num_workspace_ops()
const override
620 using traits = workspace_traits<DerivedType>;
621 return traits::num_vectors(*
self());
624 std::vector<std::string> get_workspace_op_names()
const override
626 using traits = workspace_traits<DerivedType>;
627 return traits::op_names(*
self());
637 return traits::scalars(*
self());
647 return traits::vectors(*
self());
651 void set_system_matrix(std::shared_ptr<const MatrixType> new_system_matrix)
653 auto exec =
self()->get_executor();
654 if (new_system_matrix) {
655 GKO_ASSERT_EQUAL_DIMENSIONS(
self(), new_system_matrix);
656 GKO_ASSERT_IS_SQUARE_MATRIX(new_system_matrix);
657 if (new_system_matrix->get_executor() != exec) {
658 new_system_matrix =
gko::clone(exec, new_system_matrix);
661 this->set_system_matrix_base(new_system_matrix);
664 void setup_workspace()
const
666 using traits = workspace_traits<DerivedType>;
667 this->set_workspace_size(traits::num_vectors(*
self()),
668 traits::num_arrays(*
self()));
672 DerivedType*
self() {
return static_cast<DerivedType*>(
this); }
674 const DerivedType*
self()
const
676 return static_cast<const DerivedType*>(
this);
699 return stop_factory_;
708 std::shared_ptr<const stop::CriterionFactory> new_stop_factory)
710 stop_factory_ = new_stop_factory;
714 std::shared_ptr<const stop::CriterionFactory> stop_factory_;
727 template <
typename DerivedType>
736 if (&other !=
this) {
749 if (&other !=
this) {
751 other.set_stop_criterion_factory(
nullptr);
759 std::shared_ptr<const stop::CriterionFactory> stop_factory)
775 *
this = std::move(other);
779 std::shared_ptr<const stop::CriterionFactory> new_stop_factory)
override
781 auto exec =
self()->get_executor();
782 if (new_stop_factory && new_stop_factory->get_executor() != exec) {
784 new_stop_factory =
gko::clone(exec, new_stop_factory);
790 DerivedType*
self() {
return static_cast<DerivedType*>(
this); }
792 const DerivedType*
self()
const
794 return static_cast<const DerivedType*>(
this);
806 template <
typename Solver>
840 template <
typename ValueType,
typename DerivedType>
849 std::shared_ptr<const LinOp> system_matrix,
850 std::shared_ptr<const stop::CriterionFactory> stop_factory,
851 std::shared_ptr<const LinOp> preconditioner)
857 template <
typename FactoryParameters>
859 std::shared_ptr<const LinOp> system_matrix,
860 const FactoryParameters& params)
863 generate_preconditioner(system_matrix, params)}
866 if ((params.nullspace || params.left_nullspace) &&
867 !traits::is_supported) {
869 __FILE__, __LINE__,
"nullspace projection",
870 name_demangling::get_type_name(
typeid(DerivedType)));
873 auto exec = get_solver_executor();
874 nullspace_ = detail::prepare_nullspace<ValueType>(
875 params.nullspace,
"nullspace", system_matrix.get(),
876 system_matrix->get_size()[1], exec);
877 left_nullspace_ = detail::prepare_nullspace<ValueType>(
878 params.left_nullspace,
"left_nullspace", system_matrix.get(),
879 system_matrix->get_size()[0], exec);
881 if (traits::requires_hermitian) {
882 if (!left_nullspace_) {
883 left_nullspace_ = nullspace_;
886 nullspace_ = left_nullspace_;
908 if (&other !=
this) {
909 nullspace_ = on_executor(other.nullspace_);
910 left_nullspace_ = on_executor(other.left_nullspace_);
925 if (&other !=
this) {
926 nullspace_ = on_executor(std::exchange(other.nullspace_,
nullptr));
928 on_executor(std::exchange(other.left_nullspace_,
nullptr));
950 return left_nullspace_;
961 if (new_precond && nullspace_ &&
962 !removes_nullspaces(new_precond.get())) {
963 new_precond = project_preconditioner(std::move(new_precond));
966 std::move(new_precond));
980 if (
auto projection = std::dynamic_pointer_cast<
981 const ::gko::preconditioner::NullspaceProjection<
982 ValueType>>(precond)) {
983 return projection->get_preconditioner();
999 nullspace_->project(v);
1014 const AbstractMultiVector* get_consistent_rhs(
const AbstractMultiVector* b,
1015 int workspace_id)
const
1017 if (!left_nullspace_) {
1020 auto rhs = this->create_workspace_op_with_config_of(workspace_id, b);
1022 left_nullspace_->project(
rhs);
1030 std::shared_ptr<const LinOp> get_transposed_nullspace(
bool conjugate)
const
1032 assert_nullspace_transposable(conjugate);
1033 return left_nullspace_;
1040 std::shared_ptr<const LinOp> get_transposed_left_nullspace(
1041 bool conjugate)
const
1043 assert_nullspace_transposable(conjugate);
1049 bool removes_nullspaces(
const LinOp* precond)
const
1051 auto projection =
dynamic_cast<
1052 const ::gko::preconditioner::NullspaceProjection<ValueType>*
>(
1054 return projection && projection->get_nullspace() &&
1056 !left_nullspace_ || projection->get_left_nullspace());
1062 void assert_nullspace_transposable(
bool conjugate)
const
1064 if (!conjugate && is_complex<ValueType>() &&
1065 (nullspace_ || left_nullspace_)) {
1067 __FILE__, __LINE__,
"transpose with nullspace",
1068 name_demangling::get_type_name(
typeid(DerivedType)));
1072 std::shared_ptr<const Executor> get_solver_executor()
const
1074 return static_cast<const DerivedType*>(
this)->get_executor();
1077 std::shared_ptr<const Nullspace<ValueType>> on_executor(
1078 std::shared_ptr<
const Nullspace<ValueType>> ns)
const
1080 auto exec = get_solver_executor();
1081 if (ns && ns->get_executor() != exec) {
1088 std::shared_ptr<const LinOp> project_preconditioner(
1089 std::shared_ptr<const LinOp> precond)
const
1091 auto projection_params =
1093 .with_generated_preconditioner(std::move(precond))
1094 .with_nullspace(nullspace_);
1096 projection_params.with_left_nullspace(left_nullspace_);
1098 return projection_params.on(get_solver_executor())
1102 template <
typename FactoryParameters>
1103 static std::shared_ptr<const LinOp> generate_preconditioner(
1104 std::shared_ptr<const LinOp> system_matrix,
1105 const FactoryParameters& params)
1107 if (params.generated_preconditioner) {
1108 return params.generated_preconditioner;
1109 }
else if (params.preconditioner) {
1110 return params.preconditioner->generate(system_matrix);
1113 system_matrix->get_executor(), system_matrix->get_size());
1117 std::shared_ptr<const Nullspace<ValueType>> nullspace_{};
1118 std::shared_ptr<const Nullspace<ValueType>> left_nullspace_{};
1122 template <
typename Parameters,
typename Factory>
1128 std::vector<std::shared_ptr<const stop::CriterionFactory>>
1133 template <
typename Parameters,
typename Factory>
1140 std::shared_ptr<const LinOpFactory> GKO_DEFERRED_FACTORY_PARAMETER(
1199 GKO_END_DISABLE_DEPRECATION_WARNINGS
1202 #endif // GKO_PUBLIC_CORE_SOLVER_SOLVER_BASE_HPP_