blob: 71747fcdd215e667a03658fe0f4f554f5652db2d [file] [log] [blame]
Matteo Martincighd73cecb2019-07-24 09:15:00 +01001//
Jim Flynn6da6a452020-07-14 14:26:27 +01002// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
Matteo Martincighd73cecb2019-07-24 09:15:00 +01003// 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
James Ward47fce872020-09-10 11:57:28 +010013#include <fmt/format.h>
Matteo Martincighd73cecb2019-07-24 09:15:00 +010014
Rob Hughes91e1d892019-08-23 10:11:58 +010015#include <string>
16#include <vector>
Jim Flynn6da6a452020-07-14 14:26:27 +010017#if defined(__unix__) || defined(__APPLE__)
Rob Hughes91e1d892019-08-23 10:11:58 +010018#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{
Jim Flynn6da6a452020-07-14 14:26:27 +010064#if defined(__unix__) || defined(__APPLE__)
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 {
James Ward47fce872020-09-10 11:57:28 +010078 throw RuntimeException(fmt::format("GetEntryPoint error: {}", GetDlError()));
Matteo Martincighd73cecb2019-07-24 09:15:00 +010079 }
80
81 return entryPoint;
Rob Hughes91e1d892019-08-23 10:11:58 +010082#else
Jim Flynn870b96c2022-03-25 21:24:56 +000083 armnn::IgnoreUnused(sharedObjectHandle);
84 armnn::IgnoreUnused(symbolName);
Rob Hughes91e1d892019-08-23 10:11:58 +010085 throw RuntimeException("Dynamic backends not supported on this platform");
86#endif
Matteo Martincighd73cecb2019-07-24 09:15:00 +010087}
88
89} // namespace armnn