diff options
author | Matteo Martincigh <matteo.martincigh@arm.com> | 2019-08-05 14:12:11 +0100 |
---|---|---|
committer | Matteo Martincigh <matteo.martincigh@arm.com> | 2019-08-07 15:04:51 +0100 |
commit | e54aa06ec19813b737513ecb8224285d98e871ba (patch) | |
tree | 36bdb824d79abec37aa15e811010e5004854a063 /src/backends/backendsCommon/DynamicBackendUtils.cpp | |
parent | 5488cfaaa1a411cb5a18c81a98b90c6e3011abdc (diff) | |
download | armnn-e54aa06ec19813b737513ecb8224285d98e871ba.tar.gz |
IVGCVSW-3595 Implement the LoadDynamicBackends function in the Runtime class
* Changed the way the handle is acquired, loaded symbols are now kept local
* Updated the makefiles to add more test files for the dynamic backends
* Fixed the GetSharedObjects method so that the files are parsed in
alphabetical order
* Updated the unit tests to make them more strict wrt the order of the
files
* Created a new CreateDynamicBackends method in the utils class
* Added new unit tests for the new function
* Added LoadDynamicBackends in the Runtime class
!android-nn-driver:1707
Change-Id: I1ef9ff3d5455ca6a7fd51cb7cfb3819686234f70
Signed-off-by: Matteo Martincigh <matteo.martincigh@arm.com>
Diffstat (limited to 'src/backends/backendsCommon/DynamicBackendUtils.cpp')
-rw-r--r-- | src/backends/backendsCommon/DynamicBackendUtils.cpp | 67 |
1 files changed, 62 insertions, 5 deletions
diff --git a/src/backends/backendsCommon/DynamicBackendUtils.cpp b/src/backends/backendsCommon/DynamicBackendUtils.cpp index 1dea802016..57a605608c 100644 --- a/src/backends/backendsCommon/DynamicBackendUtils.cpp +++ b/src/backends/backendsCommon/DynamicBackendUtils.cpp @@ -21,7 +21,7 @@ void* DynamicBackendUtils::OpenHandle(const std::string& sharedObjectPath) throw RuntimeException("OpenHandle error: shared object path must not be empty"); } - void* sharedObjectHandle = dlopen(sharedObjectPath.c_str(), RTLD_LAZY | RTLD_GLOBAL); + void* sharedObjectHandle = dlopen(sharedObjectPath.c_str(), RTLD_LAZY); if (!sharedObjectHandle) { throw RuntimeException(boost::str(boost::format("OpenHandle error: %1%") % GetDlError())); @@ -168,11 +168,16 @@ std::vector<std::string> DynamicBackendUtils::GetSharedObjects(const std::vector continue; } + // Get all the files in the current path in alphabetical order + std::vector<path> backendPathFiles; + std::copy(directory_iterator(backendPath), directory_iterator(), std::back_inserter(backendPathFiles)); + std::sort(backendPathFiles.begin(), backendPathFiles.end()); + // Go through all the files in the current backend path - for (directory_iterator fileIterator(backendPath); fileIterator != directory_iterator(); fileIterator++) + for (const path& backendPathFile : backendPathFiles) { - path filePath = *fileIterator; - std::string filename = filePath.filename().string(); + // Get only the name of the file (without the full path) + std::string filename = backendPathFile.filename().string(); if (filename.empty()) { @@ -185,7 +190,7 @@ std::vector<std::string> DynamicBackendUtils::GetSharedObjects(const std::vector { // Get the canonical path for the current file, it will throw if for example the file is a // symlink that cannot be resolved - canonicalPath = canonical(filePath); + canonicalPath = canonical(backendPathFile); } catch (const filesystem_error& e) { @@ -235,4 +240,56 @@ std::vector<std::string> DynamicBackendUtils::GetSharedObjects(const std::vector return sharedObjects; } +std::vector<DynamicBackendPtr> DynamicBackendUtils::CreateDynamicBackends(const std::vector<std::string>& sharedObjects) +{ + // Create a list of dynamic backends + std::vector<DynamicBackendPtr> dynamicBackends; + for (const std::string& sharedObject : sharedObjects) + { + // Create a handle to the shared object + void* sharedObjectHandle = nullptr; + try + { + sharedObjectHandle = DynamicBackendUtils::OpenHandle(sharedObject); + } + catch (const RuntimeException& e) + { + BOOST_LOG_TRIVIAL(warning) << "Cannot create a handle to the shared object file \"" + << sharedObject << "\": " << e.what(); + continue; + } + if (!sharedObjectHandle) + { + BOOST_LOG_TRIVIAL(warning) << "Invalid handle to the shared object file \"" << sharedObject << "\""; + + continue; + } + + // Create a dynamic backend object + DynamicBackendPtr dynamicBackend; + try + { + dynamicBackend.reset(new DynamicBackend(sharedObjectHandle)); + } + catch (const Exception& e) + { + BOOST_LOG_TRIVIAL(warning) << "Cannot create a valid dynamic backend from the shared object file \"" + << sharedObject << "\": " << e.what(); + continue; + } + if (!dynamicBackend) + { + BOOST_LOG_TRIVIAL(warning) << "Invalid dynamic backend object for the shared object file \"" + << sharedObject << "\""; + + continue; + } + + // Append the newly created dynamic backend to the list + dynamicBackends.push_back(std::move(dynamicBackend)); + } + + return dynamicBackends; +} + } // namespace armnn |