Skip to content

Commit

Permalink
add the common solver and test
Browse files Browse the repository at this point in the history
  • Loading branch information
yhmtsai committed Aug 18, 2023
1 parent a2b62b8 commit fd5b59a
Show file tree
Hide file tree
Showing 8 changed files with 468 additions and 51 deletions.
1 change: 1 addition & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ target_sources(ginkgo
base/version.cpp
config/config.cpp
config/stop_config.cpp
config/solver_config.cpp
distributed/partition.cpp
factorization/cholesky.cpp
factorization/elimination_forest.cpp
Expand Down
6 changes: 5 additions & 1 deletion core/config/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ namespace config {

buildfromconfig_map generate_config_map()
{
return {{"Cg", build_from_config<LinOpFactoryType::Cg>}};
return {{"Cg", build_from_config<LinOpFactoryType::Cg>},
{"Bicg", build_from_config<LinOpFactoryType::Bicg>},
{"Bicgstab", build_from_config<LinOpFactoryType::Bicgstab>},
{"Cgs", build_from_config<LinOpFactoryType::Cgs>},
{"Fcg", build_from_config<LinOpFactoryType::Fcg>}};
}


Expand Down
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>
build_from_config(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> build_from_config<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> build_from_config<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>
build_from_config<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> build_from_config<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> build_from_config<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
69 changes: 69 additions & 0 deletions core/config/solver_config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*******************************<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& factory, const pnode& config,
const registry& context,
std::shared_ptr<const Executor> exec,
type_descriptor td_for_child)
{
SET_POINTER(factory, const LinOp, generated_preconditioner, config, context,
exec, td_for_child);
// handle parameter requires exec
// criteria and preconditioner are almost in each solver -> to another
// function.
SET_POINTER_VECTOR(factory, const stop::CriterionFactory, criteria, config,
context, exec, td_for_child);
SET_POINTER(factory, const LinOpFactory, preconditioner, config, context,
exec, td_for_child);
}


} // namespace config
} // namespace gko

#endif // GKO_CORE_CONFIG_SOLVER_CONFIG_HPP_
49 changes: 0 additions & 49 deletions core/solver/cg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,63 +40,14 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#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.hpp"
#include "core/config/dispatch.hpp"
#include "core/distributed/helpers.hpp"
#include "core/solver/cg_kernels.hpp"
#include "core/solver/solver_boilerplate.hpp"


namespace gko {
namespace config {


// It can also be directly in solver::Cg (or in proteced part) if we also allow
// the executor as input there.
template <typename ValueType>
class CgConfigurer {
public:
static std::unique_ptr<typename solver::Cg<ValueType>::Factory>
build_from_config(const pnode& config, const registry& context,
std::shared_ptr<const Executor> exec,
type_descriptor td_for_child)
{
auto factory = solver::Cg<ValueType>::build();
SET_POINTER(factory, const LinOp, generated_preconditioner, config,
context, exec, td_for_child);
// handle parameter requires exec
// criteria and preconditioner are almost in each solver -> to another
// function.
SET_POINTER_VECTOR(factory, const stop::CriterionFactory, criteria,
config, context, exec, td_for_child);
SET_POINTER(factory, const LinOpFactory, preconditioner, config,
context, exec, td_for_child);
// can also handle preconditioner, criterion here if they are in
// context.
return factory.on(exec);
}
};


template <>
std::unique_ptr<gko::LinOpFactory>
build_from_config<static_cast<int>(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, CgConfigurer>(
updated.first, config, context, exec, updated, value_type_list());
}


} // namespace config


namespace solver {
namespace cg {
namespace {
Expand Down
1 change: 1 addition & 0 deletions core/test/config/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ ginkgo_create_test(config)
ginkgo_create_test(data)
ginkgo_create_test(property_tree)
ginkgo_create_test(registry)
ginkgo_create_test(solver)
Loading

0 comments on commit fd5b59a

Please sign in to comment.