#[cfg(any(feature = "cuda", feature = "vulkan"))]
use fuel_ir::DeviceLocation;
#[cfg(any(feature = "cuda", feature = "vulkan", test))]
use fuel_ir::probe::DeviceIdentity;
#[cfg(any(feature = "cuda", feature = "vulkan"))]
use std::collections::HashMap;
#[derive(Default)]
pub struct DeviceRegistry {
#[cfg(feature = "cuda")]
cuda: HashMap<DeviceLocation, fuel_cuda_backend::CudaDevice>,
#[cfg(feature = "vulkan")]
vulkan: HashMap<DeviceLocation, std::sync::Arc<fuel_vulkan_backend::VulkanBackend>>,
}
#[cfg(any(feature = "cuda", feature = "vulkan", test))]
fn unique_by_identity<'a, T>(
entries: impl IntoIterator<Item = (Option<DeviceIdentity>, &'a T)>,
wanted: &DeviceIdentity,
) -> Option<&'a T> {
let mut hits = entries
.into_iter()
.filter(|(id, _)| id.is_some_and(|i| i.same_physical_device(wanted)))
.map(|(_, handle)| handle);
let first = hits.next()?;
hits.next().is_none().then_some(first)
}
impl DeviceRegistry {
pub fn new() -> Self {
Self::default()
}
#[cfg(feature = "cuda")]
pub fn with_cuda(mut self, device: fuel_cuda_backend::CudaDevice) -> Self {
self.cuda.insert(device.location(), device);
self
}
#[cfg(feature = "vulkan")]
pub fn with_vulkan(
mut self,
backend: std::sync::Arc<fuel_vulkan_backend::VulkanBackend>,
) -> Self {
self.vulkan.insert(
DeviceLocation::Vulkan {
gpu_id: backend.gpu_id,
},
backend,
);
self
}
#[cfg(feature = "cuda")]
pub fn registered_cuda(&self, gpu_id: usize) -> Option<&fuel_cuda_backend::CudaDevice> {
self.cuda.get(&DeviceLocation::Cuda { gpu_id })
}
#[cfg(feature = "vulkan")]
pub fn registered_vulkan(
&self,
gpu_id: usize,
) -> Option<&std::sync::Arc<fuel_vulkan_backend::VulkanBackend>> {
self.vulkan.get(&DeviceLocation::Vulkan { gpu_id })
}
#[cfg(feature = "vulkan")]
pub fn registered_vulkan_by_identity(
&self,
wanted: &DeviceIdentity,
) -> Option<&std::sync::Arc<fuel_vulkan_backend::VulkanBackend>> {
unique_by_identity(self.vulkan.values().map(|b| (b.identity(), b)), wanted)
}
#[cfg(feature = "cuda")]
pub fn registered_cuda_by_identity(
&self,
wanted: &DeviceIdentity,
) -> Option<&fuel_cuda_backend::CudaDevice> {
let ids = fuel_cuda_backend::probe::device_identities().ok()?;
unique_by_identity(
self.cuda.iter().map(|(place, device)| {
let id = match place {
DeviceLocation::Cuda { gpu_id } => ids.get(*gpu_id).copied().flatten(),
_ => None,
};
(id, device)
}),
wanted,
)
}
#[cfg(feature = "cuda")]
pub(crate) fn cuda(&self, gpu_id: usize) -> Option<fuel_cuda_backend::CudaDevice> {
self.registered_cuda(gpu_id).cloned()
}
#[cfg(feature = "vulkan")]
pub(crate) fn vulkan(
&self,
gpu_id: usize,
) -> Option<std::sync::Arc<fuel_vulkan_backend::VulkanBackend>> {
self.registered_vulkan(gpu_id).cloned()
}
}
#[cfg(test)]
mod tests {
use super::unique_by_identity;
use fuel_ir::probe::DeviceIdentity;
fn id(b: u8) -> Option<DeviceIdentity> {
Some(DeviceIdentity {
uuid: Some([b; 16]),
pci: None,
})
}
#[test]
fn finds_the_entry_in_either_registration_order() {
let wanted = id(1).unwrap();
assert_eq!(
unique_by_identity([(id(1), &"a"), (id(2), &"b")], &wanted),
Some(&"a")
);
assert_eq!(
unique_by_identity([(id(2), &"b"), (id(1), &"a")], &wanted),
Some(&"a")
);
}
#[test]
fn unknown_entries_never_match_and_no_match_is_none() {
let wanted = id(1).unwrap();
assert_eq!(unique_by_identity([(None, &"x")], &wanted), None);
assert_eq!(unique_by_identity([(id(2), &"b")], &wanted), None);
let nobody = DeviceIdentity {
uuid: None,
pci: None,
};
assert_eq!(unique_by_identity([(None, &"x")], &nobody), None);
}
#[test]
fn two_matches_decline_instead_of_taking_the_first() {
let wanted = id(1).unwrap();
assert_eq!(
unique_by_identity([(id(1), &"a"), (id(2), &"b"), (id(1), &"c")], &wanted),
None
);
}
}