blob: 2763b9d7d2156a09f18e59337d8574fc4f3d24ff [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);
Matteo Martincighe54aa062019-08-05 14:12:11 +010045
Matteo Martincighac60d282019-07-25 15:25:44 +010046protected:
Matteo Martincighe7d44982019-08-05 12:16:47 +010047 /// Protected methods for testing purposes
Matteo Martincighac60d282019-07-25 15:25:44 +010048 static bool IsBackendCompatibleImpl(const BackendVersion& backendApiVersion, const BackendVersion& backendVersion);
Matteo Martincighe7d44982019-08-05 12:16:47 +010049 static std::vector<std::string> GetBackendPathsImpl(const std::string& backendPaths);
Matteo Martincigh89533902019-08-15 12:08:06 +010050 static BackendIdSet RegisterDynamicBackendsImpl(BackendRegistry& backendRegistry,
51 const std::vector<DynamicBackendPtr>& dynamicBackends);
Matteo Martincighac60d282019-07-25 15:25:44 +010052
Matteo Martincighd73cecb2019-07-24 09:15:00 +010053private:
54 static std::string GetDlError();
55
56 /// This class is to hold utility functions only
57 DynamicBackendUtils() = delete;
58};
59
60template<typename EntryPointType>
61EntryPointType DynamicBackendUtils::GetEntryPoint(const void* sharedObjectHandle, const char* symbolName)
62{
Rob Hughes91e1d892019-08-23 10:11:58 +010063#if defined(__unix__)
Matteo Martincighd73cecb2019-07-24 09:15:00 +010064 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 Martincighb19d2e92019-07-25 14:04:40 +010077 throw RuntimeException(boost::str(boost::format("GetEntryPoint error: %1%") % GetDlError()));
Matteo Martincighd73cecb2019-07-24 09:15:00 +010078 }
79
80 return entryPoint;
Rob Hughes91e1d892019-08-23 10:11:58 +010081#else
82 throw RuntimeException("Dynamic backends not supported on this platform");
83#endif
Matteo Martincighd73cecb2019-07-24 09:15:00 +010084}
85
86} // namespace armnn