Skip to content

Commit

Permalink
WIP: use deferred factory
Browse files Browse the repository at this point in the history
  • Loading branch information
yhmtsai committed Nov 27, 2023
1 parent bdc8e45 commit eb722e3
Show file tree
Hide file tree
Showing 27 changed files with 328 additions and 400 deletions.
327 changes: 30 additions & 297 deletions core/config/solver_config.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,7 @@
/*******************************<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>*******************************/
// Copyright (c) 2017-2023, the Ginkgo authors
// SPDX-FileCopyrightText: 2017-2023 The Ginkgo authors
//
// SPDX-License-Identifier: BSD-3-Clause

#include <ginkgo/core/base/exception_helpers.hpp>
#include <ginkgo/core/config/config.hpp>
Expand All @@ -55,279 +28,39 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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());
}


template <typename ValueType>
class IrConfigurator {
public:
static std::unique_ptr<typename solver::Ir<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::Ir<ValueType>::build();
SET_POINTER_VECTOR(factory, const stop::CriterionFactory, criteria,
config, context, exec, td_for_child);
SET_POINTER(factory, const LinOpFactory, solver, config, context, exec,
td_for_child);

SET_POINTER(factory, const LinOp, generated_solver, config, context,
exec, td_for_child);
SET_VALUE(factory, ValueType, relaxation_factor, config);
SET_VALUE(factory, solver::initial_guess_mode, default_initial_guess,
config);
return factory.on(exec);
}
};


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


template <typename ValueType>
class IdrConfigurator {
public:
static std::unique_ptr<typename solver::Idr<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::Idr<ValueType>::build();
common_solver_configure(factory, config, context, exec, td_for_child);
SET_VALUE(factory, size_type, subspace_dim, config);
SET_VALUE(factory, remove_complex<ValueType>, kappa, config);
SET_VALUE(factory, bool, deterministic, config);
SET_VALUE(factory, bool, complex_subspace, config);
return factory.on(exec);
}
};


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


template <typename ValueType>
class GcrConfigurator {
public:
static std::unique_ptr<typename solver::Gcr<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::Gcr<ValueType>::build();
common_solver_configure(factory, config, context, exec, td_for_child);
SET_VALUE(factory, size_type, krylov_dim, config);
return factory.on(exec);
}
};


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


template <typename ValueType>
class GmresConfigurator {
public:
static std::unique_ptr<typename solver::Gmres<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::Gmres<ValueType>::build();
common_solver_configure(factory, config, context, exec, td_for_child);
SET_VALUE(factory, size_type, krylov_dim, config);
SET_VALUE(factory, bool, flexible, config);
return factory.on(exec);
}
};


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


template <typename ValueType>
class CbGmresConfigurator {
public:
static std::unique_ptr<typename solver::CbGmres<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::CbGmres<ValueType>::build();
common_solver_configure(factory, config, context, exec, td_for_child);
SET_VALUE(factory, size_type, krylov_dim, config);
if (config.contains("storage_precision")) {
auto get_storage_precision = [](std::string str) {
using gko::solver::cb_gmres::storage_precision;
if (str == "keep") {
return storage_precision::keep;
} else if (str == "reduce1") {
return storage_precision::reduce1;
} else if (str == "reduce2") {
return storage_precision::reduce2;
} else if (str == "integer") {
return storage_precision::integer;
} else if (str == "ireduce1") {
return storage_precision::ireduce1;
} else if (str == "ireduce2") {
return storage_precision::ireduce2;
}
GKO_INVALID_STATE("Wrong value for storage_precision");
};
factory.with_storage_precision(get_storage_precision(
config.at("storage_precision").get_data<std::string>()));
}
return factory.on(exec);
// for valuetype only
#define BUILD_FROM_CONFIG(_enum) \
template <> \
deferred_factory_parameter<gko::LinOpFactory> \
build_from_config<LinOpFactoryType::_enum>( \
const pnode& config, const registry& context, \
gko::config::type_descriptor td) \
{ \
auto updated = update_type(config, td); \
return dispatch<gko::LinOpFactory, gko::solver::_type>( \
updated.first, config, context, updated, value_type_list()); \
}
};


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


template <typename ValueType, typename IndexType>
class DirectConfigurator {
public:
static std::unique_ptr<
typename experimental::solver::Direct<ValueType, IndexType>::Factory>
build_from_config(const pnode& config, const registry& context,
std::shared_ptr<const Executor> exec,
type_descriptor td_for_child)
{
auto factory =
experimental::solver::Direct<ValueType, IndexType>::build();
SET_VALUE(factory, size_type, num_rhs, config);
SET_POINTER(factory, const LinOpFactory, factorization, config, context,
exec, td_for_child);
return factory.on(exec);
}
};
BUILD_FROM_CONFIG(Cg)
BUILD_FROM_CONFIG(Bicg)
BUILD_FROM_CONFIG(Bicgstab)
BUILD_FROM_CONFIG(Cgs)
BUILD_FROM_CONFIG(Fcg)
BUILD_FROM_CONFIG(Ir)
BUILD_FROM_CONFIG(Idr)
BUILD_FROM_CONFIG(Gcr)
BUILD_FROM_CONFIG(Gmres)
BUILD_FROM_CONFIG(CbGmres)


template <>
std::unique_ptr<gko::LinOpFactory> build_from_config<LinOpFactoryType::Direct>(
deferred_factory_parameter<gko::LinOpFactory>
build_from_config<LinOpFactoryType::Direct>(
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, DirectConfigurator>(
return dispatch<gko::LinOpFactory, gko::experimental::solver::Direct>(
updated.first + "," + updated.second, config, context, exec, updated,
value_type_list(), index_type_list());
}
Expand Down Expand Up @@ -365,7 +98,7 @@ class trs_helper {


template <>
std::unique_ptr<gko::LinOpFactory>
deferred_factory_parameter<gko::LinOpFactory>
build_from_config<LinOpFactoryType::LowerTrs>(
const pnode& config, const registry& context,
std::shared_ptr<const Executor>& exec, gko::config::type_descriptor td)
Expand All @@ -378,7 +111,7 @@ build_from_config<LinOpFactoryType::LowerTrs>(
}

template <>
std::unique_ptr<gko::LinOpFactory>
deferred_factory_parameter<gko::LinOpFactory>
build_from_config<LinOpFactoryType::UpperTrs>(
const pnode& config, const registry& context,
std::shared_ptr<const Executor>& exec, gko::config::type_descriptor td)
Expand Down
Loading

0 comments on commit eb722e3

Please sign in to comment.