Commit 6fe8f8e5 authored by Yixing Lao's avatar Yixing Lao

support for runtime dlopen

parent 82563638
......@@ -130,6 +130,27 @@ if (NGRAPH_CPU_ENABLE AND LLVM_INCLUDE_DIR AND
endif()
add_library(ngraph SHARED ${SRC})
# Colon separated string for specified runtime plugin loading, this is made explicit s.t. if a
# plugin is specified at compile time but the corresponding library could not be resolved at run-
# time, an error will be generated.
# E.g. assume compiling with Argon and Xpu, then -DRUNTIME_PLUGIN_LIBS="libargon.so:libxpu.so".
if (RUNTIME_PLUGIN_LIBS)
target_compile_definitions(ngraph PRIVATE RUNTIME_PLUGIN_LIBS=${RUNTIME_PLUGIN_LIBS})
else()
target_compile_definitions(ngraph PRIVATE RUNTIME_PLUGIN_LIBS="")
endif()
# This is used to ensure that libngraph.so and libargon.so are in the same directory for dlopen.
# Effective at build time. Does not affect `make install` logics.
if (COMMON_LIBRARY_OUTPUT_DIRECTORY)
set_target_properties(ngraph PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${COMMON_LIBRARY_OUTPUT_DIRECTORY})
message(STATUS "LIBRARY_OUTPUT_DIRECTORY set to: ${COMMON_LIBRARY_OUTPUT_DIRECTORY}")
else()
message(STATUS "LIBRARY_OUTPUT_DIRECTORY not set, will use the defualt value")
endif()
target_include_directories(ngraph PUBLIC "${NGRAPH_INCLUDE_PATH}")
if(NGRAPH_CPU_ENABLE AND LLVM_LINK_LIBS)
......@@ -146,6 +167,7 @@ if(NGRAPH_CPU_ENABLE AND MKLDNN_LIB_DIR)
target_link_libraries(ngraph LINK_PRIVATE mkldnn)
endif()
#-----------------------------------------------------------------------------------------------
# Installation logic...
#-----------------------------------------------------------------------------------------------
......
......@@ -13,12 +13,53 @@
// ----------------------------------------------------------------------------
#include "ngraph/runtime/manager.hpp"
#include <dlfcn.h>
#include <iostream>
#include <sstream>
#include <string>
using namespace ngraph::runtime;
bool Manager::load_plugins(const std::string& runtime_plugin_libs)
{
std::istringstream ss(runtime_plugin_libs);
std::string plugin_lib_path;
while (std::getline(ss, plugin_lib_path, ':'))
{
if (plugin_lib_path.size() > 0)
{
void* lib_handle = dlopen(plugin_lib_path.c_str(), RTLD_NOW);
if (!lib_handle)
{
std::cerr << "Cannot open library: " << plugin_lib_path << ", " << dlerror()
<< std::endl;
return false;
}
else
{
std::cerr << "Loaded runtime at " << lib_handle << std::endl;
}
}
}
return true;
}
Manager::FactoryMap& Manager::get_factory_map()
{
// Stores Manager Factories
static FactoryMap factory_map;
// Try to load runtime plugins
if (!Manager::m_is_factory_map_initialized)
{
if (!Manager::load_plugins(RUNTIME_PLUGIN_LIBS))
{
std::cerr << "Failed to load at least one of the following libraries: "
<< RUNTIME_PLUGIN_LIBS << std::endl;
}
Manager::m_is_factory_map_initialized = true;
}
return factory_map;
}
......@@ -32,3 +73,5 @@ Manager::Factory Manager::register_factory(std::string name, Factory factory)
get_factory_map()[name] = factory;
return factory;
}
bool Manager::m_is_factory_map_initialized = false;
......@@ -46,13 +46,19 @@ namespace ngraph
compile(const std::shared_ptr<ngraph::Function>& fun) = 0;
using Factory = std::function<std::shared_ptr<Manager>(const std::string&)>;
using FactoryMap = std::map<std::string, Factory>;
static FactoryMap& get_factory_map();
static std::shared_ptr<Manager> get(const std::string& name);
static Factory register_factory(std::string name, Factory factory);
private:
static bool load_plugins(const std::string& runtime_plugin_libs);
static bool m_is_factory_map_initialized;
using FactoryMap = std::map<std::string, Factory>;
static FactoryMap& get_factory_map();
};
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment