use crate::{error::LaError, DefaultDenseMatrix, Matrix, Vector};
#[cfg(feature = "cuda")]
#[allow(deprecated)]
pub mod cuda;
#[cfg(feature = "cuda-oxide")]
pub mod cuda_oxide;
#[cfg(feature = "nalgebra")]
pub mod nalgebra;
#[cfg(feature = "faer")]
pub mod faer;
#[cold]
#[inline(never)]
fn panic_incompatible_nbatch(src_nbatch: usize, dest_nbatch: usize, op: &str) -> ! {
panic!(
"incompatible nbatch in {}: lhs={}, rhs={}",
op, dest_nbatch, src_nbatch
);
}
#[inline]
pub(crate) fn broadcast_batch(dest: usize, src_nbatch: usize, dest_nbatch: usize) -> usize {
if src_nbatch == dest_nbatch {
return dest;
}
dest * src_nbatch / dest_nbatch
}
pub trait Context: Clone + Default {
#[inline]
fn nbatch(&self) -> usize {
1
}
#[inline]
fn synchronize(&self) {}
fn clone_with_nbatch(&self, nbatch: usize) -> Result<Self, LaError>;
#[inline]
fn assert_broadcastable_into(&self, src_nbatch: usize, op: &str) {
let nbatch = self.nbatch();
if src_nbatch == nbatch || src_nbatch == 1 {
return;
}
if src_nbatch == 0 || !nbatch.is_multiple_of(src_nbatch) {
panic_incompatible_nbatch(src_nbatch, nbatch, op);
}
}
fn vector_from_element<V: Vector<C = Self>>(&self, len: usize, value: V::T) -> V {
V::from_element(len, value, self.clone())
}
fn vector_from_vec<V: Vector<C = Self>>(&self, vec: Vec<V::T>) -> V {
V::from_vec(vec, self.clone())
}
fn vector_zeros<V: Vector<C = Self>>(&self, len: usize) -> V {
V::zeros(len, self.clone())
}
fn dense_mat_zeros<V: Vector<C = Self> + DefaultDenseMatrix>(
&self,
rows: usize,
cols: usize,
) -> <V as DefaultDenseMatrix>::M {
<<V as DefaultDenseMatrix>::M as Matrix>::zeros(rows, cols, self.clone())
}
}