David Beck | 32cbb0c | 2018-10-09 15:46:08 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | #pragma once |
| 6 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 7 | #include <armnn/Types.hpp> |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 8 | #include <armnn/BackendId.hpp> |
Finn Williams | 45a7362 | 2020-05-15 18:41:05 +0100 | [diff] [blame] | 9 | #include <armnn/Optional.hpp> |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 10 | |
| 11 | #include <memory> |
| 12 | #include <unordered_map> |
jimfly01 | 07bb521 | 2018-11-14 14:52:40 +0000 | [diff] [blame] | 13 | #include <functional> |
David Beck | 32cbb0c | 2018-10-09 15:46:08 +0100 | [diff] [blame] | 14 | |
| 15 | namespace armnn |
| 16 | { |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 17 | |
Finn Williams | 45a7362 | 2020-05-15 18:41:05 +0100 | [diff] [blame] | 18 | namespace profiling |
| 19 | { |
| 20 | class ProfilingService; |
| 21 | } |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 22 | class IBackendInternal; |
| 23 | using IBackendInternalUniquePtr = std::unique_ptr<IBackendInternal>; |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 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; |
Finn Williams | 45a7362 | 2020-05-15 18:41:05 +0100 | [diff] [blame] | 37 | void SetProfilingService(armnn::Optional<profiling::ProfilingService&> profilingService); |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 38 | |
| 39 | BackendRegistry() {} |
| 40 | virtual ~BackendRegistry() {} |
| 41 | |
| 42 | struct StaticRegistryInitializer |
| 43 | { |
| 44 | StaticRegistryInitializer(BackendRegistry& instance, |
| 45 | const BackendId& id, |
| 46 | FactoryFunction factory) |
| 47 | { |
| 48 | instance.Register(id, factory); |
| 49 | } |
| 50 | }; |
| 51 | |
Narumol Prangnawarat | 60a20fb | 2019-12-09 17:24:41 +0000 | [diff] [blame] | 52 | void Deregister(const BackendId& id); |
| 53 | |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 54 | protected: |
| 55 | using FactoryStorage = std::unordered_map<BackendId, FactoryFunction>; |
| 56 | |
Ryan OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 57 | /// For testing only |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 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; |
Finn Williams | 45a7362 | 2020-05-15 18:41:05 +0100 | [diff] [blame] | 65 | armnn::Optional<profiling::ProfilingService&> m_ProfilingService; |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 66 | }; |
David Beck | 32cbb0c | 2018-10-09 15:46:08 +0100 | [diff] [blame] | 67 | |
David Beck | 3e9e115 | 2018-10-17 14:17:50 +0100 | [diff] [blame] | 68 | BackendRegistry& BackendRegistryInstance(); |
| 69 | |
jimfly01 | 07bb521 | 2018-11-14 14:52:40 +0000 | [diff] [blame] | 70 | } // namespace armnn |