use cubecl::prelude::*;
use crate::accelerate::Accelerator;
use crate::device::Device;
use crate::probe::open_client;
pub fn sniff_best_accelerator(enable: &[Accelerator], device: &Device) -> Option<Accelerator> {
for accelerator in enable {
let is_enabled = match accelerator {
#[cfg(feature = "cuda")]
Accelerator::Cuda => match device.to_cuda() {
Ok(dev) => probe_runtime::<cubecl::cuda::CudaRuntime>(*accelerator, &dev),
Err(_) => false,
},
#[cfg(feature = "rocm")]
Accelerator::Rocm => match device.to_amd() {
Ok(dev) => probe_runtime::<cubecl::hip::HipRuntime>(*accelerator, &dev),
Err(_) => false,
},
#[cfg(feature = "vulkan")]
Accelerator::Vulkan => match device.to_wgpu() {
Ok(dev) => probe_runtime::<cubecl::wgpu::WgpuRuntime>(*accelerator, &dev),
Err(_) => false,
},
#[cfg(feature = "metal")]
Accelerator::Metal => match device.to_wgpu() {
Ok(dev) => probe_runtime::<cubecl::wgpu::WgpuRuntime>(*accelerator, &dev),
Err(_) => false,
},
#[cfg(docsrs)]
#[expect(
unreachable_patterns,
reason = "the arm only keeps the match exhaustive on docs.rs"
)]
_ => unreachable!(),
};
if is_enabled {
return Some(*accelerator);
}
}
None
}
fn probe_runtime<R: Runtime>(accelerator: Accelerator, device: &R::Device) -> bool {
open_client::<R>(accelerator, device).is_some()
}