Skip to content

Commit

Permalink
Make dynlink_xxx use statically linked functions to load symbols. (NV…
Browse files Browse the repository at this point in the history
…IDIA#2931)

* dynlink_xxx now directly link to XxxLoadSymbol instead of having setters
* removed cufileInit as it only loaded the library
* removed explicit loading from xxxInitChecked

Signed-off-by: Michał Zientkiewicz <[email protected]>
  • Loading branch information
mzient authored May 6, 2021
1 parent 7b68f3c commit 1e68f6e
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 68 deletions.
16 changes: 4 additions & 12 deletions dali/core/dynlink_cuda.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,15 @@ CUDADRIVER loadCudaLibrary() {
return ret;
}

void *LoadSymbol(const char *name) {
} // namespace

void *CudaLoadSymbol(const char *name) {
static CUDADRIVER cudaDrvLib = loadCudaLibrary();
void *ret = cudaDrvLib ? dlsym(cudaDrvLib, name) : nullptr;
return ret;
}

} // namespace

// it is defined in the generated file
typedef void *tLoadSymbol(const char *name);
void CudaSetSymbolLoader(tLoadSymbol loader_func);

bool cuInitChecked() {
#if !LINK_DRIVER_ENABLED
static std::once_flag cuda_once;
std::call_once(cuda_once, CudaSetSymbolLoader, LoadSymbol);
#endif
static CUresult res = cuInit(0);
return res == CUDA_SUCCESS;
}
Expand All @@ -68,7 +60,7 @@ bool cuIsSymbolAvailable(const char *name) {
std::lock_guard<std::mutex> lock(symbol_mutex);
auto it = symbol_map.find(name);
if (it == symbol_map.end()) {
auto *ptr = LoadSymbol(name);
auto *ptr = CudaLoadSymbol(name);
symbol_map.insert({name, ptr});
return ptr != nullptr;
}
Expand Down
17 changes: 3 additions & 14 deletions dali/core/dynlink_cufile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,10 @@ CUFILE loadCufileLibrary() {
return ret;
}

void *LoadSymbol(const char *name) {
} // namespace

void *CufileLoadSymbol(const char *name) {
static CUFILE cufileDrvLib = loadCufileLibrary();
void *ret = cufileDrvLib ? dlsym(cufileDrvLib, name) : nullptr;
return ret;
}

} // namespace

// it is defined in the generated file
typedef void *tLoadSymbol(const char *name);
void CufileSetSymbolLoader(tLoadSymbol loader_func);

void cufileInit() {
#if CUFILE_ENABLED
static std::once_flag cufile_once;
std::call_once(cufile_once, CufileSetSymbolLoader, LoadSymbol);
#endif
}
15 changes: 4 additions & 11 deletions dali/operators/reader/nvdecoder/dynlink_nvcuvid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,15 @@ CUVIDDRIVER loadNvcuvidLibrary() {
return ret;
}

void *LoadSymbol(const char *name) {
} // namespace

void *NvcuvidLoadSymbol(const char *name) {
static CUVIDDRIVER nvcuvidDrvLib = loadNvcuvidLibrary();
void *ret = nvcuvidDrvLib ? dlsym(nvcuvidDrvLib, name) : nullptr;
return ret;
}

} // namespace

// it is defined in the generated file
typedef void *tLoadSymbol(const char *name);
void NvcuvidSetSymbolLoader(tLoadSymbol loader_func);

