ArmNN
 22.11
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>
12 
13 #include <algorithm>
14 #include <memory>
15 #include <unordered_map>
16 #include <functional>
17 
18 namespace arm
19 {
20 namespace pipe
21 {
22 
23 class IProfilingService;
24 
25 } // namespace arm
26 } // namespace pipe
27 
28 namespace armnn
29 {
30 
31 class IBackendInternal;
32 using IBackendInternalUniquePtr = std::unique_ptr<IBackendInternal>;
33 using MemoryOptimizerStrategiesMapRef = std::unordered_map<BackendId, std::shared_ptr<IMemoryOptimizerStrategy>>;
34 
36 {
37 public:
39  using FactoryFunction = std::function<PointerType()>;
40 
41  void Register(const BackendId& id, FactoryFunction factory);
42  bool IsBackendRegistered(const BackendId& id) const;
43  FactoryFunction GetFactory(const BackendId& id) const;
44  size_t Size() const;
45  BackendIdSet GetBackendIds() const;
46  std::string GetBackendIdsAsString() const;
47  void SetProfilingService(armnn::Optional<arm::pipe::IProfilingService&> profilingService);
48  void RegisterAllocator(const BackendId& id, std::shared_ptr<ICustomAllocator> alloc);
49  std::unordered_map<BackendId, std::shared_ptr<ICustomAllocator>> GetAllocators();
50  void RegisterMemoryOptimizerStrategy(const BackendId& id, std::shared_ptr<IMemoryOptimizerStrategy> strategy);
51  MemoryOptimizerStrategiesMapRef GetMemoryOptimizerStrategies();
52 
54  virtual ~BackendRegistry() {}
55 
57  {
59  const BackendId& id,
60  FactoryFunction factory)
61  {
62  instance.Register(id, factory);
63  }
64  };
65 
66  void Deregister(const BackendId& id);
67  void DeregisterAllocator(const BackendId &id);
68  void DeregisterMemoryOptimizerStrategy(const BackendId &id);
69 
70 protected:
71  using FactoryStorage = std::unordered_map<BackendId, FactoryFunction>;
72 
73  /// For testing only
74  static void Swap(BackendRegistry& instance, FactoryStorage& other);
75 
76 private:
77  BackendRegistry(const BackendRegistry&) = delete;
78  BackendRegistry& operator=(const BackendRegistry&) = delete;
79 
80  FactoryStorage m_Factories;
82  std::unordered_map<BackendId, std::shared_ptr<ICustomAllocator>> m_CustomMemoryAllocatorMap;
83  std::unordered_map<BackendId, std::shared_ptr<IMemoryOptimizerStrategy>> m_MemoryOptimizerStrategyMap;
84 };
85 
87 
88 } // 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:193
std::unordered_map< BackendId, FactoryFunction > FactoryStorage
std::unordered_map< BackendId, std::shared_ptr< IMemoryOptimizerStrategy > > MemoryOptimizerStrategiesMapRef
BackendRegistry & BackendRegistryInstance()
Copyright (c) 2021 ARM Limited and Contributors.
std::unique_ptr< IBackendInternal > IBackendInternalUniquePtr