5 #ifndef GKO_PUBLIC_CORE_PRECONDITIONER_IC_HPP_
6 #define GKO_PUBLIC_CORE_PRECONDITIONER_IC_HPP_
10 #include <type_traits>
13 #include <ginkgo/core/base/abstract_factory.hpp>
14 #include <ginkgo/core/base/composition.hpp>
15 #include <ginkgo/core/base/exception.hpp>
16 #include <ginkgo/core/base/exception_helpers.hpp>
17 #include <ginkgo/core/base/lin_op.hpp>
18 #include <ginkgo/core/base/precision_dispatch.hpp>
19 #include <ginkgo/core/base/std_extensions.hpp>
20 #include <ginkgo/core/config/config.hpp>
21 #include <ginkgo/core/config/registry.hpp>
22 #include <ginkgo/core/factorization/par_ic.hpp>
23 #include <ginkgo/core/matrix/dense.hpp>
24 #include <ginkgo/core/preconditioner/isai.hpp>
25 #include <ginkgo/core/preconditioner/utils.hpp>
26 #include <ginkgo/core/solver/gmres.hpp>
27 #include <ginkgo/core/solver/ir.hpp>
28 #include <ginkgo/core/solver/solver_traits.hpp>
29 #include <ginkgo/core/solver/triangular.hpp>
30 #include <ginkgo/core/stop/combined.hpp>
31 #include <ginkgo/core/stop/iteration.hpp>
32 #include <ginkgo/core/stop/residual_norm.hpp>
36 namespace preconditioner {
40 template <
typename Type>
41 constexpr
bool support_ic_parse =
42 is_instantiation_of<Type, solver::LowerTrs>::value ||
43 is_instantiation_of<Type, solver::Ir>::value ||
44 is_instantiation_of<Type, solver::Gmres>::value ||
45 is_instantiation_of<Type, preconditioner::LowerIsai>::value;
50 std::enable_if_t<!support_ic_parse<typename Ic::l_solver_type>>* =
nullptr>
56 "preconditioner::Ic only supports limited type for parse.");
61 std::enable_if_t<support_ic_parse<typename Ic::l_solver_type>>* =
nullptr>
114 template <
typename LSolverType = solver::LowerTrs<>,
typename IndexType =
int32>
121 std::is_same<
typename LSolverType::transposed_type::transposed_type,
123 "LSolverType::transposed_type must be symmetric");
124 using value_type =
typename LSolverType::value_type;
125 using l_solver_type = LSolverType;
126 using lh_solver_type =
typename LSolverType::transposed_type;
127 using index_type = IndexType;
137 std::shared_ptr<const typename l_solver_type::Factory>
145 GKO_DEPRECATED(
"use with_l_solver instead")
150 return with_l_solver(std::move(solver));
153 parameters_type& with_l_solver(
157 this->l_solver_generator = std::move(solver);
158 this->deferred_factories[
"l_solver"] = [](
const auto& exec,
160 if (!params.l_solver_generator.is_empty()) {
161 params.l_solver_factory =
162 params.l_solver_generator.on(exec);
168 GKO_DEPRECATED(
"use with_factorization instead")
169 parameters_type& with_factorization_factory(
170 deferred_factory_parameter<const LinOpFactory> factorization)
172 return with_factorization(std::move(factorization));
175 parameters_type& with_factorization(
176 deferred_factory_parameter<const LinOpFactory> factorization)
178 this->factorization_generator = std::move(factorization);
179 this->deferred_factories[
"factorization"] = [](
const auto& exec,
181 if (!params.factorization_generator.is_empty()) {
182 params.factorization_factory =
183 params.factorization_generator.on(exec);
190 deferred_factory_parameter<const typename l_solver_type::Factory>
193 deferred_factory_parameter<const LinOpFactory> factorization_generator;
218 config::make_type_descriptor<value_type, index_type>())
220 return detail::ic_parse<Ic>(config, context, td_for_child);
245 std::unique_ptr<transposed_type> transposed{
248 transposed->l_solver_ =
249 share(as<typename lh_solver_type::transposed_type>(
251 transposed->lh_solver_ =
252 share(as<typename l_solver_type::transposed_type>(
255 return std::move(transposed);
260 std::unique_ptr<transposed_type> transposed{
263 transposed->l_solver_ =
264 share(as<typename lh_solver_type::transposed_type>(
266 transposed->lh_solver_ =
267 share(as<typename l_solver_type::transposed_type>(
270 return std::move(transposed);
280 if (&other !=
this) {
283 l_solver_ = other.l_solver_;
284 lh_solver_ = other.lh_solver_;
285 parameters_ = other.parameters_;
302 if (&other !=
this) {
305 l_solver_ = std::move(other.l_solver_);
306 lh_solver_ = std::move(other.lh_solver_);
308 if (other.get_executor() != exec) {
330 void apply_impl(
const LinOp* b,
LinOp* x)
const override
333 precision_dispatch_real_complex<value_type>(
334 [&](
auto dense_b,
auto dense_x) {
335 this->set_cache_to(dense_b);
336 l_solver_->apply(dense_b, cache_.intermediate);
337 if (lh_solver_->apply_uses_initial_guess()) {
338 dense_x->copy_from(cache_.intermediate);
340 lh_solver_->apply(cache_.intermediate, dense_x);
346 LinOp* x)
const override
348 precision_dispatch_real_complex<value_type>(
349 [&](
auto dense_alpha,
auto dense_b,
auto dense_beta,
auto dense_x) {
350 this->set_cache_to(dense_b);
351 l_solver_->apply(dense_b, cache_.intermediate);
352 lh_solver_->apply(dense_alpha, cache_.intermediate, dense_beta,
358 explicit Ic(std::shared_ptr<const Executor> exec)
359 : EnableLinOp<
Ic>(std::move(exec))
362 explicit Ic(
const Factory* factory, std::shared_ptr<const LinOp> lin_op)
364 parameters_{
factory->get_parameters()}
367 std::dynamic_pointer_cast<
const Composition<value_type>>(lin_op);
368 std::shared_ptr<const LinOp> l_factor;
372 auto exec = lin_op->get_executor();
375 factorization::ParIc<value_type, index_type>::build()
376 .with_both_factors(
false)
379 auto fact = std::shared_ptr<const LinOp>(
383 std::dynamic_pointer_cast<
const Composition<value_type>>(fact);
385 GKO_NOT_SUPPORTED(comp);
389 if (comp->get_operators().size() > 2 || comp->get_operators().empty()) {
390 GKO_NOT_SUPPORTED(comp);
392 l_factor = comp->get_operators()[0];
393 GKO_ASSERT_IS_SQUARE_MATRIX(l_factor);
399 l_solver_ = generate_default_solver<l_solver_type>(exec, l_factor);
402 if (comp->get_operators().size() == 2) {
403 auto lh_factor = comp->get_operators()[1];
404 GKO_ASSERT_EQUAL_DIMENSIONS(l_factor, lh_factor);
405 lh_solver_ = as<lh_solver_type>(l_solver_->conj_transpose());
407 lh_solver_ = as<lh_solver_type>(l_solver_->conj_transpose());
411 lh_solver_ = as<lh_solver_type>(l_solver_->conj_transpose());
422 void set_cache_to(
const LinOp* b)
const
424 if (cache_.intermediate ==
nullptr) {
425 cache_.intermediate =
429 cache_.intermediate->copy_from(b);
440 template <
typename SolverType>
441 static std::enable_if_t<solver::has_with_criteria<SolverType>::value,
442 std::unique_ptr<SolverType>>
443 generate_default_solver(
const std::shared_ptr<const Executor>& exec,
444 const std::shared_ptr<const LinOp>& mtx)
447 const unsigned int default_max_iters{
448 static_cast<unsigned int>(mtx->get_size()[0])};
450 return SolverType::build()
452 gko::stop::Iteration::build().with_max_iters(default_max_iters),
454 .with_reduction_factor(default_reduce_residual))
462 template <
typename SolverType>
463 static std::enable_if_t<!solver::has_with_criteria<SolverType>::value,
464 std::unique_ptr<SolverType>>
465 generate_default_solver(
const std::shared_ptr<const Executor>& exec,
466 const std::shared_ptr<const LinOp>& mtx)
468 return SolverType::build().on(exec)->generate(mtx);
472 std::shared_ptr<const l_solver_type> l_solver_{};
473 std::shared_ptr<const lh_solver_type> lh_solver_{};
484 mutable struct cache_struct {
485 cache_struct() =
default;
486 ~cache_struct() =
default;
487 cache_struct(
const cache_struct&) {}
488 cache_struct(cache_struct&&) {}
489 cache_struct&
operator=(
const cache_struct&) {
return *
this; }
490 cache_struct&
operator=(cache_struct&&) {
return *
this; }
491 std::unique_ptr<LinOp> intermediate{};
500 #endif // GKO_PUBLIC_CORE_PRECONDITIONER_IC_HPP_