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

Solver config #1395

Merged
merged 8 commits into from
May 27, 2024
Merged
Changes from 1 commit
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
Next Next commit
add the common solver and test
yhmtsai committed May 27, 2024
commit 68b3b70969feaa28cd0c9d5df3af0cadedeaaf3d
1 change: 1 addition & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ target_sources(ginkgo
config/config_helper.cpp
config/property_tree.cpp
config/registry.cpp
config/solver_config.cpp
config/stop_config.cpp
config/type_descriptor.cpp
distributed/index_map.cpp
2 changes: 1 addition & 1 deletion core/config/config_helper.hpp
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ namespace config {
* LinOpFactoryType enum is to avoid forward declaration, linopfactory header,
* two template versions of parse
*/
enum class LinOpFactoryType : int { Cg = 0 };
enum class LinOpFactoryType : int { Cg = 0, Bicg, Bicgstab, Fcg, Cgs };


/**
130 changes: 130 additions & 0 deletions core/config/solver_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*******************************<GINKGO LICENSE>******************************
Copyright (c) 2017-2023, the Ginkgo authors
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************<GINKGO LICENSE>*******************************/

#include <ginkgo/core/base/exception_helpers.hpp>
#include <ginkgo/core/config/config.hpp>
#include <ginkgo/core/config/registry.hpp>
#include <ginkgo/core/solver/bicg.hpp>
#include <ginkgo/core/solver/bicgstab.hpp>
#include <ginkgo/core/solver/cg.hpp>
#include <ginkgo/core/solver/cgs.hpp>
#include <ginkgo/core/solver/fcg.hpp>


#include "core/config/config.hpp"
#include "core/config/dispatch.hpp"
#include "core/config/solver_config.hpp"

namespace gko {
namespace config {


// It can also be directly in solver (or in proteced part) if we also allow
// the executor as input there.
template <template <class> class Solver>
class solver_helper {
public:
template <typename ValueType>
class configurator {
public:
static std::unique_ptr<typename Solver<ValueType>::Factory>
parse(const pnode& config, const registry& context,
std::shared_ptr<const Executor> exec,
type_descriptor td_for_child)
{
auto factory = Solver<ValueType>::build();
common_solver_configure(factory, config, context, exec,
td_for_child);
return factory.on(exec);
}
};
};


template <>
std::unique_ptr<gko::LinOpFactory> parse<LinOpFactoryType::Cg>(
const pnode& config, const registry& context,
std::shared_ptr<const Executor>& exec, gko::config::type_descriptor td)
{
auto updated = update_type(config, td);
return dispatch<gko::LinOpFactory, solver_helper<solver::Cg>::configurator>(
updated.first, config, context, exec, updated, value_type_list());
}

template <>
std::unique_ptr<gko::LinOpFactory> parse<LinOpFactoryType::Bicg>(
const pnode& config, const registry& context,
std::shared_ptr<const Executor>& exec, gko::config::type_descriptor td)
{
auto updated = update_type(config, td);
return dispatch<gko::LinOpFactory,
solver_helper<solver::Bicg>::configurator>(
updated.first, config, context, exec, updated, value_type_list());
}

template <>
std::unique_ptr<gko::LinOpFactory>
parse<LinOpFactoryType::Bicgstab>(
const pnode& config, const registry& context,
std::shared_ptr<const Executor>& exec, gko::config::type_descriptor td)
{
auto updated = update_type(config, td);
return dispatch<gko::LinOpFactory,
solver_helper<solver::Bicgstab>::configurator>(
updated.first, config, context, exec, updated, value_type_list());
}

template <>
std::unique_ptr<gko::LinOpFactory> parse<LinOpFactoryType::Cgs>(
const pnode& config, const registry& context,
std::shared_ptr<const Executor>& exec, gko::config::type_descriptor td)
{
auto updated = update_type(config, td);
return dispatch<gko::LinOpFactory,
solver_helper<solver::Cgs>::configurator>(
updated.first, config, context, exec, updated, value_type_list());
}

template <>
std::unique_ptr<gko::LinOpFactory> parse<LinOpFactoryType::Fcg>(
const pnode& config, const registry& context,
std::shared_ptr<const Executor>& exec, gko::config::type_descriptor td)
{
auto updated = update_type(config, td);
return dispatch<gko::LinOpFactory,
solver_helper<solver::Fcg>::configurator>(
updated.first, config, context, exec, updated, value_type_list());
}


} // namespace config
} // namespace gko
75 changes: 75 additions & 0 deletions core/config/solver_config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*******************************<GINKGO LICENSE>******************************
Copyright (c) 2017-2023, the Ginkgo authors
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************<GINKGO LICENSE>*******************************/

#ifndef GKO_CORE_CONFIG_SOLVER_CONFIG_HPP_
#define GKO_CORE_CONFIG_SOLVER_CONFIG_HPP_


#include <ginkgo/core/config/config.hpp>
#include <ginkgo/core/config/registry.hpp>


#include "core/config/config.hpp"
#include "core/config/dispatch.hpp"

namespace gko {
namespace config {


template <typename SolverFactory>
inline void common_solver_configure(SolverFactory& params, const pnode& config,
const registry& context,
std::shared_ptr<const Executor> exec,
type_descriptor td_for_child)
{
// The following will be moved to the common solver function in another pr
if (auto& obj = config.get("generated_preconditioner")) {
params.with_generated_preconditioner(
gko::config::get_stored_obj<const LinOp>(obj, context));
}
if (auto& obj = config.get("criteria")) {
params.with_criteria(
gko::config::build_or_get_factory_vector<
const stop::CriterionFactory>(obj, context, td_for_child));
}
if (auto& obj = config.get("preconditioner")) {
params.with_preconditioner(
gko::config::build_or_get_factory<const LinOpFactory>(
obj, context, td_for_child));
}
}


} // namespace config
} // namespace gko

#endif // GKO_CORE_CONFIG_SOLVER_CONFIG_HPP_
47 changes: 0 additions & 47 deletions core/solver/cg.cpp
Original file line number Diff line number Diff line change
@@ -12,36 +12,14 @@
#include <ginkgo/core/base/name_demangling.hpp>
#include <ginkgo/core/base/precision_dispatch.hpp>
#include <ginkgo/core/base/utils.hpp>
#include <ginkgo/core/config/config.hpp>
#include <ginkgo/core/stop/iteration.hpp>


#include "core/config/config_helper.hpp"
#include "core/config/dispatch.hpp"
#include "core/config/type_descriptor_helper.hpp"
#include "core/distributed/helpers.hpp"
#include "core/solver/cg_kernels.hpp"
#include "core/solver/solver_boilerplate.hpp"


namespace gko {
namespace config {


template <>
deferred_factory_parameter<gko::LinOpFactory> parse<LinOpFactoryType::Cg>(
const pnode& config, const registry& context, const type_descriptor& td)
{
auto updated = update_type(config, td);
return dispatch<gko::LinOpFactory, gko::solver::Cg>(
config, context, updated,
make_type_selector(updated.get_value_typestr(), value_type_list()));
}


} // namespace config


namespace solver {
namespace cg {
namespace {
@@ -56,31 +34,6 @@ GKO_REGISTER_OPERATION(step_2, cg::step_2);
} // namespace cg


template <typename ValueType>
typename Cg<ValueType>::parameters_type Cg<ValueType>::parse(
const config::pnode& config, const config::registry& context,
const config::type_descriptor& td_for_child)
{
auto params = solver::Cg<ValueType>::build();
// The following will be moved to the common solver function in another pr
if (auto& obj = config.get("generated_preconditioner")) {
params.with_generated_preconditioner(
gko::config::get_stored_obj<const LinOp>(obj, context));
}
if (auto& obj = config.get("criteria")) {
params.with_criteria(
gko::config::parse_or_get_factory_vector<
const stop::CriterionFactory>(obj, context, td_for_child));
}
if (auto& obj = config.get("preconditioner")) {
params.with_preconditioner(
gko::config::parse_or_get_factory<const LinOpFactory>(
obj, context, td_for_child));
}
return params;
}


template <typename ValueType>
std::unique_ptr<LinOp> Cg<ValueType>::transpose() const
{
1 change: 1 addition & 0 deletions core/test/config/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ginkgo_create_test(config)
ginkgo_create_test(property_tree)
ginkgo_create_test(registry)
ginkgo_create_test(solver)
Loading