ArmNN
 25.11
Loading...
Searching...
No Matches
BackendRegistry.hpp
Go to the documentation of this file.
1//
2// Copyright © 2017, 2022, 2024 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5#pragma once
6
7#include <armnn/BackendId.hpp>
8#include <armnn/Optional.hpp>
9#include <memory>
10#include <unordered_map>
11#include <functional>
12#include <stddef.h>
13#include <string>
14
15namespace arm
16{
17namespace pipe
18{
19
20class IProfilingService;
21
22} // namespace arm
23} // namespace pipe
24
25namespace armnn
26{
27
28class IBackendInternal;
29class ICustomAllocator;
30class IMemoryOptimizerStrategy;
31
32using IBackendInternalUniquePtr = std::unique_ptr<IBackendInternal>;
33using MemoryOptimizerStrategiesMapRef = std::unordered_map<BackendId, std::shared_ptr<IMemoryOptimizerStrategy>>;
34
36{
37public:
39 using FactoryFunction = std::function<PointerType()>;
40
41 void Register(const BackendId& id, FactoryFunction factory);
42 bool IsBackendRegistered(const BackendId& id) const;
43 FactoryFunction GetFactory(const BackendId& id) const;
44 size_t Size() const;
46 std::string GetBackendIdsAsString() const;
48 void RegisterAllocator(const BackendId& id, std::shared_ptr<ICustomAllocator> alloc);
49 std::unordered_map<BackendId, std::shared_ptr<ICustomAllocator>> GetAllocators();
50 void RegisterMemoryOptimizerStrategy(const BackendId& id, std::shared_ptr<IMemoryOptimizerStrategy> strategy);
52 void AddMappedGpuBackend(const BackendId& id);
54
56 virtual ~BackendRegistry() {}
57
59 {
61 const BackendId& id,
62 FactoryFunction factory)
63 {
64 instance.Register(id, factory);
65 }
66 };
67
68 void Deregister(const BackendId& id);
69 void DeregisterAllocator(const BackendId &id);
71
72protected:
73 using FactoryStorage = std::unordered_map<BackendId, FactoryFunction>;
74
75 /// For testing only
76 static void Swap(BackendRegistry& instance, FactoryStorage& other);
77
78private:
79 BackendRegistry(const BackendRegistry&) = delete;
80 BackendRegistry& operator=(const BackendRegistry&) = delete;
81
82 FactoryStorage m_Factories;
84 std::unordered_map<BackendId, std::shared_ptr<ICustomAllocator>> m_CustomMemoryAllocatorMap;
85 std::unordered_map<BackendId, std::shared_ptr<IMemoryOptimizerStrategy>> m_MemoryOptimizerStrategyMap;
86 BackendIdVector m_MappedGpuBackends = {};
87};
88
89BackendRegistry& BackendRegistryInstance();
90
91} // namespace armnn
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)
IBackendInternalUniquePtr PointerType
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
std::unique_ptr< IBackendInternal > IBackendInternalUniquePtr
BackendRegistry & BackendRegistryInstance()
std::vector< BackendId > BackendIdVector
StaticRegistryInitializer(BackendRegistry &instance, const BackendId &id, FactoryFunction factory)