use std::ffi::c_char;
use std::fmt;
use cudarc::nccl::{self, sys as nccl_sys};
use super::convert::nccl_err;
use crate::error::{Error, Result};
pub const NCCL_ID_BYTES: usize = 128;
#[derive(Clone, Copy)]
pub struct NcclId {
id: nccl::Id,
}
impl NcclId {
pub fn new() -> Result<Self> {
let id = nccl::Id::new().map_err(|e| Error::Backend(nccl_err("ncclGetUniqueId", e)))?;
Ok(Self { id })
}
pub fn from_bytes(bytes: [u8; NCCL_ID_BYTES]) -> Self {
let internal = bytes.map(|b| c_char::from_ne_bytes([b]));
Self {
id: nccl::Id::uninit(internal),
}
}
pub fn from_slice(bytes: &[u8]) -> Result<Self> {
let array: [u8; NCCL_ID_BYTES] =
bytes.try_into().map_err(|_| Error::InvalidArgument {
arg: "bytes",
reason: format!(
"NCCL unique id needs {NCCL_ID_BYTES} bytes, got {}; send the full output of NcclId::bytes",
bytes.len()
),
})?;
Ok(Self::from_bytes(array))
}
pub fn bytes(&self) -> [u8; NCCL_ID_BYTES] {
self.id.internal().map(|c| c.to_ne_bytes()[0])
}
pub(super) fn raw(&self) -> nccl_sys::ncclUniqueId {
nccl_sys::ncclUniqueId {
internal: *self.id.internal(),
}
}
}
impl PartialEq for NcclId {
fn eq(&self, other: &Self) -> bool {
self.id.internal() == other.id.internal()
}
}
impl Eq for NcclId {}
impl fmt::Debug for NcclId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("NcclId").finish_non_exhaustive()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bytes_round_trip() {
let mut bytes = [0u8; NCCL_ID_BYTES];
for (i, b) in bytes.iter_mut().enumerate() {
*b = (i * 7 + 3) as u8;
}
bytes[0] = 0xFF;
bytes[1] = 0x80;
let id = NcclId::from_bytes(bytes);
assert_eq!(id.bytes(), bytes);
assert_eq!(id.raw().internal.map(|c| c.to_ne_bytes()[0]), bytes);
}
#[test]
fn test_from_slice_round_trip() {
let bytes = [0xA5u8; NCCL_ID_BYTES];
let id = NcclId::from_slice(&bytes).unwrap();
assert_eq!(id, NcclId::from_bytes(bytes));
}
#[test]
fn test_from_slice_rejects_wrong_length() {
assert!(NcclId::from_slice(&[0u8; NCCL_ID_BYTES - 1]).is_err());
assert!(NcclId::from_slice(&[0u8; NCCL_ID_BYTES + 1]).is_err());
assert!(NcclId::from_slice(&[]).is_err());
}
#[test]
fn test_debug_hides_bytes() {
let id = NcclId::from_bytes([0x41u8; NCCL_ID_BYTES]);
assert_eq!(format!("{id:?}"), "NcclId { .. }");
}
#[test]
fn test_new_id_round_trips_through_bytes() {
let id = NcclId::new().unwrap();
let rebuilt = NcclId::from_slice(&id.bytes()).unwrap();
assert_eq!(rebuilt, id);
assert_eq!(rebuilt.raw(), id.raw());
}
}