#[cfg(feature = "distributed")]
use crate::exec::ReduceOp;
#[cfg(feature = "distributed")]
use alloc::vec::Vec;
#[cfg(feature = "distributed")]
use core::fmt;
use core::marker::PhantomData;
#[cfg(feature = "distributed")]
use typenum::Unsigned;
use crate::shapes::Dyn;
#[non_exhaustive]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PlacementKind {
#[default]
Local,
#[cfg(feature = "distributed")]
Replicated,
#[cfg(feature = "distributed")]
Sharded {
axis: usize,
},
#[cfg(feature = "distributed")]
Partial {
reduction: ReduceOp,
},
#[cfg(feature = "distributed")]
PipelineStage {
index: usize,
},
}
impl PlacementKind {
#[must_use]
pub const fn is_complete(self) -> bool {
match self {
#[cfg(feature = "distributed")]
Self::Partial { .. } => false,
_ => true,
}
}
#[must_use]
pub const fn is_distributed(self) -> bool {
match self {
Self::Local => false,
#[cfg(feature = "distributed")]
_ => true,
}
}
}
pub trait Placement: 'static + Clone + core::fmt::Debug + Send + Sync {
type Field: Clone + core::fmt::Debug + Default + Send + Sync;
#[doc(hidden)]
fn try_from_incin(kind: PlacementKind, rank: usize) -> Option<Self::Field>;
fn to_incin(field: &Self::Field) -> PlacementKind;
fn rank(field: &Self::Field) -> usize;
#[doc(hidden)]
const RANKS: usize = 1;
#[doc(hidden)]
const SHARD_DEGREE: usize = 1;
#[doc(hidden)]
const PIPELINE_DEGREE: usize = 1;
}
pub trait ConstPlacement: Placement {
const PLACEMENT: PlacementKind;
}
#[cfg(feature = "distributed")]
pub trait PlacementOn<Mesh: crate::dist::mesh::ValidMesh>: Placement {}
#[doc(hidden)]
pub struct RankedPlacement<P> {
rank: usize,
marker: PhantomData<fn() -> P>,
}
impl<P> RankedPlacement<P> {
fn new(rank: usize) -> Self {
Self {
rank,
marker: PhantomData,
}
}
}
impl<P> Clone for RankedPlacement<P> {
fn clone(&self) -> Self {
*self
}
}
impl<P> Copy for RankedPlacement<P> {}
impl<P> Default for RankedPlacement<P> {
fn default() -> Self {
Self::new(0)
}
}
impl<P> core::fmt::Debug for RankedPlacement<P> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("RankedPlacement")
.field("rank", &self.rank)
.finish()
}
}
#[doc(hidden)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub struct DynamicPlacement {
kind: PlacementKind,
rank: usize,
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Local;
impl Placement for Local {
type Field = PhantomData<Self>;
fn try_from_incin(kind: PlacementKind, rank: usize) -> Option<Self::Field> {
(kind == Self::PLACEMENT && rank == 0).then_some(PhantomData)
}
fn to_incin(_: &Self::Field) -> PlacementKind {
Self::PLACEMENT
}
fn rank(_: &Self::Field) -> usize {
0
}
}
impl ConstPlacement for Local {
const PLACEMENT: PlacementKind = PlacementKind::Local;
}
impl Placement for Dyn {
type Field = DynamicPlacement;
fn try_from_incin(kind: PlacementKind, rank: usize) -> Option<Self::Field> {
Some(DynamicPlacement { kind, rank })
}
fn to_incin(field: &Self::Field) -> PlacementKind {
field.kind
}
fn rank(field: &Self::Field) -> usize {
field.rank
}
}
#[cfg(feature = "distributed")]
pub struct Replicated<Mesh>(PhantomData<fn() -> Mesh>);
#[cfg(feature = "distributed")]
pub struct Sharded<Mesh, Axis>(PhantomData<fn() -> (Mesh, Axis)>);
#[cfg(feature = "distributed")]
pub struct Partial<Mesh, Reduction>(PhantomData<fn() -> (Mesh, Reduction)>);
#[cfg(feature = "distributed")]
pub struct PipelineStage<Mesh, const INDEX: usize>(PhantomData<fn() -> Mesh>);
#[cfg(feature = "distributed")]
macro_rules! marker_impls {
($name:ident<$($parameter:ident),+>, $rendered:literal) => {
impl<$($parameter),+> Copy for $name<$($parameter),+> {}
impl<$($parameter),+> Clone for $name<$($parameter),+> {
fn clone(&self) -> Self {
*self
}
}
impl<$($parameter),+> Default for $name<$($parameter),+> {
fn default() -> Self {
Self(PhantomData)
}
}
impl<$($parameter),+> fmt::Debug for $name<$($parameter),+> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str($rendered)
}
}
};
}
#[cfg(feature = "distributed")]
marker_impls!(Replicated<Mesh>, "Replicated");
#[cfg(feature = "distributed")]
marker_impls!(Sharded<Mesh, Axis>, "Sharded");
#[cfg(feature = "distributed")]
marker_impls!(Partial<Mesh, Reduction>, "Partial");
#[cfg(feature = "distributed")]
impl<Mesh, const INDEX: usize> Copy for PipelineStage<Mesh, INDEX> {}
#[cfg(feature = "distributed")]
impl<Mesh, const INDEX: usize> Clone for PipelineStage<Mesh, INDEX> {
fn clone(&self) -> Self {
*self
}
}
#[cfg(feature = "distributed")]
impl<Mesh, const INDEX: usize> Default for PipelineStage<Mesh, INDEX> {
fn default() -> Self {
Self(PhantomData)
}
}
#[cfg(feature = "distributed")]
impl<Mesh, const INDEX: usize> fmt::Debug for PipelineStage<Mesh, INDEX> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "PipelineStage<{INDEX}>")
}
}
#[cfg(feature = "distributed")]
pub trait PlacementAxis: 'static {
const INDEX: usize;
}
#[cfg(feature = "distributed")]
impl<Axis> PlacementAxis for Axis
where
Axis: Unsigned + 'static,
{
const INDEX: usize = Axis::USIZE;
}
#[cfg(feature = "distributed")]
pub trait PartialReduction: 'static {
const OP: ReduceOp;
}
#[cfg(feature = "distributed")]
macro_rules! reduction_markers {
($(($name:ident, $op:ident)),+ $(,)?) => {
$(
#[doc = concat!("Type-level `", stringify!($op), "` reduction.")]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub struct $name;
impl PartialReduction for $name {
const OP: ReduceOp = ReduceOp::$op;
}
)+
};
}
#[cfg(feature = "distributed")]
reduction_markers!(
(Sum, Sum),
(Mean, Mean),
(Max, Max),
(Min, Min),
(Prod, Prod),
);
#[cfg(feature = "distributed")]
impl<Mesh> Placement for Replicated<Mesh>
where
Mesh: crate::dist::mesh::ValidMesh,
{
type Field = RankedPlacement<Self>;
const RANKS: usize = Mesh::WORLD;
fn try_from_incin(kind: PlacementKind, rank: usize) -> Option<Self::Field> {
(kind == Self::PLACEMENT).then(|| RankedPlacement::new(rank))
}
fn to_incin(_: &Self::Field) -> PlacementKind {
Self::PLACEMENT
}
fn rank(field: &Self::Field) -> usize {
field.rank
}
}
#[cfg(feature = "distributed")]
impl<Mesh> ConstPlacement for Replicated<Mesh>
where
Mesh: crate::dist::mesh::ValidMesh,
{
const PLACEMENT: PlacementKind = PlacementKind::Replicated;
}
#[cfg(feature = "distributed")]
impl<Mesh> PlacementOn<Mesh> for Replicated<Mesh> where Mesh: crate::dist::mesh::ValidMesh {}
#[cfg(feature = "distributed")]
impl<Mesh, Axis> Placement for Sharded<Mesh, Axis>
where
Mesh: crate::dist::mesh::ValidMesh,
Axis: PlacementAxis,
{
type Field = RankedPlacement<Self>;
const RANKS: usize = Mesh::WORLD;
const SHARD_DEGREE: usize = Mesh::TENSOR;
fn try_from_incin(kind: PlacementKind, rank: usize) -> Option<Self::Field> {
(kind == Self::PLACEMENT).then(|| RankedPlacement::new(rank))
}
fn to_incin(_: &Self::Field) -> PlacementKind {
Self::PLACEMENT
}
fn rank(field: &Self::Field) -> usize {
field.rank
}
}
#[cfg(feature = "distributed")]
impl<Mesh, Axis> ConstPlacement for Sharded<Mesh, Axis>
where
Mesh: crate::dist::mesh::ValidMesh,
Axis: PlacementAxis,
{
const PLACEMENT: PlacementKind = PlacementKind::Sharded { axis: Axis::INDEX };
}
#[cfg(feature = "distributed")]
impl<Mesh, Axis> PlacementOn<Mesh> for Sharded<Mesh, Axis>
where
Mesh: crate::dist::mesh::ValidMesh,
Axis: PlacementAxis,
{
}
#[cfg(feature = "distributed")]
impl<Mesh, Reduction> Placement for Partial<Mesh, Reduction>
where
Mesh: crate::dist::mesh::ValidMesh,
Reduction: PartialReduction,
{
type Field = RankedPlacement<Self>;
const RANKS: usize = Mesh::WORLD;
fn try_from_incin(kind: PlacementKind, rank: usize) -> Option<Self::Field> {
(kind == Self::PLACEMENT).then(|| RankedPlacement::new(rank))
}
fn to_incin(_: &Self::Field) -> PlacementKind {
Self::PLACEMENT
}
fn rank(field: &Self::Field) -> usize {
field.rank
}
}
#[cfg(feature = "distributed")]
impl<Mesh, Reduction> ConstPlacement for Partial<Mesh, Reduction>
where
Mesh: crate::dist::mesh::ValidMesh,
Reduction: PartialReduction,
{
const PLACEMENT: PlacementKind = PlacementKind::Partial {
reduction: Reduction::OP,
};
}
#[cfg(feature = "distributed")]
impl<Mesh, Reduction> PlacementOn<Mesh> for Partial<Mesh, Reduction>
where
Mesh: crate::dist::mesh::ValidMesh,
Reduction: PartialReduction,
{
}
#[cfg(feature = "distributed")]
impl<Mesh, const INDEX: usize> Placement for PipelineStage<Mesh, INDEX>
where
Mesh: crate::dist::mesh::ValidMesh,
{
type Field = RankedPlacement<Self>;
const RANKS: usize = Mesh::WORLD;
const PIPELINE_DEGREE: usize = Mesh::PIPELINE;
fn try_from_incin(kind: PlacementKind, rank: usize) -> Option<Self::Field> {
(kind == Self::PLACEMENT).then(|| RankedPlacement::new(rank))
}
fn to_incin(_: &Self::Field) -> PlacementKind {
Self::PLACEMENT
}
fn rank(field: &Self::Field) -> usize {
field.rank
}
}
#[cfg(feature = "distributed")]
impl<Mesh, const INDEX: usize> ConstPlacement for PipelineStage<Mesh, INDEX>
where
Mesh: crate::dist::mesh::ValidMesh,
{
const PLACEMENT: PlacementKind = PlacementKind::PipelineStage { index: INDEX };
}
#[cfg(feature = "distributed")]
impl<Mesh, const INDEX: usize> PlacementOn<Mesh> for PipelineStage<Mesh, INDEX> where
Mesh: crate::dist::mesh::ValidMesh
{
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
#[cfg(feature = "distributed")]
pub struct PlacementBuf {
placements: Vec<PlacementKind>,
}
#[cfg(feature = "distributed")]
impl PlacementBuf {
#[must_use]
pub fn from_slice(placements: &[PlacementKind]) -> Self {
Self {
placements: placements.to_vec(),
}
}
#[must_use]
pub fn len(&self) -> usize {
self.placements.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.placements.is_empty()
}
#[must_use]
pub fn as_slice(&self) -> &[PlacementKind] {
&self.placements
}
}
#[cfg(feature = "distributed")]
impl<const N: usize> From<[PlacementKind; N]> for PlacementBuf {
fn from(placements: [PlacementKind; N]) -> Self {
Self {
placements: placements.into(),
}
}
}