Expand description
Loads the NVIDIA libraries behind Singe’s *-sys crates at run time, so a program that uses them builds and starts on a machine without them.
A function whose library or symbol is missing returns a fixed value of its return type instead of failing to link.
This crate provides:
Library: one library made of one or more files, with aconstconstructor per NVIDIA library, such asLibrary::cublas()orLibrary::nvjpeg2k(), andLibrary::newfor any other.functions!: turns the declarations of a bindgenextern "C"block into functions of the same names and signatures that look up their symbol on first call.Missing: what a function returns when it cannot be called: zero for integers, null for pointers, and nothing for(), withmissing_status!andmissing_value!to implement it for a library’s own types.take_absence: returns and clears theAbsencethat the last status-returning call on the current thread recorded, so a safe wrapper can turn that status into a load error.LoadError: a library with no files on this platform, a library that would not load, with eachAttemptthe loader made, or a missing symbol.
§Example
Load cuBLAS and print where each of its files was found, or each place the loader tried:
use singe_loader::{Library, LoadError};
static CUBLAS: Library = Library::cublas();
fn main() -> anyhow::Result<()> {
match CUBLAS.paths() {
Ok(paths) => {
for path in paths {
println!("{} loaded from {}", CUBLAS.name(), path.display());
}
}
Err(LoadError::NotFound { library, attempts }) => {
for attempt in &attempts {
println!("{library}: {}: {}", attempt.path.display(), attempt.reason);
}
}
Err(error) => return Err(error.into()),
}
Ok(())
}A library loads at most once and stays loaded for the life of the process, so the functions that cached its symbols stay valid.
Its load result is cached too: a library that failed to load returns the same LoadError on every later call.
§Search order
The loader tries each file by name through the system loader’s search first, then in the library directories under CUDA_PATH and CUDA_HOME, and on Linux under /usr/local/cuda and in /run/opengl-driver/lib.
The NVIDIA libraries have file names only on Linux and Windows; elsewhere, loading one returns LoadError::Unsupported.
Macros§
- functions
- Turns the declarations of an
extern "C"block, as bindgen writes them, into functions of the same names and signatures that look up their symbol in$libraryon first call. - missing_
status - Implements
Missingfor a library’s status type, with the status for a missing library and for a missing symbol. - missing_
value - Implements
Missingfor a value type that is not a status, returning$valueand recording nothing.
Structs§
- Attempt
- One place the loader tried a library file, and why it failed.
- Library
- One library behind a set of declarations, made of every file in
files, each of which must load.
Enums§
Traits§
- Missing
- What a function returns when it cannot be called.
Functions§
- take_
absence - Returns and clears the absence the last status-returning function called on this thread recorded.