Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check config key #1792

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions core/config/config_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ parse_minimal_criteria(const pnode& config, const registry& context,

type_descriptor updated_td = update_type(config, td);

// Although it can still be caught in the following map, it gives consistent
// error exception when some keys are not allowed.
std::set<std::string> allowed_keys{"time",
"iteration",
"relative_residual_norm",
"initial_residual_norm",
"absolute_residual_norm",
"relative_implicit_residual_norm",
"initial_implicit_residual_norm",
"absolute_implicit_residual_norm"};
check_allowed_keys(config, allowed_keys);

std::vector<deferred_factory_parameter<const stop::CriterionFactory>> res;
for (const auto& it : config.get_map()) {
if (it.first == "value_type") {
Expand Down
34 changes: 34 additions & 0 deletions core/config/config_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define GKO_CORE_CONFIG_CONFIG_HELPER_HPP_


#include <set>
#include <string>
#include <type_traits>

Expand Down Expand Up @@ -312,6 +313,39 @@ inline std::shared_ptr<typename Csr::strategy_type> get_strategy(
}


// It is to check whether the keys of config (map type) are mentioned in
// allowed_keys. If not, it will throw an error message.
inline void check_allowed_keys(const pnode& config,
const std::set<std::string>& allowed_keys)
{
if (config.get_tag() != pnode::tag_t::map) {
// we only check the key in the map
return;
}
const auto& map = config.get_map();
auto set_output = [](auto& set) {
std::string output = "[";
for (const auto& item : set) {
output = output + " " + item;
}
output += " ]";
return output;
};
for (const auto& item : map) {
if (item.first == "value_type" || item.first == "type") {
// We always allow value_type in any class and use type to choose
// the class
continue;
}
auto search = allowed_keys.find(item.first);
GKO_THROW_IF_INVALID(
search != allowed_keys.end(),
item.first + " is not a allowed key. The allowed keys here is " +
set_output(allowed_keys));
}
}


} // namespace config
} // namespace gko

Expand Down
11 changes: 8 additions & 3 deletions core/config/solver_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#ifndef GKO_CORE_CONFIG_SOLVER_CONFIG_HPP_
#define GKO_CORE_CONFIG_SOLVER_CONFIG_HPP_

#include <set>
#include <string>

#include <ginkgo/core/config/config.hpp>
#include <ginkgo/core/config/registry.hpp>
Expand All @@ -17,9 +19,10 @@ namespace config {


template <typename SolverParam>
inline void common_solver_parse(SolverParam& params, const pnode& config,
const registry& context,
type_descriptor td_for_child)
inline std::set<std::string> common_solver_parse(SolverParam& params,
const pnode& config,
const registry& context,
type_descriptor td_for_child)
{
if (auto& obj = config.get("generated_preconditioner")) {
params.with_generated_preconditioner(
Expand All @@ -34,6 +37,8 @@ inline void common_solver_parse(SolverParam& params, const pnode& config,
gko::config::parse_or_get_factory<const LinOpFactory>(
obj, context, td_for_child));
}
return std::set<std::string>{"generated_preconditioner", "criteria",
"preconditioner"};
}


Expand Down
13 changes: 12 additions & 1 deletion core/config/stop_config.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
//
// SPDX-License-Identifier: BSD-3-Clause

#include "core/config/stop_config.hpp"

#include <set>
#include <string>

#include <ginkgo/core/base/exception_helpers.hpp>
#include <ginkgo/core/config/config.hpp>
#include <ginkgo/core/config/registry.hpp>
Expand All @@ -27,6 +30,8 @@ namespace config {
deferred_factory_parameter<stop::CriterionFactory> configure_time(
const pnode& config, const registry& context, const type_descriptor& td)
{
std::set<std::string> allowed_keys{"time_limit"};
gko::config::check_allowed_keys(config, allowed_keys);
auto params = stop::Time::build();
if (auto& obj = config.get("time_limit")) {
params.with_time_limit(gko::config::get_value<long long int>(obj));
Expand All @@ -38,6 +43,8 @@ deferred_factory_parameter<stop::CriterionFactory> configure_time(
deferred_factory_parameter<stop::CriterionFactory> configure_iter(
const pnode& config, const registry& context, const type_descriptor& td)
{
std::set<std::string> allowed_keys{"max_iters"};
gko::config::check_allowed_keys(config, allowed_keys);
auto params = stop::Iteration::build();
if (auto& obj = config.get("max_iters")) {
params.with_max_iters(gko::config::get_value<size_type>(obj));
Expand Down Expand Up @@ -68,6 +75,8 @@ class ResidualNormConfigurer {
const gko::config::registry& context,
const gko::config::type_descriptor& td_for_child)
{
std::set<std::string> allowed_keys{"reduction_factor", "baseline"};
gko::config::check_allowed_keys(config, allowed_keys);
auto params = stop::ResidualNorm<ValueType>::build();
if (auto& obj = config.get("reduction_factor")) {
params.with_reduction_factor(
Expand Down Expand Up @@ -100,6 +109,8 @@ class ImplicitResidualNormConfigurer {
const gko::config::registry& context,
const gko::config::type_descriptor& td_for_child)
{
std::set<std::string> allowed_keys{"reduction_factor", "baseline"};
gko::config::check_allowed_keys(config, allowed_keys);
auto params = stop::ImplicitResidualNorm<ValueType>::build();
if (auto& obj = config.get("reduction_factor")) {
params.with_reduction_factor(
Expand Down
11 changes: 7 additions & 4 deletions core/config/trisolver_config.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
//
// SPDX-License-Identifier: BSD-3-Clause

#ifndef GKO_CORE_CONFIG_TRISOLVER_CONFIG_HPP_
#define GKO_CORE_CONFIG_TRISOLVER_CONFIG_HPP_

#include <set>
#include <string>

#include <ginkgo/core/config/config.hpp>
#include <ginkgo/core/config/registry.hpp>
Expand All @@ -18,9 +20,9 @@ namespace config {


template <typename SolverParam>
inline void common_trisolver_parse(SolverParam& params, const pnode& config,
const registry& context,
type_descriptor td_for_child)
inline std::set<std::string> common_trisolver_parse(
SolverParam& params, const pnode& config, const registry& context,
type_descriptor td_for_child)
{
if (auto& obj = config.get("num_rhs")) {
params.with_num_rhs(gko::config::get_value<size_type>(obj));
Expand All @@ -39,6 +41,7 @@ inline void common_trisolver_parse(SolverParam& params, const pnode& config,
GKO_INVALID_CONFIG_VALUE("algorithm", str);
}
}
return {"num_rhs", "unit_diagonal", "algorithm"};
}


Expand Down
5 changes: 5 additions & 0 deletions core/distributed/preconditioner/schwarz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "ginkgo/core/distributed/preconditioner/schwarz.hpp"

#include <memory>
#include <set>
#include <string>

#include <ginkgo/core/base/exception_helpers.hpp>
#include <ginkgo/core/base/executor.hpp>
Expand Down Expand Up @@ -36,6 +38,9 @@ Schwarz<ValueType, LocalIndexType, GlobalIndexType>::parse(
const config::pnode& config, const config::registry& context,
const config::type_descriptor& td_for_child)
{
std::set<std::string> allowed_keys{"generated_local_solver",
"local_solver"};
gko::config::check_allowed_keys(config, allowed_keys);
auto params = Schwarz<ValueType, LocalIndexType, GlobalIndexType>::build();

if (auto& obj = config.get("generated_local_solver")) {
Expand Down
6 changes: 6 additions & 0 deletions core/factorization/cholesky.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

#include "ginkgo/core/factorization/cholesky.hpp"

#include <set>
#include <string>

#include <ginkgo/core/base/exception_helpers.hpp>
#include <ginkgo/core/base/executor.hpp>
#include <ginkgo/core/base/types.hpp>
Expand Down Expand Up @@ -42,6 +45,9 @@ Cholesky<ValueType, IndexType>::parse(
const config::pnode& config, const config::registry& context,
const config::type_descriptor& td_for_child)
{
std::set<std::string> allowed_keys{"symbolic_factorization",
"skip_sorting"};
gko::config::check_allowed_keys(config, allowed_keys);
auto params = Cholesky<ValueType, IndexType>::build();
if (auto& obj = config.get("symbolic_factorization")) {
params.with_symbolic_factorization(
Expand Down
5 changes: 5 additions & 0 deletions core/factorization/ic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "ginkgo/core/factorization/ic.hpp"

#include <memory>
#include <set>
#include <string>

#include <ginkgo/core/base/array.hpp>
#include <ginkgo/core/base/composition.hpp>
Expand Down Expand Up @@ -53,6 +55,9 @@ Ic<ValueType, IndexType>::parse(const config::pnode& config,
const config::registry& context,
const config::type_descriptor& td_for_child)
{
std::set<std::string> allowed_keys{"l_strategy", "skip_sorting",
"both_factors", "algorithm"};
gko::config::check_allowed_keys(config, allowed_keys);
auto params = factorization::Ic<ValueType, IndexType>::build();
if (auto& obj = config.get("l_strategy")) {
params.with_l_strategy(config::get_strategy<matrix_type>(obj));
Expand Down
5 changes: 5 additions & 0 deletions core/factorization/ilu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "ginkgo/core/factorization/ilu.hpp"

#include <memory>
#include <set>
#include <string>

#include <ginkgo/core/base/array.hpp>
#include <ginkgo/core/base/exception_helpers.hpp>
Expand Down Expand Up @@ -50,6 +52,9 @@ Ilu<ValueType, IndexType>::parse(const config::pnode& config,
const config::registry& context,
const config::type_descriptor& td_for_child)
{
std::set<std::string> allowed_keys{"l_strategy", "u_strategy",
"skip_sorting", "algorithm"};
gko::config::check_allowed_keys(config, allowed_keys);
auto params = factorization::Ilu<ValueType, IndexType>::build();
if (auto& obj = config.get("l_strategy")) {
params.with_l_strategy(config::get_strategy<matrix_type>(obj));
Expand Down
6 changes: 6 additions & 0 deletions core/factorization/lu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

#include "ginkgo/core/factorization/lu.hpp"

#include <set>
#include <string>

#include <ginkgo/core/base/exception_helpers.hpp>
#include <ginkgo/core/base/executor.hpp>
#include <ginkgo/core/base/types.hpp>
Expand Down Expand Up @@ -43,6 +46,9 @@ Lu<ValueType, IndexType>::parse(const config::pnode& config,
const config::registry& context,
const config::type_descriptor& td_for_child)
{
std::set<std::string> allowed_keys{"symbolic_factorization",
"symbolic_algorithm", "skip_sorting"};
gko::config::check_allowed_keys(config, allowed_keys);
auto params =
experimental::factorization::Lu<ValueType, IndexType>::build();

Expand Down
7 changes: 6 additions & 1 deletion core/factorization/par_ic.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
//
// SPDX-License-Identifier: BSD-3-Clause

#include "ginkgo/core/factorization/par_ic.hpp"

#include <memory>
#include <set>
#include <string>

#include <ginkgo/core/base/array.hpp>
#include <ginkgo/core/base/composition.hpp>
Expand Down Expand Up @@ -51,6 +53,9 @@ ParIc<ValueType, IndexType>::parse(const config::pnode& config,
const config::registry& context,
const config::type_descriptor& td_for_child)
{
std::set<std::string> allowed_keys{"iterations", "skip_sorting",
"l_strategy", "both_factors"};
gko::config::check_allowed_keys(config, allowed_keys);
auto params = factorization::ParIc<ValueType, IndexType>::build();

if (auto& obj = config.get("iterations")) {
Expand Down
9 changes: 8 additions & 1 deletion core/factorization/par_ict.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
//
// SPDX-License-Identifier: BSD-3-Clause

#include "ginkgo/core/factorization/par_ict.hpp"

#include <memory>
#include <set>
#include <string>

#include <ginkgo/core/base/array.hpp>
#include <ginkgo/core/base/exception_helpers.hpp>
Expand Down Expand Up @@ -150,6 +152,11 @@ ParIct<ValueType, IndexType>::parse(const config::pnode& config,
const config::registry& context,
const config::type_descriptor& td_for_child)
{
std::set<std::string> allowed_keys{
"iterations", "skip_sorting", "approximate_select",
"deterministic_sample", "fill_in_limit", "l_strategy",
"lt_strategy"};
gko::config::check_allowed_keys(config, allowed_keys);
auto params = factorization::ParIct<ValueType, IndexType>::build();

if (auto& obj = config.get("iterations")) {
Expand Down
7 changes: 6 additions & 1 deletion core/factorization/par_ilu.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
//
// SPDX-License-Identifier: BSD-3-Clause

#include "ginkgo/core/factorization/par_ilu.hpp"

#include <memory>
#include <set>
#include <string>

#include <ginkgo/core/base/array.hpp>
#include <ginkgo/core/base/exception_helpers.hpp>
Expand Down Expand Up @@ -48,6 +50,9 @@ ParIlu<ValueType, IndexType>::parse(const config::pnode& config,
const config::registry& context,
const config::type_descriptor& td_for_child)
{
std::set<std::string> allowed_keys{"iterations", "skip_sorting",
"l_strategy", "u_strategy"};
gko::config::check_allowed_keys(config, allowed_keys);
auto params = factorization::ParIlu<ValueType, IndexType>::build();

if (auto& obj = config.get("iterations")) {
Expand Down
10 changes: 9 additions & 1 deletion core/factorization/par_ilut.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
//
// SPDX-License-Identifier: BSD-3-Clause

#include "ginkgo/core/factorization/par_ilut.hpp"

#include <memory>
#include <set>
#include <string>

#include <ginkgo/core/base/array.hpp>
#include <ginkgo/core/base/exception_helpers.hpp>
Expand Down Expand Up @@ -166,6 +168,12 @@ ParIlut<ValueType, IndexType>::parse(
const config::pnode& config, const config::registry& context,
const config::type_descriptor& td_for_child)
{
std::set<std::string> allowed_keys{
"iterations", "skip_sorting",
"approximate_select", "deterministic_sample",
"fill_in_limit", "l_strategy",
"u_strategy"};
gko::config::check_allowed_keys(config, allowed_keys);
auto params = factorization::ParIlut<ValueType, IndexType>::build();

if (auto& obj = config.get("iterations")) {
Expand Down
Loading
Loading