use burn::backend::wgpu::WgpuDevice;
use std::sync::atomic::{AtomicBool, Ordering};
static GPU_AVAILABLE: AtomicBool = AtomicBool::new(false);
static GPU_CHECKED: AtomicBool = AtomicBool::new(false);
pub fn is_gpu_available() -> bool {
if GPU_CHECKED.load(Ordering::Relaxed) {
return GPU_AVAILABLE.load(Ordering::Relaxed);
}
let available = init_gpu_backend().is_ok();
GPU_AVAILABLE.store(available, Ordering::Relaxed);
GPU_CHECKED.store(true, Ordering::Relaxed);
available
}
fn init_gpu_backend() -> anyhow::Result<()> {
let _device = WgpuDevice::default();
Ok(())
}
pub struct GpuBackend {
device: WgpuDevice,
}
impl GpuBackend {
pub fn new() -> anyhow::Result<Self> {
let device = WgpuDevice::default();
Ok(Self { device })
}
pub fn device(&self) -> &WgpuDevice {
&self.device
}
}
impl Default for GpuBackend {
fn default() -> Self {
Self::new().expect("GPU backend should be initialized after availability check")
}
}