Matteo Martincigh | d73cecb | 2019-07-24 09:15:00 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Matteo Martincigh | ac60d28 | 2019-07-25 15:25:44 +0100 | [diff] [blame] | 8 | #include "IBackendInternal.hpp" |
Matteo Martincigh | e54aa06 | 2019-08-05 14:12:11 +0100 | [diff] [blame] | 9 | #include "DynamicBackend.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 | |
Matteo Martincigh | d73cecb | 2019-07-24 09:15:00 +0100 | [diff] [blame] | 13 | #include <boost/format.hpp> |
| 14 | |
Rob Hughes | 91e1d89 | 2019-08-23 10:11:58 +0100 | [diff] [blame] | 15 | #include <string> |
| 16 | #include <vector> |
| 17 | #if defined(__unix__) |
| 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); |
Matteo Martincigh | e54aa06 | 2019-08-05 14:12:11 +0100 | [diff] [blame] | 45 | |
Matteo Martincigh | ac60d28 | 2019-07-25 15:25:44 +0100 | [diff] [blame] | 46 | protected: |
Matteo Martincigh | e7d4498 | 2019-08-05 12:16:47 +0100 | [diff] [blame] | 47 | /// Protected methods for testing purposes |
Matteo Martincigh | ac60d28 | 2019-07-25 15:25:44 +0100 | [diff] [blame] | 48 | static bool IsBackendCompatibleImpl(const BackendVersion& backendApiVersion, const BackendVersion& backendVersion); |
Matteo Martincigh | e7d4498 | 2019-08-05 12:16:47 +0100 | [diff] [blame] | 49 | static std::vector<std::string> GetBackendPathsImpl(const std::string& backendPaths); |
Matteo Martincigh | 8953390 | 2019-08-15 12:08:06 +0100 | [diff] [blame] | 50 | static BackendIdSet RegisterDynamicBackendsImpl(BackendRegistry& backendRegistry, |
| 51 | const std::vector<DynamicBackendPtr>& dynamicBackends); |
Matteo Martincigh | ac60d28 | 2019-07-25 15:25:44 +0100 | [diff] [blame] | 52 | |
Matteo Martincigh | d73cecb | 2019-07-24 09:15:00 +0100 | [diff] [blame] | 53 | private: |
| 54 | static std::string GetDlError(); |
| 55 | |
| 56 | /// This class is to hold utility functions only |
| 57 | DynamicBackendUtils() = delete; |
| 58 | }; |
| 59 | |
| 60 | template<typename EntryPointType> |
| 61 | EntryPointType DynamicBackendUtils::GetEntryPoint(const void* sharedObjectHandle, const char* symbolName) |
| 62 | { |
Rob Hughes | 91e1d89 | 2019-08-23 10:11:58 +0100 | [diff] [blame] | 63 | #if defined(__unix__) |
Matteo Martincigh | d73cecb | 2019-07-24 09:15:00 +0100 | [diff] [blame] | 64 | if (sharedObjectHandle == nullptr) |
| 65 | { |
| 66 | throw RuntimeException("GetEntryPoint error: invalid handle"); |
| 67 | } |
| 68 | |
| 69 | if (symbolName == nullptr) |
| 70 | { |
| 71 | throw RuntimeException("GetEntryPoint error: invalid symbol"); |
| 72 | } |
| 73 | |
| 74 | auto entryPoint = reinterpret_cast<EntryPointType>(dlsym(const_cast<void*>(sharedObjectHandle), symbolName)); |
| 75 | if (!entryPoint) |
| 76 | { |
Matteo Martincigh | b19d2e9 | 2019-07-25 14:04:40 +0100 | [diff] [blame] | 77 | throw RuntimeException(boost::str(boost::format("GetEntryPoint error: %1%") % GetDlError())); |
Matteo Martincigh | d73cecb | 2019-07-24 09:15:00 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | return entryPoint; |
Rob Hughes | 91e1d89 | 2019-08-23 10:11:58 +0100 | [diff] [blame] | 81 | #else |
| 82 | throw RuntimeException("Dynamic backends not supported on this platform"); |
| 83 | #endif |
Matteo Martincigh | d73cecb | 2019-07-24 09:15:00 +0100 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | } // namespace armnn |