use coeus_core::{ComputeBackend, Float, Layout, Scalar, StorageMut};
use super::super::ops::{BinaryOp, UnaryOp};
pub unsafe trait ElementwiseOps<T: Scalar>: ComputeBackend {
fn elementwise_binary(
&self,
op: BinaryOp,
a: &Self::DeviceBuffer<T>,
a_layout: &Layout,
b: &Self::DeviceBuffer<T>,
b_layout: &Layout,
c: &mut Self::DeviceBuffer<T>,
c_layout: &Layout,
) -> Result<(), Self::Error>;
fn elementwise_binary_assign(
&self,
op: BinaryOp,
a: &mut Self::DeviceBuffer<T>,
a_layout: &mut Layout,
b: &Self::DeviceBuffer<T>,
b_layout: &Layout,
) -> Result<(), Self::Error> {
let output_layout = Layout::new(a_layout.shape_cloned());
let mut output = self.allocate_zeroed(output_layout.numel())?;
self.elementwise_binary(op, a, a_layout, b, b_layout, &mut output, &output_layout)?;
*a = output;
*a_layout = output_layout;
Ok(())
}
fn elementwise_binary_update(
&self,
op: BinaryOp,
destination: &mut Self::DeviceBuffer<T>,
destination_layout: &Layout,
rhs: &Self::DeviceBuffer<T>,
rhs_layout: &Layout,
) -> Result<(), Self::Error> {
let source = destination.clone();
let mut candidate = source.clone();
candidate.make_unique()?;
self.elementwise_binary(
op,
&source,
destination_layout,
rhs,
rhs_layout,
&mut candidate,
destination_layout,
)?;
*destination = candidate;
Ok(())
}
fn elementwise_unary(
&self,
op: UnaryOp,
a: &Self::DeviceBuffer<T>,
a_layout: &Layout,
c: &mut Self::DeviceBuffer<T>,
c_layout: &Layout,
) -> Result<(), Self::Error>;
fn elementwise_unary_assign(
&self,
op: UnaryOp,
input: &mut Self::DeviceBuffer<T>,
input_layout: &mut Layout,
) -> Result<(), Self::Error> {
let output_layout = Layout::new(input_layout.shape_cloned());
let mut output = self.allocate_zeroed(output_layout.numel())?;
self.elementwise_unary(op, input, input_layout, &mut output, &output_layout)?;
*input = output;
*input_layout = output_layout;
Ok(())
}
}
pub unsafe trait ScalarPowerOps<T: Float>: ComputeBackend {
fn elementwise_pow_scalar(
&self,
input: &Self::DeviceBuffer<T>,
input_layout: &Layout,
exponent: T,
output: &mut Self::DeviceBuffer<T>,
output_layout: &Layout,
) -> Result<(), Self::Error>;
}