coeus-ops 0.12.0

Numerical tensor operators for Coeus.
Documentation
//! Elementwise CPU kernel delegations: unary and binary ops.

use coeus_core::{CpuAddressableStorage, CpuAddressableStorageMut, Float, Layout, Scalar};

use super::error::map_leto_error;
use super::CpuBackend;
use crate::backend_ops::ops::{BinaryOp, UnaryOp};

#[inline]
pub(super) fn elementwise_binary<T, B>(
    _backend: &B,
    op: BinaryOp,
    a: &B::DeviceBuffer<T>,
    a_layout: &Layout,
    b: &B::DeviceBuffer<T>,
    b_layout: &Layout,
    c: &mut B::DeviceBuffer<T>,
    c_layout: &Layout,
) -> Result<(), B::Error>
where
    T: Scalar + leto_ops::Scalar,
    B: CpuBackend,
    B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
{
    coeus_leto::elementwise_binary_into(
        op,
        a_layout,
        a.as_slice(),
        b_layout,
        b.as_slice(),
        c_layout,
        c.as_mut_slice(),
    )
    .map_err(|error| map_leto_error("elementwise binary", error))
}

#[inline]
pub(super) fn elementwise_binary_assign<T, B>(
    _backend: &B,
    op: BinaryOp,
    a: &mut B::DeviceBuffer<T>,
    a_layout: &Layout,
    b: &B::DeviceBuffer<T>,
    b_layout: &Layout,
) -> Result<(), B::Error>
where
    T: Scalar + leto_ops::Scalar,
    B: CpuBackend,
    B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
{
    coeus_leto::elementwise_binary_assign(op, a_layout, a.as_mut_slice(), b_layout, b.as_slice())
        .map_err(|error| map_leto_error("elementwise binary assign", error))
}

#[inline]
pub(super) fn elementwise_unary<T, B>(
    _backend: &B,
    op: UnaryOp,
    a: &B::DeviceBuffer<T>,
    a_layout: &Layout,
    c: &mut B::DeviceBuffer<T>,
    c_layout: &Layout,
) -> Result<(), B::Error>
where
    T: Scalar + leto_ops::Scalar,
    B: CpuBackend,
    B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
{
    coeus_leto::elementwise_unary_into(op, a_layout, a.as_slice(), c_layout, c.as_mut_slice())
        .map_err(|error| map_leto_error("elementwise unary", error))
}

#[inline]
pub(super) fn elementwise_pow_scalar<T, B>(
    _backend: &B,
    exponent: T,
    a: &B::DeviceBuffer<T>,
    a_layout: &Layout,
    c: &mut B::DeviceBuffer<T>,
    c_layout: &Layout,
) -> Result<(), B::Error>
where
    T: Scalar + Float + leto_ops::Scalar + leto_ops::RealScalar,
    B: CpuBackend,
    B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
{
    coeus_leto::elementwise_pow_scalar_into(
        exponent,
        a_layout,
        a.as_slice(),
        c_layout,
        c.as_mut_slice(),
    )
    .map_err(|error| map_leto_error("elementwise scalar power", error))
}