Ginkgo  Generated from pipelines/1330831941 branch based on master. Ginkgo version 1.8.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 
17 #include <ginkgo/core/base/exception_helpers.hpp>
18 #include <ginkgo/core/base/lin_op.hpp>
19 #include <ginkgo/core/base/types.hpp>
20 #include <ginkgo/core/base/utils_helper.hpp>
21 #include <ginkgo/core/config/property_tree.hpp>
22 #include <ginkgo/core/stop/criterion.hpp>
23 
24 
25 namespace gko {
26 namespace config {
27 
28 
29 class registry;
30 
31 class type_descriptor;
32 
33 using configuration_map =
34  std::map<std::string,
35  std::function<deferred_factory_parameter<gko::LinOpFactory>(
36  const pnode&, const registry&, type_descriptor)>>;
37 
38 
39 namespace detail {
40 
41 
42 class registry_accessor;
43 
44 
50 template <typename T, typename = void>
51 struct base_type {};
52 
53 template <typename T>
54 struct base_type<T, std::enable_if_t<std::is_convertible<T*, LinOp*>::value>> {
55  using type = LinOp;
56 };
57 
58 template <typename T>
59 struct base_type<
60  T, std::enable_if_t<std::is_convertible<T*, LinOpFactory*>::value>> {
61  using type = LinOpFactory;
62 };
63 
64 template <typename T>
65 struct base_type<
66  T,
67  std::enable_if_t<std::is_convertible<T*, stop::CriterionFactory*>::value>> {
68  using type = stop::CriterionFactory;
69 };
70 
71 
76 class allowed_ptr {
77 public:
85  template <typename Type>
86  allowed_ptr(std::shared_ptr<Type> obj);
87 
93  template <typename Type>
94  bool contains() const;
95 
103  template <typename Type>
104  std::shared_ptr<Type> get() const;
105 
106 private:
107  struct generic_container {
108  virtual ~generic_container() = default;
109  };
110 
111  template <typename Type>
112  struct concrete_container : generic_container {
113  concrete_container(std::shared_ptr<Type> obj) : ptr{obj}
114  {
115  static_assert(
116  std::is_same<Type, typename base_type<Type>::type>::value,
117  "The given type must be a base_type");
118  }
119 
120  std::shared_ptr<Type> ptr;
121  };
122 
123  std::shared_ptr<generic_container> data_;
124 };
125 
126 
127 template <typename Type>
128 inline allowed_ptr::allowed_ptr(std::shared_ptr<Type> obj)
129 {
130  data_ =
131  std::make_shared<concrete_container<typename base_type<Type>::type>>(
132  obj);
133 }
134 
135 
136 template <typename Type>
137 inline bool allowed_ptr::contains() const
138 {
139  return dynamic_cast<const concrete_container<Type>*>(data_.get());
140 }
141 
142 
143 template <typename Type>
144 inline std::shared_ptr<Type> allowed_ptr::get() const
145 {
146  GKO_THROW_IF_INVALID(this->template contains<Type>(),
147  "does not hold the requested type.");
148  return dynamic_cast<concrete_container<Type>*>(data_.get())->ptr;
149 }
150 
151 
152 } // namespace detail
153 
154 
168 class registry final {
169 public:
170  friend class detail::registry_accessor;
171 
172 
182  registry(const configuration_map& additional_map = {});
183 
196  registry(
197  const std::unordered_map<std::string, detail::allowed_ptr>& stored_map,
198  const configuration_map& additional_map = {});
199 
208  template <typename T>
209  bool emplace(std::string key, std::shared_ptr<T> data);
210 
211 protected:
221  template <typename T>
222  std::shared_ptr<T> get_data(std::string key) const;
223 
227  const configuration_map& get_build_map() const { return build_map_; }
228 
229 private:
230  std::unordered_map<std::string, detail::allowed_ptr> stored_map_;
231  configuration_map build_map_;
232 };
233 
234 
235 template <typename T>
236 inline bool registry::emplace(std::string key, std::shared_ptr<T> data)
237 {
238  auto it = stored_map_.emplace(key, data);
239  return it.second;
240 }
241 
242 
243 template <typename T>
244 inline std::shared_ptr<T> registry::get_data(std::string key) const
245 {
246  return gko::as<T>(stored_map_.at(key)
247  .template get<typename detail::base_type<T>::type>());
248 }
249 
250 } // namespace config
251 } // namespace gko
252 
253 #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:168
gko::config::registry::emplace
bool emplace(std::string key, std::shared_ptr< T > data)
Store the data with the key.
Definition: registry.hpp:236