use std::fmt;
use crate::error::{Error, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Device {
Cpu,
Cuda(usize),
}
impl Device {
pub fn cuda(index: usize) -> Result<Device> {
if !cfg!(feature = "cuda") {
return Err(Error::NoCuda);
}
let count = crate::cuda::device_count();
if index >= count {
return Err(Error::Cuda(format!(
"requested cuda:{index} but only {count} device(s) present"
)));
}
crate::cuda::init(index)?;
Ok(Device::Cuda(index))
}
pub fn best() -> Device {
Device::cuda(0).unwrap_or(Device::Cpu)
}
pub fn is_cuda(self) -> bool {
matches!(self, Device::Cuda(_))
}
}
impl fmt::Display for Device {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Device::Cpu => write!(f, "cpu"),
Device::Cuda(i) => write!(f, "cuda:{i}"),
}
}
}