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 const matrix::Dense<ValueType>* create_workspace_fixed_scalar(
480 int vector_id,
size_type size, ValueType val)
const
482 return workspace_.template create_or_get_op<matrix::Dense<ValueType>>(
486 workspace_.get_executor(), dim<2>{1, size});
490 typeid(matrix::Dense<ValueType>),
gko::dim<2>{1, size}, size);
493 template <
typename ValueType>
494 array<ValueType>& create_workspace_array(
int array_id,
size_type size)
const
496 return workspace_.template create_or_get_array<ValueType>(array_id,
500 template <
typename ValueType>
501 array<ValueType>& create_workspace_array(
int array_id)
const
503 return workspace_.template init_or_get_array<ValueType>(array_id);
507 mutable detail::workspace workspace_;
509 std::shared_ptr<const LinOp> system_matrix_;
516 template <
typename MatrixType>
519 GKO_DEPRECATED(
"This class will be replaced by the template-less detail::SolverBaseLinOp in a future release")
SolverBase
521 : public detail::SolverBaseLinOp {
523 using detail::SolverBaseLinOp::SolverBaseLinOp;
534 return std::dynamic_pointer_cast<const MatrixType>(
535 SolverBaseLinOp::get_system_matrix());
539 void set_system_matrix_base(std::shared_ptr<const MatrixType> system_matrix)
541 SolverBaseLinOp::set_system_matrix_base(std::move(system_matrix));
555 template <
typename DerivedType,
typename MatrixType = LinOp>
564 if (&other !=
this) {
576 if (&other !=
this) {
577 set_system_matrix(other.get_system_matrix());
578 other.set_system_matrix(
nullptr);
585 EnableSolverBase(std::shared_ptr<const MatrixType> system_matrix)
586 : SolverBase<MatrixType>{
self()->get_executor()}
588 set_system_matrix(std::move(system_matrix));
595 :
SolverBase<MatrixType>{other.self()->get_executor()}
605 :
SolverBase<MatrixType>{other.self()->get_executor()}
607 *
this = std::move(other);
610 int get_num_workspace_ops()
const override
612 using traits = workspace_traits<DerivedType>;
613 return traits::num_vectors(*
self());
616 std::vector<std::string> get_workspace_op_names()
const override
618 using traits = workspace_traits<DerivedType>;
619 return traits::op_names(*
self());
629 return traits::scalars(*
self());
639 return traits::vectors(*
self());
643 void set_system_matrix(std::shared_ptr<const MatrixType> new_system_matrix)
645 auto exec =
self()->get_executor();
646 if (new_system_matrix) {
647 GKO_ASSERT_EQUAL_DIMENSIONS(
self(), new_system_matrix);
648 GKO_ASSERT_IS_SQUARE_MATRIX(new_system_matrix);
649 if (new_system_matrix->get_executor() != exec) {
650 new_system_matrix =
gko::clone(exec, new_system_matrix);
653 this->set_system_matrix_base(new_system_matrix);
656 void setup_workspace()
const
658 using traits = workspace_traits<DerivedType>;
659 this->set_workspace_size(traits::num_vectors(*
self()),
660 traits::num_arrays(*
self()));
664 DerivedType*
self() {
return static_cast<DerivedType*>(
this); }
666 const DerivedType*
self()
const
668 return static_cast<const DerivedType*>(
this);
689 return stop_factory_;
698 std::shared_ptr<const stop::CriterionFactory> new_stop_factory)
700 stop_factory_ = new_stop_factory;
704 std::shared_ptr<const stop::CriterionFactory> stop_factory_;
717 template <
typename DerivedType>
726 if (&other !=
this) {
739 if (&other !=
this) {
741 other.set_stop_criterion_factory(
nullptr);
749 std::shared_ptr<const stop::CriterionFactory> stop_factory)
765 *
this = std::move(other);
769 std::shared_ptr<const stop::CriterionFactory> new_stop_factory)
override
771 auto exec =
self()->get_executor();
772 if (new_stop_factory && new_stop_factory->get_executor() != exec) {
773 new_stop_factory =
gko::clone(exec, new_stop_factory);
779 DerivedType*
self() {
return static_cast<DerivedType*>(
this); }
781 const DerivedType*
self()
const
783 return static_cast<const DerivedType*>(
this);
798 template <
typename ValueType,
typename DerivedType>
807 std::shared_ptr<const LinOp> system_matrix,
808 std::shared_ptr<const stop::CriterionFactory> stop_factory,
809 std::shared_ptr<const LinOp> preconditioner)
815 template <
typename FactoryParameters>
817 std::shared_ptr<const LinOp> system_matrix,
818 const FactoryParameters& params)
821 generate_preconditioner(system_matrix, params)}
825 template <
typename FactoryParameters>
826 static std::shared_ptr<const LinOp> generate_preconditioner(
827 std::shared_ptr<const LinOp> system_matrix,
828 const FactoryParameters& params)
830 if (params.generated_preconditioner) {
831 return params.generated_preconditioner;
832 }
else if (params.preconditioner) {
833 return params.preconditioner->generate(system_matrix);
836 system_matrix->get_executor(), system_matrix->get_size());
842 template <
typename Parameters,
typename Factory>
848 std::vector<std::shared_ptr<const stop::CriterionFactory>>
853 template <
typename Parameters,
typename Factory>
860 std::shared_ptr<const LinOpFactory> GKO_DEFERRED_FACTORY_PARAMETER(
876 GKO_END_DISABLE_DEPRECATION_WARNINGS
879 #endif // GKO_PUBLIC_CORE_SOLVER_SOLVER_BASE_HPP_