Skip to content

Commit

Permalink
move the def to source of registry
Browse files Browse the repository at this point in the history
  • Loading branch information
yhmtsai committed Mar 29, 2024
1 parent 0232dae commit 3fb1a9c
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 64 deletions.
1 change: 1 addition & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ target_sources(ginkgo
config/config.cpp
config/config_helper.cpp
config/property_tree.cpp
config/registry.cpp
config/stop_config.cpp
distributed/partition.cpp
factorization/cholesky.cpp
Expand Down
50 changes: 50 additions & 0 deletions core/config/registry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
//
// SPDX-License-Identifier: BSD-3-Clause

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


namespace gko {
namespace config {


template <>
linop_map& registry::get_map_impl<linop_map>()
{
return linop_map_;
}

template <>
linopfactory_map& registry::get_map_impl<linopfactory_map>()
{
return linopfactory_map_;
}

template <>
criterionfactory_map& registry::get_map_impl<criterionfactory_map>()
{
return criterionfactory_map_;
}

template <>
const linop_map& registry::get_map_impl<linop_map>() const
{
return linop_map_;
}

template <>
const linopfactory_map& registry::get_map_impl<linopfactory_map>() const
{
return linopfactory_map_;
}

template <>
const criterionfactory_map& registry::get_map_impl<criterionfactory_map>() const
{
return criterionfactory_map_;
}


} // namespace config
} // namespace gko
119 changes: 55 additions & 64 deletions include/ginkgo/core/config/registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,36 +41,47 @@ using buildfromconfig_map =


/**
* map_type gives the map type according to the base type of given type.
*
* @tparam T the type
* registry is the storage for file config usage. It stores the building
* function, linop, linop_factory, criterion
*/
template <typename T, typename = void>
struct map_type {
using type = void;
};

template <typename T>
struct map_type<
T, typename std::enable_if<std::is_convertible<T*, LinOp*>::value>::type> {
using type = linop_map;
};
class registry {
private:
/**
* map_type gives the map type according to the base type of given type.
*
* @tparam T the type
*/
template <typename T, typename = void>
struct map_type {
using type = void;
};

template <typename T>
struct map_type<T, typename std::enable_if<
std::is_convertible<T*, LinOpFactory*>::value>::type> {
using type = linopfactory_map;
};
template <typename T>
struct map_type<T, typename std::enable_if<
std::is_convertible<T*, LinOp*>::value>::type> {
using type = linop_map;
};

template <typename T>
struct map_type<T, typename std::enable_if<std::is_convertible<
T*, stop::CriterionFactory*>::value>::type> {
using type = criterionfactory_map;
};
template <typename T>
struct map_type<T, typename std::enable_if<std::is_convertible<
T*, LinOpFactory*>::value>::type> {
using type = linopfactory_map;
};

template <typename T>
struct map_type<T, typename std::enable_if<std::is_convertible<
T*, stop::CriterionFactory*>::value>::type> {
using type = criterionfactory_map;
};

class registry {
public:
/**
* registry constructor
*
* @param build_map the build map to dispatch the class base. Ginkgo
* provides `generate_config_map()` in config.hpp to provide the ginkgo
* build map. Users can extend this map to fit their own LinOpFactory.
*/
registry(buildfromconfig_map build_map) : build_map_(build_map) {}

/**
Expand All @@ -82,11 +93,7 @@ class registry {
* @param data the shared pointer of the object
*/
template <typename T>
bool emplace(std::string key, std::shared_ptr<T> data)
{
auto it = this->get_map<T>().emplace(key, data);
return it.second;
}
bool emplace(std::string key, std::shared_ptr<T> data);

/**
* search_data searches the key on the corresponding map.
Expand All @@ -98,11 +105,11 @@ class registry {
* @return the shared pointer of the object
*/
template <typename T>
std::shared_ptr<T> search_data(std::string key) const
{
return gko::as<T>(this->get_map<T>().at(key));
}
std::shared_ptr<T> search_data(std::string key) const;

/**
* get the stored build map
*/
const buildfromconfig_map& get_build_map() const { return build_map_; }

protected:
Expand All @@ -114,16 +121,10 @@ class registry {
* @return the map
*/
template <typename T>
typename map_type<T>::type& get_map()
{
return this->get_map_impl<typename map_type<T>::type>();
}
typename map_type<T>::type& get_map();

template <typename T>
const typename map_type<T>::type& get_map() const
{
return this->get_map_impl<typename map_type<T>::type>();
}
const typename map_type<T>::type& get_map() const;

/**
* get_map_impl is the implementation of get_map
Expand All @@ -146,41 +147,31 @@ class registry {
};


template <>
inline linop_map& registry::get_map_impl<linop_map>()
template <typename T>
inline bool registry::emplace(std::string key, std::shared_ptr<T> data)
{
return linop_map_;
auto it = this->get_map<T>().emplace(key, data);
return it.second;
}

template <>
inline linopfactory_map& registry::get_map_impl<linopfactory_map>()
{
return linopfactory_map_;
}

template <>
inline criterionfactory_map& registry::get_map_impl<criterionfactory_map>()
template <typename T>
inline std::shared_ptr<T> registry::search_data(std::string key) const
{
return criterionfactory_map_;
return gko::as<T>(this->get_map<T>().at(key));
}

template <>
inline const linop_map& registry::get_map_impl<linop_map>() const
{
return linop_map_;
}

template <>
inline const linopfactory_map& registry::get_map_impl<linopfactory_map>() const
template <typename T>
inline typename registry::map_type<T>::type& registry::get_map()
{
return linopfactory_map_;
return this->get_map_impl<typename map_type<T>::type>();
}

template <>
inline const criterionfactory_map&
registry::get_map_impl<criterionfactory_map>() const
template <typename T>
inline const typename registry::map_type<T>::type& registry::get_map() const
{
return criterionfactory_map_;
return this->get_map_impl<typename map_type<T>::type>();
}


Expand Down

0 comments on commit 3fb1a9c

Please sign in to comment.