zc2 0.0.48

P2P compute broker with yen-denominated billing, WAL, and broker mesh support
//! GPU probes: CUDA (dlopen'd at runtime) on Linux/Windows, Metal on macOS.
use super::budget::Budget;
use super::fingerprint::GpuId;
use super::report::GpuMetrics;

pub mod cuda;
pub mod metal;

pub struct GpuOutcome {
    pub gpus: Vec<GpuMetrics>,
    pub skipped: Option<String>,
}

pub fn ids(o: &GpuOutcome) -> Vec<GpuId> {
    o.gpus
        .iter()
        .map(|g| GpuId {
            vendor: g.vendor.clone(),
            model: g.model.clone(),
            uuid: g.uuid.clone(),
            vram_bytes: g.vram_bytes,
        })
        .collect()
}

/// Which GPUs are present, without timing anything (what the idle scheduler
/// fingerprints). Mirrors the identities `probe` reports.
pub fn enumerate() -> Vec<GpuId> {
    if std::env::var("ZAKURO_BENCH_GPU").as_deref() == Ok("0") {
        return vec![];
    }
    #[cfg(any(target_os = "linux", target_os = "windows"))]
    {
        let ids = cuda::enumerate();
        if !ids.is_empty() {
            return ids;
        }
    }
    metal::enumerate()
}

pub fn probe(b: &Budget) -> GpuOutcome {
    probe_inner(std::env::var("ZAKURO_BENCH_GPU").ok().as_deref(), b)
}

fn probe_inner(env: Option<&str>, b: &Budget) -> GpuOutcome {
    if env == Some("0") {
        return GpuOutcome {
            gpus: vec![],
            skipped: Some("gpu: disabled by ZAKURO_BENCH_GPU=0".into()),
        };
    }
    #[cfg(any(target_os = "linux", target_os = "windows"))]
    let mut reason = {
        let o = cuda::probe(b);
        if !o.gpus.is_empty() {
            return o;
        }
        o.skipped
    };
    #[cfg(not(any(target_os = "linux", target_os = "windows")))]
    let mut reason: Option<String> = None;
    match metal::probe(b) {
        Some(Ok(g)) => {
            return GpuOutcome {
                gpus: vec![g],
                skipped: None,
            }
        }
        Some(Err(e)) => reason = Some(format!("gpu: metal: {e}")),
        None => {}
    }
    GpuOutcome {
        gpus: vec![],
        skipped: Some(reason.unwrap_or_else(|| "gpu: no CUDA or Metal runtime".into())),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::{atomic::AtomicBool, Arc};
    use std::time::Duration;

    fn budget(secs: u64) -> Budget {
        Budget::new(Duration::from_secs(secs), Arc::new(AtomicBool::new(false)))
    }

    #[test]
    fn enumerate_names_the_same_gpus_the_probe_measures() {
        let probed = ids(&probe(&budget(10)));
        if !probed.is_empty() {
            assert_eq!(enumerate(), probed);
        }
    }
    #[test]
    fn disabled_by_env_is_skipped() {
        let o = probe_inner(Some("0"), &budget(5));
        assert!(o.gpus.is_empty());
        assert!(o.skipped.unwrap().contains("ZAKURO_BENCH_GPU=0"));
    }
    #[test]
    fn on_a_machine_without_gpus_probe_never_panics() {
        let _ = probe(&budget(5));
    }
    #[test]
    fn ids_mirror_the_metrics() {
        let o = GpuOutcome {
            gpus: vec![GpuMetrics {
                vendor: "nvidia".into(),
                model: "m".into(),
                uuid: "GPU-1".into(),
                tflops_f32: 1.0,
                tflops_f16: 2.0,
                mem_bw_gbps: 3.0,
                vram_bytes: 7,
            }],
            skipped: None,
        };
        let i = ids(&o);
        assert_eq!((i[0].uuid.as_str(), i[0].vram_bytes), ("GPU-1", 7));
    }
    #[test]
    #[ignore]
    fn print_real_gpus() {
        let o = probe(&budget(60));
        println!("gpus: {:#?}\nskipped: {:?}", o.gpus, o.skipped);
    }
}