Skip to main content

cuda_host

Function cuda_host 

Source
pub fn cuda_host() -> Option<CudaHost>
Expand description

Host-wide CUDA properties, or None when NVML is unavailable — no NVIDIA driver, the nvidia feature disabled, no device, or a driver reporting values that aren’t usable.

Shares the one process-wide NVML handle with detect, so calling this on a timer does not accumulate resources.

use gpu_probe::ComputeCapability;

if let Some(cuda) = gpu_probe::cuda_host() {
    println!("sm_{}{} on CUDA {}",
        cuda.compute_capability.major,
        cuda.compute_capability.minor,
        cuda.driver_version);

    if cuda.compute_capability >= ComputeCapability::new(8, 0) {
        // pick an Ampere-or-newer build
    }
}
Examples found in repository?
examples/watch.rs (line 93)
44fn main() {
45    let gpus = gpu_probe::detect();
46
47    println!("gpu-probe · {} GPU(s)", gpus.len());
48    println!();
49
50    if gpus.is_empty() {
51        println!("  no GPUs detected");
52    }
53
54    for (index, gpu) in gpus.iter().enumerate() {
55        println!("[{index}] {} · {}", gpu.name, gpu.vendor);
56        // The artifact-selection target, whichever form this vendor reports:
57        // `gfx1013` for ROCm/HIP, `sm_89` for CUDA.
58        match gpu.arch_target {
59            Some(arch) => println!("     arch   {:>10}", arch.to_string()),
60            None => println!("     arch      unavailable"),
61        }
62        println!("     total  {:>10}", gib(gpu.total_bytes));
63        match gpu.used_bytes {
64            Some(used) => println!("     used   {:>10}", gib(used)),
65            None => println!("     used      unknown"),
66        }
67        match gpu.free_bytes {
68            Some(free) => println!("     free   {:>10}", gib(free)),
69            None => println!("     free      unknown"),
70        }
71        println!("     {}", bar(gpu.used_bytes, gpu.total_bytes));
72        println!();
73    }
74
75    // Host-wide toolchains, unlike the per-GPU fields above. Every row always
76    // prints, including when absent: "we looked and found nothing" is the point
77    // of a probe tool, and a lone row for one vendor reads like it describes
78    // the GPU above it rather than the host.
79    //
80    // They are not the same measurement. `cuda` is the driver version from
81    // NVML; `rocm` and `oneapi` are userspace installs, because neither AMD nor
82    // Intel exposes a driver version anywhere. The architecture each build
83    // targets is the per-GPU `arch` row, which needs none of them installed.
84    println!("host");
85    match gpu_probe::oneapi_host() {
86        Some(oneapi) => println!("     oneapi {:>10}", oneapi.version.to_string()),
87        None => println!("     oneapi    unavailable"),
88    }
89    match gpu_probe::rocm_host() {
90        Some(rocm) => println!("     rocm   {:>10}", rocm.version.to_string()),
91        None => println!("     rocm      unavailable"),
92    }
93    match gpu_probe::cuda_host() {
94        Some(cuda) => println!("     cuda   {:>10}", cuda.driver_version.to_string()),
95        None => println!("     cuda      unavailable"),
96    }
97    match gpu_probe::vulkan_host() {
98        Some(vulkan) => println!("     vulkan {:>10}", vulkan.api_version.to_string()),
99        None => println!("     vulkan    unavailable"),
100    }
101}