use std::ptr;
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use cudarc::driver::CudaStream;
use cudarc::driver::sys::{CUresult, cuStreamQuery};
use cudarc::nccl::result::{NcclError, NcclStatus};
use cudarc::nccl::{result as nccl_result, sys as nccl_sys};
use super::convert::{context_err, nccl_err, to_nccl_int};
use super::id::NcclId;
use crate::error::{Error, Result};
use crate::runtime::cuda::context::bind_context;
struct RawComm(nccl_sys::ncclComm_t);
unsafe impl Send for RawComm {}
pub struct NcclCommunicator {
comm: Mutex<RawComm>,
pub(super) stream: Arc<CudaStream>,
pub(super) rank: usize,
pub(super) world_size: usize,
}
impl NcclCommunicator {
pub fn from_clients(clients: &[&super::super::CudaClient]) -> Result<Vec<Self>> {
let streams = clients.iter().map(|c| c.stream_arc().clone()).collect();
Self::from_streams_inner(streams)
}
pub fn from_client_rank(
client: &super::super::CudaClient,
rank: usize,
world_size: usize,
id: NcclId,
) -> Result<Self> {
Self::from_rank_inner(client.stream_arc().clone(), rank, world_size, id)
}
#[cfg(feature = "cuda-raw-interop")]
pub fn from_streams(streams: Vec<Arc<CudaStream>>) -> Result<Vec<Self>> {
Self::from_streams_inner(streams)
}
fn from_streams_inner(streams: Vec<Arc<CudaStream>>) -> Result<Vec<Self>> {
if streams.is_empty() {
return Err(Error::InvalidArgument {
arg: "streams",
reason: "NCCL needs at least one stream; pass one stream per GPU".into(),
});
}
let world_size = streams.len();
let n_ranks = to_nccl_int("streams", world_size)?;
let ordinals = streams
.iter()
.map(|s| to_nccl_int("device ordinal", s.context().ordinal()))
.collect::<Result<Vec<i32>>>()?;
let mut handles: Vec<nccl_sys::ncclComm_t> = vec![ptr::null_mut(); world_size];
unsafe { nccl_result::comm_init_all(handles.as_mut_ptr(), n_ranks, ordinals.as_ptr()) }
.map_err(|e| {
Error::Backend(format!(
"NCCL init of {world_size} ranks on devices {ordinals:?}: {}",
nccl_err("ncclCommInitAll", e)
))
})?;
Ok(handles
.into_iter()
.zip(streams)
.enumerate()
.map(|(rank, (handle, stream))| Self {
comm: Mutex::new(RawComm(handle)),
stream,
rank,
world_size,
})
.collect())
}
#[cfg(feature = "cuda-raw-interop")]
pub fn from_rank(
stream: Arc<CudaStream>,
rank: usize,
world_size: usize,
id: NcclId,
) -> Result<Self> {
Self::from_rank_inner(stream, rank, world_size, id)
}
fn from_rank_inner(
stream: Arc<CudaStream>,
rank: usize,
world_size: usize,
id: NcclId,
) -> Result<Self> {
if world_size == 0 || rank >= world_size {
return Err(Error::InvalidArgument {
arg: "rank",
reason: format!(
"rank {rank} is outside world size {world_size}; pass 0 <= rank < world_size"
),
});
}
let n_ranks = to_nccl_int("world_size", world_size)?;
let nccl_rank = to_nccl_int("rank", rank)?;
bind_context(stream.context())
.map_err(|e| Error::Backend(format!("NCCL rank {rank}: {}", context_err(e))))?;
let mut handle: nccl_sys::ncclComm_t = ptr::null_mut();
unsafe { nccl_result::comm_init_rank(&mut handle, n_ranks, id.raw(), nccl_rank) }.map_err(
|e| {
Error::Backend(format!(
"NCCL rank {rank} of {world_size}: {}",
nccl_err("ncclCommInitRank", e)
))
},
)?;
Ok(Self {
comm: Mutex::new(RawComm(handle)),
stream,
rank,
world_size,
})
}
#[cfg(feature = "cuda-raw-interop")]
pub fn stream(&self) -> &Arc<CudaStream> {
&self.stream
}
#[cfg(not(feature = "cuda-raw-interop"))]
pub(crate) fn stream(&self) -> &Arc<CudaStream> {
&self.stream
}
pub fn abort(&self) -> Result<()> {
let handle = std::mem::replace(&mut self.lock().0, ptr::null_mut());
if handle.is_null() {
return Ok(());
}
unsafe { nccl_result::comm_abort(handle) }
.map(|_| ())
.map_err(|e| self.rank_err(nccl_err("ncclCommAbort", e)))
}
pub fn async_error(&self) -> Result<()> {
let mut state = nccl_sys::ncclResult_t::ncclSuccess;
self.with_comm("ncclCommGetAsyncError", |comm, _| unsafe {
nccl_sys::ncclCommGetAsyncError(comm, &mut state).result()
})?;
state
.result()
.map(|_| ())
.map_err(|e| self.rank_err(nccl_err("an asynchronous NCCL operation", e)))
}
pub(super) fn with_comm<F>(&self, call: &'static str, f: F) -> Result<()>
where
F: FnOnce(
nccl_sys::ncclComm_t,
nccl_sys::cudaStream_t,
) -> std::result::Result<NcclStatus, NcclError>,
{
let guard = self.lock();
if guard.0.is_null() {
return Err(self.rank_err(format!(
"{call} on an aborted communicator; create a new communicator"
)));
}
let stream = self.bound_stream()?;
f(guard.0, stream)
.map(|_| ())
.map_err(|e| self.rank_err(nccl_err(call, e)))
}
pub(super) fn wait_stream(&self) -> Result<()> {
self.bind()?;
loop {
let status = unsafe { cuStreamQuery(self.stream.cu_stream()) };
match status {
CUresult::CUDA_SUCCESS => return self.async_error(),
CUresult::CUDA_ERROR_NOT_READY => {}
other => {
return Err(self.rank_err(format!(
"cuStreamQuery on the communicator stream failed with {other:?}"
)));
}
}
if let Err(e) = self.async_error() {
return Err(match self.abort() {
Ok(()) => e,
Err(abort_err) => {
let bare = |err: Error| match err {
Error::Backend(msg) => msg,
other => other.to_string(),
};
Error::Backend(format!("{}; then {}", bare(e), bare(abort_err)))
}
});
}
std::thread::yield_now();
}
}
pub(super) fn bound_stream(&self) -> Result<nccl_sys::cudaStream_t> {
self.bind()?;
Ok(self.stream.cu_stream() as nccl_sys::cudaStream_t)
}
pub(super) fn bind(&self) -> Result<()> {
bind_context(self.stream.context()).map_err(|e| self.rank_err(context_err(e)))
}
fn rank_err(&self, msg: impl std::fmt::Display) -> Error {
Error::Backend(format!(
"NCCL rank {} of {}: {msg}",
self.rank, self.world_size
))
}
fn lock(&self) -> MutexGuard<'_, RawComm> {
self.comm.lock().unwrap_or_else(PoisonError::into_inner)
}
}
impl Drop for NcclCommunicator {
fn drop(&mut self) {
let raw = self.comm.get_mut().unwrap_or_else(PoisonError::into_inner);
let handle = std::mem::replace(&mut raw.0, ptr::null_mut());
if handle.is_null() {
return;
}
let _ = unsafe { nccl_result::comm_abort(handle) };
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dtype::DType;
use crate::runtime::communicator::{Communicator, ReduceOp};
use crate::runtime::cuda::communicator::id::NCCL_ID_BYTES;
use cudarc::driver::{CudaContext, DevicePtr};
fn device_count() -> usize {
CudaContext::device_count().map_or(0, |n| n.max(0) as usize)
}
fn single_rank(test: &str) -> Option<NcclCommunicator> {
let n = device_count();
if n < 1 {
eprintln!("skipping {test}: needs 1 CUDA device, found {n}");
return None;
}
let stream = CudaContext::new(0).unwrap().default_stream();
Some(NcclCommunicator::from_rank(stream, 0, 1, NcclId::new().unwrap()).unwrap())
}
#[test]
fn test_send_sync_bounds() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<NcclCommunicator>();
}
#[test]
fn test_from_streams_rejects_empty() {
assert!(NcclCommunicator::from_streams(Vec::new()).is_err());
}
#[test]
fn test_from_rank_rejects_rank_outside_world() {
let n = device_count();
if n < 1 {
eprintln!(
"skipping test_from_rank_rejects_rank_outside_world: needs 1 CUDA device, found {n}"
);
return;
}
let stream = CudaContext::new(0).unwrap().default_stream();
let id = NcclId::from_bytes([0u8; NCCL_ID_BYTES]);
assert!(NcclCommunicator::from_rank(stream.clone(), 1, 1, id).is_err());
assert!(NcclCommunicator::from_rank(stream, 0, 0, id).is_err());
}
#[test]
fn test_world_size_one_metadata_and_async_error() {
let Some(comm) = single_rank("test_world_size_one_metadata_and_async_error") else {
return;
};
assert_eq!(comm.rank(), 0);
assert_eq!(comm.world_size(), 1);
comm.async_error().unwrap();
}
#[test]
fn test_world_size_one_all_reduce_and_barrier() {
let Some(comm) = single_rank("test_world_size_one_all_reduce_and_barrier") else {
return;
};
let stream = comm.stream().clone();
let slice = stream.clone_htod(&[1.5f32, -2.0, 3.0]).unwrap();
let ptr = {
let (ptr, _guard) = slice.device_ptr(&stream);
ptr as u64
};
unsafe { comm.all_reduce(ptr, 3, DType::F32, ReduceOp::Sum).unwrap() };
comm.sync().unwrap();
assert_eq!(stream.clone_dtoh(&slice).unwrap(), vec![1.5f32, -2.0, 3.0]);
comm.barrier().unwrap();
}
#[test]
fn test_abort_is_idempotent() {
let Some(comm) = single_rank("test_abort_is_idempotent") else {
return;
};
comm.abort().unwrap();
comm.abort().unwrap();
assert!(comm.async_error().is_err());
assert!(comm.sync().is_err());
assert!(comm.barrier().is_err());
assert!(unsafe { comm.all_reduce(0, 0, DType::F32, ReduceOp::Sum) }.is_err());
}
#[test]
fn test_from_streams_world_size_one_abort() {
let n = device_count();
if n < 1 {
eprintln!(
"skipping test_from_streams_world_size_one_abort: needs 1 CUDA device, found {n}"
);
return;
}
let stream = CudaContext::new(0).unwrap().default_stream();
let comms = NcclCommunicator::from_streams(vec![stream]).unwrap();
assert_eq!(comms.len(), 1);
assert_eq!(comms[0].world_size(), 1);
comms[0].sync().unwrap();
comms[0].abort().unwrap();
comms[0].abort().unwrap();
}
}