Ginkgo  Generated from pipelines/1589998975 branch based on develop. Ginkgo version 1.10.0
A numerical linear algebra library targeting many-core architectures
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 
24 namespace gko {
25 namespace config {
26 
27 
28 class registry;
29 
30 class type_descriptor;
31 
32 using configuration_map =
33  std::map<std::string,
34  std::function<deferred_factory_parameter<gko::LinOpFactory>(
35  const pnode&, const registry&, type_descriptor)>>;
36 
37 
38 namespace detail {
39 
40 
41 class registry_accessor;
42 
43 
49 template <typename T, typename = void>
50 struct base_type {};
51 
52 template <typename T>
53 struct base_type<T, std::enable_if_t<std::is_convertible<T*, LinOp*>::value>> {
54  using type = LinOp;
55 };
56 
57 template <typename T>
58 struct base_type<
59  T, std::enable_if_t<std::is_convertible<T*, LinOpFactory*>::value>> {
60  using type = LinOpFactory;
61 };
62 
63 template <typename T>
64 struct base_type<
65  T,
66  std::enable_if_t<std::is_convertible<T*, stop::CriterionFactory*>::value>> {
67  using type = stop::CriterionFactory;
68 };
69 
70 
75 class allowed_ptr {
76 public:
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 
105 private:
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 
126 template <typename Type>
127 inline 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 
135 template <typename Type>
136 inline bool allowed_ptr::contains() const
137 {
138  return dynamic_cast<const concrete_container<Type>*>(data_.get());
139 }
140 
141 
142 template <typename Type>
143 inline 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 {
168 public:
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 
210 protected:
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 
228 private:
229  std::unordered_map<std::string, detail::allowed_ptr> stored_map_;
230  configuration_map build_map_;
231 };
232 
233 
234 template <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 
242 template <typename T>
243 inline 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::registry::registry
registry(const configuration_map &additional_map={})
registry constructor
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::config::registry
This class stores additional context for creating Ginkgo objects from configuration files.
Definition: registry.hpp:167
gko::config::registry::emplace
bool emplace(std::string key, std::shared_ptr< T > data)
Store the data with the key.
Definition: registry.hpp:235