use coeus_core::{
ComputeBackend, CpuAddressableStorageMut, Layout, MoiraiBackend, NumericElement, Scalar,
SequentialBackend, Shape,
};
use coeus_ops::backend_ops::ElementwiseOps;
use coeus_ops::{BinaryOp, CpuBackend};
const SIZES: &[usize] = &[1, 2, 7, 8, 31, 8192, 8193, 16_384, 20_001];
fn reference<T: Scalar + coeus_core::FloatElement>(op: BinaryOp, x: T, y: T) -> T {
match op {
BinaryOp::Add => x + y,
BinaryOp::Sub => x - y,
BinaryOp::Mul => x * y,
BinaryOp::Div => x / y,
BinaryOp::Eq => <T as coeus_core::FloatElement>::from_f64(if x == y { 1.0 } else { 0.0 }),
BinaryOp::Ne => <T as coeus_core::FloatElement>::from_f64(if x != y { 1.0 } else { 0.0 }),
BinaryOp::Lt => <T as coeus_core::FloatElement>::from_f64(if x < y { 1.0 } else { 0.0 }),
BinaryOp::Gt => <T as coeus_core::FloatElement>::from_f64(if x > y { 1.0 } else { 0.0 }),
BinaryOp::Le => <T as coeus_core::FloatElement>::from_f64(if x <= y { 1.0 } else { 0.0 }),
BinaryOp::Ge => <T as coeus_core::FloatElement>::from_f64(if x >= y { 1.0 } else { 0.0 }),
}
}
fn device_binary<T: Scalar + leto_ops::Scalar, B: CpuBackend>(
backend: &B,
op: BinaryOp,
a: &[T],
b: &[T],
) -> Vec<T>
where
B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
{
let n = a.len();
let layout = Layout::new(Shape::from(vec![n]));
let mut a_buf = unsafe { ComputeBackend::allocate::<T>(backend, n) }
.expect("invariant: test backend storage operation succeeds");
let mut b_buf = unsafe { ComputeBackend::allocate::<T>(backend, n) }
.expect("invariant: test backend storage operation succeeds");
let mut c_buf = backend
.allocate_zeroed::<T>(n)
.expect("invariant: test backend storage operation succeeds");
backend
.copy_to_device(a, &mut a_buf)
.expect("invariant: test backend storage operation succeeds");
backend
.copy_to_device(b, &mut b_buf)
.expect("invariant: test backend storage operation succeeds");
backend
.elementwise_binary(op, &a_buf, &layout, &b_buf, &layout, &mut c_buf, &layout)
.expect("valid binary test layouts");
let mut out = vec![T::ZERO; n];
backend
.copy_to_host(&c_buf, &mut out)
.expect("invariant: test backend storage operation succeeds");
out
}
fn check_op<T: Scalar + leto_ops::Scalar + coeus_core::FloatElement, B: CpuBackend>(
backend: &B,
op: BinaryOp,
) where
B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
{
for &n in SIZES {
let a: Vec<T> = (0..n)
.map(|i| <T as coeus_core::FloatElement>::from_f64(i as f64 * 0.5 - 3.0))
.collect();
let b: Vec<T> = (0..n)
.map(|i| <T as coeus_core::FloatElement>::from_f64((i % 7) as f64 + 1.0))
.collect();
let got = device_binary(backend, op, &a, &b);
let expected: Vec<T> = a
.iter()
.zip(&b)
.map(|(&x, &y)| reference(op, x, y))
.collect();
for i in 0..n {
assert_eq!(
NumericElement::to_f64(got[i]).to_bits(),
NumericElement::to_f64(expected[i]).to_bits(),
"{op:?} mismatch at i={i}, n={n}",
);
}
}
}
const OPS: &[BinaryOp] = &[BinaryOp::Add, BinaryOp::Sub, BinaryOp::Mul, BinaryOp::Div];
#[test]
fn sequential_f32_matches_scalar_reference() {
let backend = SequentialBackend;
for &op in OPS {
check_op::<f32, _>(&backend, op);
}
}
#[test]
fn sequential_f64_matches_scalar_reference() {
let backend = SequentialBackend;
for &op in OPS {
check_op::<f64, _>(&backend, op);
}
}
#[test]
fn moirai_f32_matches_scalar_reference() {
let backend = MoiraiBackend;
for &op in OPS {
check_op::<f32, _>(&backend, op);
}
}
#[test]
fn moirai_f64_matches_scalar_reference() {
let backend = MoiraiBackend;
for &op in OPS {
check_op::<f64, _>(&backend, op);
}
}