ArmNN
 20.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  if (profiling::ProfilingService::Instance().IsProfilingEnabled())
28  {
29  profiling::ProfilingService::Instance().IncrementCounterValue(armnn::profiling::REGISTERED_BACKENDS);
30  }
31  m_Factories[id] = factory;
32 }
33 
35 {
36  if (profiling::ProfilingService::Instance().IsProfilingEnabled())
37  {
38  profiling::ProfilingService::Instance().IncrementCounterValue(armnn::profiling::UNREGISTERED_BACKENDS);
39  }
40  m_Factories.erase(id);
41 }
42 
44 {
45  return (m_Factories.find(id) != m_Factories.end());
46 }
47 
49 {
50  auto it = m_Factories.find(id);
51  if (it == m_Factories.end())
52  {
54  std::string(id) + " has no IBackend factory registered",
55  CHECK_LOCATION());
56  }
57 
58  return it->second;
59 }
60 
61 size_t BackendRegistry::Size() const
62 {
63  return m_Factories.size();
64 }
65 
67 {
68  BackendIdSet result;
69  for (const auto& it : m_Factories)
70  {
71  result.insert(it.first);
72  }
73  return result;
74 }
75 
77 {
78  static const std::string delimitator = ", ";
79 
80  std::stringstream output;
81  for (auto& backendId : GetBackendIds())
82  {
83  if (output.tellp() != std::streampos(0))
84  {
85  output << delimitator;
86  }
87  output << backendId;
88  }
89 
90  return output.str();
91 }
92 
94 {
95  std::swap(instance.m_Factories, other);
96 }
97 
98 
99 } // namespace armnn
static ProfilingService & Instance()
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) 2020 ARM Limited.
std::string GetBackendIdsAsString() const
uint32_t IncrementCounterValue(uint16_t counterUid) override
#define CHECK_LOCATION()
Definition: Exceptions.hpp:192
void Deregister(const BackendId &id)