Skip to content

Commit

Permalink
add macro for exception and clean code
Browse files Browse the repository at this point in the history
Co-authored-by: Marcel Koch <[email protected]>
  • Loading branch information
yhmtsai and MarcelKoch committed May 17, 2024
1 parent cd31c59 commit 7603816
Show file tree
Hide file tree
Showing 32 changed files with 72 additions and 112 deletions.
2 changes: 1 addition & 1 deletion core/config/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ deferred_factory_parameter<gko::LinOpFactory> parse(const pnode& config,
obj.get_string());
return func(config, context, td);
}
GKO_INVALID_STATE("Should contain type property");
GKO_MISS_CONFIG_ENTRY("type");
}


Expand Down
37 changes: 10 additions & 27 deletions core/config/config_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <type_traits>


#include <ginkgo/core/base/array.hpp>
#include <ginkgo/core/base/exception_helpers.hpp>
#include <ginkgo/core/base/lin_op.hpp>
#include <ginkgo/core/base/math.hpp>
Expand All @@ -27,6 +26,15 @@ namespace gko {
namespace config {


#define GKO_INVALID_CONFIG_VALUE(_entry, _value) \
GKO_INVALID_STATE(_value + std::string(" is invalid for the entry ") + \
_entry)


#define GKO_MISS_CONFIG_ENTRY(_entry) \
GKO_INVALID_STATE(std::string("miss the entry ") + _entry)


/**
* LinOpFactoryType enum is to avoid forward declaration, linopfactory header,
* two template versions of parse
Expand Down Expand Up @@ -257,7 +265,7 @@ get_value(const pnode& config)
} else if (val == "provided") {
return solver::initial_guess_mode::provided;
}
GKO_INVALID_STATE("Wrong value for initial_guess_mode");
GKO_INVALID_CONFIG_VALUE("default_initial_guess", val);
}


Expand All @@ -275,31 +283,6 @@ get_value(const pnode& config)
}


// template <typename T>
// struct is_array_t : std::false_type {};

// template <typename V>
// struct is_array_t<array<V>> : std::true_type {};

// template <typename ArrayType>
// inline typename std::enable_if<is_array_t<ArrayType>::value, ArrayType>::type
// get_value(const pnode& config, std::shared_ptr<const Executor> exec)
// {
// using T = typename ArrayType::value_type;
// std::vector<T> res;
// // for loop in config
// if (config.get_tag() == pnode::tag_t::array) {
// for (const auto& it : config.get_array()) {
// res.push_back(get_value<T>(it));
// }
// } else {
// // only one config can be passed without array
// res.push_back(get_value<T>(config));
// }
// return ArrayType(exec, res.begin(), res.end());
// }


template <typename Csr>
inline std::shared_ptr<typename Csr::strategy_type> get_strategy(
const pnode& config)
Expand Down
8 changes: 4 additions & 4 deletions core/config/preconditioner_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ deferred_factory_parameter<gko::LinOpFactory> parse<LinOpFactoryType::Ic>(
make_type_selector(updated.get_value_typestr(), value_type_list()),
make_type_selector(updated.get_index_typestr(), index_type_list()));
} else {
GKO_INVALID_STATE("does not have valid LSolverType");
GKO_INVALID_CONFIG_VALUE("l_solver_type", str);
}
}

Expand Down Expand Up @@ -221,7 +221,7 @@ deferred_factory_parameter<gko::LinOpFactory> parse<LinOpFactoryType::Ilu>(
make_type_selector(updated.get_index_typestr(),
index_type_list()));
} else {
GKO_INVALID_STATE("does not have valid LSolverType");
GKO_INVALID_CONFIG_VALUE("l_solver_type", str);
}
};
bool reverse_apply = false;
Expand Down Expand Up @@ -280,10 +280,10 @@ deferred_factory_parameter<gko::LinOpFactory> parse<LinOpFactoryType::Isai>(
make_type_selector(updated.get_index_typestr(),
index_type_list()));
} else {
GKO_INVALID_STATE("does not have valid IsaiType");
GKO_INVALID_CONFIG_VALUE("isai_type", str);
}
} else {
GKO_INVALID_STATE("does not contain IsaiType");
GKO_MISS_CONFIG_ENTRY("isai_type");
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/config/stop_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ stop::mode get_mode(const std::string& str)
} else if (str == "rhs_norm") {
return stop::mode::rhs_norm;
}
GKO_INVALID_STATE("Not valid " + str);
GKO_INVALID_CONFIG_VALUE("baseline", str);
}


Expand Down
2 changes: 0 additions & 2 deletions core/factorization/cholesky.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ Cholesky<ValueType, IndexType>::parse(
const config::pnode& config, const config::registry& context,
const config::type_descriptor& td_for_child)
{
using sparsity_pattern_type =
Cholesky<ValueType, IndexType>::sparsity_pattern_type;
auto params = Cholesky<ValueType, IndexType>::build();
if (auto& obj = config.get("symbolic_factorization")) {
params.with_symbolic_factorization(
Expand Down
2 changes: 0 additions & 2 deletions core/factorization/ic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ Ic<ValueType, IndexType>::parse(const config::pnode& config,
const config::registry& context,
const config::type_descriptor& td_for_child)
{
using matrix_type =
typename factorization::Ic<ValueType, IndexType>::matrix_type;
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
2 changes: 0 additions & 2 deletions core/factorization/ilu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ Ilu<ValueType, IndexType>::parse(const config::pnode& config,
const config::registry& context,
const config::type_descriptor& td_for_child)
{
using matrix_type =
typename factorization::Ilu<ValueType, IndexType>::matrix_type;
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
4 changes: 1 addition & 3 deletions core/factorization/lu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ Lu<ValueType, IndexType>::parse(const config::pnode& config,
const config::registry& context,
const config::type_descriptor& td_for_child)
{
using sparsity_pattern_type = typename experimental::factorization::Lu<
ValueType, IndexType>::sparsity_pattern_type;
auto params =
experimental::factorization::Lu<ValueType, IndexType>::build();

Expand All @@ -67,7 +65,7 @@ Lu<ValueType, IndexType>::parse(const config::pnode& config,
} else if (str == "symmetric") {
params.with_symbolic_algorithm(symbolic_type::symmetric);
} else {
GKO_INVALID_STATE("Wrong value for symbolic_type");
GKO_INVALID_CONFIG_VALUE("symbolic_type", str);
}
}
if (auto& obj = config.get("skip_sorting")) {
Expand Down
2 changes: 0 additions & 2 deletions core/factorization/par_ic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ ParIc<ValueType, IndexType>::parse(const config::pnode& config,
const config::registry& context,
const config::type_descriptor& td_for_child)
{
using matrix_type =
typename factorization::ParIc<ValueType, IndexType>::matrix_type;
auto params = factorization::ParIc<ValueType, IndexType>::build();

if (auto& obj = config.get("iterations")) {
Expand Down
2 changes: 0 additions & 2 deletions core/factorization/par_ict.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ ParIct<ValueType, IndexType>::parse(const config::pnode& config,
const config::registry& context,
const config::type_descriptor& td_for_child)
{
using matrix_type =
typename factorization::ParIct<ValueType, IndexType>::matrix_type;
auto params = factorization::ParIct<ValueType, IndexType>::build();

if (auto& obj = config.get("iterations")) {
Expand Down
2 changes: 0 additions & 2 deletions core/factorization/par_ilu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ ParIlu<ValueType, IndexType>::parse(const config::pnode& config,
const config::registry& context,
const config::type_descriptor& td_for_child)
{
using matrix_type =
typename factorization::ParIlu<ValueType, IndexType>::matrix_type;
auto params = factorization::ParIlu<ValueType, IndexType>::build();

if (auto& obj = config.get("iterations")) {
Expand Down
2 changes: 0 additions & 2 deletions core/factorization/par_ilut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,6 @@ ParIlut<ValueType, IndexType>::parse(
const config::pnode& config, const config::registry& context,
const config::type_descriptor& td_for_child)
{
using matrix_type =
typename factorization::ParIlut<ValueType, IndexType>::matrix_type;
auto params = factorization::ParIlut<ValueType, IndexType>::build();

if (auto& obj = config.get("iterations")) {
Expand Down
5 changes: 3 additions & 2 deletions core/preconditioner/ic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ Ic<LSolverType, IndexType>::parse(const config::pnode& config,
td_for_child));
}
if (auto& obj = config.get("factorization")) {
params.with_factorization(gko::config::get_factory<const LinOpFactory>(
obj, context, td_for_child));
params.with_factorization(
gko::config::build_or_get_factory<const LinOpFactory>(
obj, context, td_for_child));
}

return params;
Expand Down
5 changes: 3 additions & 2 deletions core/preconditioner/ilu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ Ilu<LSolverType, USolverType, ReverseApply, IndexType>::parse(
td_for_child));
}
if (auto& obj = config.get("factorization")) {
params.with_factorization(gko::config::get_factory<const LinOpFactory>(
obj, context, td_for_child));
params.with_factorization(
gko::config::build_or_get_factory<const LinOpFactory>(
obj, context, td_for_child));
}

return params;
Expand Down
4 changes: 2 additions & 2 deletions core/preconditioner/isai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ Isai<IsaiType, ValueType, IndexType>::parse(
}
if (auto& obj = config.get("excess_solver_factory")) {
params.with_excess_solver_factory(
gko::config::get_factory<const LinOpFactory>(obj, context,
td_for_child));
gko::config::build_or_get_factory<const LinOpFactory>(
obj, context, td_for_child));
}
if (auto& obj = config.get("excess_solver_reduction")) {
params.with_excess_solver_reduction(
Expand Down
10 changes: 2 additions & 8 deletions core/preconditioner/jacobi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,10 @@ Jacobi<ValueType, IndexType>::parse(const config::pnode& config,
if (auto& obj = config.get("skip_sorting")) {
params.with_skip_sorting(gko::config::get_value<bool>(obj));
}
// SET_VALUE_ARRAY(factory, gko::array<IndexType>, block_pointers,
// config);
// storage_optimization_type is not public. It uses precision_reduction
// as input. Also, it allows value and array input
// Each precision_reduction is created by two values.
// [x, y] -> one precision_reduction (value mode)
// [[x, y], ...] -> array mode
// as input. It allows value and array input, but we only support the value
// input [x, y] -> one precision_reduction (value mode)
if (auto& obj = config.get("storage_optimization")) {
// only support value mode
// TODO: more than one precision_reduction -> array mode.
params.with_storage_optimization(
gko::config::get_value<precision_reduction>(obj));
}
Expand Down
2 changes: 1 addition & 1 deletion core/solver/cb_gmres.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ typename CbGmres<ValueType>::parameters_type CbGmres<ValueType>::parse(
} else if (str == "ireduce2") {
return storage_precision::ireduce2;
}
GKO_INVALID_STATE("Wrong value for storage_precision");
GKO_INVALID_CONFIG_VALUE("storage_precision", str);
};
factory.with_storage_precision(get_storage_precision(obj.get_string()));
}
Expand Down
2 changes: 1 addition & 1 deletion core/solver/lower_trs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ LowerTrs<ValueType, IndexType>::parse(
} else if (str == "syncfree") {
factory.with_algorithm(trisolve_algorithm::syncfree);
} else {
GKO_INVALID_STATE("Wrong value for algorithm");
GKO_INVALID_CONFIG_VALUE("algorithm", str);
}
}
return factory;
Expand Down
2 changes: 1 addition & 1 deletion core/solver/upper_trs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ UpperTrs<ValueType, IndexType>::parse(
} else if (str == "syncfree") {
factory.with_algorithm(trisolve_algorithm::syncfree);
} else {
GKO_INVALID_STATE("Wrong value for algorithm");
GKO_INVALID_CONFIG_VALUE("algorithm", str);
}
}
return factory;
Expand Down
27 changes: 6 additions & 21 deletions core/test/config/preconditioner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ struct Isai
config_map["excess_limit"] = pnode{32};
param.with_excess_limit(32u);
config_map["excess_solver_reduction"] = pnode{1e-4};
param.with_excess_solver_reduction(1e-4);
param.with_excess_solver_reduction(
gko::remove_complex<typename explicit_type::value_type>{1e-4});
if (from_reg) {
config_map["excess_solver_factory"] = pnode{"solver"};
param.with_excess_solver_factory(
Expand Down Expand Up @@ -268,8 +269,8 @@ struct Jacobi
static void set(pnode::map_type& config_map, ParamType& param, registry reg,
std::shared_ptr<const gko::Executor> exec)
{
config_map["max_block_size"] = pnode{32};
param.with_max_block_size(32u);
config_map["max_block_size"] = pnode{16};
param.with_max_block_size(16u);
config_map["max_block_stride"] = pnode{32u};
param.with_max_block_stride(32u);
config_map["skip_sorting"] = pnode{true};
Expand All @@ -280,7 +281,8 @@ struct Jacobi
pnode{std::vector<pnode>{pnode{0}, pnode{1}}};
param.with_storage_optimization(gko::precision_reduction(0, 1));
config_map["accuracy"] = pnode{1e-2};
param.with_accuracy(1e-2);
param.with_accuracy(
gko::remove_complex<typename explicit_type::value_type>{1e-2});
}

template <bool from_reg, typename AnswerType>
Expand All @@ -304,23 +306,6 @@ struct Jacobi
};


// struct JacobiByArray : Jacobi {
// template <bool from_reg, typename ParamType>
// static void set(pnode::map_type& config_map, ParamType& param, registry
// reg,
// std::shared_ptr<const gko::Executor> exec)
// {
// Jacobi::template set<from_reg>(config_map, param, reg, exec);
// using pvec = std::vector<pnode>;
// config_map["storage_optimization"] =
// pnode{pvec{pvec{{0}, {1}}, pvec{{0}, {0}}, pvec{{1}, {1}}}};
// using pr = gko::precision_reduction;
// gko::array<pr> storage(exec, {pr(0, 1), pr(0, 0), pr(1, 1)});
// param.with_storage_optimization(storage);
// }
// };


template <typename T>
class Preconditioner : public ::testing::Test {
protected:
Expand Down
5 changes: 3 additions & 2 deletions include/ginkgo/core/factorization/cholesky.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ class Cholesky
/**
* 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.
* 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
*/
Expand Down
5 changes: 3 additions & 2 deletions include/ginkgo/core/factorization/ic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,13 @@ class Ic : public Composition<ValueType> {
/**
* 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.
* 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
*/
Expand Down
5 changes: 3 additions & 2 deletions include/ginkgo/core/factorization/ilu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,13 @@ class Ilu : public Composition<ValueType> {
/**
* 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.
* 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
*/
Expand Down
5 changes: 3 additions & 2 deletions include/ginkgo/core/factorization/lu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,13 @@ class Lu
/**
* 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.
* 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
*/
Expand Down
Loading

0 comments on commit 7603816

Please sign in to comment.