ArmNN
 24.08
BackendRegistry.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 #include <armnn/Exceptions.hpp>
9 
10 #include <client/include/IProfilingService.hpp>
11 
12 namespace armnn
13 {
14 
16 {
17  static BackendRegistry instance;
18  return instance;
19 }
20 
22 {
23  if (m_Factories.find(id) != m_Factories.end())
24  {
26  std::string(id) + " already registered as IBackend factory",
27  CHECK_LOCATION());
28  }
29  m_Factories[id] = factory;
30 
31  if (m_ProfilingService.has_value())
32  {
33  if (m_ProfilingService.has_value() && m_ProfilingService.value().IsProfilingEnabled())
34  {
35  m_ProfilingService.value().IncrementCounterValue(arm::pipe::REGISTERED_BACKENDS);
36  }
37  }
38 
39 }
40 
42 {
43  m_Factories.erase(id);
45 
46  if (m_ProfilingService.has_value() && m_ProfilingService.value().IsProfilingEnabled())
47  {
48  m_ProfilingService.value().IncrementCounterValue(arm::pipe::UNREGISTERED_BACKENDS);
49  }
50 }
51 
53 {
54  return (m_Factories.find(id) != m_Factories.end());
55 }
56 
58 {
59  auto it = m_Factories.find(id);
60  if (it == m_Factories.end())
61  {
63  std::string(id) + " has no IBackend factory registered",
64  CHECK_LOCATION());
65  }
66 
67  return it->second;
68 }
69 
70 size_t BackendRegistry::Size() const
71 {
72  return m_Factories.size();
73 }
74 
76 {
77  BackendIdSet result;
78  for (const auto& it : m_Factories)
79  {
80  result.insert(it.first);
81  }
82  return result;
83 }
84 
86 {
87  static const std::string delimitator = ", ";
88 
89  std::stringstream output;
90  for (auto& backendId : GetBackendIds())
91  {
92  if (output.tellp() != std::streampos(0))
93  {
94  output << delimitator;
95  }
96  output << backendId;
97  }
98 
99  return output.str();
100 }
101 
103 {
104  std::swap(instance.m_Factories, other);
105 }
106 
108 {
109  m_ProfilingService = profilingService;
110 }
111 
112 void BackendRegistry::RegisterAllocator(const BackendId& id, std::shared_ptr<ICustomAllocator> alloc)
113 {
114  if (m_CustomMemoryAllocatorMap.find(id) != m_CustomMemoryAllocatorMap.end())
115  {
117  std::string(id) + " already has an allocator associated with it",
118  CHECK_LOCATION());
119  }
120  m_CustomMemoryAllocatorMap[id] = alloc;
121 }
122 
124 {
125  m_CustomMemoryAllocatorMap.erase(id);
126 }
127 
128 std::unordered_map<BackendId, std::shared_ptr<ICustomAllocator>> BackendRegistry::GetAllocators()
129 {
130  return m_CustomMemoryAllocatorMap;
131 }
132 
134  std::shared_ptr<IMemoryOptimizerStrategy> strategy)
135 {
136  if (m_MemoryOptimizerStrategyMap.find(id) != m_MemoryOptimizerStrategyMap.end())
137  {
139  std::string(id) + " already has an memory optimizer strategy associated with it",
140  CHECK_LOCATION());
141  }
142  m_MemoryOptimizerStrategyMap[id] = strategy;
143 }
144 
146 {
147  m_MemoryOptimizerStrategyMap.erase(id);
148 }
149 
151 {
152  return m_MemoryOptimizerStrategyMap;
153 }
154 
155 } // namespace armnn
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
CHECK_LOCATION
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
armnn::swap
void swap(OriginsDescriptor &first, OriginsDescriptor &second)
Definition: Descriptors.cpp:357
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
BackendRegistry.hpp
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
armnn::BackendRegistry::Swap
static void Swap(BackendRegistry &instance, FactoryStorage &other)
For testing only.
Definition: BackendRegistry.cpp:102
armnn::BackendRegistryInstance
BackendRegistry & BackendRegistryInstance()
Definition: BackendRegistry.cpp:15
armnn::InvalidArgumentException
Definition: Exceptions.hpp:80
armnn::BackendRegistry::GetBackendIdsAsString
std::string GetBackendIdsAsString() const
Definition: BackendRegistry.cpp:85
armnn::BackendRegistry::IsBackendRegistered
bool IsBackendRegistered(const BackendId &id) const
Definition: BackendRegistry.cpp:52
ArmNNProfiling.hpp
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
Exceptions.hpp
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::OptionalReferenceSwitch::value
const T & value() const
Definition: Optional.hpp:146
armnn::OptionalBase::has_value
bool has_value() const noexcept
Definition: Optional.hpp:53
armnn::MemoryOptimizerStrategiesMapRef
std::unordered_map< BackendId, std::shared_ptr< IMemoryOptimizerStrategy > > MemoryOptimizerStrategiesMapRef
Definition: BackendRegistry.hpp:33