Use no-redirect dlopen in performance libraries

This commit is contained in:
Andrzej Janik
2025-07-29 23:46:53 +00:00
parent c48fd9f1d8
commit 87f74c8752
8 changed files with 43 additions and 12 deletions

1
Cargo.lock generated
View File

@ -1897,6 +1897,7 @@ dependencies = [
"cuda_types",
"dark_api",
"format",
"libc",
"libloading",
]

View File

@ -7,7 +7,7 @@ fn get_library() -> Option<Library> {
let cuda_lib = std::env::var("ZLUDA_BLAS_LIB")
.ok()
.unwrap_or_else(|| "/usr/local/cuda/lib64/libcublas.so".to_string());
unsafe { Library::new(cuda_lib) }.ok()
zluda_dump_common::dlopen_local_noredirect(cuda_lib).ok()
}
macro_rules! unimplemented {

View File

@ -7,7 +7,7 @@ fn get_library() -> Option<Library> {
let cuda_lib = std::env::var("ZLUDA_BLASLT_LIB")
.ok()
.unwrap_or_else(|| "/usr/local/cuda/lib64/libcublasLt.so".to_string());
unsafe { Library::new(cuda_lib) }.ok()
zluda_dump_common::dlopen_local_noredirect(cuda_lib).ok()
}
macro_rules! unimplemented {

View File

@ -11,3 +11,6 @@ cuda_types = { path = "../cuda_types" }
dark_api = { path = "../dark_api" }
format = { path = "../format" }
cglue = "0.3.5"
[target.'cfg(not(windows))'.dependencies]
libc = "0.2"

View File

@ -33,21 +33,48 @@ fn open_driver() -> Result<libloading::Library, libloading::Error> {
os::open_driver()
}
pub fn dlopen_local_noredirect(
path: String,
) -> Result<libloading::Library, libloading::Error> {
unsafe { os::dlopen_local_noredirect(path) }
}
#[cfg(unix)]
pub(crate) mod os {
use libc::{c_char, c_int};
use libloading::os;
const RTLD_NOLOAD: i32 = 0x4;
use std::{ffi::c_void, mem};
pub fn open_driver() -> Result<libloading::Library, libloading::Error> {
unsafe {
os::unix::Library::open(Some("libcuda.so.1"), RTLD_NOLOAD | os::unix::RTLD_LAZY)
.or_else(|_| {
os::unix::Library::open(Some("libcuda.so"), RTLD_NOLOAD | os::unix::RTLD_LAZY)
})
.map(Into::into)
os::unix::Library::open(
Some("libcuda.so.1"),
libc::RTLD_NOLOAD | os::unix::RTLD_LAZY,
)
.or_else(|_| {
os::unix::Library::open(Some("libcuda.so"), libc::RTLD_NOLOAD | os::unix::RTLD_LAZY)
})
.map(Into::into)
}
}
pub unsafe fn dlopen_local_noredirect(
mut path: String,
) -> Result<libloading::Library, libloading::Error> {
let zluda_dlopen_noredirect =
unsafe { libc::dlsym(libc::RTLD_DEFAULT, c"zluda_dlopen_noredirect".as_ptr()) };
let zluda_dlopen_noredirect = mem::transmute::<
_,
Option<unsafe extern "C" fn(*const c_char, c_int) -> *mut c_void>,
>(zluda_dlopen_noredirect);
let dlopen = zluda_dlopen_noredirect.unwrap_or(libc::dlopen);
path.push('\0');
Ok(libloading::os::unix::Library::from_raw(dlopen(
path.as_ptr().cast(),
os::unix::RTLD_LOCAL | os::unix::RTLD_LAZY,
))
.into())
}
}
#[cfg(windows)]

View File

@ -7,7 +7,7 @@ fn get_library() -> Option<Library> {
let cuda_lib = std::env::var("ZLUDA_DNN_LIB")
.ok()
.unwrap_or_else(|| "/usr/lib/x86_64-linux-gnu/libcudnn.so.9".to_string());
unsafe { Library::new(cuda_lib) }.ok()
zluda_dump_common::dlopen_local_noredirect(cuda_lib).ok()
}
macro_rules! unimplemented {

View File

@ -7,7 +7,7 @@ fn get_library() -> Option<Library> {
let cuda_lib = std::env::var("ZLUDA_FFT_LIB")
.ok()
.unwrap_or_else(|| "/usr/local/cuda/lib64/libcufft.so".to_string());
unsafe { Library::new(cuda_lib) }.ok()
zluda_dump_common::dlopen_local_noredirect(cuda_lib).ok()
}
macro_rules! unimplemented {

View File

@ -7,7 +7,7 @@ fn get_library() -> Option<Library> {
let cuda_lib = std::env::var("ZLUDA_SPARSE_LIB")
.ok()
.unwrap_or_else(|| "/usr/local/cuda/lib64/libcusparse.so".to_string());
unsafe { Library::new(cuda_lib) }.ok()
zluda_dump_common::dlopen_local_noredirect(cuda_lib).ok()
}
macro_rules! unimplemented {