#include <DynamicBackendUtils.hpp>
Definition at line 28 of file DynamicBackendUtils.hpp.
◆ CloseHandle()
void CloseHandle |
( |
const void * |
sharedObjectHandle | ) |
|
|
static |
Definition at line 37 of file DynamicBackendUtils.cpp.
39 #if defined(__unix__) || defined(__APPLE__)
40 if (!sharedObjectHandle)
45 dlclose(
const_cast<void*
>(sharedObjectHandle));
48 throw RuntimeException(
"Dynamic backends not supported on this platform");
void IgnoreUnused(Ts &&...)
References armnn::IgnoreUnused().
◆ CreateDynamicBackends()
std::vector< DynamicBackendPtr > CreateDynamicBackends |
( |
const std::vector< std::string > & |
sharedObjects | ) |
|
|
static |
Definition at line 269 of file DynamicBackendUtils.cpp.
272 std::vector<DynamicBackendPtr> dynamicBackends;
273 for (
const std::string& sharedObject : sharedObjects)
276 void* sharedObjectHandle =
nullptr;
281 catch (
const RuntimeException& e)
283 ARMNN_LOG(warning) <<
"Cannot create a handle to the shared object file \""
284 << sharedObject <<
"\": " << e.what();
287 if (!sharedObjectHandle)
289 ARMNN_LOG(warning) <<
"Invalid handle to the shared object file \"" << sharedObject <<
"\"";
298 dynamicBackend.reset(
new DynamicBackend(sharedObjectHandle));
300 catch (
const Exception& e)
302 ARMNN_LOG(warning) <<
"Cannot create a valid dynamic backend from the shared object file \""
303 << sharedObject <<
"\": " << e.what();
308 ARMNN_LOG(warning) <<
"Invalid dynamic backend object for the shared object file \""
309 << sharedObject <<
"\"";
314 dynamicBackends.push_back(std::move(dynamicBackend));
317 return dynamicBackends;
#define ARMNN_LOG(severity)
static void * OpenHandle(const std::string &sharedObjectPath)
std::unique_ptr< DynamicBackend > DynamicBackendPtr
References ARMNN_LOG, DynamicBackendUtils::OpenHandle(), armnn::warning, and Exception::what().
◆ DeregisterDynamicBackends()
void DeregisterDynamicBackends |
( |
const BackendIdSet & |
dynamicBackends | ) |
|
|
static |
◆ GetBackendPaths()
std::vector< std::string > GetBackendPaths |
( |
const std::string & |
overrideBackendPath = "" | ) |
|
|
static |
◆ GetBackendPathsImpl()
std::vector< std::string > GetBackendPathsImpl |
( |
const std::string & |
backendPaths | ) |
|
|
staticprotected |
Definition at line 103 of file DynamicBackendUtils.cpp.
106 if (backendPaths.empty())
113 std::unordered_set<std::string> uniqueBackendPaths;
114 std::vector<std::string> validBackendPaths;
119 for (
const std::string& path : tempBackendPaths)
128 auto it = uniqueBackendPaths.find(path);
129 if (it != uniqueBackendPaths.end())
136 uniqueBackendPaths.insert(path);
139 validBackendPaths.push_back(path);
142 return validBackendPaths;
std::vector< std::string > StringTokenizer(const std::string &str, const char *delimiters, bool tokenCompression=true)
Function to take a string and a list of delimiters and split the string into tokens based on those de...
References DynamicBackendUtils::IsPathValid(), and armnn::stringUtils::StringTokenizer().
Referenced by DynamicBackendUtils::GetBackendPaths().
◆ GetEntryPoint()
EntryPointType GetEntryPoint |
( |
const void * |
sharedObjectHandle, |
|
|
const char * |
symbolName |
|
) |
| |
|
static |
Definition at line 62 of file DynamicBackendUtils.hpp.
64 #if defined(__unix__) || defined(__APPLE__)
65 if (sharedObjectHandle ==
nullptr)
67 throw RuntimeException(
"GetEntryPoint error: invalid handle");
70 if (symbolName ==
nullptr)
72 throw RuntimeException(
"GetEntryPoint error: invalid symbol");
75 auto entryPoint =
reinterpret_cast<EntryPointType
>(dlsym(
const_cast<void*
>(sharedObjectHandle), symbolName));
78 throw RuntimeException(fmt::format(
"GetEntryPoint error: {}", GetDlError()));
85 throw RuntimeException(
"Dynamic backends not supported on this platform");
References armnn::IgnoreUnused().
◆ GetSharedObjects()
std::vector< std::string > GetSharedObjects |
( |
const std::vector< std::string > & |
backendPaths | ) |
|
|
static |
Definition at line 178 of file DynamicBackendUtils.cpp.
180 std::unordered_set<std::string> uniqueSharedObjects;
181 std::vector<std::string> sharedObjects;
183 #if !defined(ARMNN_DISABLE_FILESYSTEM)
184 for (
const std::string& backendPath : backendPaths)
195 std::vector<path> backendPathFiles;
196 std::copy(directory_iterator(backendPath), directory_iterator(), std::back_inserter(backendPathFiles));
197 std::sort(backendPathFiles.begin(), backendPathFiles.end());
200 for (
const path& backendPathFile : backendPathFiles)
203 std::string filename = backendPathFile.filename().string();
205 if (filename.empty())
216 canonicalPath = canonical(backendPathFile);
218 catch (
const filesystem_error& e)
220 ARMNN_LOG(warning) <<
"GetSharedObjects warning: " << e.what();
222 if (canonicalPath.empty())
231 const std::regex dynamicBackendRegex(
"^[a-zA-Z0-9]+_[a-zA-Z0-9]+_backend.so(\\.[0-9]+)*$");
233 bool filenameMatch =
false;
237 filenameMatch = std::regex_match(filename, dynamicBackendRegex);
239 catch (
const std::exception& e)
241 ARMNN_LOG(warning) <<
"GetSharedObjects warning: " << e.what();
250 std::string validCanonicalPath = canonicalPath.string();
251 auto it = uniqueSharedObjects.find(validCanonicalPath);
252 if (it == uniqueSharedObjects.end())
255 sharedObjects.push_back(validCanonicalPath);
258 uniqueSharedObjects.insert(validCanonicalPath);
266 return sharedObjects;
References ARMNN_LOG, armnn::IgnoreUnused(), DynamicBackendUtils::IsPathValid(), and armnn::warning.
◆ IsBackendCompatible()
◆ IsBackendCompatibleImpl()
◆ IsPathValid()
bool IsPathValid |
( |
const std::string & |
path | ) |
|
|
static |
◆ OpenHandle()
void * OpenHandle |
( |
const std::string & |
sharedObjectPath | ) |
|
|
static |
Definition at line 16 of file DynamicBackendUtils.cpp.
18 #if defined(__unix__) || defined(__APPLE__)
19 if (sharedObjectPath.empty())
21 throw RuntimeException(
"OpenHandle error: shared object path must not be empty");
24 void* sharedObjectHandle = dlopen(sharedObjectPath.c_str(), RTLD_LAZY);
25 if (!sharedObjectHandle)
27 throw RuntimeException(fmt::format(
"OpenHandle error: {}", GetDlError()));
30 return sharedObjectHandle;
33 throw RuntimeException(
"Dynamic backends not supported on this platform");
References armnn::IgnoreUnused().
Referenced by DynamicBackendUtils::CreateDynamicBackends().
◆ RegisterDynamicBackends()
◆ RegisterDynamicBackendsImpl()
Definition at line 341 of file DynamicBackendUtils.cpp.
351 BackendId dynamicBackendId;
354 dynamicBackendId = dynamicBackend->GetBackendId();
356 catch (
const RuntimeException& e)
358 ARMNN_LOG(warning) <<
"Cannot register dynamic backend, "
359 <<
"an error has occurred when getting the backend id: " << e.what();
362 if (dynamicBackendId.IsEmpty() ||
363 dynamicBackendId.IsUndefined())
365 ARMNN_LOG(warning) <<
"Cannot register dynamic backend, invalid backend id: " << dynamicBackendId;
370 bool backendAlreadyRegistered = backendRegistry.IsBackendRegistered(dynamicBackendId);
371 if (backendAlreadyRegistered)
373 ARMNN_LOG(warning) <<
"Cannot register dynamic backend \"" << dynamicBackendId
374 <<
"\": backend already registered";
382 dynamicBackendFactoryFunction = dynamicBackend->GetFactoryFunction();
384 catch (
const RuntimeException& e)
386 ARMNN_LOG(warning) <<
"Cannot register dynamic backend \"" << dynamicBackendId
387 <<
"\": an error has occurred when getting the backend factory function: "
391 if (dynamicBackendFactoryFunction ==
nullptr)
393 ARMNN_LOG(warning) <<
"Cannot register dynamic backend \"" << dynamicBackendId
394 <<
"\": invalid backend factory function";
401 backendRegistry.Register(dynamicBackendId, dynamicBackendFactoryFunction);
403 catch (
const InvalidArgumentException& e)
405 ARMNN_LOG(warning) <<
"An error has occurred when registering the dynamic backend \""
406 << dynamicBackendId <<
"\": " << e.what();
411 registeredBackendIds.insert(dynamicBackendId);
414 return registeredBackendIds;
std::function< PointerType()> FactoryFunction
std::unordered_set< BackendId > BackendIdSet
References ARMNN_LOG, BackendRegistry::IsBackendRegistered(), BackendId::IsEmpty(), BackendId::IsUndefined(), BackendRegistry::Register(), armnn::warning, and Exception::what().
Referenced by DynamicBackendUtils::RegisterDynamicBackends().
The documentation for this class was generated from the following files: