use super::GpuOutcome;
use crate::bench_node::budget::Budget;
use crate::bench_node::fingerprint::GpuId;
use crate::bench_node::report::GpuMetrics;
use std::ffi::c_void;
use std::time::{Duration, Instant};
const N_START: usize = 8192;
const N_MIN: usize = 2048;
const TIMED_RUNS: usize = 5;
const COPY_BYTES: u64 = 1 << 30;
const COPY_MIN_BYTES: u64 = 64 << 20;
const MIN_FREE_FRACTION: f64 = 0.8;
const FREE_SHARE_DIVISOR: u64 = 4;
const GIB: f64 = (1u64 << 30) as f64;
pub const MUSL_REASON: &str = "gpu: this zc build is statically linked (musl) and cannot load the NVIDIA driver; run `zc update` on a GPU host with glibc >= 2.31 to get the glibc build";
fn static_build_reason(musl: bool) -> Option<&'static str> {
musl.then_some(MUSL_REASON)
}
const CUDA_R_32F: i32 = 0;
const CUDA_R_16F: i32 = 2;
const CUBLAS_COMPUTE_32F: i32 = 68;
const CUBLAS_GEMM_DEFAULT: i32 = -1;
const CUBLAS_GEMM_DEFAULT_TENSOR_OP: i32 = 99;
const CUBLAS_OP_N: i32 = 0;
#[derive(Clone, Copy, PartialEq, Debug)]
enum Dtype {
F32,
F16,
}
impl Dtype {
fn size(self) -> u64 {
match self {
Dtype::F32 => 4,
Dtype::F16 => 2,
}
}
}
trait Api {
fn cu_init(&self) -> i32;
fn device_count(&self, n: &mut i32) -> i32;
fn device_name(&self, dev: i32, name: &mut String) -> i32;
fn device_uuid(&self, dev: i32, uuid: &mut [u8; 16]) -> i32;
fn total_mem(&self, dev: i32, bytes: &mut u64) -> i32;
fn mem_info(&self, free: &mut u64, total: &mut u64) -> i32;
fn ctx_create(&self, dev: i32, ctx: &mut usize) -> i32;
fn ctx_destroy(&self, ctx: usize) -> i32;
fn mem_alloc(&self, bytes: u64, ptr: &mut u64) -> i32;
fn memset_d8(&self, ptr: u64, value: u8, bytes: u64) -> i32;
fn memcpy_dtod(&self, dst: u64, src: u64, bytes: u64) -> i32;
fn ctx_sync(&self) -> i32;
fn free(&self, ptr: u64) -> i32;
fn cublas_create(&self, handle: &mut usize) -> i32;
fn cublas_destroy(&self, handle: usize) -> i32;
fn gemm_ex(&self, handle: usize, n: usize, dtype: Dtype, a: u64, b: u64, c: u64) -> i32;
}
fn tflops(n: usize, d: Duration) -> f64 {
2.0 * (n as f64).powi(3) / d.as_secs_f64() / 1e12
}
fn uuid_string(b: &[u8; 16]) -> String {
let h: String = b.iter().map(|x| format!("{x:02x}")).collect();
format!(
"GPU-{}-{}-{}-{}-{}",
&h[0..8],
&h[8..12],
&h[12..16],
&h[16..20],
&h[20..32]
)
}
fn check(what: &str, code: i32) -> Result<(), String> {
if code == 0 {
Ok(())
} else {
Err(format!("{what} returned {code}"))
}
}
struct Guard<'a> {
api: &'a dyn Api,
ctx: usize,
handle: Option<usize>,
ptrs: Vec<u64>,
}
impl Guard<'_> {
fn alloc(&mut self, bytes: u64) -> Result<u64, String> {
let mut p = 0u64;
check("cuMemAlloc", self.api.mem_alloc(bytes, &mut p))?;
self.ptrs.push(p);
Ok(p)
}
fn free_all(&mut self) {
for p in self.ptrs.drain(..) {
self.api.free(p);
}
}
}
impl Drop for Guard<'_> {
fn drop(&mut self) {
self.free_all();
if let Some(h) = self.handle.take() {
self.api.cublas_destroy(h);
}
self.api.ctx_destroy(self.ctx);
}
}
fn not_expired(b: &Budget) -> Result<(), String> {
if b.expired() {
Err("budget expired".into())
} else {
Ok(())
}
}
fn gemm_n_for(cap_bytes: u64) -> usize {
let mut n = N_START;
while n > N_MIN && 3 * (n * n) as u64 * Dtype::F32.size() > cap_bytes {
n /= 2;
}
n
}
fn copy_bytes_for(vram: u64, free: u64) -> u64 {
COPY_BYTES.min(vram / 4).min(free / FREE_SHARE_DIVISOR / 2)
}
fn busy_reason(model: &str, free: u64, total: u64) -> Option<String> {
((free as f64) < total as f64 * MIN_FREE_FRACTION).then(|| {
format!(
"gpu: {model} busy ({:.1} GiB used by other processes)",
total.saturating_sub(free) as f64 / GIB
)
})
}
pub(crate) fn is_busy_note(note: &str) -> bool {
note.starts_with("gpu: ") && note.contains(" busy (")
}
enum DeviceSkip {
Failed(String),
Busy(String),
}
impl From<String> for DeviceSkip {
fn from(e: String) -> Self {
DeviceSkip::Failed(e)
}
}
fn alloc_gemm_buffers(g: &mut Guard, cap_bytes: u64) -> Result<(usize, [u64; 3]), String> {
let mut n = gemm_n_for(cap_bytes);
loop {
let bytes = (n * n) as u64 * Dtype::F32.size();
let got = (|| Ok::<_, String>([g.alloc(bytes)?, g.alloc(bytes)?, g.alloc(bytes)?]))();
match got {
Ok(p) => return Ok((n, p)),
Err(e) if n <= N_MIN => return Err(e),
Err(_) => {
g.free_all();
n /= 2;
}
}
}
}
fn alloc_copy_buffers(g: &mut Guard, mut bytes: u64) -> Result<(u64, u64, u64), String> {
loop {
match (g.alloc(bytes), g.alloc(bytes)) {
(Ok(s), Ok(d)) => return Ok((bytes, s, d)),
(_, Err(e)) | (Err(e), _) if bytes <= COPY_MIN_BYTES => return Err(e),
_ => {
g.free_all();
bytes /= 2;
}
}
}
}
fn time_gemm(g: &Guard, h: usize, n: usize, dt: Dtype, p: [u64; 3]) -> Result<f64, String> {
check("cublasGemmEx", g.api.gemm_ex(h, n, dt, p[0], p[1], p[2]))?; check("cuCtxSynchronize", g.api.ctx_sync())?;
let t = Instant::now();
for _ in 0..TIMED_RUNS {
check("cublasGemmEx", g.api.gemm_ex(h, n, dt, p[0], p[1], p[2]))?;
}
check("cuCtxSynchronize", g.api.ctx_sync())?;
Ok(tflops(n, t.elapsed() / TIMED_RUNS as u32))
}
fn probe_device(
api: &dyn Api,
dev: i32,
b: &Budget,
) -> Result<(GpuMetrics, Option<String>), DeviceSkip> {
let mut name = String::new();
check("cuDeviceGetName", api.device_name(dev, &mut name))?;
let mut uuid = [0u8; 16];
check("cuDeviceGetUuid", api.device_uuid(dev, &mut uuid))?;
let mut vram = 0u64;
check("cuDeviceTotalMem", api.total_mem(dev, &mut vram))?;
let mut ctx = 0usize;
check("cuCtxCreate", api.ctx_create(dev, &mut ctx))?;
let mut g = Guard {
api,
ctx,
handle: None,
ptrs: vec![],
};
let (mut free, mut total) = (0u64, 0u64);
check("cuMemGetInfo", api.mem_info(&mut free, &mut total))?;
if let Some(why) = busy_reason(&name, free, total) {
return Err(DeviceSkip::Busy(why));
}
let mut h = 0usize;
check("cublasCreate", api.cublas_create(&mut h))?;
g.handle = Some(h);
not_expired(b)?;
let (n, p) = alloc_gemm_buffers(&mut g, free / FREE_SHARE_DIVISOR)?;
for ptr in p {
check(
"cuMemsetD8",
api.memset_d8(ptr, 0x3c, (n * n) as u64 * Dtype::F32.size()),
)?;
}
let tflops_f32 = time_gemm(&g, h, n, Dtype::F32, p)?;
let mut notes: Vec<String> = vec![];
let tflops_f16 = if b.expired() {
notes.push("budget expired before fp16".to_string());
0.0
} else {
match time_gemm(&g, h, n, Dtype::F16, p) {
Ok(t) => t,
Err(e) => {
notes.push(format!("fp16 not measured ({e})"));
0.0
}
}
};
g.free_all();
let mem_bw_gbps = if b.expired() {
notes.push("budget expired before the copy bandwidth".to_string());
0.0
} else {
match copy_bandwidth(&mut g, copy_bytes_for(vram, free)) {
Ok(v) => v,
Err(e) => {
notes.push(format!("copy bandwidth not measured ({e})"));
0.0
}
}
};
let m = GpuMetrics {
vendor: "nvidia".into(),
model: name,
uuid: uuid_string(&uuid),
tflops_f32,
tflops_f16,
mem_bw_gbps,
vram_bytes: vram,
};
Ok((m, (!notes.is_empty()).then(|| notes.join(", "))))
}
fn copy_bandwidth(g: &mut Guard, bytes: u64) -> Result<f64, String> {
let api = g.api;
let (bytes, src, dst) = alloc_copy_buffers(g, bytes)?;
check("cuMemcpyDtoD", api.memcpy_dtod(dst, src, bytes))?; check("cuCtxSynchronize", api.ctx_sync())?;
let t = Instant::now();
for _ in 0..TIMED_RUNS {
check("cuMemcpyDtoD", api.memcpy_dtod(dst, src, bytes))?;
}
check("cuCtxSynchronize", api.ctx_sync())?;
Ok(2.0 * bytes as f64 * TIMED_RUNS as f64 / t.elapsed().as_secs_f64() / 1e9)
}
fn run_with_api(api: &dyn Api, b: &Budget) -> GpuOutcome {
let skip = |s: String| GpuOutcome {
gpus: vec![],
skipped: Some(s),
};
let code = api.cu_init();
if code != 0 {
return skip(format!("gpu: cuInit returned {code}"));
}
let mut count = 0;
let code = api.device_count(&mut count);
if code != 0 {
return skip(format!("gpu: cuDeviceGetCount returned {code}"));
}
if count == 0 {
return skip("gpu: CUDA reports no devices".into());
}
let (mut gpus, mut skipped) = (vec![], vec![]);
for dev in 0..count {
if b.expired() {
let rest: Vec<String> = (dev..count).map(|d| d.to_string()).collect();
skipped.push(format!(
"gpu {}: budget expired, not measured",
rest.join(",")
));
break;
}
match probe_device(api, dev, b) {
Ok((m, note)) => {
gpus.push(m);
skipped.extend(note.map(|n| format!("gpu {dev}: {n}")));
}
Err(DeviceSkip::Busy(why)) => skipped.push(why),
Err(DeviceSkip::Failed(e)) => skipped.push(format!("gpu {dev}: {e}")),
}
}
GpuOutcome {
gpus,
skipped: (!skipped.is_empty()).then(|| skipped.join("; ")),
}
}
fn lib_paths() -> (&'static [&'static str], &'static [&'static str]) {
#[cfg(windows)]
return (&["nvcuda.dll"], &["cublas64_12.dll", "cublas64_11.dll"]);
#[cfg(not(windows))]
return (
&["libcuda.so.1", "libcuda.so"],
&["libcublas.so.12", "libcublas.so.11"],
);
}
pub fn probe(b: &Budget) -> GpuOutcome {
if let Some(reason) = static_build_reason(cfg!(target_env = "musl")) {
return GpuOutcome {
gpus: vec![],
skipped: Some(reason.into()),
};
}
match shared_dl() {
Ok(dl) => run_with_api(dl, b),
Err(e) => GpuOutcome {
gpus: vec![],
skipped: Some(format!("gpu: {e}")),
},
}
}
pub fn enumerate() -> Vec<GpuId> {
if static_build_reason(cfg!(target_env = "musl")).is_some() {
return vec![];
}
match shared_dl() {
Ok(dl) => enumerate_with_api(dl),
Err(_) => vec![],
}
}
fn enumerate_with_api(api: &dyn Api) -> Vec<GpuId> {
let mut count = 0;
if api.cu_init() != 0 || api.device_count(&mut count) != 0 {
return vec![];
}
(0..count)
.filter_map(|dev| {
let mut name = String::new();
let mut uuid = [0u8; 16];
let mut vram = 0u64;
(api.device_name(dev, &mut name) == 0
&& api.device_uuid(dev, &mut uuid) == 0
&& api.total_mem(dev, &mut vram) == 0)
.then(|| GpuId {
vendor: "nvidia".into(),
model: name,
uuid: uuid_string(&uuid),
vram_bytes: vram,
})
})
.collect()
}
static SHARED: std::sync::Mutex<Option<&'static Dl>> = std::sync::Mutex::new(None);
fn shared_dl() -> Result<&'static Dl, String> {
let (cuda, cublas) = lib_paths();
load_once(&SHARED, || Dl::open(cuda, cublas))
}
fn load_once<T>(
slot: &std::sync::Mutex<Option<&'static T>>,
load: impl FnOnce() -> Result<T, String>,
) -> Result<&'static T, String> {
let mut g = slot.lock().unwrap_or_else(|e| e.into_inner());
if let Some(v) = *g {
return Ok(v);
}
let v: &'static T = Box::leak(Box::new(load()?));
*g = Some(v);
Ok(v)
}
struct Dl {
cuda: libloading::Library,
cublas: libloading::Library,
}
fn open_first(kind: &str, paths: &[&str]) -> Result<libloading::Library, String> {
for p in paths {
if let Ok(l) = unsafe { libloading::Library::new(p) } {
return Ok(l);
}
}
Err(format!("{kind} not found (tried {})", paths.join(", ")))
}
impl Dl {
fn open(cuda: &[&str], cublas: &[&str]) -> Result<Dl, String> {
let cuda = open_first("libcuda", cuda)?;
let cublas = open_first("libcublas", cublas)?;
Ok(Dl { cuda, cublas })
}
}
macro_rules! call {
($lib:expr, $name:literal, ($($t:ty),*), $($a:expr),*) => {{
match unsafe { $lib.get::<unsafe extern "C" fn($($t),*) -> i32>(concat!($name, "\0").as_bytes()) } {
Ok(f) => unsafe { f($($a),*) },
Err(_) => -1,
}
}};
}
type Ptr = *mut c_void;
impl Api for Dl {
fn cu_init(&self) -> i32 {
call!(self.cuda, "cuInit", (u32), 0)
}
fn device_count(&self, n: &mut i32) -> i32 {
call!(self.cuda, "cuDeviceGetCount", (*mut i32), n)
}
fn device_name(&self, dev: i32, name: &mut String) -> i32 {
let mut buf = [0u8; 256];
let c = call!(
self.cuda,
"cuDeviceGetName",
(*mut u8, i32, i32),
buf.as_mut_ptr(),
256,
dev
);
if c == 0 {
let end = buf.iter().position(|&x| x == 0).unwrap_or(buf.len());
*name = String::from_utf8_lossy(&buf[..end]).trim().to_string();
}
c
}
fn device_uuid(&self, dev: i32, uuid: &mut [u8; 16]) -> i32 {
call!(
self.cuda,
"cuDeviceGetUuid_v2",
(*mut u8, i32),
uuid.as_mut_ptr(),
dev
)
}
fn total_mem(&self, dev: i32, bytes: &mut u64) -> i32 {
call!(
self.cuda,
"cuDeviceTotalMem_v2",
(*mut u64, i32),
bytes,
dev
)
}
fn mem_info(&self, free: &mut u64, total: &mut u64) -> i32 {
let (mut f, mut t) = (0usize, 0usize);
let c = call!(
self.cuda,
"cuMemGetInfo_v2",
(*mut usize, *mut usize),
&mut f,
&mut t
);
*free = f as u64;
*total = t as u64;
c
}
fn ctx_create(&self, dev: i32, ctx: &mut usize) -> i32 {
let mut p: Ptr = std::ptr::null_mut();
let c = call!(
self.cuda,
"cuCtxCreate_v2",
(*mut Ptr, u32, i32),
&mut p,
0,
dev
);
*ctx = p as usize;
c
}
fn ctx_destroy(&self, ctx: usize) -> i32 {
call!(self.cuda, "cuCtxDestroy_v2", (Ptr), ctx as Ptr)
}
fn mem_alloc(&self, bytes: u64, ptr: &mut u64) -> i32 {
call!(
self.cuda,
"cuMemAlloc_v2",
(*mut u64, usize),
ptr,
bytes as usize
)
}
fn memset_d8(&self, ptr: u64, value: u8, bytes: u64) -> i32 {
call!(
self.cuda,
"cuMemsetD8_v2",
(u64, u8, usize),
ptr,
value,
bytes as usize
)
}
fn memcpy_dtod(&self, dst: u64, src: u64, bytes: u64) -> i32 {
call!(
self.cuda,
"cuMemcpyDtoD_v2",
(u64, u64, usize),
dst,
src,
bytes as usize
)
}
fn ctx_sync(&self) -> i32 {
call!(self.cuda, "cuCtxSynchronize", (),)
}
fn free(&self, ptr: u64) -> i32 {
call!(self.cuda, "cuMemFree_v2", (u64), ptr)
}
fn cublas_create(&self, handle: &mut usize) -> i32 {
let mut p: Ptr = std::ptr::null_mut();
let c = call!(self.cublas, "cublasCreate_v2", (*mut Ptr), &mut p);
*handle = p as usize;
c
}
fn cublas_destroy(&self, handle: usize) -> i32 {
call!(self.cublas, "cublasDestroy_v2", (Ptr), handle as Ptr)
}
fn gemm_ex(&self, handle: usize, n: usize, dtype: Dtype, a: u64, b: u64, c: u64) -> i32 {
let (ty, algo) = match dtype {
Dtype::F32 => (CUDA_R_32F, CUBLAS_GEMM_DEFAULT),
Dtype::F16 => (CUDA_R_16F, CUBLAS_GEMM_DEFAULT_TENSOR_OP),
};
let (alpha, beta) = (1.0f32, 0.0f32);
let n = n as i32;
call!(
self.cublas,
"cublasGemmEx",
(
Ptr,
i32,
i32,
i32,
i32,
i32,
*const f32,
u64,
i32,
i32,
u64,
i32,
i32,
*const f32,
u64,
i32,
i32,
i32,
i32
),
handle as Ptr,
CUBLAS_OP_N,
CUBLAS_OP_N,
n,
n,
n,
&alpha,
a,
ty,
n,
b,
ty,
n,
&beta,
c,
ty,
n,
CUBLAS_COMPUTE_32F,
algo
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{atomic::AtomicBool, Arc};
fn budget(secs: u64) -> Budget {
Budget::new(Duration::from_secs(secs), Arc::new(AtomicBool::new(false)))
}
#[derive(Default)]
struct FakeApi {
init_code: i32,
devices: i32,
bad_ctx_dev: Option<i32>,
max_alloc: u64,
gemm_sleep_ms: u64,
fail_f16: bool,
vram: u64,
mem_free: Option<u64>,
largest_alloc: AtomicU64,
next_ptr: AtomicU64,
live: AtomicU64,
}
impl FakeApi {
fn total(&self) -> u64 {
if self.vram == 0 {
24 << 30
} else {
self.vram
}
}
}
impl Api for FakeApi {
fn cu_init(&self) -> i32 {
self.init_code
}
fn device_count(&self, n: &mut i32) -> i32 {
*n = self.devices;
0
}
fn device_name(&self, dev: i32, name: &mut String) -> i32 {
*name = format!("Fake GPU {dev}");
0
}
fn device_uuid(&self, dev: i32, uuid: &mut [u8; 16]) -> i32 {
uuid[0] = 0xab;
uuid[15] = dev as u8;
0
}
fn total_mem(&self, _: i32, bytes: &mut u64) -> i32 {
*bytes = self.total();
0
}
fn mem_info(&self, free: &mut u64, total: &mut u64) -> i32 {
*total = self.total();
*free = self.mem_free.unwrap_or(*total);
0
}
fn ctx_create(&self, dev: i32, ctx: &mut usize) -> i32 {
if self.bad_ctx_dev == Some(dev) {
return 201;
}
*ctx = 1;
0
}
fn ctx_destroy(&self, _: usize) -> i32 {
0
}
fn mem_alloc(&self, bytes: u64, ptr: &mut u64) -> i32 {
if self.max_alloc != 0 && bytes > self.max_alloc {
return 2;
}
self.largest_alloc.fetch_max(bytes, Ordering::SeqCst);
*ptr = self.next_ptr.fetch_add(1, Ordering::SeqCst) + 1;
self.live.fetch_add(1, Ordering::SeqCst);
0
}
fn memset_d8(&self, _: u64, _: u8, _: u64) -> i32 {
0
}
fn memcpy_dtod(&self, _: u64, _: u64, _: u64) -> i32 {
std::thread::sleep(Duration::from_millis(1));
0
}
fn ctx_sync(&self) -> i32 {
0
}
fn free(&self, _: u64) -> i32 {
self.live.fetch_sub(1, Ordering::SeqCst);
0
}
fn cublas_create(&self, h: &mut usize) -> i32 {
*h = 1;
0
}
fn cublas_destroy(&self, _: usize) -> i32 {
0
}
fn gemm_ex(&self, _: usize, _: usize, d: Dtype, _: u64, _: u64, _: u64) -> i32 {
if self.fail_f16 && d == Dtype::F16 {
return 13;
}
std::thread::sleep(Duration::from_millis(self.gemm_sleep_ms));
0
}
}
#[test]
fn missing_library_is_empty_with_a_reason() {
let e = Dl::open(
&["/nonexistent/libcuda.so.1"],
&["/nonexistent/libcublas.so.12"],
)
.err()
.unwrap();
assert!(e.contains("libcuda"), "{e}");
}
#[test]
fn cuda_init_failure_is_empty_not_error() {
let o = run_with_api(
&FakeApi {
init_code: 100,
..FakeApi::default()
},
&budget(5),
);
assert!(o.gpus.is_empty());
assert!(o.skipped.unwrap().contains("cuInit"));
}
#[test]
fn timing_turns_into_tflops() {
assert!((tflops(8192, Duration::from_millis(10)) - 109.951).abs() < 0.01);
}
#[test]
fn a_healthy_device_is_measured_and_everything_is_freed() {
let api = FakeApi {
devices: 1,
gemm_sleep_ms: 10,
..FakeApi::default()
};
let o = run_with_api(&api, &budget(30));
assert!(o.skipped.is_none(), "{:?}", o.skipped);
let g = &o.gpus[0];
assert_eq!(
(g.vendor.as_str(), g.model.as_str()),
("nvidia", "Fake GPU 0")
);
assert!(g.uuid.starts_with("GPU-ab000000-") && g.uuid.ends_with("-000000000000"));
assert!(g.tflops_f32 > 0.0 && g.tflops_f16 > 0.0 && g.mem_bw_gbps > 0.0);
assert_eq!(g.vram_bytes, 24 << 30);
assert_eq!(api.live.load(Ordering::SeqCst), 0, "leaked device memory");
}
#[test]
fn allocation_failure_halves_n_until_it_fits() {
let api = FakeApi {
devices: 1,
max_alloc: 64 << 20,
gemm_sleep_ms: 1,
..FakeApi::default()
};
let o = run_with_api(&api, &budget(30));
assert_eq!(o.gpus.len(), 1, "{:?}", o.skipped);
assert_eq!(api.live.load(Ordering::SeqCst), 0);
}
#[test]
fn a_failing_device_is_skipped_and_the_others_still_run() {
let api = FakeApi {
devices: 2,
bad_ctx_dev: Some(0),
gemm_sleep_ms: 1,
..FakeApi::default()
};
let o = run_with_api(&api, &budget(30));
assert_eq!(o.gpus.len(), 1);
assert_eq!(o.gpus[0].model, "Fake GPU 1");
let s = o.skipped.unwrap();
assert!(s.contains("gpu 0") && s.contains("cuCtxCreate"), "{s}");
}
#[test]
fn an_expired_budget_stops_before_any_gemm() {
let c = Arc::new(AtomicBool::new(true));
let o = run_with_api(
&FakeApi {
devices: 2,
..FakeApi::default()
},
&Budget::new(Duration::from_secs(60), c),
);
assert!(o.gpus.is_empty());
assert!(o.skipped.unwrap().contains("budget expired"));
}
#[test]
fn an_fp16_failure_keeps_the_fp32_result() {
let api = FakeApi {
devices: 1,
fail_f16: true,
..FakeApi::default()
};
let o = run_with_api(&api, &budget(30));
assert_eq!(o.gpus.len(), 1);
assert!(o.gpus[0].tflops_f32 > 0.0);
assert_eq!(o.gpus[0].tflops_f16, 0.0);
assert!(o.skipped.unwrap().contains("fp16 not measured"));
assert_eq!(api.live.load(Ordering::SeqCst), 0);
}
#[test]
fn devices_left_unmeasured_by_an_expired_budget_are_named() {
let api = FakeApi {
devices: 3,
gemm_sleep_ms: 1,
..FakeApi::default()
};
let b = Budget::new(Duration::from_millis(20), Arc::new(AtomicBool::new(false)));
let o = run_with_api(&api, &b);
let s = o.skipped.unwrap();
assert!(s.contains("budget expired") && s.contains('2'), "{s}");
}
#[test]
fn a_gpu_busy_with_another_process_is_skipped_and_nothing_is_timed() {
let api = FakeApi {
devices: 1,
mem_free: Some(4 << 30), ..FakeApi::default()
};
let o = run_with_api(&api, &budget(30));
assert!(o.gpus.is_empty());
let s = o.skipped.unwrap();
assert_eq!(s, "gpu: Fake GPU 0 busy (20.0 GiB used by other processes)");
assert_eq!(
api.largest_alloc.load(Ordering::SeqCst),
0,
"allocated on a busy GPU"
);
assert_eq!(api.live.load(Ordering::SeqCst), 0);
}
#[test]
fn the_busy_threshold_is_eighty_percent_free() {
assert!(busy_reason("m", 79, 100).is_some());
assert!(busy_reason("m", 80, 100).is_none());
assert!(busy_reason("m", 100, 100).is_none());
}
#[test]
fn every_busy_device_is_named() {
let api = FakeApi {
devices: 2,
mem_free: Some(1 << 30),
..FakeApi::default()
};
let s = run_with_api(&api, &budget(30)).skipped.unwrap();
assert!(
s.contains("Fake GPU 0 busy") && s.contains("Fake GPU 1 busy"),
"{s}"
);
}
#[test]
fn allocations_are_capped_at_a_quarter_of_the_free_memory() {
let api = FakeApi {
devices: 1,
vram: 1 << 30,
gemm_sleep_ms: 1,
..FakeApi::default()
};
let o = run_with_api(&api, &budget(30));
assert_eq!(o.gpus.len(), 1, "{:?}", o.skipped);
assert_eq!(gemm_n_for(256 << 20), 4096);
assert_eq!(copy_bytes_for(1 << 30, 1 << 30), 128 << 20);
assert!(api.largest_alloc.load(Ordering::SeqCst) * 2 <= 256 << 20);
assert_eq!(gemm_n_for(24 << 30), N_START);
assert_eq!(copy_bytes_for(24 << 30, 24 << 30), COPY_BYTES);
assert_eq!(gemm_n_for(0), N_MIN);
}
#[test]
fn busy_notes_are_recognised() {
let n = busy_reason("RTX", 1 << 30, 24 << 30).unwrap();
assert!(is_busy_note(&n), "{n}");
assert!(!is_busy_note("gpu 0: cuMemAlloc failed"));
}
#[test]
fn fp32_survives_a_budget_that_expires_after_it() {
let api = FakeApi {
devices: 1,
gemm_sleep_ms: 60,
..FakeApi::default()
};
let b = Budget::new(Duration::from_millis(250), Arc::new(AtomicBool::new(false)));
let o = run_with_api(&api, &b);
assert_eq!(o.gpus.len(), 1, "fp32 result was dropped: {:?}", o.skipped);
assert!(o.gpus[0].tflops_f32 > 0.0);
assert_eq!(o.gpus[0].tflops_f16, 0.0);
assert!(o.skipped.unwrap().contains("budget expired"));
assert_eq!(api.live.load(Ordering::SeqCst), 0);
}
#[test]
fn the_driver_is_loaded_once_and_reused() {
use std::sync::atomic::AtomicUsize;
static SLOT: std::sync::Mutex<Option<&'static u32>> = std::sync::Mutex::new(None);
static LOADS: AtomicUsize = AtomicUsize::new(0);
let load = || {
LOADS.fetch_add(1, Ordering::SeqCst);
Ok(7u32)
};
let a = load_once(&SLOT, load).unwrap();
let b = load_once(&SLOT, load).unwrap();
assert!(std::ptr::eq(a, b));
assert_eq!(LOADS.load(Ordering::SeqCst), 1);
static EMPTY: std::sync::Mutex<Option<&'static u32>> = std::sync::Mutex::new(None);
assert!(load_once(&EMPTY, || Err("no driver".into())).is_err());
assert_eq!(*load_once(&EMPTY, || Ok(1u32)).unwrap(), 1);
}
#[test]
fn a_static_musl_build_says_why_it_has_no_gpu() {
let r = static_build_reason(true).unwrap();
assert!(
r.contains("statically linked (musl)")
&& r.contains("zc update")
&& r.contains("glibc build"),
"{r}"
);
assert_eq!(static_build_reason(false), None);
#[cfg(target_env = "musl")]
assert_eq!(probe(&budget(1)).skipped.as_deref(), Some(MUSL_REASON));
}
}