Matteo Martincigh | d73cecb | 2019-07-24 09:15:00 +0100 | [diff] [blame] | 1 | // |
Jim Flynn | 6da6a45 | 2020-07-14 14:26:27 +0100 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. |
Matteo Martincigh | d73cecb | 2019-07-24 09:15:00 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Matteo Martincigh | e5b8eb9 | 2019-11-28 15:45:42 +0000 | [diff] [blame] | 8 | #include <armnn/backends/DynamicBackend.hpp> |
| 9 | #include <armnn/backends/IBackendInternal.hpp> |
Matteo Martincigh | ac60d28 | 2019-07-25 15:25:44 +0100 | [diff] [blame] | 10 | |
Matteo Martincigh | d73cecb | 2019-07-24 09:15:00 +0100 | [diff] [blame] | 11 | #include <armnn/Exceptions.hpp> |
| 12 | |
James Ward | 47fce87 | 2020-09-10 11:57:28 +0100 | [diff] [blame] | 13 | #include <fmt/format.h> |
Matteo Martincigh | d73cecb | 2019-07-24 09:15:00 +0100 | [diff] [blame] | 14 | |
Rob Hughes | 91e1d89 | 2019-08-23 10:11:58 +0100 | [diff] [blame] | 15 | #include <string> |
| 16 | #include <vector> |
Jim Flynn | 6da6a45 | 2020-07-14 14:26:27 +0100 | [diff] [blame] | 17 | #if defined(__unix__) || defined(__APPLE__) |
Rob Hughes | 91e1d89 | 2019-08-23 10:11:58 +0100 | [diff] [blame] | 18 | #include <dlfcn.h> |
| 19 | #endif |
| 20 | |
Matteo Martincigh | e7d4498 | 2019-08-05 12:16:47 +0100 | [diff] [blame] | 21 | #if !defined(DYNAMIC_BACKEND_PATHS) |
| 22 | #define DYNAMIC_BACKEND_PATHS "" |
| 23 | #endif |
| 24 | |
Matteo Martincigh | d73cecb | 2019-07-24 09:15:00 +0100 | [diff] [blame] | 25 | namespace armnn |
| 26 | { |
| 27 | |
| 28 | class DynamicBackendUtils |
| 29 | { |
| 30 | public: |
| 31 | static void* OpenHandle(const std::string& sharedObjectPath); |
| 32 | static void CloseHandle(const void* sharedObjectHandle); |
| 33 | |
| 34 | template<typename EntryPointType> |
| 35 | static EntryPointType GetEntryPoint(const void* sharedObjectHandle, const char* symbolName); |
| 36 | |
Matteo Martincigh | ac60d28 | 2019-07-25 15:25:44 +0100 | [diff] [blame] | 37 | static bool IsBackendCompatible(const BackendVersion& backendVersion); |
| 38 | |
Matteo Martincigh | e7d4498 | 2019-08-05 12:16:47 +0100 | [diff] [blame] | 39 | static std::vector<std::string> GetBackendPaths(const std::string& overrideBackendPath = ""); |
| 40 | static bool IsPathValid(const std::string& path); |
Jan Eilers | 4a539fc | 2019-07-25 17:08:37 +0100 | [diff] [blame] | 41 | static std::vector<std::string> GetSharedObjects(const std::vector<std::string>& backendPaths); |
Matteo Martincigh | e7d4498 | 2019-08-05 12:16:47 +0100 | [diff] [blame] | 42 | |
Matteo Martincigh | e54aa06 | 2019-08-05 14:12:11 +0100 | [diff] [blame] | 43 | static std::vector<DynamicBackendPtr> CreateDynamicBackends(const std::vector<std::string>& sharedObjects); |
Matteo Martincigh | 8953390 | 2019-08-15 12:08:06 +0100 | [diff] [blame] | 44 | static BackendIdSet RegisterDynamicBackends(const std::vector<DynamicBackendPtr>& dynamicBackends); |
Narumol Prangnawarat | 60a20fb | 2019-12-09 17:24:41 +0000 | [diff] [blame] | 45 | static void DeregisterDynamicBackends(const BackendIdSet& dynamicBackends); |
Matteo Martincigh | e54aa06 | 2019-08-05 14:12:11 +0100 | [diff] [blame] | 46 | |
Matteo Martincigh | ac60d28 | 2019-07-25 15:25:44 +0100 | [diff] [blame] | 47 | protected: |
Matteo Martincigh | e7d4498 | 2019-08-05 12:16:47 +0100 | [diff] [blame] | 48 | /// Protected methods for testing purposes |
Matteo Martincigh | ac60d28 | 2019-07-25 15:25:44 +0100 | [diff] [blame] | 49 | static bool IsBackendCompatibleImpl(const BackendVersion& backendApiVersion, const BackendVersion& backendVersion); |
Matteo Martincigh | e7d4498 | 2019-08-05 12:16:47 +0100 | [diff] [blame] | 50 | static std::vector<std::string> GetBackendPathsImpl(const std::string& backendPaths); |
Matteo Martincigh | 8953390 | 2019-08-15 12:08:06 +0100 | [diff] [blame] | 51 | static BackendIdSet RegisterDynamicBackendsImpl(BackendRegistry& backendRegistry, |
| 52 | const std::vector<DynamicBackendPtr>& dynamicBackends); |
Matteo Martincigh | ac60d28 | 2019-07-25 15:25:44 +0100 | [diff] [blame] | 53 | |
Matteo Martincigh | d73cecb | 2019-07-24 09:15:00 +0100 | [diff] [blame] | 54 | private: |
| 55 | static std::string GetDlError(); |
| 56 | |
| 57 | /// This class is to hold utility functions only |
| 58 | DynamicBackendUtils() = delete; |
| 59 | }; |
| 60 | |
| 61 | template<typename EntryPointType> |
| 62 | EntryPointType DynamicBackendUtils::GetEntryPoint(const void* sharedObjectHandle, const char* symbolName) |
| 63 | { |
Jim Flynn | 6da6a45 | 2020-07-14 14:26:27 +0100 | [diff] [blame] | 64 | #if defined(__unix__) || defined(__APPLE__) |
Matteo Martincigh | d73cecb | 2019-07-24 09:15:00 +0100 | [diff] [blame] | 65 | if (sharedObjectHandle == nullptr) |
| 66 | { |
| 67 | throw RuntimeException("GetEntryPoint error: invalid handle"); |
| 68 | } |
| 69 | |
| 70 | if (symbolName == nullptr) |
| 71 | { |
| 72 | throw RuntimeException("GetEntryPoint error: invalid symbol"); |
| 73 | } |
| 74 | |
| 75 | auto entryPoint = reinterpret_cast<EntryPointType>(dlsym(const_cast<void*>(sharedObjectHandle), symbolName)); |
| 76 | if (!entryPoint) |
| 77 | { |
James Ward | 47fce87 | 2020-09-10 11:57:28 +0100 | [diff] [blame] | 78 | throw RuntimeException(fmt::format("GetEntryPoint error: {}", GetDlError())); |
Matteo Martincigh | d73cecb | 2019-07-24 09:15:00 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | return entryPoint; |
Rob Hughes | 91e1d89 | 2019-08-23 10:11:58 +0100 | [diff] [blame] | 82 | #else |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame] | 83 | armnn::IgnoreUnused(sharedObjectHandle); |
| 84 | armnn::IgnoreUnused(symbolName); |
Rob Hughes | 91e1d89 | 2019-08-23 10:11:58 +0100 | [diff] [blame] | 85 | throw RuntimeException("Dynamic backends not supported on this platform"); |
| 86 | #endif |
Matteo Martincigh | d73cecb | 2019-07-24 09:15:00 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | } // namespace armnn |