Skip to content

Commit

Permalink
Remove the runtime plugin infrastructure (#1309)
Browse files Browse the repository at this point in the history
* [CMAKE] remove the runtime plugins
- combine the content of libmcwamp_hsa.so into libmcwamp.so
- remove the CPU plugin (unmaintained)

* Remove the plugin code in the HCC runtime
The runtime plugin was there to support multiple
runtime backends in the past.  Since we've only
supported the ROCR runtime backend since ROCm 1.x
and there has been no support for other runtime
backends, remove the runtime plugin clutter
  • Loading branch information
scchan authored Nov 15, 2019
1 parent cbad504 commit b990103
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 292 deletions.
2 changes: 1 addition & 1 deletion cmake-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ endif()
set_target_properties(cmake-test PROPERTIES LINK_FLAGS ${new_cmake_test_link_flags})

if(TARGET hccrt)
add_dependencies(cmake-test clang_links rocdl_links mcwamp_hsa mcwamp)
add_dependencies(cmake-test clang_links rocdl_links mcwamp)
target_link_libraries(cmake-test hccrt hc_am)
else()
# Append default hcc installation
Expand Down
3 changes: 1 addition & 2 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ set( CONFIG_PACKAGE_INSTALL_DIR ${LIB_INSTALL_DIR}/cmake/hcc )
####################
# C++AMP runtime (mcwamp)
####################
add_mcwamp_shared_library(mcwamp mcwamp.cpp)
add_mcwamp_shared_library(mcwamp mcwamp.cpp hsa/mcwamp_hsa.cpp hsa/unpinned_copy_engine.cpp)

# Library interface to use runtime
add_library(hccrt INTERFACE)
Expand Down Expand Up @@ -67,7 +67,6 @@ endif (USE_CODEXL_ACTIVITY_LOGGER EQUAL 1)
# add subdirectories
####################
add_subdirectory(hsa)
add_subdirectory(cpu)

####################
# install targets
Expand Down
11 changes: 0 additions & 11 deletions lib/cpu/CMakeLists.txt

This file was deleted.

95 changes: 0 additions & 95 deletions lib/cpu/mcwamp_cpu.cpp

This file was deleted.

4 changes: 1 addition & 3 deletions lib/hsa/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
# HCC runtime (HSA implementation)
####################
if (HAS_ROCM EQUAL 1)
add_mcwamp_library_hsa(mcwamp_hsa mcwamp_hsa.cpp unpinned_copy_engine.cpp)
add_mcwamp_library_hc_am(hc_am hc_am.cpp)
install(TARGETS mcwamp_hsa hc_am
install(TARGETS hc_am
EXPORT hcc-targets
RUNTIME DESTINATION bin
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
Expand All @@ -20,7 +19,6 @@ endif (HAS_ROCM EQUAL 1)
####################
if (USE_CODEXL_ACTIVITY_LOGGER EQUAL 1)
if (CODEXL_ACTIVITY_LOGGER_LIBRARY)
target_link_libraries(mcwamp_hsa ${CODEXL_ACTIVITY_LOGGER_LIBRARY}/libCXLActivityLogger.so)
target_link_libraries(hc_am ${CODEXL_ACTIVITY_LOGGER_LIBRARY}/libCXLActivityLogger.so)
endif (CODEXL_ACTIVITY_LOGGER_LIBRARY)
endif (USE_CODEXL_ACTIVITY_LOGGER EQUAL 1)
180 changes: 18 additions & 162 deletions lib/mcwamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,53 +26,34 @@
#define STRINGIFY(X) #X
#define LIB_NAME_WITH_VERSION(library) library "." XSTRINGIFY(HCC_MAJOR_VERSION) "." XSTRINGIFY(HCC_MINOR_VERSION)

extern "C" void* PushArgImpl(void*, int, size_t, const void*);
extern "C" void* PushArgPtrImpl(void*, int, size_t, const void*);
extern "C" void* GetContextImpl();
extern "C" void ShutdownImpl();
extern "C" void InitActivityCallbackImpl(void*, void*, void*);
extern "C" bool EnableActivityCallbackImpl(unsigned, bool);
extern "C" const char* GetCmdNameImpl(unsigned);

// interface of HCC runtime implementation
struct RuntimeImpl {
RuntimeImpl(const char* libraryName) :
m_ImplName(libraryName),
m_RuntimeHandle(nullptr),
m_PushArgImpl(nullptr),
m_PushArgPtrImpl(nullptr),
m_GetContextImpl(nullptr),
m_ShutdownImpl(nullptr),
m_InitActivityCallbackImpl(nullptr),
m_EnableActivityCallbackImpl(nullptr),
m_GetCmdNameImpl(nullptr),
RuntimeImpl() :
m_PushArgImpl(PushArgImpl),
m_PushArgPtrImpl(PushArgPtrImpl),
m_GetContextImpl(GetContextImpl),
m_ShutdownImpl(ShutdownImpl),
m_InitActivityCallbackImpl(InitActivityCallbackImpl),
m_EnableActivityCallbackImpl(EnableActivityCallbackImpl),
m_GetCmdNameImpl(GetCmdNameImpl),
isCPU(false) {
//std::cout << "dlopen(" << libraryName << ")\n";
m_RuntimeHandle = dlopen(libraryName, RTLD_LAZY);
if (!m_RuntimeHandle) {
std::cerr << "C++AMP runtime load error: " << dlerror() << std::endl;
return;
}
LoadSymbols();
}

~RuntimeImpl() {
if (m_RuntimeHandle) {
m_ShutdownImpl();
// shutdown call above has already cleaned up all resources,
// dlclose not needed and was causing seg fault for some applications
//dlclose(m_RuntimeHandle);
}
}

// load symbols from C++AMP runtime implementation
void LoadSymbols() {
m_PushArgImpl = (PushArgImpl_t) dlsym(m_RuntimeHandle, "PushArgImpl");
m_PushArgPtrImpl = (PushArgPtrImpl_t) dlsym(m_RuntimeHandle, "PushArgPtrImpl");
m_GetContextImpl= (GetContextImpl_t) dlsym(m_RuntimeHandle, "GetContextImpl");
m_ShutdownImpl= (ShutdownImpl_t) dlsym(m_RuntimeHandle, "ShutdownImpl");
m_InitActivityCallbackImpl = (InitActivityCallbackImpl_t) dlsym(m_RuntimeHandle, "InitActivityCallbackImpl");
m_EnableActivityCallbackImpl = (EnableActivityCallbackImpl_t) dlsym(m_RuntimeHandle, "EnableActivityCallbackImpl");
m_GetCmdNameImpl = (GetCmdNameImpl_t) dlsym(m_RuntimeHandle, "GetCmdNameImpl");
m_ShutdownImpl();
}

void set_cpu() { isCPU = true; }
bool is_cpu() const { return isCPU; }

std::string m_ImplName;
void* m_RuntimeHandle;
PushArgImpl_t m_PushArgImpl;
PushArgPtrImpl_t m_PushArgPtrImpl;
GetContextImpl_t m_GetContextImpl;
Expand Down Expand Up @@ -117,136 +98,11 @@ static std::string& get_library_path()
return library_path;
}

/**
* \brief Base class of platform detection
*/
class PlatformDetect {
public:
PlatformDetect(const std::string& name,
const std::string& ampRuntimeLibrary,
void* const kernel_source)
: m_name(name),
m_ampRuntimeLibrary(ampRuntimeLibrary),
m_kernel_source(kernel_source) {}

virtual bool detect() {
void* handle = nullptr;

// detect if C++AMP runtime is available and
// whether all platform library dependencies are satisfied
//std::cout << "dlopen(" << m_ampRuntimeLibrary << ")\n";
handle = dlopen(m_ampRuntimeLibrary.c_str(), RTLD_LAZY);
if (!handle) {
//std::cout << " C++AMP runtime not found" << std::endl;
//std::cout << dlerror() << std::endl;
return false;
}
dlerror(); // clear any existing error
//std::cout << " C++AMP runtime found" << std::endl;
dlclose(handle);

return true;
}

private:
std::string m_ampRuntimeLibrary;
std::string m_name;
void* m_kernel_source;
};

/**
* \brief HSA runtime detection
*/
class HSAPlatformDetect : public PlatformDetect {
public:
HSAPlatformDetect() : PlatformDetect("HSA", get_library_path() + LIB_NAME_WITH_VERSION("libmcwamp_hsa.so"), nullptr) {}
};


/**
* \brief Flag to turn on/off platform-dependent runtime messages
*/
static bool mcwamp_verbose = false;

static RuntimeImpl* LoadHSARuntime() {
RuntimeImpl* runtimeImpl = nullptr;
// load HSA C++AMP runtime
if (mcwamp_verbose)
std::cout << "Use HSA runtime" << std::endl;
std::string lib = get_library_path() + LIB_NAME_WITH_VERSION("libmcwamp_hsa.so");
runtimeImpl = new RuntimeImpl(lib.c_str());
if (!runtimeImpl->m_RuntimeHandle) {
std::cerr << "Can't load HSA runtime!" << std::endl;
delete runtimeImpl;
exit(-1);
} else {
//std::cout << "HSA C++AMP runtime loaded" << std::endl;
}
return runtimeImpl;
}

static RuntimeImpl* LoadCPURuntime() {
RuntimeImpl* runtimeImpl = nullptr;
// load CPU runtime
if (mcwamp_verbose)
std::cout << "Use CPU runtime" << std::endl;
std::string lib = get_library_path() + LIB_NAME_WITH_VERSION("libmcwamp_cpu.so");
runtimeImpl = new RuntimeImpl(lib.c_str());
if (!runtimeImpl->m_RuntimeHandle) {
std::cerr << "Can't load CPU runtime!" << std::endl;
delete runtimeImpl;
exit(-1);
}
return runtimeImpl;
}

static RuntimeImpl* GetOrInitRuntime_impl() {
RuntimeImpl* runtimeImpl = nullptr;
HSAPlatformDetect hsa_rt;

char* verbose_env = getenv("HCC_VERBOSE");
if (verbose_env != nullptr) {
if (std::string("ON") == verbose_env) {
mcwamp_verbose = true;
}
}

// force use certain C++AMP runtime from HCC_RUNTIME environment variable
char* runtime_env = getenv("HCC_RUNTIME");
if (runtime_env != nullptr) {
if (std::string("HSA") == runtime_env) {
if (hsa_rt.detect()) {
runtimeImpl = LoadHSARuntime();
} else {
std::cerr << "Ignore unsupported HCC_RUNTIME environment variable: " << runtime_env << std::endl;
}
} else if(std::string("CPU") == runtime_env) {
// CPU runtime should be available
runtimeImpl = LoadCPURuntime();
runtimeImpl->set_cpu();
} else {
std::cerr << "Ignore unknown HCC_RUNTIME environment variable:" << runtime_env << std::endl;
}
}

// If can't determined by environment variable, try detect what can be used
if (runtimeImpl == nullptr) {
if (hsa_rt.detect()) {
runtimeImpl = LoadHSARuntime();
} else {
runtimeImpl = LoadCPURuntime();
runtimeImpl->set_cpu();
std::cerr << "No suitable runtime detected. Fall back to CPU!" << std::endl;
}
}
return runtimeImpl;
}

static std::unique_ptr<RuntimeImpl> runtime;
RuntimeImpl* GetOrInitRuntime() {
static std::once_flag f;
std::call_once(f, []() {
runtime = std::move(std::unique_ptr<RuntimeImpl>(GetOrInitRuntime_impl()));
runtime = std::make_unique<RuntimeImpl>();
});
return runtime.get();
}
Expand Down
6 changes: 0 additions & 6 deletions packaging/debian/postinst.in
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ SOFTLINKS=(
"$LIBRARY_DIR" "lib" "libmcwamp.so"
"$LIBRARY_DIR" "lib" "libmcwamp.so.${HCC_VERSION_MAJOR}"
"$LIBRARY_DIR" "lib" "libmcwamp.so.${HCC_VERSION_MAJOR}.${HCC_VERSION_MINOR}"
"$LIBRARY_DIR" "lib" "libmcwamp_cpu.so"
"$LIBRARY_DIR" "lib" "libmcwamp_cpu.so.${HCC_VERSION_MAJOR}"
"$LIBRARY_DIR" "lib" "libmcwamp_cpu.so.${HCC_VERSION_MAJOR}.${HCC_VERSION_MINOR}"
"$LIBRARY_DIR" "lib" "libmcwamp_hsa.so"
"$LIBRARY_DIR" "lib" "libmcwamp_hsa.so.${HCC_VERSION_MAJOR}"
"$LIBRARY_DIR" "lib" "libmcwamp_hsa.so.${HCC_VERSION_MAJOR}.${HCC_VERSION_MINOR}"
"$LIBRARY_DIR" "lib" "libclang_rt.builtins-${PROCESSOR}.a"
)

Expand Down
Loading

0 comments on commit b990103

Please sign in to comment.