ArmNN
 21.08
BackendRegistry.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #include <armnn/Types.hpp>
8 #include <armnn/BackendId.hpp>
9 #include <armnn/Optional.hpp>
11 
12 #include <memory>
13 #include <unordered_map>
14 #include <functional>
15 
16 namespace armnn
17 {
18 
19 namespace profiling
20 {
21  class ProfilingService;
22 }
23 class IBackendInternal;
24 using IBackendInternalUniquePtr = std::unique_ptr<IBackendInternal>;
25 
27 {
28 public:
30  using FactoryFunction = std::function<PointerType()>;
31 
32  void Register(const BackendId& id, FactoryFunction factory);
33  bool IsBackendRegistered(const BackendId& id) const;
34  FactoryFunction GetFactory(const BackendId& id) const;
35  size_t Size() const;
36  BackendIdSet GetBackendIds() const;
37  std::string GetBackendIdsAsString() const;
38  void SetProfilingService(armnn::Optional<profiling::ProfilingService&> profilingService);
39  void RegisterAllocator(const BackendId& id, std::shared_ptr<ICustomAllocator> alloc);
40  std::unordered_map<BackendId, std::shared_ptr<ICustomAllocator>> GetAllocators();
41 
43  virtual ~BackendRegistry() {}
44 
46  {
48  const BackendId& id,
49  FactoryFunction factory)
50  {
51  instance.Register(id, factory);
52  }
53  };
54 
55  void Deregister(const BackendId& id);
56  void DeregisterAllocator(const BackendId &id);
57 
58 protected:
59  using FactoryStorage = std::unordered_map<BackendId, FactoryFunction>;
60 
61  /// For testing only
62  static void Swap(BackendRegistry& instance, FactoryStorage& other);
63 
64 private:
65  BackendRegistry(const BackendRegistry&) = delete;
66  BackendRegistry& operator=(const BackendRegistry&) = delete;
67 
68  FactoryStorage m_Factories;
70  std::unordered_map<BackendId, std::shared_ptr<ICustomAllocator>> m_CustomMemoryAllocatorMap;
71 };
72 
74 
75 } // namespace armnn
StaticRegistryInitializer(BackendRegistry &instance, const BackendId &id, FactoryFunction factory)
IBackendInternalUniquePtr PointerType
std::function< PointerType()> FactoryFunction
void Register(const BackendId &id, FactoryFunction factory)
std::unordered_set< BackendId > BackendIdSet
Definition: BackendId.hpp:191
std::unordered_map< BackendId, FactoryFunction > FactoryStorage
BackendRegistry & BackendRegistryInstance()
Copyright (c) 2021 ARM Limited and Contributors.
std::unique_ptr< IBackendInternal > IBackendInternalUniquePtr