ArmNN
 24.02
BackendRegistry.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017,2022 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #include <armnn/BackendId.hpp>
8 #include <armnn/Optional.hpp>
9 #include <memory>
10 #include <unordered_map>
11 #include <functional>
12 #include <stddef.h>
13 #include <string>
14 
15 namespace arm
16 {
17 namespace pipe
18 {
19 
20 class IProfilingService;
21 
22 } // namespace arm
23 } // namespace pipe
24 
25 namespace armnn
26 {
27 
28 class IBackendInternal;
29 class ICustomAllocator;
30 class IMemoryOptimizerStrategy;
31 
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;
46  std::string GetBackendIdsAsString() const;
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);
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);
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
armnn::BackendRegistry::~BackendRegistry
virtual ~BackendRegistry()
Definition: BackendRegistry.hpp:54
armnn::BackendRegistry::PointerType
IBackendInternalUniquePtr PointerType
Definition: BackendRegistry.hpp:38
arm
Definition: BackendRegistry.hpp:15
armnn::Optional< arm::pipe::IProfilingService & >
armnn::BackendRegistry::Deregister
void Deregister(const BackendId &id)
Definition: BackendRegistry.cpp:41
armnn::BackendIdSet
std::unordered_set< BackendId > BackendIdSet
Definition: BackendId.hpp:193
BackendId.hpp
armnn::BackendRegistry::RegisterMemoryOptimizerStrategy
void RegisterMemoryOptimizerStrategy(const BackendId &id, std::shared_ptr< IMemoryOptimizerStrategy > strategy)
Definition: BackendRegistry.cpp:133
armnn::BackendRegistry::GetBackendIds
BackendIdSet GetBackendIds() const
Definition: BackendRegistry.cpp:75
armnn::BackendRegistry::GetMemoryOptimizerStrategies
MemoryOptimizerStrategiesMapRef GetMemoryOptimizerStrategies()
Definition: BackendRegistry.cpp:150
armnn::BackendRegistry
Definition: BackendRegistry.hpp:35
armnn::BackendRegistry::FactoryFunction
std::function< PointerType()> FactoryFunction
Definition: BackendRegistry.hpp:39
armnn::BackendRegistry::SetProfilingService
void SetProfilingService(armnn::Optional< arm::pipe::IProfilingService & > profilingService)
Definition: BackendRegistry.cpp:107
Optional.hpp
armnn::BackendRegistry::Swap
static void Swap(BackendRegistry &instance, FactoryStorage &other)
For testing only.
Definition: BackendRegistry.cpp:102
armnn::BackendRegistry::BackendRegistry
BackendRegistry()
Definition: BackendRegistry.hpp:53
armnn::BackendRegistry::StaticRegistryInitializer::StaticRegistryInitializer
StaticRegistryInitializer(BackendRegistry &instance, const BackendId &id, FactoryFunction factory)
Definition: BackendRegistry.hpp:58
armnn::BackendRegistryInstance
BackendRegistry & BackendRegistryInstance()
Definition: BackendRegistry.cpp:15
armnn::BackendRegistry::GetBackendIdsAsString
std::string GetBackendIdsAsString() const
Definition: BackendRegistry.cpp:85
armnn::BackendRegistry::IsBackendRegistered
bool IsBackendRegistered(const BackendId &id) const
Definition: BackendRegistry.cpp:52
armnn::BackendRegistry::StaticRegistryInitializer
Definition: BackendRegistry.hpp:56
armnn::BackendRegistry::GetFactory
FactoryFunction GetFactory(const BackendId &id) const
Definition: BackendRegistry.cpp:57
armnn::BackendRegistry::DeregisterMemoryOptimizerStrategy
void DeregisterMemoryOptimizerStrategy(const BackendId &id)
Definition: BackendRegistry.cpp:145
armnn::BackendRegistry::RegisterAllocator
void RegisterAllocator(const BackendId &id, std::shared_ptr< ICustomAllocator > alloc)
Definition: BackendRegistry.cpp:112
armnn::BackendRegistry::Register
void Register(const BackendId &id, FactoryFunction factory)
Definition: BackendRegistry.cpp:21
armnn::BackendRegistry::DeregisterAllocator
void DeregisterAllocator(const BackendId &id)
Definition: BackendRegistry.cpp:123
armnn::BackendRegistry::FactoryStorage
std::unordered_map< BackendId, FactoryFunction > FactoryStorage
Definition: BackendRegistry.hpp:71
armnn::BackendRegistry::GetAllocators
std::unordered_map< BackendId, std::shared_ptr< ICustomAllocator > > GetAllocators()
Definition: BackendRegistry.cpp:128
armnn::BackendId
Definition: BackendId.hpp:75
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::BackendRegistry::Size
size_t Size() const
Definition: BackendRegistry.cpp:70
armnn::IBackendInternalUniquePtr
std::unique_ptr< IBackendInternal > IBackendInternalUniquePtr
Definition: BackendRegistry.hpp:32
armnn::MemoryOptimizerStrategiesMapRef
std::unordered_map< BackendId, std::shared_ptr< IMemoryOptimizerStrategy > > MemoryOptimizerStrategiesMapRef
Definition: BackendRegistry.hpp:33