ArmNN
 21.11
BackendRegistry.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #include <armnn/Types.hpp>
8 #include <armnn/BackendId.hpp>
9 #include <armnn/Optional.hpp>
12 
13 #include <algorithm>
14 #include <memory>
15 #include <unordered_map>
16 #include <functional>
17 
18 namespace armnn
19 {
20 
21 namespace profiling
22 {
23  class ProfilingService;
24 }
25 class IBackendInternal;
26 using IBackendInternalUniquePtr = std::unique_ptr<IBackendInternal>;
27 using MemoryOptimizerStrategiesMapRef = std::unordered_map<BackendId, std::shared_ptr<IMemoryOptimizerStrategy>>;
28 
30 {
31 public:
33  using FactoryFunction = std::function<PointerType()>;
34 
35  void Register(const BackendId& id, FactoryFunction factory);
36  bool IsBackendRegistered(const BackendId& id) const;
37  FactoryFunction GetFactory(const BackendId& id) const;
38  size_t Size() const;
39  BackendIdSet GetBackendIds() const;
40  std::string GetBackendIdsAsString() const;
41  void SetProfilingService(armnn::Optional<profiling::ProfilingService&> profilingService);
42  void RegisterAllocator(const BackendId& id, std::shared_ptr<ICustomAllocator> alloc);
43  std::unordered_map<BackendId, std::shared_ptr<ICustomAllocator>> GetAllocators();
44  void RegisterMemoryOptimizerStrategy(const BackendId& id, std::shared_ptr<IMemoryOptimizerStrategy> strategy);
45  MemoryOptimizerStrategiesMapRef GetMemoryOptimizerStrategies();
46 
48  virtual ~BackendRegistry() {}
49 
51  {
53  const BackendId& id,
54  FactoryFunction factory)
55  {
56  instance.Register(id, factory);
57  }
58  };
59 
60  void Deregister(const BackendId& id);
61  void DeregisterAllocator(const BackendId &id);
62  void DeregisterMemoryOptimizerStrategy(const BackendId &id);
63 
64 protected:
65  using FactoryStorage = std::unordered_map<BackendId, FactoryFunction>;
66 
67  /// For testing only
68  static void Swap(BackendRegistry& instance, FactoryStorage& other);
69 
70 private:
71  BackendRegistry(const BackendRegistry&) = delete;
72  BackendRegistry& operator=(const BackendRegistry&) = delete;
73 
74  FactoryStorage m_Factories;
76  std::unordered_map<BackendId, std::shared_ptr<ICustomAllocator>> m_CustomMemoryAllocatorMap;
77  std::unordered_map<BackendId, std::shared_ptr<IMemoryOptimizerStrategy>> m_MemoryOptimizerStrategyMap;
78 };
79 
81 
82 } // namespace armnn
StaticRegistryInitializer(BackendRegistry &instance, const BackendId &id, FactoryFunction factory)
IBackendInternalUniquePtr PointerType
std::function< PointerType()> FactoryFunction
void Register(const BackendId &id, FactoryFunction factory)
std::unordered_set< BackendId > BackendIdSet
Definition: BackendId.hpp:193
std::unordered_map< BackendId, FactoryFunction > FactoryStorage
std::unordered_map< BackendId, std::shared_ptr< IMemoryOptimizerStrategy > > MemoryOptimizerStrategiesMapRef
BackendRegistry & BackendRegistryInstance()
Copyright (c) 2021 ARM Limited and Contributors.
std::unique_ptr< IBackendInternal > IBackendInternalUniquePtr