From 528d27e2450f59202134e7f197a6cf4d2b2bbebc Mon Sep 17 00:00:00 2001 From: "Yu-Hsiang M. Tsai" Date: Wed, 15 May 2024 00:39:53 +0200 Subject: [PATCH] remove fixed_coarsening, selector, stop::. update doc and rename MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Marcel Koch Co-authored-by: Thomas Grützmacher --- core/config/config_helper.hpp | 3 +- core/config/multigrid_config.cpp | 7 -- core/config/registry.cpp | 4 +- core/multigrid/fixed_coarsening.cpp | 15 --- core/multigrid/pgm.cpp | 13 +-- core/solver/multigrid.cpp | 91 +++++++------------ core/test/config/multigrid.cpp | 86 +++--------------- .../core/multigrid/fixed_coarsening.hpp | 19 ---- include/ginkgo/core/multigrid/pgm.hpp | 8 +- include/ginkgo/core/solver/multigrid.hpp | 9 +- 10 files changed, 69 insertions(+), 186 deletions(-) diff --git a/core/config/config_helper.hpp b/core/config/config_helper.hpp index 07bc95a526a..809ec176c71 100644 --- a/core/config/config_helper.hpp +++ b/core/config/config_helper.hpp @@ -67,8 +67,7 @@ enum class LinOpFactoryType : int { Isai, Jacobi, Multigrid, - Pgm, - FixedCoarsening + Pgm }; diff --git a/core/config/multigrid_config.cpp b/core/config/multigrid_config.cpp index 6699a0aaf82..6eb9f5ed872 100644 --- a/core/config/multigrid_config.cpp +++ b/core/config/multigrid_config.cpp @@ -2,17 +2,10 @@ // // SPDX-License-Identifier: BSD-3-Clause -#include -#include -#include -#include #include -#include "core/config/config_helper.hpp" -#include "core/config/dispatch.hpp" #include "core/config/parse_macro.hpp" -#include "core/config/type_descriptor_helper.hpp" namespace gko { diff --git a/core/config/registry.cpp b/core/config/registry.cpp index afa1fda1f4b..8ff619b4250 100644 --- a/core/config/registry.cpp +++ b/core/config/registry.cpp @@ -44,9 +44,7 @@ configuration_map generate_config_map() {"preconditioner::Isai", parse}, {"preconditioner::Jacobi", parse}, {"solver::Multigrid", parse}, - {"multigrid::Pgm", parse}, - {"multigrid::FixedCoarsening", - parse}}; + {"multigrid::Pgm", parse}}; } diff --git a/core/multigrid/fixed_coarsening.cpp b/core/multigrid/fixed_coarsening.cpp index d61961ac295..ee50a714996 100644 --- a/core/multigrid/fixed_coarsening.cpp +++ b/core/multigrid/fixed_coarsening.cpp @@ -38,21 +38,6 @@ GKO_REGISTER_OPERATION(fill_seq_array, components::fill_seq_array); } // namespace fixed_coarsening -template -typename FixedCoarsening::parameters_type -FixedCoarsening::parse( - const config::pnode& config, const config::registry& context, - const config::type_descriptor& td_for_child) -{ - auto factory = FixedCoarsening::build(); - // TODO: ARRAY - if (auto& obj = config.get("skip_sorting")) { - factory.with_skip_sorting(gko::config::get_value(obj)); - } - return factory; -} - - template void FixedCoarsening::generate() { diff --git a/core/multigrid/pgm.cpp b/core/multigrid/pgm.cpp index f1c043b05b0..d60835ca944 100644 --- a/core/multigrid/pgm.cpp +++ b/core/multigrid/pgm.cpp @@ -141,26 +141,27 @@ std::shared_ptr> generate_coarse( } // namespace +template typename Pgm::parameters_type Pgm::parse(const config::pnode& config, const config::registry& context, const config::type_descriptor& td_for_child) { - auto factory = Pgm::build(); + auto params = Pgm::build(); if (auto& obj = config.get("max_iterations")) { - factory.with_max_iterations(gko::config::get_value(obj)); + params.with_max_iterations(gko::config::get_value(obj)); } if (auto& obj = config.get("max_unassigned_ratio")) { - factory.with_max_unassigned_ratio(gko::config::get_value(obj)); + params.with_max_unassigned_ratio(gko::config::get_value(obj)); } if (auto& obj = config.get("deterministic")) { - factory.with_deterministic(gko::config::get_value(obj)); + params.with_deterministic(gko::config::get_value(obj)); } if (auto& obj = config.get("skip_sorting")) { - factory.with_skip_sorting(gko::config::get_value(obj)); + params.with_skip_sorting(gko::config::get_value(obj)); } - return factory; + return params; } diff --git a/core/solver/multigrid.cpp b/core/solver/multigrid.cpp index 54998e0f84f..6dd06747883 100644 --- a/core/solver/multigrid.cpp +++ b/core/solver/multigrid.cpp @@ -30,7 +30,6 @@ #include "core/base/dispatch_helper.hpp" #include "core/components/fill_array_kernels.hpp" -#include "core/config/config.hpp" #include "core/config/config_helper.hpp" #include "core/distributed/helpers.hpp" #include "core/solver/ir_kernels.hpp" @@ -594,115 +593,95 @@ void MultigridState::run_cycle(multigrid::cycle cycle, size_type level, } // namespace multigrid -std::function get_selector( - std::string key) -{ - static std::map> - selector_map{ - {{"first_for_top", [](const size_type level, const LinOp*) { - return (level == 0) ? 0 : 1; - }}}}; - return selector_map.at(key); -} - - typename Multigrid::parameters_type Multigrid::parse( const config::pnode& config, const config::registry& context, const config::type_descriptor& td_for_child) { - auto factory = Multigrid::build(); + auto params = Multigrid::build(); if (auto& obj = config.get("criteria")) { - factory.with_criteria( - gko::config::get_factory_vector( + params.with_criteria( + config::parse_or_get_factory_vector( obj, context, td_for_child)); } if (auto& obj = config.get("mg_level")) { - factory.with_mg_level( - gko::config::get_factory_vector( + params.with_mg_level( + config::parse_or_get_factory_vector( obj, context, td_for_child)); } - if (auto& obj = config.get("level_selector")) { - factory.with_level_selector(get_selector(obj.get_string())); - } if (auto& obj = config.get("pre_smoother")) { - factory.with_pre_smoother( - gko::config::get_factory_vector(obj, context, - td_for_child)); + params.with_pre_smoother( + config::parse_or_get_factory_vector( + obj, context, td_for_child)); } if (auto& obj = config.get("post_smoother")) { - factory.with_post_smoother( - gko::config::get_factory_vector(obj, context, - td_for_child)); + params.with_post_smoother( + config::parse_or_get_factory_vector( + obj, context, td_for_child)); } if (auto& obj = config.get("mid_smoother")) { - factory.with_mid_smoother( - gko::config::get_factory_vector(obj, context, - td_for_child)); + params.with_mid_smoother( + config::parse_or_get_factory_vector( + obj, context, td_for_child)); } if (auto& obj = config.get("post_uses_pre")) { - factory.with_post_uses_pre(gko::config::get_value(obj)); + params.with_post_uses_pre(gko::config::get_value(obj)); } if (auto& obj = config.get("mid_case")) { auto str = obj.get_string(); if (str == "both") { - factory.with_mid_case(multigrid::mid_smooth_type::both); + params.with_mid_case(multigrid::mid_smooth_type::both); } else if (str == "post_smoother") { - factory.with_mid_case(multigrid::mid_smooth_type::post_smoother); + params.with_mid_case(multigrid::mid_smooth_type::post_smoother); } else if (str == "pre_smoother") { - factory.with_mid_case(multigrid::mid_smooth_type::pre_smoother); + params.with_mid_case(multigrid::mid_smooth_type::pre_smoother); } else if (str == "standalone") { - factory.with_mid_case(multigrid::mid_smooth_type::standalone); + params.with_mid_case(multigrid::mid_smooth_type::standalone); } else { - GKO_INVALID_STATE("Not valid mid_smooth_type value"); + GKO_INVALID_CONFIG_VALUE("mid_smooth_type", str); } } if (auto& obj = config.get("max_levels")) { - factory.with_max_levels(gko::config::get_value(obj)); + params.with_max_levels(gko::config::get_value(obj)); } if (auto& obj = config.get("min_coarse_rows")) { - factory.with_min_coarse_rows(gko::config::get_value(obj)); + params.with_min_coarse_rows(gko::config::get_value(obj)); } if (auto& obj = config.get("coarsest_solver")) { - factory.with_coarsest_solver( - gko::config::get_factory_vector(obj, context, - td_for_child)); - } - if (auto& obj = config.get("solver_selector")) { - auto str = obj.get_string(); - factory.with_solver_selector(get_selector(str)); + params.with_coarsest_solver( + config::parse_or_get_factory_vector( + obj, context, td_for_child)); } if (auto& obj = config.get("cycle")) { auto str = obj.get_string(); if (str == "v") { - factory.with_cycle(multigrid::cycle::v); + params.with_cycle(multigrid::cycle::v); } else if (str == "w") { - factory.with_cycle(multigrid::cycle::w); + params.with_cycle(multigrid::cycle::w); } else if (str == "f") { - factory.with_cycle(multigrid::cycle::f); + params.with_cycle(multigrid::cycle::f); } else { - GKO_INVALID_STATE("Not valid cycle value"); + GKO_INVALID_CONFIG_VALUE("cycle", str); } } if (auto& obj = config.get("kcycle_base")) { - factory.with_kcycle_base(gko::config::get_value(obj)); + params.with_kcycle_base(gko::config::get_value(obj)); } if (auto& obj = config.get("kcycle_rel_tol")) { - factory.with_kcycle_rel_tol(gko::config::get_value(obj)); + params.with_kcycle_rel_tol(gko::config::get_value(obj)); } if (auto& obj = config.get("smoother_relax")) { - factory.with_smoother_relax( + params.with_smoother_relax( gko::config::get_value>(obj)); } if (auto& obj = config.get("smoother_iters")) { - factory.with_smoother_iters(gko::config::get_value(obj)); + params.with_smoother_iters(gko::config::get_value(obj)); } if (auto& obj = config.get("default_initial_guess")) { - factory.with_default_initial_guess( + params.with_default_initial_guess( gko::config::get_value(obj)); } - return factory; + return params; } diff --git a/core/test/config/multigrid.cpp b/core/test/config/multigrid.cpp index d683d9102d5..5c13d0525cf 100644 --- a/core/test/config/multigrid.cpp +++ b/core/test/config/multigrid.cpp @@ -25,21 +25,20 @@ using namespace gko::config; -template +template struct MultigridLevelConfigTest { - using explicit_type = ExplicitType; + using changed_type = ChangedType; using default_type = DefaultType; using multigrid_level_config_test = MultigridLevelConfigTest; static void change_template(pnode::map_type& config_map) { config_map["value_type"] = pnode{"float32"}; - config_map["index_type"] = pnode{"int64"}; } }; -struct Pgm : MultigridLevelConfigTest, +struct Pgm : MultigridLevelConfigTest, gko::multigrid::Pgm> { static pnode::map_type setup_base() { @@ -75,37 +74,6 @@ struct Pgm : MultigridLevelConfigTest, }; -struct FixedCoarsening : MultigridLevelConfigTest< - gko::multigrid::FixedCoarsening, - gko::multigrid::FixedCoarsening> { - static pnode::map_type setup_base() - { - return {{"type", pnode{"multigrid::FixedCoarsening"}}}; - } - - template - static void set(pnode::map_type& config_map, ParamType& param, registry reg, - std::shared_ptr exec) - { - // config_map["coarse_rows"] = - // pnode{std::vector{{2}, {3}, {5}}}; - // param.with_coarse_rows(gko::array(exec, {2, 3, 5})); - config_map["skip_sorting"] = pnode{true}; - param.with_skip_sorting(true); - } - - template - static void validate(gko::LinOpFactory* result, AnswerType* answer) - { - auto res_param = gko::as(result)->get_parameters(); - auto ans_param = answer->get_parameters(); - - GKO_ASSERT_ARRAY_EQ(res_param.coarse_rows, ans_param.coarse_rows); - ASSERT_EQ(res_param.skip_sorting, ans_param.skip_sorting); - } -}; - - template class MultigridLevel : public ::testing::Test { protected: @@ -121,7 +89,7 @@ class MultigridLevel : public ::testing::Test { }; -using MultigridLevelTypes = ::testing::Types<::Pgm, ::FixedCoarsening>; +using MultigridLevelTypes = ::testing::Types<::Pgm>; TYPED_TEST_SUITE(MultigridLevel, MultigridLevelTypes, TypenameNameGenerator); @@ -147,7 +115,7 @@ TYPED_TEST(MultigridLevel, ExplicitTemplate) auto config = pnode(config_map); auto res = parse(config, this->reg, this->td).on(this->exec); - auto ans = Config::explicit_type::build().on(this->exec); + auto ans = Config::changed_type::build().on(this->exec); Config::validate(res.get(), ans.get()); } @@ -158,12 +126,12 @@ TYPED_TEST(MultigridLevel, Set) using Config = typename TestFixture::Config; auto config_map = Config::setup_base(); Config::change_template(config_map); - auto param = Config::explicit_type::build(); - Config::set(config_map, param, this->reg, this->exec); + auto params = Config::changed_type::build(); + Config::set(config_map, params, this->reg, this->exec); auto config = pnode(config_map); auto res = parse(config, this->reg, this->td).on(this->exec); - auto ans = param.on(this->exec); + auto ans = params.on(this->exec); Config::validate(res.get(), ans.get()); } @@ -234,7 +202,7 @@ struct MultigridConfig { reg, "coarsest_solver")); } else { config_map["criteria"] = - pnode{pnode::map_type{{"type", pnode{"stop::Iteration"}}}}; + pnode{pnode::map_type{{"type", pnode{"Iteration"}}}}; param.with_criteria(DummyStop::build().on(exec)); config_map["mg_level"] = pnode{std::vector{ pnode{pnode::map_type{{"type", pnode{"multigrid::Pgm"}}}}, @@ -254,16 +222,6 @@ struct MultigridConfig { pnode{pnode::map_type{{"type", pnode{"solver::Ir"}}}}; param.with_coarsest_solver(DummySmoother::build().on(exec)); } - config_map["level_selector"] = pnode{"first_for_top"}; - param.with_level_selector( - [](const gko::size_type level, const gko::LinOp*) { - return level == 0 ? 0 : 1; - }); - config_map["solver_selector"] = pnode{"first_for_top"}; - param.with_solver_selector( - [](const gko::size_type level, const gko::LinOp*) { - return level == 0 ? 0 : 1; - }); } template @@ -320,20 +278,6 @@ struct MultigridConfig { res_param.coarsest_solver.at(0)), nullptr); } - if (ans_param.level_selector) { - ASSERT_TRUE(res_param.level_selector); - for (gko::size_type i = 0; i < 5; i++) { - ASSERT_EQ(res_param.level_selector(i, nullptr), - ans_param.level_selector(i, nullptr)); - } - } - if (ans_param.solver_selector) { - ASSERT_TRUE(res_param.solver_selector); - for (gko::size_type i = 0; i < 5; i++) { - ASSERT_EQ(res_param.solver_selector(i, nullptr), - ans_param.solver_selector(i, nullptr)); - } - } } }; @@ -390,12 +334,12 @@ TEST_F(MultigridT, CreateDefault) TEST_F(MultigridT, SetFromRegistry) { auto config_map = Config::setup_base(); - auto param = gko::solver::Multigrid::build(); - Config::template set(config_map, param, this->reg, this->exec); + auto params = gko::solver::Multigrid::build(); + Config::template set(config_map, params, this->reg, this->exec); auto config = pnode(config_map); auto res = parse(config, this->reg, this->td).on(exec); - auto ans = param.on(this->exec); + auto ans = params.on(this->exec); Config::template validate(res.get(), ans.get()); } @@ -404,12 +348,12 @@ TEST_F(MultigridT, SetFromRegistry) TEST_F(MultigridT, SetFromConfig) { auto config_map = Config::setup_base(); - auto param = gko::solver::Multigrid::build(); - Config::template set(config_map, param, this->reg, this->exec); + auto params = gko::solver::Multigrid::build(); + Config::template set(config_map, params, this->reg, this->exec); auto config = pnode(config_map); auto res = parse(config, this->reg, this->td).on(exec); - auto ans = param.on(this->exec); + auto ans = params.on(this->exec); Config::template validate(res.get(), ans.get()); } diff --git a/include/ginkgo/core/multigrid/fixed_coarsening.hpp b/include/ginkgo/core/multigrid/fixed_coarsening.hpp index 3b19072f62c..2ab3211b609 100644 --- a/include/ginkgo/core/multigrid/fixed_coarsening.hpp +++ b/include/ginkgo/core/multigrid/fixed_coarsening.hpp @@ -13,8 +13,6 @@ #include #include #include -#include -#include #include #include #include @@ -86,23 +84,6 @@ class FixedCoarsening GKO_ENABLE_LIN_OP_FACTORY(FixedCoarsening, parameters, Factory); GKO_ENABLE_BUILD_METHOD(Factory); - /** - * Create the parameters from the property_tree. - * Because this is directly tied to the specific type. The value/index type - * settings are ignored and type_descriptor is for children objects. - * - * @param config the property tree for setting - * @param context the registry - * @param td_for_child the type descriptor for children objects. The - * default will directly from the specific type. - * - * @return parameters - */ - static parameters_type parse( - const config::pnode& config, const config::registry& context, - const config::type_descriptor& td_for_child = - config::make_type_descriptor()); - protected: void apply_impl(const LinOp* b, LinOp* x) const override { diff --git a/include/ginkgo/core/multigrid/pgm.hpp b/include/ginkgo/core/multigrid/pgm.hpp index c233e9806f0..6d57c2dcbac 100644 --- a/include/ginkgo/core/multigrid/pgm.hpp +++ b/include/ginkgo/core/multigrid/pgm.hpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -133,13 +134,14 @@ class Pgm : public EnableLinOp>, /** * Create the parameters from the property_tree. - * Because this is directly tied to the specific type. The value/index type - * settings are ignored and type_descriptor is for children objects. + * Because this is directly tied to the specific type, the value/index type + * settings within config are ignored and type_descriptor is only used + * for children objects. * * @param config the property tree for setting * @param context the registry * @param td_for_child the type descriptor for children objects. The - * default will directly from the specific type. + * default uses the value/index type of this class. * * @return parameters */ diff --git a/include/ginkgo/core/solver/multigrid.hpp b/include/ginkgo/core/solver/multigrid.hpp index f3e4f60d935..887250f5047 100644 --- a/include/ginkgo/core/solver/multigrid.hpp +++ b/include/ginkgo/core/solver/multigrid.hpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -383,13 +384,13 @@ class Multigrid : public EnableLinOp, /** * Create the parameters from the property_tree. - * Because this is directly tied to the specific type. The value/index type - * settings are ignored and type_descriptor is for children objects. + * Because this is directly tied to the specific type, the value/index type + * settings within config are ignored and type_descriptor is only used + * for children objects. * * @param config the property tree for setting * @param context the registry - * @param td_for_child the type descriptor for children objects. The - * default will directly from the specific type. + * @param td_for_child the type descriptor for children objects * * @return parameters */