use core::fmt::Debug;
use core::marker::PhantomData;
use crate::err::Result;
use crate::shapes::Dyn;
pub trait Device: 'static + Send + Sync + Clone + Eq + PartialEq + Debug + Sized {
type Arg: Clone;
type Field: Debug + Clone + Default;
fn init(arg: Self::Arg) -> Self::Field;
fn to_incin(dev: &Self::Field) -> Result<DeviceId>;
}
pub trait ConstDevice: Default + Device<Arg = ()> {}
impl Device for Dyn {
type Arg = DeviceId;
type Field = DeviceId;
fn to_incin(dev: &Self::Field) -> Result<DeviceId> {
Ok(*dev)
}
fn init(arg: Self::Arg) -> Self::Field {
arg
}
}
#[cfg(feature = "cuda")]
mod cuda_partial {
use super::{Device, DeviceId, Result};
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Cuda {
ordinal: usize,
}
impl Cuda {
#[must_use]
pub const fn new(ordinal: usize) -> Self {
Self { ordinal }
}
#[must_use]
pub const fn ordinal(self) -> usize {
self.ordinal
}
}
impl Device for Cuda {
type Arg = Self;
type Field = Self;
fn to_incin(dev: &Self::Field) -> Result<DeviceId> {
Ok(DeviceId::cuda(dev.ordinal))
}
fn init(arg: Self::Arg) -> Self::Field {
arg
}
}
}
#[cfg(feature = "cuda")]
pub use cuda_partial::Cuda;
#[cfg(feature = "wgpu")]
mod wgpu_partial {
use super::{Device, DeviceId, Result};
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Wgpu {
ordinal: usize,
}
impl Wgpu {
#[must_use]
pub const fn new(ordinal: usize) -> Self {
Self { ordinal }
}
#[must_use]
pub const fn ordinal(self) -> usize {
self.ordinal
}
}
impl Device for Wgpu {
type Arg = Self;
type Field = Self;
fn to_incin(dev: &Self::Field) -> Result<DeviceId> {
Ok(DeviceId::wgpu(dev.ordinal))
}
fn init(arg: Self::Arg) -> Self::Field {
arg
}
}
}
#[cfg(feature = "wgpu")]
pub use wgpu_partial::Wgpu;
#[cfg(feature = "cuda")]
mod cuda_static {
use super::{ConstDevice, Device, DeviceId, PhantomData, Result};
use typenum::{U0, Unsigned};
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub struct CudaN<N: Unsigned = U0>(PhantomData<N>);
impl<N: Unsigned + 'static + Send + Sync + Clone + Eq + PartialEq + Debug> ConstDevice
for CudaN<N>
{
}
use core::fmt::Debug;
impl<N: Unsigned + 'static + Send + Sync + Clone + Eq + PartialEq + Debug> Device for CudaN<N> {
type Arg = ();
type Field = PhantomData<Self>;
fn to_incin(_: &Self::Field) -> Result<DeviceId> {
Ok(DeviceId::cuda(N::USIZE))
}
fn init(_: Self::Arg) -> Self::Field {
PhantomData
}
}
}
#[cfg(feature = "cuda")]
pub use cuda_static::CudaN;
#[cfg(feature = "wgpu")]
mod wgpu_static {
use super::{ConstDevice, Device, DeviceId, PhantomData, Result};
use typenum::{U0, Unsigned};
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct WgpuN<N: Unsigned = U0>(PhantomData<N>);
impl<N: Unsigned + 'static + Send + Sync + Clone + Eq + PartialEq + Debug> ConstDevice
for WgpuN<N>
{
}
use core::fmt::Debug;
impl<N: Unsigned + 'static + Send + Sync + Clone + Eq + PartialEq + Debug> Device for WgpuN<N> {
type Arg = ();
type Field = PhantomData<Self>;
fn to_incin(_: &Self::Field) -> Result<DeviceId> {
Ok(DeviceId::wgpu(N::USIZE))
}
fn init(_: Self::Arg) -> Self::Field {
PhantomData
}
}
}
#[cfg(feature = "wgpu")]
pub use wgpu_static::WgpuN;
#[cfg(feature = "metal")]
mod metal_partial {
use super::{Device, DeviceId, Result};
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Metal {
ordinal: usize,
}
impl Metal {
#[must_use]
pub const fn new(ordinal: usize) -> Self {
Self { ordinal }
}
#[must_use]
pub const fn ordinal(&self) -> usize {
self.ordinal
}
}
impl Device for Metal {
type Arg = Self;
type Field = Self;
fn to_incin(dev: &Self::Field) -> Result<DeviceId> {
Ok(DeviceId::metal(dev.ordinal))
}
fn init(arg: Self::Arg) -> Self::Field {
arg
}
}
}
#[cfg(feature = "metal")]
pub use metal_partial::Metal;
#[cfg(feature = "metal")]
mod metal_static {
use super::{ConstDevice, Device, DeviceId, PhantomData, Result};
use core::fmt::Debug;
use typenum::{U0, Unsigned};
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct MetalN<N: Unsigned = U0>(PhantomData<N>);
impl<N: Unsigned + 'static + Send + Sync + Clone + Eq + PartialEq + Debug> ConstDevice
for MetalN<N>
{
}
impl<N: Unsigned + 'static + Send + Sync + Clone + Eq + PartialEq + Debug> Device for MetalN<N> {
type Arg = ();
type Field = PhantomData<Self>;
fn to_incin(_: &Self::Field) -> Result<DeviceId> {
Ok(DeviceId::metal(N::USIZE))
}
fn init(_: Self::Arg) -> Self::Field {
PhantomData
}
}
}
#[cfg(feature = "metal")]
pub use metal_static::MetalN;
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Cpu;
impl ConstDevice for Cpu {}
impl Device for Cpu {
type Arg = ();
type Field = PhantomData<Self>;
fn to_incin(_: &Self::Field) -> Result<DeviceId> {
Ok(DeviceId::cpu())
}
fn init(_: Self::Arg) -> Self::Field {
PhantomData
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum DeviceKind {
Cpu,
Cuda,
Wgpu,
Metal,
Custom(u64),
}
impl DeviceKind {
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Cpu => "cpu",
Self::Cuda => "cuda",
Self::Wgpu => "wgpu",
Self::Metal => "metal",
Self::Custom(_) => "custom",
}
}
pub const fn custom_key(self) -> Option<u64> {
match self {
Self::Custom(key) => Some(key),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct DeviceId {
kind: DeviceKind,
ordinal: usize,
}
impl Default for DeviceId {
fn default() -> Self {
Self::cpu()
}
}
impl DeviceId {
pub const fn kind(self) -> DeviceKind {
self.kind
}
pub const fn ordinal(self) -> usize {
self.ordinal
}
pub const CPU: Self = Self {
kind: DeviceKind::Cpu,
ordinal: 0,
};
pub fn cpu() -> Self {
Self {
kind: DeviceKind::Cpu,
ordinal: 0,
}
}
pub fn cuda(ord: usize) -> Self {
Self {
kind: DeviceKind::Cuda,
ordinal: ord,
}
}
pub fn wgpu(ord: usize) -> Self {
Self {
kind: DeviceKind::Wgpu,
ordinal: ord,
}
}
pub fn metal(ord: usize) -> Self {
Self {
kind: DeviceKind::Metal,
ordinal: ord,
}
}
pub const fn custom(namespace: u64, ord: usize) -> Self {
Self {
kind: DeviceKind::Custom(namespace),
ordinal: ord,
}
}
}
pub const fn cuda_is_available() -> bool {
cfg!(feature = "cuda")
}
pub const fn wgpu_is_available() -> bool {
cfg!(feature = "wgpu")
}
pub const fn metal_is_available() -> bool {
cfg!(feature = "metal")
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct MetalDevice {
id: usize,
}
impl MetalDevice {
pub fn new(id: usize) -> Self {
Self { id }
}
#[must_use]
pub const fn ordinal(self) -> usize {
self.id
}
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct CudaDevice {
id: usize,
}
impl CudaDevice {
pub fn new(id: usize) -> Self {
Self { id }
}
#[must_use]
pub const fn ordinal(self) -> usize {
self.id
}
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct WgpuDevice {
id: usize,
}
impl WgpuDevice {
pub fn new(id: usize) -> Self {
Self { id }
}
#[must_use]
pub const fn ordinal(self) -> usize {
self.id
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeviceSet {
devices: alloc::vec::Vec<DeviceId>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum DeviceSetError {
Empty,
Duplicate {
device: DeviceId,
},
Mixed {
first: DeviceKind,
found: DeviceKind,
},
}
impl core::fmt::Display for DeviceSetError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Empty => f.write_str("a device set must name at least one device"),
Self::Duplicate { device } => write!(
f,
"device {}:{} appears more than once",
device.kind().name(),
device.ordinal()
),
Self::Mixed { first, found } => write!(
f,
"a device set cannot mix backend families: {} and {}",
first.name(),
found.name()
),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for DeviceSetError {}
impl DeviceSet {
pub fn new(
devices: impl IntoIterator<Item = DeviceId>,
) -> core::result::Result<Self, DeviceSetError> {
let devices: alloc::vec::Vec<DeviceId> = devices.into_iter().collect();
let first = *devices.first().ok_or(DeviceSetError::Empty)?;
for (index, &device) in devices.iter().enumerate() {
if device.kind() != first.kind() {
return Err(DeviceSetError::Mixed {
first: first.kind(),
found: device.kind(),
});
}
if devices[..index].contains(&device) {
return Err(DeviceSetError::Duplicate { device });
}
}
Ok(Self { devices })
}
#[must_use]
pub fn cpu() -> Self {
Self {
devices: alloc::vec![DeviceId::cpu()],
}
}
pub fn cuda(
ordinals: impl IntoIterator<Item = usize>,
) -> core::result::Result<Self, DeviceSetError> {
Self::new(ordinals.into_iter().map(DeviceId::cuda))
}
pub fn wgpu(
ordinals: impl IntoIterator<Item = usize>,
) -> core::result::Result<Self, DeviceSetError> {
Self::new(ordinals.into_iter().map(DeviceId::wgpu))
}
#[must_use]
pub fn devices(&self) -> &[DeviceId] {
&self.devices
}
#[must_use]
pub fn primary(&self) -> DeviceId {
self.devices[0]
}
#[must_use]
pub fn len(&self) -> usize {
self.devices.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
false
}
#[must_use]
pub fn is_multi_device(&self) -> bool {
self.devices.len() > 1
}
pub fn kind(&self) -> Option<DeviceKind> {
self.devices.first().map(|device| device.kind())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum DevicePreference {
Exactly(DeviceSet),
Fastest,
Cpu,
}
impl Default for DevicePreference {
fn default() -> Self {
Self::Cpu
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_device_set_keeps_the_order_it_was_given() {
let set = DeviceSet::new([DeviceId::cuda(2), DeviceId::cuda(0), DeviceId::cuda(1)])
.expect("three distinct CUDA devices are a valid set");
assert_eq!(set.primary(), DeviceId::cuda(2));
assert_eq!(set.len(), 3);
assert!(set.is_multi_device());
}
#[test]
fn a_device_set_rejects_what_cannot_describe_a_run() {
assert_eq!(DeviceSet::new([]), Err(DeviceSetError::Empty));
assert_eq!(
DeviceSet::new([DeviceId::cuda(0), DeviceId::cuda(0)]),
Err(DeviceSetError::Duplicate {
device: DeviceId::cuda(0)
})
);
assert_eq!(
DeviceSet::new([DeviceId::cuda(0), DeviceId::cpu()]),
Err(DeviceSetError::Mixed {
first: DeviceKind::Cuda,
found: DeviceKind::Cpu
})
);
}
#[test]
fn the_rfcs_three_gpu_call_builds_three_cuda_devices() {
let set = DeviceSet::cuda(0..3).expect("an ordinal range is a valid set");
assert_eq!(
set.devices(),
[DeviceId::cuda(0), DeviceId::cuda(1), DeviceId::cuda(2)]
);
assert_eq!(set.kind(), Some(DeviceKind::Cuda));
}
#[test]
fn an_empty_ordinal_range_is_rejected_rather_than_defaulted() {
assert_eq!(DeviceSet::cuda(0..0), Err(DeviceSetError::Empty));
assert_eq!(DeviceSet::wgpu(0..0), Err(DeviceSetError::Empty));
}
#[test]
fn the_default_preference_is_the_cpu() {
assert_eq!(DevicePreference::default(), DevicePreference::Cpu);
}
#[test]
fn test_device_variants() {
let cpu = DeviceId::cpu();
assert_eq!(cpu.kind(), DeviceKind::Cpu);
assert_eq!(cpu.ordinal(), 0);
#[cfg(feature = "cuda")]
{
let cuda = DeviceId::cuda(0);
assert_eq!(cuda.kind(), DeviceKind::Cuda);
assert_eq!(cuda.ordinal(), 0);
let cuda2 = DeviceId::cuda(2);
assert_eq!(cuda2.ordinal(), 2);
}
#[cfg(feature = "wgpu")]
{
let wgpu = DeviceId::wgpu(0);
assert_eq!(wgpu.kind(), DeviceKind::Wgpu);
assert_eq!(wgpu.ordinal(), 0);
}
}
#[cfg(feature = "cuda")]
#[test]
fn test_cuda_tier2_runtime_ordinal() {
let field = <Cuda as Device>::init(Cuda::new(3));
assert_eq!(field.ordinal(), 3);
let id = Cuda::to_incin(&field).unwrap();
assert_eq!(id, DeviceId::cuda(3));
}
#[cfg(feature = "cuda")]
#[test]
fn test_cuda_tier3_static_ordinal() {
use typenum::U2;
let field = <CudaN<U2> as Device>::init(());
let id = CudaN::<U2>::to_incin(&field).unwrap();
assert_eq!(id, DeviceId::cuda(2));
}
#[cfg(feature = "wgpu")]
#[test]
fn test_wgpu_tier2_runtime_ordinal() {
let field = <Wgpu as Device>::init(Wgpu::new(1));
assert_eq!(field.ordinal(), 1);
let id = Wgpu::to_incin(&field).unwrap();
assert_eq!(id, DeviceId::wgpu(1));
}
#[cfg(feature = "metal")]
#[test]
fn test_metal_tier2_runtime_ordinal() {
let field = <Metal as Device>::init(Metal::new(2));
assert_eq!(field.ordinal(), 2);
assert_eq!(Metal::to_incin(&field).unwrap(), DeviceId::metal(2));
}
#[cfg(feature = "wgpu")]
#[test]
fn test_wgpu_tier3_static_ordinal() {
use typenum::U0;
let field = <WgpuN<U0> as Device>::init(());
let id = WgpuN::<U0>::to_incin(&field).unwrap();
assert_eq!(id, DeviceId::wgpu(0));
}
#[test]
fn test_dyn_tier1_fully_runtime() {
let id = DeviceId::cuda(5);
let field = <Dyn as Device>::init(id);
assert_eq!(Dyn::to_incin(&field).unwrap(), DeviceId::cuda(5));
}
#[test]
fn external_device_identity_is_open_and_stable() {
let id = DeviceId::custom(0x434f_4d50_414e_5901, 7);
assert_eq!(id.kind().name(), "custom");
assert_eq!(id.kind().custom_key(), Some(0x434f_4d50_414e_5901));
assert_eq!(id.ordinal(), 7);
}
}