use std::sync::Arc;
use oxidelake_core::{BackendKind, EngineError};
use crate::backend::GpuBackend;
use crate::cpu::CpuBackend;
pub const BACKEND_ENV: &str = "OXIDE_BACKEND";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Availability {
pub cuda: bool,
pub metal: bool,
}
impl Availability {
pub const fn best(self) -> BackendKind {
if self.cuda {
BackendKind::Cuda
} else if self.metal {
BackendKind::Metal
} else {
BackendKind::CpuSimd
}
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct HardwareDetector;
impl HardwareDetector {
pub fn availability() -> Availability {
Availability {
cuda: cuda_probe().is_ok(),
metal: metal_available(),
}
}
pub fn detected_kind() -> BackendKind {
Self::availability().best()
}
pub fn select() -> Result<Arc<dyn GpuBackend>, EngineError> {
Self::select_with(std::env::var(BACKEND_ENV).ok().as_deref())
}
pub fn select_with(requested: Option<&str>) -> Result<Arc<dyn GpuBackend>, EngineError> {
let cuda = cuda_probe();
let availability = Availability {
cuda: cuda.is_ok(),
metal: metal_available(),
};
let kind = match requested.map(str::trim).filter(|s| !s.is_empty()) {
Some(name) => name.parse::<BackendKind>()?,
None => {
let best = availability.best();
if let Err(why) = &cuda {
passed_over_cuda(why, best);
}
best
}
};
Self::instantiate(kind, availability, cuda.err())
}
fn instantiate(
kind: BackendKind,
availability: Availability,
cuda_unavailable: Option<EngineError>,
) -> Result<Arc<dyn GpuBackend>, EngineError> {
match kind {
BackendKind::CpuSimd => Ok(Arc::new(CpuBackend::new())),
BackendKind::Cuda => {
if !availability.cuda {
return Err(unavailable(
kind,
cfg!(feature = "cuda"),
cuda_unavailable.as_ref(),
));
}
instantiate_cuda()
}
BackendKind::Metal => {
if !availability.metal {
return Err(unavailable(
kind,
cfg!(all(feature = "metal", target_os = "macos")),
None,
));
}
instantiate_metal()
}
other => Err(EngineError::plan(format!(
"backend `{other}` has no implementation in this build of \
oxidelake-device; it was added to BackendKind without a \
backend behind it"
))),
}
}
}
fn unavailable(kind: BackendKind, compiled_in: bool, why: Option<&EngineError>) -> EngineError {
let detail = match why {
_ if !compiled_in => {
format!("explicitly requested but OxideLake was built without the `{kind}` feature")
}
Some(EngineError::Device { detail, .. }) => format!("explicitly requested but {detail}"),
Some(other) => format!("explicitly requested but {other}"),
None => format!(
"explicitly requested but no {kind} device or driver is present on this machine"
),
};
EngineError::device(kind, detail)
}
#[cfg(feature = "cuda")]
fn passed_over_cuda(why: &EngineError, selected: BackendKind) {
if crate::cuda::CudaBackend::driver_present() {
tracing::warn!(
reason = %why,
selected = %selected,
"an NVIDIA driver is installed but CUDA cannot run on this machine; \
selecting the next backend"
);
} else {
tracing::debug!(reason = %why, selected = %selected, "CUDA not detected");
}
}
#[cfg(not(feature = "cuda"))]
fn passed_over_cuda(_why: &EngineError, _selected: BackendKind) {}
#[cfg(feature = "cuda")]
fn cuda_probe() -> Result<(), EngineError> {
crate::cuda::CudaBackend::probe(0)
}
#[cfg(not(feature = "cuda"))]
fn cuda_probe() -> Result<(), EngineError> {
Err(EngineError::device(
BackendKind::Cuda,
"OxideLake was built without the `cuda` feature",
))
}
#[cfg(feature = "cuda")]
fn instantiate_cuda() -> Result<Arc<dyn GpuBackend>, EngineError> {
Ok(Arc::new(crate::cuda::CudaBackend::new(0)?))
}
#[cfg(not(feature = "cuda"))]
fn instantiate_cuda() -> Result<Arc<dyn GpuBackend>, EngineError> {
Err(unavailable(BackendKind::Cuda, false, None))
}
#[cfg(all(feature = "metal", target_os = "macos"))]
fn metal_available() -> bool {
crate::metal::MetalBackend::is_available()
}
#[cfg(not(all(feature = "metal", target_os = "macos")))]
fn metal_available() -> bool {
false
}
#[cfg(all(feature = "metal", target_os = "macos"))]
fn instantiate_metal() -> Result<Arc<dyn GpuBackend>, EngineError> {
Ok(Arc::new(crate::metal::MetalBackend::new()?))
}
#[cfg(not(all(feature = "metal", target_os = "macos")))]
fn instantiate_metal() -> Result<Arc<dyn GpuBackend>, EngineError> {
Err(unavailable(BackendKind::Metal, false, None))
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
#[test]
fn default_selection_matches_detection_and_is_cpu_without_gpus() {
let availability = HardwareDetector::availability();
let backend = HardwareDetector::select_with(None).unwrap();
assert_eq!(backend.kind(), HardwareDetector::detected_kind());
if !availability.cuda && !availability.metal {
assert_eq!(backend.kind(), BackendKind::CpuSimd);
}
}
#[test]
fn explicit_cpu_and_whitespace_are_accepted() {
assert_eq!(
HardwareDetector::select_with(Some(" CPU ")).unwrap().kind(),
BackendKind::CpuSimd
);
assert_eq!(
HardwareDetector::select_with(Some("")).unwrap().kind(),
HardwareDetector::detected_kind()
);
}
#[test]
fn unavailable_backends_are_typed_errors() {
let availability = HardwareDetector::availability();
if !availability.cuda {
let err = HardwareDetector::select_with(Some("cuda")).unwrap_err();
assert!(
matches!(
err,
EngineError::Device {
backend: BackendKind::Cuda,
..
}
),
"{err}"
);
let message = err.to_string();
assert!(message.contains("cuda"), "{message}");
assert!(message.contains("explicitly requested"), "{message}");
assert!(!message.contains(BACKEND_ENV), "{message}");
}
if !availability.metal {
let err = HardwareDetector::select_with(Some("metal")).unwrap_err();
assert!(
matches!(
err,
EngineError::Device {
backend: BackendKind::Metal,
..
}
),
"{err}"
);
}
assert!(matches!(
HardwareDetector::select_with(Some("fpga")).unwrap_err(),
EngineError::Plan(_)
));
}
}