Ginkgo: ginkgo/core/config/registry.hpp Source File

| Ginkgo Generated from branch based on main. Ginkgo version 1.11.0 A numerical linear algebra library targeting many-core architectures

Loading...
Searching...
No Matches
registry.hpp
1// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#ifndef GKO_PUBLIC_CORE_CONFIG_REGISTRY_HPP_
6#define GKO_PUBLIC_CORE_CONFIG_REGISTRY_HPP_
7
8
9#include <complex>
10#include <functional>
11#include <map>
12#include <string>
13#include <unordered_map>
14#include <utility>
15
16#include <ginkgo/core/base/exception_helpers.hpp>
17#include <ginkgo/core/base/lin_op.hpp>
18#include <ginkgo/core/base/types.hpp>
19#include <ginkgo/core/base/utils_helper.hpp>
20#include <ginkgo/core/config/property_tree.hpp>
21#include <ginkgo/core/stop/criterion.hpp>
22
23
24namespace gko {
25namespace config {
26
27
28class registry;
29
30class type_descriptor;
31
32using configuration_map =
33 std::map<std::string,
34 std::function<deferred_factory_parameter<gko::LinOpFactory>(
35 const pnode&, const registry&, type_descriptor)>>;
36
37
38namespace detail {
39
40
41class registry_accessor;
42
43
49template <typename T, typename = void>
50struct base_type {};
51
52template <typename T>
53struct base_type<T, std::enable_if_t<std::is_convertible<T*, LinOp*>::value>> {
54 using type = LinOp;
55};
56
57template <typename T>
58struct base_type<
59 T, std::enable_if_t<std::is_convertible<T*, LinOpFactory*>::value>> {
60 using type = LinOpFactory;
61};
62
63template <typename T>
64struct base_type<
65 T,
66 std::enable_if_t<std::is_convertible<T*, stop::CriterionFactory*>::value>> {
67 using type = stop::CriterionFactory;
68};
69
70
75class allowed_ptr {
76public:
84 template <typename Type>
85 allowed_ptr(std::shared_ptr<Type> obj);
86
92 template <typename Type>
93 bool contains() const;
94
102 template <typename Type>
103 std::shared_ptr<Type> get() const;
104
105private:
106 struct generic_container {
107 virtual ~generic_container() = default;
108 };
109
110 template <typename Type>
111 struct concrete_container : generic_container {
112 concrete_container(std::shared_ptr<Type> obj) : ptr{obj}
113 {
114 static_assert(
115 std::is_same<Type, typename base_type<Type>::type>::value,
116 "The given type must be a base_type");
117 }
118
119 std::shared_ptr<Type> ptr;
120 };
121
122 std::shared_ptr<generic_container> data_;
123};
124
125
126template <typename Type>
127inline allowed_ptr::allowed_ptr(std::shared_ptr<Type> obj)
128{
129 data_ =
130 std::make_shared<concrete_container<typename base_type<Type>::type>>(
131 obj);
132}
133
134
135template <typename Type>
136inline bool allowed_ptr::contains() const
137{
138 return dynamic_cast<const concrete_container<Type>*>(data_.get());
139}
140
141
142template <typename Type>
143inline std::shared_ptr<Type> allowed_ptr::get() const
144{
145 GKO_THROW_IF_INVALID(this->template contains<Type>(),
146 "does not hold the requested type.");
147 return dynamic_cast<concrete_container<Type>*>(data_.get())->ptr;
148}
149
150
151} // namespace detail
152
153
167 class registry final {
168public:
169 friend class detail::registry_accessor;
170
171
181 registry(const configuration_map& additional_map = {});
182
195 registry(
196 const std::unordered_map<std::string, detail::allowed_ptr>& stored_map,
197 const configuration_map& additional_map = {});
198
207 template <typename T>
208 bool emplace(std::string key, std::shared_ptr<T> data);
209
210protected:
220 template <typename T>
221 std::shared_ptr<T> get_data(std::string key) const;
222
226 const configuration_map& get_build_map() const { return build_map_; }
227
228private:
229 std::unordered_map<std::string, detail::allowed_ptr> stored_map_;
230 configuration_map build_map_;
231};
232
233
234template <typename T>
235 inline bool registry::emplace(std::string key, std::shared_ptr<T> data)
236{
237 auto it = stored_map_.emplace(key, data);
238 return it.second;
239}
240
241
242template <typename T>
243inline std::shared_ptr<T> registry::get_data(std::string key) const
244{
245 return gko::as<T>(stored_map_.at(key)
246 .template get<typename detail::base_type<T>::type>());
247}
248
249} // namespace config
250} // namespace gko
251
252#endif // GKO_PUBLIC_CORE_CONFIG_REGISTRY_HPP_
gko::config::pnode
pnode describes a tree of properties.
Definition property_tree.hpp:28
gko::config::registry
This class stores additional context for creating Ginkgo objects from configuration files.
Definition registry.hpp:167
gko::config::registry::registry
registry(const std::unordered_map< std::string, detail::allowed_ptr > &stored_map, const configuration_map &additional_map={})
registry constructor
gko::config::registry::registry
registry(const configuration_map &additional_map={})
registry constructor
gko::config::registry::emplace
bool emplace(std::string key, std::shared_ptr< T > data)
Store the data with the key.
Definition registry.hpp:235
gko::config::type_descriptor
This class describes the value and index types to be used when building a Ginkgo type from a configur...
Definition type_descriptor.hpp:39
gko::stop::CriterionFactory
AbstractFactory< Criterion, CriterionArgs > CriterionFactory
Declares an Abstract Factory specialized for Criterions.
Definition criterion.hpp:226
gko
The Ginkgo namespace.
Definition abstract_factory.hpp:20
gko::as
std::decay_t< T > * as(U *obj)
Performs polymorphic type conversion.
Definition utils_helper.hpp:307