blob: f4cdd4db0c199a31fae62a7c101709ac86f50e11 [file] [log] [blame]
Matteo Martincighd73cecb2019-07-24 09:15:00 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
Matteo Martincighe5b8eb92019-11-28 15:45:42 +00008#include <armnn/backends/DynamicBackend.hpp>
9#include <armnn/backends/IBackendInternal.hpp>
Matteo Martincighac60d282019-07-25 15:25:44 +010010
Matteo Martincighd73cecb2019-07-24 09:15:00 +010011#include <armnn/Exceptions.hpp>
12
Matteo Martincighd73cecb2019-07-24 09:15:00 +010013#include <boost/format.hpp>
14
Rob Hughes91e1d892019-08-23 10:11:58 +010015#include <string>
16#include <vector>
17#if defined(__unix__)
18#include <dlfcn.h>
19#endif
20
Matteo Martincighe7d44982019-08-05 12:16:47 +010021#if !defined(DYNAMIC_BACKEND_PATHS)
22#define DYNAMIC_BACKEND_PATHS ""
23#endif
24
Matteo Martincighd73cecb2019-07-24 09:15:00 +010025namespace armnn
26{
27
28class DynamicBackendUtils
29{
30public:
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 Martincighac60d282019-07-25 15:25:44 +010037 static bool IsBackendCompatible(const BackendVersion& backendVersion);
38
Matteo Martincighe7d44982019-08-05 12:16:47 +010039 static std::vector<std::string> GetBackendPaths(const std::string& overrideBackendPath = "");
40 static bool IsPathValid(const std::string& path);
Jan Eilers4a539fc2019-07-25 17:08:37 +010041 static std::vector<std::string> GetSharedObjects(const std::vector<std::string>& backendPaths);
Matteo Martincighe7d44982019-08-05 12:16:47 +010042
Matteo Martincighe54aa062019-08-05 14:12:11 +010043 static std::vector<DynamicBackendPtr> CreateDynamicBackends(const std::vector<std::string>& sharedObjects);
Matteo Martincigh89533902019-08-15 12:08:06 +010044 static BackendIdSet RegisterDynamicBackends(const std::vector<DynamicBackendPtr>& dynamicBackends);
Narumol Prangnawarat60a20fb2019-12-09 17:24:41 +000045 static void DeregisterDynamicBackends(const BackendIdSet& dynamicBackends);
Matteo Martincighe54aa062019-08-05 14:12:11 +010046
Matteo Martincighac60d282019-07-25 15:25:44 +010047protected:
Matteo Martincighe7d44982019-08-05 12:16:47 +010048 /// Protected methods for testing purposes
Matteo Martincighac60d282019-07-25 15:25:44 +010049 static bool IsBackendCompatibleImpl(const BackendVersion& backendApiVersion, const BackendVersion& backendVersion);
Matteo Martincighe7d44982019-08-05 12:16:47 +010050 static std::vector<std::string> GetBackendPathsImpl(const std::string& backendPaths);
Matteo Martincigh89533902019-08-15 12:08:06 +010051 static BackendIdSet RegisterDynamicBackendsImpl(BackendRegistry& backendRegistry,
52 const std::vector<DynamicBackendPtr>& dynamicBackends);
Matteo Martincighac60d282019-07-25 15:25:44 +010053
Matteo Martincighd73cecb2019-07-24 09:15:00 +010054private:
55 static std::string GetDlError();
56
57 /// This class is to hold utility functions only
58 DynamicBackendUtils() = delete;
59};
60
61template<typename EntryPointType>
62EntryPointType DynamicBackendUtils::GetEntryPoint(const void* sharedObjectHandle, const char* symbolName)
63{
Rob Hughes91e1d892019-08-23 10:11:58 +010064#if defined(__unix__)
Matteo Martincighd73cecb2019-07-24 09:15:00 +010065 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 {
Matteo Martincighb19d2e92019-07-25 14:04:40 +010078 throw RuntimeException(boost::str(boost::format("GetEntryPoint error: %1%") % GetDlError()));
Matteo Martincighd73cecb2019-07-24 09:15:00 +010079 }
80
81 return entryPoint;
Rob Hughes91e1d892019-08-23 10:11:58 +010082#else
83 throw RuntimeException("Dynamic backends not supported on this platform");
84#endif
Matteo Martincighd73cecb2019-07-24 09:15:00 +010085}
86
87} // namespace armnn