device_query/device_query.rs
1//! Enumerate CUDA devices via the Runtime API, print compute capability
2//! and total global memory for each.
3//!
4//! Mirrors `nvidia-smi -L` (minus the rich properties), demonstrating
5//! the Runtime-API view: a device is just an ordinal, the primary
6//! context is implicit.
7//!
8//! Run with:
9//!
10//! ```text
11//! cargo run --example device_query -p baracuda-runtime
12//! ```
13
14use baracuda_runtime::{
15 device_synchronize, driver_version, query::device_properties, runtime_version, Device,
16};
17
18fn main() -> Result<(), Box<dyn std::error::Error>> {
19 println!("CUDA driver version: {}", driver_version()?);
20 println!("CUDA runtime version: {}", runtime_version()?);
21
22 let devices = Device::all()?;
23 println!("found {} CUDA device(s)", devices.len());
24
25 for dev in &devices {
26 // Switch current-thread device before querying properties:
27 // `device_properties` reads via `cudaGetDeviceProperties`, which
28 // is keyed off the ordinal we pass — but other Runtime calls
29 // (memory queries, kernel launches) operate on the current
30 // device, so it's good practice to bind first.
31 dev.set_current()?;
32 let props = device_properties(dev)?;
33 let (cc_major, cc_minor) = dev.compute_capability()?;
34 let sm_count = dev.multiprocessor_count()?;
35 let warp = dev.warp_size()?;
36
37 let gib = (props.total_global_memory_bytes as f64) / (1024.0 * 1024.0 * 1024.0);
38 println!(
39 " [{}] {:30} cc {}.{} {} SMs warp {} {:.2} GiB",
40 dev.ordinal(),
41 props.name,
42 cc_major,
43 cc_minor,
44 sm_count,
45 warp,
46 gib,
47 );
48 }
49
50 // Make sure all pending work on every device (none here) is done.
51 device_synchronize()?;
52 println!("OK");
53 Ok(())
54}