1 // 2 // Copyright © 2017 Arm Ltd. All rights reserved. 3 // SPDX-License-Identifier: MIT 4 // 5 6 #include <armnn/BackendRegistry.hpp> 7 #include <armnn/Exceptions.hpp> 8 #include <ProfilingService.hpp> 9 10 namespace armnn 11 { 12 BackendRegistryInstance()13BackendRegistry& BackendRegistryInstance() 14 { 15 static BackendRegistry instance; 16 return instance; 17 } 18 Register(const BackendId & id,BackendRegistry::FactoryFunction factory)19void BackendRegistry::Register(const BackendId& id, BackendRegistry::FactoryFunction factory) 20 { 21 if (m_Factories.find(id) != m_Factories.end()) 22 { 23 throw InvalidArgumentException( 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 Deregister(const BackendId & id)39void BackendRegistry::Deregister(const BackendId& id) 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 IsBackendRegistered(const BackendId & id) const49bool BackendRegistry::IsBackendRegistered(const BackendId& id) const 50 { 51 return (m_Factories.find(id) != m_Factories.end()); 52 } 53 GetFactory(const BackendId & id) const54BackendRegistry::FactoryFunction BackendRegistry::GetFactory(const BackendId& id) const 55 { 56 auto it = m_Factories.find(id); 57 if (it == m_Factories.end()) 58 { 59 throw InvalidArgumentException( 60 std::string(id) + " has no IBackend factory registered", 61 CHECK_LOCATION()); 62 } 63 64 return it->second; 65 } 66 Size() const67size_t BackendRegistry::Size() const 68 { 69 return m_Factories.size(); 70 } 71 GetBackendIds() const72BackendIdSet BackendRegistry::GetBackendIds() const 73 { 74 BackendIdSet result; 75 for (const auto& it : m_Factories) 76 { 77 result.insert(it.first); 78 } 79 return result; 80 } 81 GetBackendIdsAsString() const82std::string BackendRegistry::GetBackendIdsAsString() const 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 Swap(BackendRegistry & instance,BackendRegistry::FactoryStorage & other)99void BackendRegistry::Swap(BackendRegistry& instance, BackendRegistry::FactoryStorage& other) 100 { 101 std::swap(instance.m_Factories, other); 102 } 103 SetProfilingService(armnn::Optional<profiling::ProfilingService &> profilingService)104void BackendRegistry::SetProfilingService(armnn::Optional<profiling::ProfilingService&> profilingService) 105 { 106 m_ProfilingService = profilingService; 107 } 108 109 110 } // namespace armnn 111