Skip to content

Commit 94aa871

Browse files
committed
Use the path of libmcwamp to dlopen libmcwamp_hsa, as static function (#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
1 parent e4249af commit 94aa871

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

lib/mcwamp.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,31 @@ namespace CLAMP {
9090
////////////////////////////////////////////////////////////
9191
// Class declaration
9292
////////////////////////////////////////////////////////////
93+
94+
static std::string& get_library_path()
95+
{
96+
static std::string library_path;
97+
static std::once_flag once;
98+
std::call_once(once, [] () {
99+
// determine the search path for libmcwamp_hsa based on the
100+
// path of this library
101+
dl_iterate_phdr([](struct dl_phdr_info* info, size_t size, void* data) {
102+
if (info->dlpi_name) {
103+
std::string p(info->dlpi_name);
104+
auto pos = p.find("libmcwamp.so");
105+
if (pos != std::string::npos) {
106+
p.erase(p.begin()+pos, p.end());
107+
library_path = std::move(p);
108+
return 1;
109+
}
110+
}
111+
return 0;
112+
} , nullptr);
113+
});
114+
115+
return library_path;
116+
}
117+
93118
/**
94119
* \brief Base class of platform detection
95120
*/
@@ -132,7 +157,7 @@ class PlatformDetect {
132157
*/
133158
class HSAPlatformDetect : public PlatformDetect {
134159
public:
135-
HSAPlatformDetect() : PlatformDetect("HSA", LIB_NAME_WITH_VERSION("libmcwamp_hsa.so"), nullptr) {}
160+
HSAPlatformDetect() : PlatformDetect("HSA", get_library_path() + LIB_NAME_WITH_VERSION("libmcwamp_hsa.so"), nullptr) {}
136161
};
137162

138163

@@ -146,7 +171,8 @@ static RuntimeImpl* LoadHSARuntime() {
146171
// load HSA C++AMP runtime
147172
if (mcwamp_verbose)
148173
std::cout << "Use HSA runtime" << std::endl;
149-
runtimeImpl = new RuntimeImpl(LIB_NAME_WITH_VERSION("libmcwamp_hsa.so"));
174+
std::string lib = get_library_path() + LIB_NAME_WITH_VERSION("libmcwamp_hsa.so");
175+
runtimeImpl = new RuntimeImpl(lib.c_str());
150176
if (!runtimeImpl->m_RuntimeHandle) {
151177
std::cerr << "Can't load HSA runtime!" << std::endl;
152178
delete runtimeImpl;
@@ -162,7 +188,8 @@ static RuntimeImpl* LoadCPURuntime() {
162188
// load CPU runtime
163189
if (mcwamp_verbose)
164190
std::cout << "Use CPU runtime" << std::endl;
165-
runtimeImpl = new RuntimeImpl(LIB_NAME_WITH_VERSION("libmcwamp_cpu.so"));
191+
std::string lib = get_library_path() + LIB_NAME_WITH_VERSION("libmcwamp_cpu.so");
192+
runtimeImpl = new RuntimeImpl(lib.c_str());
166193
if (!runtimeImpl->m_RuntimeHandle) {
167194
std::cerr << "Can't load CPU runtime!" << std::endl;
168195
delete runtimeImpl;
@@ -428,6 +455,7 @@ KalmarContext *getContext() {
428455
class KalmarBootstrap {
429456
public:
430457
KalmarBootstrap() {
458+
431459
bool to_init = false;
432460
char* lazyinit_env = getenv("HCC_LAZYINIT");
433461
if (lazyinit_env != nullptr) {

0 commit comments

Comments
 (0)