ArmNN
 21.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>
8 #include <ProfilingService.hpp>
9 
10 namespace armnn
11 {
12 
14 {
15  static BackendRegistry instance;
16  return instance;
17 }
18 
20 {
21  if (m_Factories.find(id) != m_Factories.end())
22  {
24  std::string(id) + " already registered as IBackend factory",
25  CHECK_LOCATION());
26  }
27  m_Factories[id] = factory;
28 
29  if (m_ProfilingService.has_value())
30  {
31  if (m_ProfilingService.has_value() && m_ProfilingService.value().IsProfilingEnabled())
32  {
33  m_ProfilingService.value().IncrementCounterValue(armnn::profiling::REGISTERED_BACKENDS);
34  }
35  }
36 
37 }
38 
40 {
41  m_Factories.erase(id);
43 
44  if (m_ProfilingService.has_value() && m_ProfilingService.value().IsProfilingEnabled())
45  {
46  m_ProfilingService.value().IncrementCounterValue(armnn::profiling::UNREGISTERED_BACKENDS);
47  }
48 }
49 
51 {
52  return (m_Factories.find(id) != m_Factories.end());
53 }
54 
56 {
57  auto it = m_Factories.find(id);
58  if (it == m_Factories.end())
59  {
61  std::string(id) + " has no IBackend factory registered",
62  CHECK_LOCATION());
63  }
64 
65  return it->second;
66 }
67 
68 size_t BackendRegistry::Size() const
69 {
70  return m_Factories.size();
71 }
72 
74 {
75  BackendIdSet result;
76  for (const auto& it : m_Factories)
77  {
78  result.insert(it.first);
79  }
80  return result;
81 }
82 
84 {
85  static const std::string delimitator = ", ";
86 
87  std::stringstream output;
88  for (auto& backendId : GetBackendIds())
89  {
90  if (output.tellp() != std::streampos(0))
91  {
92  output << delimitator;
93  }
94  output << backendId;
95  }
96 
97  return output.str();
98 }
99 
101 {
102  std::swap(instance.m_Factories, other);
103 }
104 
106 {
107  m_ProfilingService = profilingService;
108 }
109 
110 void BackendRegistry::RegisterAllocator(const BackendId& id, std::shared_ptr<ICustomAllocator> alloc)
111 {
112  if (m_CustomMemoryAllocatorMap.find(id) != m_CustomMemoryAllocatorMap.end())
113  {
115  std::string(id) + " already has an allocator associated with it",
116  CHECK_LOCATION());
117  }
118  m_CustomMemoryAllocatorMap[id] = alloc;
119 }
120 
122 {
123  m_CustomMemoryAllocatorMap.erase(id);
124 }
125 
126 std::unordered_map<BackendId, std::shared_ptr<ICustomAllocator>> BackendRegistry::GetAllocators()
127 {
128  return m_CustomMemoryAllocatorMap;
129 }
130 
131 } // namespace armnn
FactoryFunction GetFactory(const BackendId &id) const
void DeregisterAllocator(const BackendId &id)
BackendIdSet GetBackendIds() const
bool IsBackendRegistered(const BackendId &id) const
std::function< PointerType()> FactoryFunction
void Register(const BackendId &id, FactoryFunction factory)
void swap(OriginsDescriptor &first, OriginsDescriptor &second)
std::unordered_set< BackendId > BackendIdSet
Definition: BackendId.hpp:191
static void Swap(BackendRegistry &instance, FactoryStorage &other)
For testing only.
std::unordered_map< BackendId, FactoryFunction > FactoryStorage
void RegisterAllocator(const BackendId &id, std::shared_ptr< ICustomAllocator > alloc)
BackendRegistry & BackendRegistryInstance()
Copyright (c) 2021 ARM Limited and Contributors.
std::string GetBackendIdsAsString() const
void SetProfilingService(armnn::Optional< profiling::ProfilingService &> profilingService)
#define CHECK_LOCATION()
Definition: Exceptions.hpp:197
std::unordered_map< BackendId, std::shared_ptr< ICustomAllocator > > GetAllocators()
void Deregister(const BackendId &id)