ArmNN
 21.02
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);
42 
43  if (m_ProfilingService.has_value() && m_ProfilingService.value().IsProfilingEnabled())
44  {
45  m_ProfilingService.value().IncrementCounterValue(armnn::profiling::UNREGISTERED_BACKENDS);
46  }
47 }
48 
50 {
51  return (m_Factories.find(id) != m_Factories.end());
52 }
53 
55 {
56  auto it = m_Factories.find(id);
57  if (it == m_Factories.end())
58  {
60  std::string(id) + " has no IBackend factory registered",
61  CHECK_LOCATION());
62  }
63 
64  return it->second;
65 }
66 
67 size_t BackendRegistry::Size() const
68 {
69  return m_Factories.size();
70 }
71 
73 {
74  BackendIdSet result;
75  for (const auto& it : m_Factories)
76  {
77  result.insert(it.first);
78  }
79  return result;
80 }
81 
83 {
84  static const std::string delimitator = ", ";
85 
86  std::stringstream output;
87  for (auto& backendId : GetBackendIds())
88  {
89  if (output.tellp() != std::streampos(0))
90  {
91  output << delimitator;
92  }
93  output << backendId;
94  }
95 
96  return output.str();
97 }
98 
100 {
101  std::swap(instance.m_Factories, other);
102 }
103 
105 {
106  m_ProfilingService = profilingService;
107 }
108 
109 
110 } // namespace armnn
FactoryFunction GetFactory(const BackendId &id) const
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
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
void Deregister(const BackendId &id)