5 #ifndef GKO_PUBLIC_CORE_PRECONDITIONER_ILU_HPP_
6 #define GKO_PUBLIC_CORE_PRECONDITIONER_ILU_HPP_
10 #include <type_traits>
12 #include <ginkgo/core/base/abstract_factory.hpp>
13 #include <ginkgo/core/base/composition.hpp>
14 #include <ginkgo/core/base/exception.hpp>
15 #include <ginkgo/core/base/exception_helpers.hpp>
16 #include <ginkgo/core/base/lin_op.hpp>
17 #include <ginkgo/core/base/precision_dispatch.hpp>
18 #include <ginkgo/core/config/config.hpp>
19 #include <ginkgo/core/config/registry.hpp>
20 #include <ginkgo/core/factorization/par_ilu.hpp>
21 #include <ginkgo/core/matrix/dense.hpp>
22 #include <ginkgo/core/preconditioner/isai.hpp>
23 #include <ginkgo/core/preconditioner/utils.hpp>
24 #include <ginkgo/core/solver/gmres.hpp>
25 #include <ginkgo/core/solver/ir.hpp>
26 #include <ginkgo/core/solver/solver_traits.hpp>
27 #include <ginkgo/core/solver/triangular.hpp>
28 #include <ginkgo/core/stop/combined.hpp>
29 #include <ginkgo/core/stop/iteration.hpp>
30 #include <ginkgo/core/stop/residual_norm.hpp>
34 namespace preconditioner {
38 template <
typename LSolverType,
typename USolverType>
39 constexpr
bool support_ilu_parse =
40 std::is_same<typename USolverType::transposed_type, LSolverType>::value &&
41 (is_instantiation_of<LSolverType, solver::LowerTrs>::value ||
42 is_instantiation_of<LSolverType, solver::Ir>::value ||
43 is_instantiation_of<LSolverType, solver::Gmres>::value ||
44 is_instantiation_of<LSolverType, preconditioner::LowerIsai>::value);
47 template <
typename Ilu,
48 std::enable_if_t<!support_ilu_parse<
typename Ilu::l_solver_type,
49 typename Ilu::u_solver_type>>* =
51 typename Ilu::parameters_type ilu_parse(
52 const config::pnode& config,
const config::registry& context,
53 const config::type_descriptor& td_for_child)
56 "preconditioner::Ilu only supports limited type for parse.");
61 std::enable_if_t<support_ilu_parse<
typename Ilu::l_solver_type,
62 typename Ilu::u_solver_type>>* =
nullptr>
63 typename Ilu::parameters_type ilu_parse(
64 const config::pnode& config,
const config::registry& context,
65 const config::type_descriptor& td_for_child);
119 template <
typename LSolverType = solver::LowerTrs<>,
120 typename USolverType = solver::UpperTrs<>,
bool ReverseApply = false,
121 typename IndexType =
int32>
123 Ilu<LSolverType, USolverType, ReverseApply, IndexType>>,
130 std::is_same<
typename LSolverType::value_type,
131 typename USolverType::value_type>::value,
132 "Both the L- and the U-solver must use the same `value_type`!");
133 using value_type =
typename LSolverType::value_type;
134 using l_solver_type = LSolverType;
135 using u_solver_type = USolverType;
136 static constexpr
bool performs_reverse_apply = ReverseApply;
137 using index_type = IndexType;
139 Ilu<
typename USolverType::transposed_type,
140 typename LSolverType::transposed_type, ReverseApply, IndexType>;
149 std::shared_ptr<const typename l_solver_type::Factory>
155 std::shared_ptr<const typename u_solver_type::Factory>
163 GKO_DEPRECATED(
"use with_l_solver instead")
168 return with_l_solver(std::move(solver));
171 parameters_type& with_l_solver(
175 this->l_solver_generator = std::move(solver);
176 this->deferred_factories[
"l_solver"] = [](
const auto& exec,
178 if (!params.l_solver_generator.is_empty()) {
179 params.l_solver_factory =
180 params.l_solver_generator.on(exec);
186 GKO_DEPRECATED(
"use with_u_solver instead")
187 parameters_type& with_u_solver_factory(
188 deferred_factory_parameter<const typename u_solver_type::Factory>
191 return with_u_solver(std::move(solver));
194 parameters_type& with_u_solver(
195 deferred_factory_parameter<const typename u_solver_type::Factory>
198 this->u_solver_generator = std::move(solver);
199 this->deferred_factories[
"u_solver"] = [](
const auto& exec,
201 if (!params.u_solver_generator.is_empty()) {
202 params.u_solver_factory =
203 params.u_solver_generator.on(exec);
209 GKO_DEPRECATED(
"use with_factorization instead")
210 parameters_type& with_factorization_factory(
211 deferred_factory_parameter<const LinOpFactory> factorization)
213 return with_factorization(std::move(factorization));
216 parameters_type& with_factorization(
217 deferred_factory_parameter<const LinOpFactory> factorization)
219 this->factorization_generator = std::move(factorization);
220 this->deferred_factories[
"factorization"] = [](
const auto& exec,
222 if (!params.factorization_generator.is_empty()) {
223 params.factorization_factory =
224 params.factorization_generator.on(exec);
231 deferred_factory_parameter<const typename l_solver_type::Factory>
234 deferred_factory_parameter<const typename u_solver_type::Factory>
237 deferred_factory_parameter<const LinOpFactory> factorization_generator;
263 config::make_type_descriptor<value_type, index_type>())
265 return detail::ilu_parse<Ilu>(config, context, td_for_child);
290 std::unique_ptr<transposed_type> transposed{
293 transposed->l_solver_ =
294 share(as<typename u_solver_type::transposed_type>(
296 transposed->u_solver_ =
297 share(as<typename l_solver_type::transposed_type>(
300 return std::move(transposed);
305 std::unique_ptr<transposed_type> transposed{
308 transposed->l_solver_ =
309 share(as<typename u_solver_type::transposed_type>(
311 transposed->u_solver_ =
312 share(as<typename l_solver_type::transposed_type>(
315 return std::move(transposed);
325 if (&other !=
this) {
328 l_solver_ = other.l_solver_;
329 u_solver_ = other.u_solver_;
330 parameters_ = other.parameters_;
347 if (&other !=
this) {
350 l_solver_ = std::move(other.l_solver_);
351 u_solver_ = std::move(other.u_solver_);
353 if (other.get_executor() != exec) {
375 void apply_impl(
const LinOp* b,
LinOp* x)
const override
378 precision_dispatch_real_complex<value_type>(
379 [&](
auto dense_b,
auto dense_x) {
380 this->set_cache_to(dense_b);
382 l_solver_->apply(dense_b, cache_.intermediate);
383 if (u_solver_->apply_uses_initial_guess()) {
384 dense_x->copy_from(cache_.intermediate);
386 u_solver_->apply(cache_.intermediate, dense_x);
388 u_solver_->apply(dense_b, cache_.intermediate);
389 if (l_solver_->apply_uses_initial_guess()) {
390 dense_x->copy_from(cache_.intermediate);
392 l_solver_->apply(cache_.intermediate, dense_x);
399 LinOp* x)
const override
401 precision_dispatch_real_complex<value_type>(
402 [&](
auto dense_alpha,
auto dense_b,
auto dense_beta,
auto dense_x) {
403 this->set_cache_to(dense_b);
405 l_solver_->apply(dense_b, cache_.intermediate);
406 u_solver_->apply(dense_alpha, cache_.intermediate,
407 dense_beta, dense_x);
409 u_solver_->apply(dense_b, cache_.intermediate);
410 l_solver_->apply(dense_alpha, cache_.intermediate,
411 dense_beta, dense_x);
417 explicit Ilu(std::shared_ptr<const Executor> exec)
418 : EnableLinOp<
Ilu>(std::move(exec))
421 explicit Ilu(
const Factory* factory, std::shared_ptr<const LinOp> lin_op)
423 parameters_{
factory->get_parameters()}
426 std::dynamic_pointer_cast<
const Composition<value_type>>(lin_op);
427 std::shared_ptr<const LinOp> l_factor;
428 std::shared_ptr<const LinOp> u_factor;
432 auto exec = lin_op->get_executor();
435 factorization::ParIlu<value_type, index_type>::build().on(
438 auto fact = std::shared_ptr<const LinOp>(
442 std::dynamic_pointer_cast<
const Composition<value_type>>(fact);
444 GKO_NOT_SUPPORTED(comp);
447 if (comp->get_operators().size() == 2) {
448 l_factor = comp->get_operators()[0];
449 u_factor = comp->get_operators()[1];
451 GKO_NOT_SUPPORTED(comp);
453 GKO_ASSERT_EQUAL_DIMENSIONS(l_factor, u_factor);
459 l_solver_ = generate_default_solver<l_solver_type>(exec, l_factor);
464 u_solver_ = generate_default_solver<u_solver_type>(exec, u_factor);
477 void set_cache_to(
const LinOp* b)
const
479 if (cache_.intermediate ==
nullptr) {
480 cache_.intermediate =
484 cache_.intermediate->copy_from(b);
495 template <
typename SolverType>
496 static std::enable_if_t<solver::has_with_criteria<SolverType>::value,
497 std::unique_ptr<SolverType>>
498 generate_default_solver(
const std::shared_ptr<const Executor>& exec,
499 const std::shared_ptr<const LinOp>& mtx)
502 const unsigned int default_max_iters{
503 static_cast<unsigned int>(mtx->get_size()[0])};
505 return SolverType::build()
507 gko::stop::Iteration::build().with_max_iters(default_max_iters),
509 .with_reduction_factor(default_reduce_residual))
517 template <
typename SolverType>
518 static std::enable_if_t<!solver::has_with_criteria<SolverType>::value,
519 std::unique_ptr<SolverType>>
520 generate_default_solver(
const std::shared_ptr<const Executor>& exec,
521 const std::shared_ptr<const LinOp>& mtx)
523 return SolverType::build().on(exec)->generate(mtx);
527 std::shared_ptr<const l_solver_type> l_solver_{};
528 std::shared_ptr<const u_solver_type> u_solver_{};
539 mutable struct cache_struct {
540 cache_struct() =
default;
541 ~cache_struct() =
default;
542 cache_struct(
const cache_struct&) {}
543 cache_struct(cache_struct&&) {}
544 cache_struct&
operator=(
const cache_struct&) {
return *
this; }
545 cache_struct&
operator=(cache_struct&&) {
return *
this; }
546 std::unique_ptr<LinOp> intermediate{};
555 #endif // GKO_PUBLIC_CORE_PRECONDITIONER_ILU_HPP_