Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

Commit

Permalink
Separate dynamic loading from static linking (#3456)
Browse files Browse the repository at this point in the history
* Separate dynamic loading from static linking

* Missed find_my_pathname
  • Loading branch information
diyessi committed Aug 17, 2019
1 parent d789370 commit 4a84f51
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Expand Up @@ -185,6 +185,7 @@ option(NGRAPH_JSON_ENABLE "Enable JSON based serialization and tracing features"
option(NGRAPH_STATIC_LIB_ENABLE "Enable build nGraph static library" FALSE)
option(NGRAPH_INTERPRETER_STATIC_LIB_ENABLE "Enable build INTERPRETER backend static library" FALSE)
option(NGRAPH_CPU_STATIC_LIB_ENABLE "Enable build CPU backend static library" FALSE)
option(NGRAPH_DYNAMIC_COMPONENTS_ENABLE "Enable dynamic loading of components" TRUE)

if (NGRAPH_CPU_ENABLE
AND
Expand Down Expand Up @@ -263,6 +264,7 @@ NORMALIZE_BOOL(NGRAPH_JSON_ENABLE)
NORMALIZE_BOOL(NGRAPH_STATIC_LIB_ENABLE)
NORMALIZE_BOOL(NGRAPH_INTERPRETER_STATIC_LIB_ENABLE)
NORMALIZE_BOOL(NGRAPH_CPU_STATIC_LIB_ENABLE)
NORMALIZE_BOOL(NGRAPH_DYNAMIC_COMPONENTS_ENABLE)

message(STATUS "NGRAPH_UNIT_TEST_ENABLE: ${NGRAPH_UNIT_TEST_ENABLE}")
message(STATUS "NGRAPH_TOOLS_ENABLE: ${NGRAPH_TOOLS_ENABLE}")
Expand All @@ -289,6 +291,7 @@ message(STATUS "NGRAPH_JSON_ENABLE: ${NGRAPH_JSON_ENABLE}")
message(STATUS "NGRAPH_STATIC_LIB_ENABLE: ${NGRAPH_STATIC_LIB_ENABLE}")
message(STATUS "NGRAPH_INTERPRETER_STATIC_LIB_ENABLE: ${NGRAPH_INTERPRETER_STATIC_LIB_ENABLE}")
message(STATUS "NGRAPH_CPU_STATIC_LIB_ENABLE: ${NGRAPH_CPU_STATIC_LIB_ENABLE}")
message(STATUS "NGRAPH_DYNAMIC_COMPONENTS_ENABLE: ${NGRAPH_DYNAMIC_COMPONENTS_ENABLE}")

#-----------------------------------------------------------------------------------------------
# Installation logic...
Expand Down Expand Up @@ -402,6 +405,10 @@ if (NGRAPH_STATIC_LIB_ENABLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNGRAPH_STATIC_LIB_ENABLE")
endif()

if (NGRAPH_DYNAMIC_COMPONENTS_ENABLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNGRAPH_DYNAMIC_COMPONENTS_ENABLE")
endif()

if (NGRAPH_PLAIDML_ENABLE)
find_package(PlaidML CONFIG)
if (NOT PLAIDML_FOUND)
Expand Down
6 changes: 3 additions & 3 deletions src/ngraph/runtime/backend.cpp
Expand Up @@ -37,9 +37,7 @@ std::string runtime::Backend::s_backend_shared_library_search_directory;
// This finds the full path of the containing shared library
static string find_my_pathname()
{
#ifdef NGRAPH_STATIC_LIB_ENABLE
return "";
#else
#ifdef NGRAPH_DYNAMIC_COMPONENTS_ENABLE
#ifdef _WIN32
HMODULE hModule = GetModuleHandleW(L"ngraph.dll");
WCHAR wpath[MAX_PATH];
Expand All @@ -55,6 +53,8 @@ static string find_my_pathname()
dladdr(reinterpret_cast<void*>(find_my_pathname), &dl_info);
return dl_info.dli_fname;
#endif
#else
return "";
#endif
}

Expand Down
12 changes: 6 additions & 6 deletions src/ngraph/runtime/backend_manager.cpp
Expand Up @@ -32,9 +32,7 @@
using namespace std;
using namespace ngraph;

#ifdef NGRAPH_STATIC_LIB_ENABLE
#define DLERROR() ""
#else
#ifdef NGRAPH_DYNAMIC_COMPONENTS_ENABLE
#ifdef _WIN32
#define CLOSE_LIBRARY(a) FreeLibrary(a)
#define DLSYM(a, b) GetProcAddress(a, b)
Expand All @@ -44,6 +42,8 @@ using namespace ngraph;
#define DLSYM(a, b) dlsym(a, b)
#define DLERROR() dlerror()
#endif
#else
#define DLERROR() ""
#endif

unordered_map<string, runtime::BackendConstructor*>& runtime::BackendManager::get_registry()
Expand Down Expand Up @@ -107,7 +107,7 @@ shared_ptr<runtime::Backend> runtime::BackendManager::create_backend(const std::
}
else
{
#ifndef NGRAPH_STATIC_LIB_ENABLE
#ifdef NGRAPH_DYNAMIC_COMPONENTS_ENABLE
DL_HANDLE handle = open_shared_library(type);
if (!handle)
{
Expand Down Expand Up @@ -153,7 +153,7 @@ DL_HANDLE runtime::BackendManager::open_shared_library(string type)
string lib_suffix = SHARED_LIB_SUFFIX;

DL_HANDLE handle = nullptr;
#ifndef NGRAPH_STATIC_LIB_ENABLE
#ifdef NGRAPH_DYNAMIC_COMPONENTS_ENABLE

// strip off attributes, IE:CPU becomes IE
auto colon = type.find(":");
Expand Down Expand Up @@ -190,7 +190,7 @@ DL_HANDLE runtime::BackendManager::open_shared_library(string type)
map<string, string> runtime::BackendManager::get_registered_device_map()
{
map<string, string> rc;
#ifndef NGRAPH_STATIC_LIB_ENABLE
#ifdef NGRAPH_DYNAMIC_COMPONENTS_ENABLE
string my_directory =
file_util::get_directory(Backend::get_backend_shared_library_search_directory());
vector<string> backend_list;
Expand Down

0 comments on commit 4a84f51

Please sign in to comment.