blob: 187b0b1eab83f08ede8c2038571d3490b82c89e7 [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 Martincighac60d282019-07-25 15:25:44 +01008#include "IBackendInternal.hpp"
Matteo Martincighe54aa062019-08-05 14:12:11 +01009#include "DynamicBackend.hpp"
Matteo Martincighac60d282019-07-25 15:25:44 +010010
Matteo Martincighd73cecb2019-07-24 09:15:00 +010011#include <armnn/Exceptions.hpp>
12
13#include <string>
14#include <dlfcn.h>
Matteo Martincighe7d44982019-08-05 12:16:47 +010015#include <vector>
Matteo Martincighd73cecb2019-07-24 09:15:00 +010016
17#include <boost/format.hpp>
18
Matteo Martincighe7d44982019-08-05 12:16:47 +010019#if !defined(DYNAMIC_BACKEND_PATHS)
20#define DYNAMIC_BACKEND_PATHS ""
21#endif
22
Matteo Martincighd73cecb2019-07-24 09:15:00 +010023namespace armnn
24{
25
26class DynamicBackendUtils
27{
28public:
29 static void* OpenHandle(const std::string& sharedObjectPath);
30 static void CloseHandle(const void* sharedObjectHandle);
31
32 template<typename EntryPointType>
33 static EntryPointType GetEntryPoint(const void* sharedObjectHandle, const char* symbolName);
34
Matteo Martincighac60d282019-07-25 15:25:44 +010035 static bool IsBackendCompatible(const BackendVersion& backendVersion);
36
Matteo Martincighe7d44982019-08-05 12:16:47 +010037 static std::vector<std::string> GetBackendPaths(const std::string& overrideBackendPath = "");
38 static bool IsPathValid(const std::string& path);
Jan Eilers4a539fc2019-07-25 17:08:37 +010039 static std::vector<std::string> GetSharedObjects(const std::vector<std::string>& backendPaths);
Matteo Martincighe7d44982019-08-05 12:16:47 +010040
Matteo Martincighe54aa062019-08-05 14:12:11 +010041 static std::vector<DynamicBackendPtr> CreateDynamicBackends(const std::vector<std::string>& sharedObjects);
Matteo Martincigh0c2b2892019-08-05 14:12:11 +010042 static void RegisterDynamicBackends(const std::vector<DynamicBackendPtr>& dynamicBackends);
Matteo Martincighe54aa062019-08-05 14:12:11 +010043
Matteo Martincighac60d282019-07-25 15:25:44 +010044protected:
Matteo Martincighe7d44982019-08-05 12:16:47 +010045 /// Protected methods for testing purposes
Matteo Martincighac60d282019-07-25 15:25:44 +010046 static bool IsBackendCompatibleImpl(const BackendVersion& backendApiVersion, const BackendVersion& backendVersion);
Matteo Martincighe7d44982019-08-05 12:16:47 +010047 static std::vector<std::string> GetBackendPathsImpl(const std::string& backendPaths);
Matteo Martincigh0c2b2892019-08-05 14:12:11 +010048 static void RegisterDynamicBackendsImpl(BackendRegistry& backendRegistry,
49 const std::vector<DynamicBackendPtr>& dynamicBackends);
Matteo Martincighac60d282019-07-25 15:25:44 +010050
Matteo Martincighd73cecb2019-07-24 09:15:00 +010051private:
52 static std::string GetDlError();
53
54 /// This class is to hold utility functions only
55 DynamicBackendUtils() = delete;
56};
57
58template<typename EntryPointType>
59EntryPointType DynamicBackendUtils::GetEntryPoint(const void* sharedObjectHandle, const char* symbolName)
60{
61 if (sharedObjectHandle == nullptr)
62 {
63 throw RuntimeException("GetEntryPoint error: invalid handle");
64 }
65
66 if (symbolName == nullptr)
67 {
68 throw RuntimeException("GetEntryPoint error: invalid symbol");
69 }
70
71 auto entryPoint = reinterpret_cast<EntryPointType>(dlsym(const_cast<void*>(sharedObjectHandle), symbolName));
72 if (!entryPoint)
73 {
Matteo Martincighb19d2e92019-07-25 14:04:40 +010074 throw RuntimeException(boost::str(boost::format("GetEntryPoint error: %1%") % GetDlError()));
Matteo Martincighd73cecb2019-07-24 09:15:00 +010075 }
76
77 return entryPoint;
78}
79
80} // namespace armnn