use crate::{PeerId, ReduceOperation};
use ruda_tensor::Backend;
use crate::{
CollectiveConfig, global::shared::GlobalCollectiveError, local::server::get_collective_client,
};
#[allow(unused)]
#[derive(Debug, Clone)]
pub enum CollectiveError {
InvalidConfig,
MultipleUnregister,
MultipleRegister,
RegisterParamsMismatch,
AllReduceShapeMismatch,
AllReduceOperationMismatch,
ReduceShapeMismatch,
ReduceOperationMismatch,
ReduceRootMismatch,
BroadcastRootMismatch,
BroadcastNoTensor,
BroadcastMultipleTensors,
LocalServerMissing,
RegisterNotFirstOperation,
Global(GlobalCollectiveError),
#[allow(unused)]
Other(String),
}
pub fn register<B: Backend>(
id: PeerId,
device: B::Device,
config: CollectiveConfig,
) -> Result<(), CollectiveError> {
log::info!("Registering peer {id} with config: {config}");
let mut client = get_collective_client::<B>();
client.register(id, device, config)
}
pub fn all_reduce<B: Backend>(
id: PeerId,
tensor: B::FloatTensorPrimitive,
op: ReduceOperation,
) -> Result<B::FloatTensorPrimitive, CollectiveError> {
let client = get_collective_client::<B>();
client.all_reduce(id, tensor, op)
}
pub fn broadcast<B: Backend>(
id: PeerId,
tensor: Option<B::FloatTensorPrimitive>,
) -> Result<B::FloatTensorPrimitive, CollectiveError> {
let client = get_collective_client::<B>();
client.broadcast(id, tensor)
}
pub fn reduce<B: Backend>(
id: PeerId,
tensor: B::FloatTensorPrimitive,
op: ReduceOperation,
root: PeerId,
) -> Result<Option<B::FloatTensorPrimitive>, CollectiveError> {
let client = get_collective_client::<B>();
client.reduce(id, tensor, op, root)
}
pub fn finish_collective<B: Backend>(id: PeerId) -> Result<(), CollectiveError> {
let client = get_collective_client::<B>();
client.finish(id)
}
pub fn reset_collective<B: Backend>() {
let client = get_collective_client::<B>();
client.reset();
}