Skip to content

Commit e5c0fa9

Browse files
committed
Search for GPU compute libs under various paths. re:#181
1 parent 2e56961 commit e5c0fa9

File tree

5 files changed

+61
-52
lines changed

5 files changed

+61
-52
lines changed

src/cbang/hw/CUDALibrary.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static const char *cudaLib = "/Library/Frameworks/CUDA.framework/CUDA";
5555
#define STDCALL
5656

5757
#else
58-
static const char *cudaLib = "libcuda.so";
58+
static vector<string> cudaLib = {"libcuda.so.1", "libcuda.so"};
5959
#define STDCALL
6060
#endif
6161

@@ -65,10 +65,10 @@ static const char *cudaLib = "libcuda.so";
6565
#define CUDA_API
6666
#endif
6767

68-
#define DYNAMIC_CALL(name, args) { \
69-
name##_t name = (name##_t)getSymbol(#name); \
70-
if ((err = name args)) THROW(#name "() returned " << err); \
71-
}
68+
#define DYNAMIC_CALL(name, args) { \
69+
name##_t name = (name##_t)getSymbol(#name); \
70+
if ((err = name args)) THROW(#name "() returned " << err); \
71+
}
7272

7373

7474
namespace {

src/cbang/hw/HIPLibrary.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ static const char *hipLib = "/Library/Frameworks/HIP.framework/HIP";
5555
#define STDCALL
5656

5757
#else
58-
static const char *hipLib = "libamdhip64.so";
58+
static vector<string> hipLib = {"/opt/rocm/lib/libamdhip64.so.7",
59+
"libamdhip64.so.7", "/opt/rocm/lib/libamdhip64.so.6", "libamdhip64.so.6",
60+
"/opt/rocm/lib/libamdhip64.so.5", "libamdhip64.so.5",
61+
"/opt/rocm/lib/libamdhip64.so", "libamdhip64.so"};
5962
#define STDCALL
6063
#endif
6164

src/cbang/hw/OpenCLLibrary.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static const char *openclLib =
5656
"/System/Library/Frameworks/OpenCL.framework/OpenCL";
5757

5858
#else
59-
static const char *openclLib = "libOpenCL.so.1";
59+
static vector<string> openclLib = {"libOpenCL.so.1", "libOpenCL.so"};
6060
#endif
6161

6262

src/cbang/os/DynamicLibrary.cpp

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "SysError.h"
3535

3636
#include <cbang/Exception.h>
37+
#include <cbang/String.h>
3738

3839
#undef CBANG_EXCEPTION
3940
#define CBANG_EXCEPTION DynamicLibraryException
@@ -53,67 +54,68 @@ using namespace cb;
5354
bool DynamicLibrary::enabled = true;
5455

5556

56-
struct DynamicLibrary::private_t {
57+
struct DynamicLibrary::impl_t {
5758
#ifdef _WIN32
5859
HMODULE handle;
59-
#else
60-
void *handle;
61-
#endif
62-
};
6360

61+
impl_t(const string &path) :
62+
handle(LoadLibraryEx(path.c_str(), 0, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) {}
63+
~impl_t() {if (handle) CloseHandle(handle);}
6464

65-
DynamicLibrary::DynamicLibrary(const string &path) :
66-
path(path), pri(new private_t) {
65+
static void clear() {SysError::clear();}
66+
string error() {return SysError().toString();}
67+
void *get(const string &name) {return GetProcAddress(handle, name.c_str());}
6768

68-
if (!enabled) THROW("DynamicLibrary disabled globally");
69+
#else // !_WIN32
70+
void *handle;
6971

70-
#ifdef _WIN32
71-
pri->handle =
72-
LoadLibraryEx(path.c_str(), 0, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
73-
if (!pri->handle)
74-
THROW("Failed to open dynamic library '" << path << "': " << SysError());
72+
impl_t(const string &path) : handle(dlopen(path.c_str(), RTLD_LAZY)) {}
73+
~impl_t() {if (handle) dlclose(handle);}
7574

76-
#else
77-
dlerror(); // Clear errors
75+
static void clear() {dlerror();} // Clear errors
76+
string error() {return dlerror();}
77+
void *get(const string &name) {return dlsym(handle, name.c_str());}
78+
#endif // !_WIN32
79+
};
7880

79-
if (path.empty()) THROW("Library path is ''");
8081

81-
pri->handle = dlopen(path.c_str(), RTLD_LAZY);
82-
if (!pri->handle)
83-
THROW("Failed to open dynamic library '" << path << "': " << dlerror());
84-
#endif
82+
DynamicLibrary::DynamicLibrary(const string &path) :
83+
path(path), impl(load(path)) {
84+
if (!impl->handle)
85+
THROW("Failed to open dynamic library '" << path << "': " << impl->error());
8586
}
8687

8788

88-
DynamicLibrary::~DynamicLibrary() {
89-
#ifdef _WIN32
90-
if (pri->handle) CloseHandle(pri->handle);
89+
DynamicLibrary::DynamicLibrary(const vector<string> &paths) {
90+
for (auto &path: paths) {
91+
impl = load(this->path = path);
92+
if (impl->handle) return;
93+
}
9194

92-
#else
93-
if (pri->handle) dlclose(pri->handle);
94-
#endif
95-
96-
delete pri;
95+
THROW("Failed to open any of the following dynamic libraries: "
96+
<< String::join(paths, ", "));
9797
}
9898

9999

100+
DynamicLibrary::~DynamicLibrary() {}
101+
102+
100103
void *DynamicLibrary::getSymbol(const string &name) {
101-
#ifdef _WIN32
102-
void *symbol = (void *)GetProcAddress(pri->handle, name.c_str());
104+
impl_t::clear();
105+
auto symbol = impl->get(name);
106+
103107
if (!symbol)
104108
THROW("Failed to load dynamic symbol '" << name << "' from library '"
105-
<< path << "': " << SysError());
109+
<< path << "': " << impl->error());
106110

107-
#else
108-
dlerror(); // Clear errors
111+
return symbol;
112+
}
109113

110-
void *symbol = dlsym(pri->handle, name.c_str());
111114

112-
char *err = dlerror();
113-
if (err)
114-
THROW("Failed to load dynamic symbol '" << name << "' from library '"
115-
<< path << "': " << err);
116-
#endif
115+
SmartPointer<DynamicLibrary::impl_t> DynamicLibrary::load(const string &path) {
116+
if (!enabled) THROW("DynamicLibrary disabled globally");
117+
if (path.empty()) THROW("Library path is ''");
117118

118-
return symbol;
119+
impl_t::clear();
120+
return new impl_t(path);
119121
}

src/cbang/os/DynamicLibrary.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,27 @@ namespace cb {
4040
CBANG_DEFINE_EXCEPTION_SUBCLASS(DynamicLibraryException);
4141

4242
class DynamicLibrary {
43-
struct private_t;
44-
45-
const std::string path;
46-
private_t *pri;
47-
4843
static bool enabled;
4944

45+
std::string path;
46+
struct impl_t;
47+
SmartPointer<impl_t> impl;
48+
5049
public:
5150
DynamicLibrary(const std::string &path);
51+
DynamicLibrary(const std::vector<std::string> &paths);
5252
~DynamicLibrary();
5353

54+
const std::string &getPath() const {return path;}
5455
void *getSymbol(const std::string &name);
5556

5657
template <typename T>
5758
T accessSymbol(const std::string &name) {return (T)getSymbol(name);}
5859

5960
static inline bool isEnabled() {return enabled;}
6061
static inline void setEnabled(bool x) {enabled = x;}
62+
63+
private:
64+
static SmartPointer<impl_t> load(const std::string &path);
6165
};
6266
}

0 commit comments

Comments
 (0)