Skip to content

Commit

Permalink
Use the path of libmcwamp to dlopen libmcwamp_hsa, as static function (
Browse files Browse the repository at this point in the history
…#1267)

* Use the path of libmcwamp to dlopen libmcwamp_hsa
* make fewer string copies
* CLAMP::library_path is now function get_library_path()
* get_library_path() now returns std::string&
* workaround bugs in std::call_once on Ubuntu 16.04

Change-Id: If1ac7cc9919016dfcc9b47939fac566a1c920bd3
  • Loading branch information
scchan committed Sep 7, 2019
1 parent e4249af commit 94aa871
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions lib/mcwamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,31 @@ namespace CLAMP {
////////////////////////////////////////////////////////////
// Class declaration
////////////////////////////////////////////////////////////

static std::string& get_library_path()
{
static std::string library_path;
static std::once_flag once;
std::call_once(once, [] () {
// determine the search path for libmcwamp_hsa based on the
// path of this library
dl_iterate_phdr([](struct dl_phdr_info* info, size_t size, void* data) {
if (info->dlpi_name) {
std::string p(info->dlpi_name);
auto pos = p.find("libmcwamp.so");
if (pos != std::string::npos) {
p.erase(p.begin()+pos, p.end());
library_path = std::move(p);
return 1;
}
}
return 0;
} , nullptr);
});

return library_path;
}

/**
* \brief Base class of platform detection
*/
Expand Down Expand Up @@ -132,7 +157,7 @@ class PlatformDetect {
*/
class HSAPlatformDetect : public PlatformDetect {
public:
HSAPlatformDetect() : PlatformDetect("HSA", LIB_NAME_WITH_VERSION("libmcwamp_hsa.so"), nullptr) {}
HSAPlatformDetect() : PlatformDetect("HSA", get_library_path() + LIB_NAME_WITH_VERSION("libmcwamp_hsa.so"), nullptr) {}
};


Expand All @@ -146,7 +171,8 @@ static RuntimeImpl* LoadHSARuntime() {
// load HSA C++AMP runtime
if (mcwamp_verbose)
std::cout << "Use HSA runtime" << std::endl;
runtimeImpl = new RuntimeImpl(LIB_NAME_WITH_VERSION("libmcwamp_hsa.so"));
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;
Expand All @@ -162,7 +188,8 @@ static RuntimeImpl* LoadCPURuntime() {
// load CPU runtime
if (mcwamp_verbose)
std::cout << "Use CPU runtime" << std::endl;
runtimeImpl = new RuntimeImpl(LIB_NAME_WITH_VERSION("libmcwamp_cpu.so"));
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;
Expand Down Expand Up @@ -428,6 +455,7 @@ KalmarContext *getContext() {
class KalmarBootstrap {
public:
KalmarBootstrap() {

bool to_init = false;
char* lazyinit_env = getenv("HCC_LAZYINIT");
if (lazyinit_env != nullptr) {
Expand Down

0 comments on commit 94aa871

Please sign in to comment.