use core::fmt;
use core::marker::PhantomData;
use core::sync::atomic::Ordering;
#[cfg(feature = "std")]
use alloc::sync::Arc;
#[cfg(feature = "std")]
use std::net::TcpStream;
#[cfg(feature = "std")]
use std::sync::Mutex;
#[cfg(feature = "std")]
use std::time::Duration;
use crate::shapes::Dyn;
use super::error::ContextError;
use super::identity::DistributedIdentity;
#[cfg(feature = "std")]
use super::rendezvous::RendezvousEndpoint;
#[cfg(feature = "std")]
use super::state::ContextFailure;
use super::state::{DistributedContextHandle, DistributedContextState};
#[cfg(feature = "std")]
use super::wire::{ControlMessage, MessageKind, read_control, write_control};
use super::{STATE_ACTIVE, STATE_SHUTDOWN, STATE_SHUTTING_DOWN};
pub struct DistributedContext<M = Dyn, R = Dyn> {
pub(super) identity: DistributedIdentity,
pub(super) handle: DistributedContextHandle,
#[cfg(feature = "std")]
pub(super) control: Arc<Mutex<TcpStream>>,
#[cfg(feature = "std")]
pub(super) endpoint: RendezvousEndpoint,
#[cfg(feature = "std")]
pub(super) timeout: Duration,
pub(super) marker: PhantomData<(M, R)>,
}
impl<M, R> fmt::Debug for DistributedContext<M, R> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("DistributedContext")
.field("identity", &self.identity)
.field("state", &self.state())
.finish_non_exhaustive()
}
}
impl<M, R> Clone for DistributedContext<M, R> {
fn clone(&self) -> Self {
Self {
identity: self.identity.clone(),
handle: self.handle.clone(),
#[cfg(feature = "std")]
control: self.control.clone(),
#[cfg(feature = "std")]
endpoint: self.endpoint,
#[cfg(feature = "std")]
timeout: self.timeout,
marker: PhantomData,
}
}
}
impl<M, R> DistributedContext<M, R> {
#[must_use]
pub const fn identity(&self) -> &DistributedIdentity {
&self.identity
}
#[must_use]
pub const fn rank(&self) -> usize {
self.identity.rank
}
#[must_use]
pub const fn world_size(&self) -> usize {
self.identity.world
}
#[must_use]
pub const fn local_cuda_device(&self) -> usize {
self.identity.local_cuda_device
}
#[must_use]
pub fn state(&self) -> DistributedContextState {
self.handle.state()
}
#[must_use]
pub fn handle(&self) -> DistributedContextHandle {
self.handle.clone()
}
pub fn ensure_active(&self) -> Result<(), ContextError> {
self.handle.ensure_active()
}
#[must_use]
pub fn into_dyn(self) -> DistributedContext<Dyn, Dyn> {
DistributedContext {
identity: self.identity,
handle: self.handle,
#[cfg(feature = "std")]
control: self.control,
#[cfg(feature = "std")]
endpoint: self.endpoint,
#[cfg(feature = "std")]
timeout: self.timeout,
marker: PhantomData,
}
}
#[cfg(feature = "std")]
#[must_use]
pub const fn endpoint(&self) -> RendezvousEndpoint {
self.endpoint
}
#[cfg(feature = "std")]
#[must_use]
pub const fn timeout(&self) -> Duration {
self.timeout
}
#[cfg(feature = "std")]
pub fn abort(&self, failure: ContextFailure) -> Result<(), ContextError> {
self.handle.invalidate();
let mut stream = self.lock_control()?;
write_control(&mut stream, ControlMessage::abort(failure))
.map_err(|error| self.network_failure(error))
}
#[cfg(feature = "std")]
pub fn wait_for_peer(&self) -> Result<DistributedContextState, ContextError> {
let mut stream = self.lock_control()?;
match read_control(&mut stream).map_err(|error| self.network_failure(error))? {
message if message.kind == MessageKind::Shutdown => {
write_control(&mut stream, ControlMessage::shutdown())
.map_err(|error| self.network_failure(error))?;
self.handle.state.store(STATE_SHUTDOWN, Ordering::Release);
Ok(DistributedContextState::Shutdown)
}
message if message.kind == MessageKind::Abort => {
self.handle.invalidate();
Err(ContextError::PeerAborted {
failure: ContextFailure::from_code(message.code),
})
}
_ => {
self.handle.invalidate();
Err(ContextError::UnexpectedControlMessage)
}
}
}
#[cfg(feature = "std")]
pub fn shutdown(&self) -> Result<(), ContextError> {
match self.handle.state.compare_exchange(
STATE_ACTIVE,
STATE_SHUTTING_DOWN,
Ordering::AcqRel,
Ordering::Acquire,
) {
Ok(_) => {}
Err(STATE_SHUTDOWN) => return Ok(()),
Err(value) => {
return Err(ContextError::ContextNotActive {
state: DistributedContextState::decode(value),
});
}
}
let result = (|| {
let mut stream = self.lock_control()?;
write_control(&mut stream, ControlMessage::shutdown())?;
let response = read_control(&mut stream)?;
match response.kind {
MessageKind::Shutdown => Ok(()),
MessageKind::Abort => Err(ContextError::PeerAborted {
failure: ContextFailure::from_code(response.code),
}),
_ => Err(ContextError::UnexpectedControlMessage),
}
})();
match result {
Ok(()) => {
self.handle.state.store(STATE_SHUTDOWN, Ordering::Release);
Ok(())
}
Err(error) => {
self.handle.invalidate();
Err(error)
}
}
}
#[cfg(feature = "std")]
fn lock_control(&self) -> Result<std::sync::MutexGuard<'_, TcpStream>, ContextError> {
self.control
.lock()
.map_err(|_| ContextError::ControlLockPoisoned)
}
#[cfg(feature = "std")]
fn network_failure(&self, error: ContextError) -> ContextError {
self.handle.invalidate();
error
}
}