ArmNN
 24.11
BackendRegistry.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017, 2022, 2024 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  void AddMappedGpuBackend(const BackendId& id);
54 
56  virtual ~BackendRegistry() {}
57 
59  {
61  const BackendId& id,
62  FactoryFunction factory)
63  {
64  instance.Register(id, factory);
65  }
66  };
67 
68  void Deregister(const BackendId& id);
69  void DeregisterAllocator(const BackendId &id);
71 
72 protected:
73  using FactoryStorage = std::unordered_map<BackendId, FactoryFunction>;
74 
75  /// For testing only
76  static void Swap(BackendRegistry& instance, FactoryStorage& other);
77 
78 private:
79  BackendRegistry(const BackendRegistry&) = delete;
80  BackendRegistry& operator=(const BackendRegistry&) = delete;
81 
82  FactoryStorage m_Factories;
84  std::unordered_map<BackendId, std::shared_ptr<ICustomAllocator>> m_CustomMemoryAllocatorMap;
85  std::unordered_map<BackendId, std::shared_ptr<IMemoryOptimizerStrategy>> m_MemoryOptimizerStrategyMap;
86  BackendIdVector m_MappedGpuBackends = {};
87 };
88 
89 BackendRegistry& BackendRegistryInstance();
90 
91 } // namespace armnn
armnn::BackendRegistry::~BackendRegistry
virtual ~BackendRegistry()
Definition: BackendRegistry.hpp:56
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:55
armnn::BackendRegistry::StaticRegistryInitializer::StaticRegistryInitializer
StaticRegistryInitializer(BackendRegistry &instance, const BackendId &id, FactoryFunction factory)
Definition: BackendRegistry.hpp:60
armnn::BackendRegistry::AddMappedGpuBackend
void AddMappedGpuBackend(const BackendId &id)
Definition: BackendRegistry.cpp:155
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:58
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::GetMappedGpuBackends
BackendIdVector GetMappedGpuBackends()
Definition: BackendRegistry.cpp:160
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:73
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::BackendIdVector
std::vector< BackendId > BackendIdVector
Definition: BackendId.hpp:192
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