Skip to content

Commit 3be1c4b

Browse files
Pierre-Andre Saulaiskbenzie
authored andcommitted
[CUDA] Use LibLoader for cross-platform loading of libraries
1 parent 311afb9 commit 3be1c4b

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

source/adapters/cuda/tracing.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
#include <cuda.h>
1717
#ifdef XPTI_ENABLE_INSTRUMENTATION
1818
#include <cupti.h>
19-
#include <dlfcn.h>
2019
#endif // XPTI_ENABLE_INSTRUMENTATION
2120

2221
#include "tracing.hpp"
22+
#include "ur_lib_loader.hpp"
2323
#include <exception>
2424
#include <iostream>
2525

@@ -42,8 +42,9 @@ using cuptiEnableCallback_fn = CUPTIAPI
4242
CUptiResult (*)(uint32_t enable, CUpti_SubscriberHandle subscriber,
4343
CUpti_CallbackDomain domain, CUpti_CallbackId cbid);
4444

45-
#define LOAD_CUPTI_SYM(p, x) \
46-
p->x = (cupti##x##_fn)dlsym(p->Library, "cupti" #x);
45+
#define LOAD_CUPTI_SYM(p, lib, x) \
46+
p->x = (cupti##x##_fn)ur_loader::LibLoader::getFunctionPtr(lib.get(), \
47+
"cupti" #x);
4748

4849
#else
4950
using tracing_event_t = void *;
@@ -58,7 +59,7 @@ struct cuda_tracing_context_t_ {
5859
tracing_event_t CallEvent = nullptr;
5960
tracing_event_t DebugEvent = nullptr;
6061
subscriber_handle_t Subscriber = nullptr;
61-
void *Library = nullptr;
62+
ur_loader::LibLoader::Lib Library;
6263
cuptiSubscribe_fn Subscribe = nullptr;
6364
cuptiUnsubscribe_fn Unsubscribe = nullptr;
6465
cuptiEnableDomain_fn EnableDomain = nullptr;
@@ -137,18 +138,19 @@ bool loadCUDATracingLibrary(cuda_tracing_context_t_ *Ctx) {
137138
return false;
138139
if (Ctx->Library)
139140
return true;
140-
Ctx->Library = dlopen(CUPTI_LIB_PATH, RTLD_NOW);
141-
if (!Ctx->Library)
141+
auto Lib{ur_loader::LibLoader::loadAdapterLibrary(CUPTI_LIB_PATH)};
142+
if (!Lib)
142143
return false;
143-
LOAD_CUPTI_SYM(Ctx, Subscribe)
144-
LOAD_CUPTI_SYM(Ctx, Unsubscribe)
145-
LOAD_CUPTI_SYM(Ctx, EnableDomain)
146-
LOAD_CUPTI_SYM(Ctx, EnableCallback)
144+
LOAD_CUPTI_SYM(Ctx, Lib, Subscribe)
145+
LOAD_CUPTI_SYM(Ctx, Lib, Unsubscribe)
146+
LOAD_CUPTI_SYM(Ctx, Lib, EnableDomain)
147+
LOAD_CUPTI_SYM(Ctx, Lib, EnableCallback)
147148
if (!Ctx->Subscribe || !Ctx->Unsubscribe || !Ctx->EnableDomain ||
148149
!Ctx->EnableCallback) {
149150
unloadCUDATracingLibrary(Ctx);
150151
return false;
151152
}
153+
Ctx->Library = std::move(Lib);
152154
return true;
153155
#else
154156
(void)Ctx;
@@ -164,8 +166,8 @@ void unloadCUDATracingLibrary(cuda_tracing_context_t_ *Ctx) {
164166
Ctx->Unsubscribe = nullptr;
165167
Ctx->EnableDomain = nullptr;
166168
Ctx->EnableCallback = nullptr;
167-
dlclose(Ctx->Library);
168-
Ctx->Library = nullptr;
169+
170+
Ctx->Library.reset();
169171
#else
170172
(void)Ctx;
171173
#endif // XPTI_ENABLE_INSTRUMENTATION

source/common/ur_lib_loader.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ class LibLoader {
2727
void operator()(HMODULE handle) { freeAdapterLibrary(handle); }
2828
};
2929

30-
static std::unique_ptr<HMODULE, lib_dtor>
31-
loadAdapterLibrary(const char *name);
30+
using Lib = std::unique_ptr<HMODULE, lib_dtor>;
31+
32+
static Lib loadAdapterLibrary(const char *name);
3233

3334
static void freeAdapterLibrary(HMODULE handle);
3435

0 commit comments

Comments
 (0)