Skip to main content

ReductionOp

Trait ReductionOp 

Source
pub trait ReductionOp<T>:
    Sealed
    + Copy
    + 'static
where T: NumericElement,
{ // Required methods unsafe fn accumulate<Arch>( acc: <Arch as SimdKernel<T>>::Vector, v: <Arch as SimdKernel<T>>::Vector, ) -> <Arch as SimdKernel<T>>::Vector where Arch: SimdKernel<T>; unsafe fn finalize<Arch>(acc: <Arch as SimdKernel<T>>::Vector) -> T where Arch: SimdKernel<T>; fn identity_scalar() -> T; fn scalar_combine(a: T, b: T) -> T; // Provided methods unsafe fn fma_pair_accumulate<Arch>( acc: <Arch as SimdKernel<T>>::Vector, a: <Arch as SimdKernel<T>>::Vector, b: <Arch as SimdKernel<T>>::Vector, ) -> <Arch as SimdKernel<T>>::Vector where Arch: SimdKernel<T> { ... } fn scalar_accumulate(acc: T, elem: T) -> T { ... } unsafe fn identity_vector<Arch>() -> <Arch as SimdKernel<T>>::Vector where Arch: SimdKernel<T> { ... } unsafe fn transform_vector<Arch>( v: <Arch as SimdKernel<T>>::Vector, ) -> <Arch as SimdKernel<T>>::Vector where Arch: SimdKernel<T> { ... } unsafe fn combine_vectors<Arch>( a: <Arch as SimdKernel<T>>::Vector, b: <Arch as SimdKernel<T>>::Vector, ) -> <Arch as SimdKernel<T>>::Vector where Arch: SimdKernel<T> { ... } }
Expand description

Sealed ZST trait for SIMD horizontal reduction strategies.

Implementors define how a vector accumulator is updated (accumulate) and how the final scalar result is extracted (finalize). Both methods are #[inline(always)] and carry no branching — DCE eliminates unused strategies entirely.

§Identity Element

identity_scalar() returns the reduction identity (0 for Sum, T::MAX_VALUE for Min, T::MIN_VALUE for Max). It is used for empty-slice fast paths and for combining the scalar tail with the SIMD result via scalar_combine.

Required Methods§

Source

unsafe fn accumulate<Arch>( acc: <Arch as SimdKernel<T>>::Vector, v: <Arch as SimdKernel<T>>::Vector, ) -> <Arch as SimdKernel<T>>::Vector
where Arch: SimdKernel<T>,

Merge a new data vector v into accumulator acc.

§Safety

Processor must support the target feature of Arch.

Source

unsafe fn finalize<Arch>(acc: <Arch as SimdKernel<T>>::Vector) -> T
where Arch: SimdKernel<T>,

Reduce the final accumulator to a scalar.

§Safety

Processor must support the target feature of Arch.

Source

fn identity_scalar() -> T

The identity element for this reduction as a scalar.

Default: must be overridden. Sum returns T::ZERO, Min returns T::MAX_VALUE, Max returns T::MIN_VALUE.

Source

fn scalar_combine(a: T, b: T) -> T

Combine two scalar partial results using this reduction.

For Sum: addition. For Min: min_scalar. For Max: max_scalar.

Provided Methods§

Source

unsafe fn fma_pair_accumulate<Arch>( acc: <Arch as SimdKernel<T>>::Vector, a: <Arch as SimdKernel<T>>::Vector, b: <Arch as SimdKernel<T>>::Vector, ) -> <Arch as SimdKernel<T>>::Vector
where Arch: SimdKernel<T>,

FMA-aware pairwise accumulation: acc = fuse(acc, a, b) where fuse may use a single fused multiply-add instruction rather than a separate mul + accumulate.

Default: Self::accumulate(acc, Arch::mul(a, b)) — a correct two-instruction fallback. Override this for Dot and similar operations that can exploit Arch::fmadd.

§Safety

Processor must support the target feature of Arch.

Source

fn scalar_accumulate(acc: T, elem: T) -> T

Accumulate a single scalar element elem into a scalar accumulator acc.

Default: delegates to Self::scalar_combine(acc, elem). Override this for reductions whose SIMD accumulate applies a per-element transform (e.g. SquaredSum applies elem * elem before adding). The scalar tail path uses this method instead of scalar_combine to maintain correctness for slices shorter than Arch::LANE_COUNT.

Source

unsafe fn identity_vector<Arch>() -> <Arch as SimdKernel<T>>::Vector
where Arch: SimdKernel<T>,

Splat the identity element into a vector register.

Default: Arch::splat(Self::identity_scalar()). Backends may override.

§Safety

Processor must support the target feature of Arch.

Source

unsafe fn transform_vector<Arch>( v: <Arch as SimdKernel<T>>::Vector, ) -> <Arch as SimdKernel<T>>::Vector
where Arch: SimdKernel<T>,

Per-element lane transform applied before combining (identity by default).

Reductions with a per-element transform (AbsSum applies abs) override this so the reduce loop can seed unrolled accumulators with transform_vector(load(...)) instead of raw loads.

§Safety

Processor must support the target feature of Arch.

Source

unsafe fn combine_vectors<Arch>( a: <Arch as SimdKernel<T>>::Vector, b: <Arch as SimdKernel<T>>::Vector, ) -> <Arch as SimdKernel<T>>::Vector
where Arch: SimdKernel<T>,

Merge two partial accumulators WITHOUT the per-element transform.

accumulate is combine_vectors(acc, transform_vector(v)); the reduce loop’s cross-accumulator merge must use this method, because the partials are already transformed. Default delegates to accumulate, which is correct exactly when transform_vector is the identity — transform-bearing ops must override both.

§Safety

Processor must support the target feature of Arch.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<T> ReductionOp<T> for AbsMax
where T: NumericElement,

Source§

impl<T> ReductionOp<T> for AbsSum
where T: NumericElement,

Source§

impl<T> ReductionOp<T> for Dot
where T: NumericElement,

Source§

impl<T> ReductionOp<T> for Max
where T: NumericElement,

Source§

impl<T> ReductionOp<T> for Min
where T: NumericElement,

Source§

impl<T> ReductionOp<T> for Product
where T: NumericElement,

Source§

impl<T> ReductionOp<T> for Sum
where T: NumericElement,