ArmNN
 25.11
Loading...
Searching...
No Matches
BackendRegistry.cpp
Go to the documentation of this file.
1//
2// Copyright © 2017, 2024 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
9
10#include <client/include/IProfilingService.hpp>
11
12namespace 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",
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",
65 }
66
67 return it->second;
68}
69
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
112void 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",
119 }
120 m_CustomMemoryAllocatorMap[id] = alloc;
121}
122
124{
125 m_CustomMemoryAllocatorMap.erase(id);
126}
127
128std::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",
141 }
142 m_MemoryOptimizerStrategyMap[id] = strategy;
143}
144
146{
147 m_MemoryOptimizerStrategyMap.erase(id);
148}
149
151{
152 return m_MemoryOptimizerStrategyMap;
153}
154
156{
157 m_MappedGpuBackends.insert(m_MappedGpuBackends.begin(), id);
158}
159
161{
162 return m_MappedGpuBackends;
163}
164
165} // namespace armnn
#define CHECK_LOCATION()
void Register(const BackendId &id, FactoryFunction factory)
void DeregisterMemoryOptimizerStrategy(const BackendId &id)
std::unordered_map< BackendId, FactoryFunction > FactoryStorage
void AddMappedGpuBackend(const BackendId &id)
bool IsBackendRegistered(const BackendId &id) const
void Deregister(const BackendId &id)
static void Swap(BackendRegistry &instance, FactoryStorage &other)
For testing only.
void SetProfilingService(armnn::Optional< arm::pipe::IProfilingService & > profilingService)
BackendIdSet GetBackendIds() const
std::unordered_map< BackendId, std::shared_ptr< ICustomAllocator > > GetAllocators()
void RegisterAllocator(const BackendId &id, std::shared_ptr< ICustomAllocator > alloc)
MemoryOptimizerStrategiesMapRef GetMemoryOptimizerStrategies()
std::function< PointerType()> FactoryFunction
void RegisterMemoryOptimizerStrategy(const BackendId &id, std::shared_ptr< IMemoryOptimizerStrategy > strategy)
std::string GetBackendIdsAsString() const
BackendIdVector GetMappedGpuBackends()
void DeregisterAllocator(const BackendId &id)
FactoryFunction GetFactory(const BackendId &id) const
Copyright (c) 2021 ARM Limited and Contributors.
std::unordered_set< BackendId > BackendIdSet
std::unordered_map< BackendId, std::shared_ptr< IMemoryOptimizerStrategy > > MemoryOptimizerStrategiesMapRef
BackendRegistry & BackendRegistryInstance()
std::vector< BackendId > BackendIdVector