Skip to main content

Crate singe_loader

Crate singe_loader 

Source
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 a const constructor per NVIDIA library, such as Library::cublas() or Library::nvjpeg2k(), and Library::new for any other.
  • functions!: turns the declarations of a bindgen extern "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 (), with missing_status! and missing_value! to implement it for a library’s own types.
  • take_absence: returns and clears the Absence that 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 each Attempt the 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 $library on first call.
missing_status
Implements Missing for a library’s status type, with the status for a missing library and for a missing symbol.
missing_value
Implements Missing for a value type that is not a status, returning $value and 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§

Absence
Why a function could not be called.
LoadError
A library or symbol that would not load.

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.