bool cuvidInitChecked() {
static std::once_flag cuvid_once;
std::call_once(cuvid_once, NvcuvidSetSymbolLoader, LoadSymbol);

static CUVIDDRIVER nvcuvidDrvLib = loadNvcuvidLibrary();
return nvcuvidDrvLib != nullptr;
}
Expand All @@ -67,7 +60,7 @@ bool cuvidIsSymbolAvailable(const char *name) {
std::lock_guard<std::mutex> lock(symbol_mutex);
auto it = symbol_map.find(name);
if (it == symbol_map.end()) {
auto *ptr = LoadSymbol(name);
auto *ptr = NvcuvidLoadSymbol(name);
symbol_map.insert({name, ptr});
return ptr != nullptr;
}
Expand Down
2 changes: 0 additions & 2 deletions dali/util/cufile_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ class CUFileDriverHandle{
public:
explicit CUFileDriverHandle(const int& device = 0) {
dali::DeviceGuard g(device);
// make sure library is opened
cufileInit();
CUDA_CALL(cuFileDriverOpen());
}

Expand Down
25 changes: 8 additions & 17 deletions dali/util/nvml_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,36 +44,27 @@ NVMLRIVER loadNvmlLibrary() {
return ret;
}

void *LoadSymbol(const char *name) {
std::atomic_bool initialized{false};

} // namespace

void *NvmlLoadSymbol(const char *name) {
static NVMLRIVER nvmlDrvLib = loadNvmlLibrary();
void *ret = nvmlDrvLib ? dlsym(nvmlDrvLib, name) : nullptr;
return ret;
}

} // namespace

std::atomic_bool symbolsLoaded{false};

// it is defined in the generated file
typedef void *tLoadSymbol(const char *name);
void NvmlSetSymbolLoader(tLoadSymbol loader_func);

nvmlReturn_t nvmlInitChecked() {
// set symbol loader for this library
#if !LINK_DRIVER_ENABLED
static std::once_flag nvml_once;
std::call_once(nvml_once, NvmlSetSymbolLoader, LoadSymbol);
#endif
symbolsLoaded = true;
nvmlReturn_t ret = nvmlInit();
if (ret != NVML_SUCCESS) {
DALI_WARN("nvmlInitChecked failed: ", nvmlErrorString(ret));
}
initialized = true;
return ret;
}

bool nvmlIsInitialized(void) {
return symbolsLoaded;
return initialized;
}

bool nvmlIsSymbolAvailable(const char *name) {
Expand All @@ -82,7 +73,7 @@ bool nvmlIsSymbolAvailable(const char *name) {
std::lock_guard<std::mutex> lock(symbol_mutex);
auto it = symbol_map.find(name);
if (it == symbol_map.end()) {
auto *ptr = LoadSymbol(name);
auto *ptr = NvmlLoadSymbol(name);
symbol_map.insert({name, ptr});
return ptr != nullptr;
}
Expand Down
3 changes: 0 additions & 3 deletions dali/util/std_cufile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ static void cufile_open(cufile::CUFileHandle& fh, size_t& length, const char* pa
namespace dali {

StdCUFileStream::StdCUFileStream(const std::string& path) : CUFileStream(path) {
// make sure lib is loaded
cufileInit();

// open file
cufile_open(f_, length_, path.c_str());

Expand Down
2 changes: 0 additions & 2 deletions include/dali/core/dynlink_cufile.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,4 @@ inline void cudaResultCheck<CUfileError_t>(CUfileError_t status) {

} // namespace dali

void cufileInit();

#endif // DALI_CORE_DYNLINK_CUFILE_H_
12 changes: 5 additions & 7 deletions tools/stub_generator/stub_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,18 @@ def main():
{0} {{
using FuncPtr = {return_type} (%s *)({2});
static auto func_ptr = reinterpret_cast<FuncPtr>(load_symbol_func("{1}")) ?
reinterpret_cast<FuncPtr>(load_symbol_func("{1}")) :
static auto func_ptr = reinterpret_cast<FuncPtr>(LOAD_SYMBOL_FUNC("{1}")) ?
reinterpret_cast<FuncPtr>(LOAD_SYMBOL_FUNC("{1}")) :
{1}NotFound;
return func_ptr({3});
}}\n""" % (config['calling_conv'], config['calling_conv'])

prolog = """
typedef void *tLoadSymbol(const char *name);
void *{0}LoadSymbol(const char *name);
static tLoadSymbol *load_symbol_func;
#define LOAD_SYMBOL_FUNC {0}##LoadSymbol
void {0}SetSymbolLoader(tLoadSymbol *loader_func) {{
load_symbol_func = loader_func;
}}\n"""
"""

index = clang.cindex.Index.create()
header = args.header
Expand Down

0 comments on commit 1e68f6e

Please sign in to comment.