use coeus_core::{
BackendError, ClosedReduction, CpuAddressableStorage, CpuAddressableStorageMut, Layout, Scalar,
};
use super::error::map_leto_error;
use super::CpuBackend;
use crate::backend_ops::ReductionOp;
#[inline]
pub(super) fn reduce<T, B>(
_backend: &B,
op: ReductionOp,
a: &B::DeviceBuffer<T>,
a_layout: &Layout,
axis: usize,
c: &mut B::DeviceBuffer<T>,
c_layout: &Layout,
) -> Result<(), BackendError>
where
T: Scalar + leto_ops::Scalar,
B: CpuBackend,
B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
{
let op = ClosedReduction::from_op("reduction", op)?;
coeus_cpu::reduce_into(
op,
a_layout,
a.as_slice(),
axis,
c_layout,
c.as_mut_slice()?,
)
.map_err(|error| map_leto_error("reduction", error))
}
pub(super) fn reduce_mean<T, B>(
_backend: &B,
a: &B::DeviceBuffer<T>,
a_layout: &Layout,
axis: usize,
c: &mut B::DeviceBuffer<T>,
c_layout: &Layout,
) -> Result<(), BackendError>
where
T: Scalar + leto_ops::Scalar + coeus_core::FloatElement,
B: CpuBackend,
B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
{
coeus_cpu::mean_into(a_layout, a.as_slice(), axis, c_layout, c.as_mut_slice()?)
.map_err(|error| map_leto_error("reduction", error))
}
pub(super) fn reduce_prod<T, B>(
_backend: &B,
a: &B::DeviceBuffer<T>,
a_layout: &Layout,
axis: usize,
c: &mut B::DeviceBuffer<T>,
c_layout: &Layout,
) -> Result<(), BackendError>
where
T: Scalar + leto_ops::Scalar,
B: CpuBackend,
B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
{
coeus_cpu::reduce_into(
ClosedReduction::Prod,
a_layout,
a.as_slice(),
axis,
c_layout,
c.as_mut_slice()?,
)
.map_err(|error| map_leto_error("reduction", error))
}
#[inline]
pub(super) fn argmax<T, B>(
backend: &B,
a: &B::DeviceBuffer<T>,
a_layout: &Layout,
axis: usize,
c: &mut B::DeviceBuffer<i64>,
c_layout: &Layout,
) -> Result<(), BackendError>
where
T: Scalar + leto_ops::Scalar,
B: CpuBackend,
B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
B::DeviceBuffer<i64>: CpuAddressableStorageMut<i64>,
{
coeus_cpu::argmax_into(
a_layout,
a.as_slice(),
axis,
c_layout,
backend.as_mut_index_buffer(c)?,
)
.map_err(|error| map_leto_error("argmax", error))
}
#[inline]
pub(super) fn argmin<T, B>(
backend: &B,
a: &B::DeviceBuffer<T>,
a_layout: &Layout,
axis: usize,
c: &mut B::DeviceBuffer<i64>,
c_layout: &Layout,
) -> Result<(), BackendError>
where
T: Scalar + leto_ops::Scalar,
B: CpuBackend,
B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
B::DeviceBuffer<i64>: CpuAddressableStorageMut<i64>,
{
coeus_cpu::argmin_into(
a_layout,
a.as_slice(),
axis,
c_layout,
backend.as_mut_index_buffer(c)?,
)
.map_err(|error| map_leto_error("argmin", error))
}
#[inline]
pub(super) fn topk<T, B>(
backend: &B,
a: &B::DeviceBuffer<T>,
a_layout: &Layout,
k: usize,
axis: usize,
largest: bool,
values: &mut B::DeviceBuffer<T>,
_values_layout: &Layout,
indices: &mut B::DeviceBuffer<i64>,
_indices_layout: &Layout,
) -> Result<(), BackendError>
where
T: Scalar + leto_ops::Scalar,
B: CpuBackend,
B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
B::DeviceBuffer<i64>: CpuAddressableStorageMut<i64>,
{
crate::reduction::topk::topk_impl(
a.as_slice(),
a_layout.shape(),
k,
axis,
largest,
values.as_mut_slice()?,
backend.as_mut_index_buffer(indices)?,
);
Ok(())
}
#[inline]
pub(super) fn cumsum<T, B>(
_backend: &B,
a: &B::DeviceBuffer<T>,
a_layout: &Layout,
axis: usize,
c: &mut B::DeviceBuffer<T>,
c_layout: &Layout,
) -> Result<(), BackendError>
where
T: Scalar + leto_ops::Scalar,
B: CpuBackend,
B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
{
coeus_cpu::cumsum_into(a_layout, a.as_slice(), axis, c_layout, c.as_mut_slice()?)
.map_err(|error| map_leto_error("cumsum", error))
}
#[inline]
pub(super) fn suffix_sum<T, B>(
_backend: &B,
a: &B::DeviceBuffer<T>,
a_layout: &Layout,
axis: usize,
c: &mut B::DeviceBuffer<T>,
c_layout: &Layout,
) -> Result<(), BackendError>
where
T: Scalar + leto_ops::Scalar,
B: CpuBackend,
B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
{
coeus_cpu::suffix_sum_into(a_layout, a.as_slice(), axis, c_layout, c.as_mut_slice()?)
.map_err(|error| map_leto_error("suffix_sum", error))
}
#[inline]
pub(super) fn cumprod<T, B>(
_backend: &B,
a: &B::DeviceBuffer<T>,
a_layout: &Layout,
axis: usize,
c: &mut B::DeviceBuffer<T>,
c_layout: &Layout,
) -> Result<(), BackendError>
where
T: Scalar + leto_ops::Scalar,
B: CpuBackend,
B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
{
coeus_cpu::cumprod_into(a_layout, a.as_slice(), axis, c_layout, c.as_mut_slice()?)
.map_err(|error| map_leto_error("cumprod", error))
}
#[inline]
pub(super) fn suffix_prod<T, B>(
_backend: &B,
a: &B::DeviceBuffer<T>,
a_layout: &Layout,
axis: usize,
c: &mut B::DeviceBuffer<T>,
c_layout: &Layout,
) -> Result<(), BackendError>
where
T: Scalar + leto_ops::Scalar,
B: CpuBackend,
B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
{
coeus_cpu::suffix_prod_into(a_layout, a.as_slice(), axis, c_layout, c.as_mut_slice()?)
.map_err(|error| map_leto_error("suffix_prod", error))
}