Skip to main content

singe_cuda/device/
clique.rs

1use num_enum::{IntoPrimitive, TryFromPrimitive};
2use singe_core::impl_enum_display;
3use singe_cuda_sys::driver;
4
5use super::{Device, Uuid};
6use crate::{
7    driver::entry_points::{
8        cuDeviceGetCliqueCount, cuDeviceGetCliqueInfo, cuDeviceGetFabricClusterUuid,
9    },
10    error::Result,
11    utility::to_usize,
12};
13
14singe_core::native_enum! {
15    /// Fabric clique types.
16    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive, IntoPrimitive)]
17    #[repr(u32)]
18    #[non_exhaustive]
19    pub enum CliqueType: driver::CUcliqueType_enum {
20        /// Unicast pointer clique.
21        UnicastPointer = CU_CLIQUE_TYPE_UNICAST_POINTER,
22        /// Multicast pointer clique.
23        MulticastPointer = CU_CLIQUE_TYPE_MULTICAST_POINTER,
24        /// Unicast logical endpoint clique.
25        UnicastLogicalEndpoint = CU_CLIQUE_TYPE_UNICAST_LOGICAL_ENDPOINT,
26        /// Multicast logical endpoint clique.
27        MulticastLogicalEndpoint = CU_CLIQUE_TYPE_MULTICAST_LOGICAL_ENDPOINT,
28    }
29}
30
31/// Fabric clique information.
32#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33pub struct CliqueInfo {
34    /// Type of the fabric clique.
35    pub kind: CliqueType,
36    /// ID of the fabric clique.
37    pub id: u32,
38}
39
40impl Device {
41    /// Retrieves the fabric cluster UUID for the given device.
42    ///
43    /// # Errors
44    ///
45    /// Returns an error if the driver entry point is unavailable or the native query fails, including unsupported fabric features or an invalid device.
46    pub fn fabric_cluster_uuid(self) -> Result<Uuid> {
47        let mut value = driver::CUuuid::default();
48        // SAFETY: `value` is a local that the driver writes through an exclusive pointer.
49        unsafe {
50            cuDeviceGetFabricClusterUuid(&raw mut value, self.id())?;
51        }
52        Ok(value.into())
53    }
54
55    /// Returns the fabric clique information for the cliques that the device is part of.
56    ///
57    /// # Errors
58    ///
59    /// Returns an error if either required driver entry point is unavailable, the native count or clique-information query fails, or the reported count does not fit `usize`.
60    /// No partial list is returned.
61    pub fn cliques(self) -> Result<Vec<CliqueInfo>> {
62        let mut count = 0;
63        // SAFETY: `count` is a local that the driver writes through an exclusive pointer.
64        unsafe {
65            cuDeviceGetCliqueCount(&raw mut count, self.id())?;
66        }
67
68        let mut values = vec![driver::CUcliqueInfo::default(); to_usize(count, "clique count")?];
69        // SAFETY: `values` holds `count` elements, the capacity the driver reads from `count` before writing at most that many.
70        unsafe {
71            cuDeviceGetCliqueInfo(values.as_mut_ptr(), &raw mut count, self.id())?;
72        }
73
74        values.truncate(to_usize(count, "clique count")?);
75        values
76            .into_iter()
77            .map(|v| {
78                Ok(CliqueInfo {
79                    kind: v.type_.try_into()?,
80                    id: v.id,
81                })
82            })
83            .collect()
84    }
85}
86
87impl_enum_display!(CliqueType, {
88    CliqueType::UnicastPointer => "CU_CLIQUE_TYPE_UNICAST_POINTER",
89    CliqueType::MulticastPointer => "CU_CLIQUE_TYPE_MULTICAST_POINTER",
90    CliqueType::UnicastLogicalEndpoint => "CU_CLIQUE_TYPE_UNICAST_LOGICAL_ENDPOINT",
91    CliqueType::MulticastLogicalEndpoint => "CU_CLIQUE_TYPE_MULTICAST_LOGICAL_ENDPOINT",
92});