#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeviceClass {
DiscreteGpu,
IntegratedGpu,
Software,
Unknown,
}
impl DeviceClass {
#[must_use]
pub fn from_device_type(device_type: wgpu::DeviceType, name: &str) -> Self {
match device_type {
wgpu::DeviceType::DiscreteGpu => Self::DiscreteGpu,
wgpu::DeviceType::IntegratedGpu => Self::IntegratedGpu,
wgpu::DeviceType::Cpu => Self::Software,
_ => {
let lower = name.to_lowercase();
if lower.contains("llvmpipe")
|| lower.contains("software")
|| lower.contains("swiftshader")
{
Self::Software
} else {
Self::Unknown
}
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WorkloadType {
Streaming,
Iterative,
F64Builtins,
}
#[derive(Debug, Clone)]
pub struct GpuInfo {
pub index: usize,
pub name: String,
pub device_class: DeviceClass,
pub gflops: f64,
pub busy: bool,
pub f64_builtins_available: bool,
}
impl GpuInfo {
#[must_use]
pub fn supports_f64_builtins(&self) -> bool {
self.f64_builtins_available
}
#[must_use]
pub fn is_compute_capable(&self) -> bool {
matches!(
self.device_class,
DeviceClass::DiscreteGpu | DeviceClass::IntegratedGpu
) && self.gflops >= 500.0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn device_class_from_discrete() {
assert_eq!(
DeviceClass::from_device_type(wgpu::DeviceType::DiscreteGpu, "RTX 4090"),
DeviceClass::DiscreteGpu
);
}
#[test]
fn device_class_from_integrated() {
assert_eq!(
DeviceClass::from_device_type(wgpu::DeviceType::IntegratedGpu, "Intel Iris Xe"),
DeviceClass::IntegratedGpu
);
}
#[test]
fn device_class_from_cpu() {
assert_eq!(
DeviceClass::from_device_type(wgpu::DeviceType::Cpu, "llvmpipe"),
DeviceClass::Software
);
}
#[test]
fn device_class_from_name_fallback() {
assert_eq!(
DeviceClass::from_device_type(wgpu::DeviceType::Other, "SwiftShader"),
DeviceClass::Software
);
assert_eq!(
DeviceClass::from_device_type(wgpu::DeviceType::Other, "unknown device"),
DeviceClass::Unknown
);
}
#[test]
fn gpu_info_f64_builtins() {
let with_builtins = GpuInfo {
index: 0,
name: "RTX 4090".into(),
device_class: DeviceClass::DiscreteGpu,
gflops: 10_000.0,
busy: false,
f64_builtins_available: true,
};
assert!(with_builtins.supports_f64_builtins());
let without_builtins = GpuInfo {
index: 0,
name: "RTX 4090 (NVK)".into(),
device_class: DeviceClass::DiscreteGpu,
gflops: 10_000.0,
busy: false,
f64_builtins_available: false,
};
assert!(!without_builtins.supports_f64_builtins());
}
#[test]
fn gpu_info_compute_capable() {
let discrete = GpuInfo {
index: 0,
name: "RTX 4090".into(),
device_class: DeviceClass::DiscreteGpu,
gflops: 10_000.0,
busy: false,
f64_builtins_available: true,
};
assert!(discrete.is_compute_capable());
let software = GpuInfo {
index: 0,
name: "llvmpipe".into(),
device_class: DeviceClass::Software,
gflops: 50.0,
busy: false,
f64_builtins_available: false,
};
assert!(!software.is_compute_capable());
let weak_gpu = GpuInfo {
index: 0,
name: "Intel Iris".into(),
device_class: DeviceClass::IntegratedGpu,
gflops: 200.0,
busy: false,
f64_builtins_available: false,
};
assert!(!weak_gpu.is_compute_capable());
}
}