Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cudarc::driver::is_available() #315

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,8 @@ pub mod safe;
pub mod sys;

pub use safe::*;

/// Check if the driver library is available on the system.
pub fn is_available() -> bool {
sys::load_lib().is_some()
}
24 changes: 15 additions & 9 deletions src/driver/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,22 @@ mod sys_12060;
#[cfg(feature = "cuda-12060")]
pub use sys_12060::*;

pub const LIB_NAME: &str = "cuda";
pub const LIB_NAME_CHOICES: &[&str] = &[LIB_NAME, "nvcuda"];

pub fn load_lib() -> Option<Lib> {
for choice in LIB_NAME_CHOICES {
if let Ok(lib) = unsafe { Lib::new(libloading::library_filename(choice)) } {
return Some(lib);
}
}
None
}

pub unsafe fn lib() -> &'static Lib {
static LIB: std::sync::OnceLock<Lib> = std::sync::OnceLock::new();
LIB.get_or_init(|| {
let lib_name = "cuda";
let choices = [lib_name, "nvcuda"];
for choice in choices {
if let Ok(lib) = Lib::new(libloading::library_filename(choice)) {
return lib;
}
}
crate::panic_no_lib_found(lib_name, &choices);
LIB.get_or_init(|| match load_lib() {
Some(lib) => lib,
None => crate::panic_no_lib_found(LIB_NAME, LIB_NAME_CHOICES),
})
}
Loading