• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
10 
11 #include <memory>
12 #include <unordered_map>
13 #include <functional>
14 
15 namespace armnn
16 {
17 
18 namespace profiling
19 {
20     class ProfilingService;
21 }
22 class IBackendInternal;
23 using IBackendInternalUniquePtr = std::unique_ptr<IBackendInternal>;
24 
25 class BackendRegistry
26 {
27 public:
28     using PointerType = IBackendInternalUniquePtr;
29     using FactoryFunction = std::function<PointerType()>;
30 
31     void Register(const BackendId& id, FactoryFunction factory);
32     bool IsBackendRegistered(const BackendId& id) const;
33     FactoryFunction GetFactory(const BackendId& id) const;
34     size_t Size() const;
35     BackendIdSet GetBackendIds() const;
36     std::string GetBackendIdsAsString() const;
37     void SetProfilingService(armnn::Optional<profiling::ProfilingService&> profilingService);
38 
BackendRegistry()39     BackendRegistry() {}
~BackendRegistry()40     virtual ~BackendRegistry() {}
41 
42     struct StaticRegistryInitializer
43     {
StaticRegistryInitializerarmnn::BackendRegistry::StaticRegistryInitializer44         StaticRegistryInitializer(BackendRegistry& instance,
45                                   const BackendId& id,
46                                   FactoryFunction factory)
47         {
48             instance.Register(id, factory);
49         }
50     };
51 
52     void Deregister(const BackendId& id);
53 
54 protected:
55     using FactoryStorage = std::unordered_map<BackendId, FactoryFunction>;
56 
57     /// For testing only
58     static void Swap(BackendRegistry& instance, FactoryStorage& other);
59 
60 private:
61     BackendRegistry(const BackendRegistry&) = delete;
62     BackendRegistry& operator=(const BackendRegistry&) = delete;
63 
64     FactoryStorage m_Factories;
65     armnn::Optional<profiling::ProfilingService&> m_ProfilingService;
66 };
67 
68 BackendRegistry& BackendRegistryInstance();
69 
70 } // namespace armnn
71