arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/backend/src/hal/api/vmp_pmat.rs
backend/src/hal/api/vmp_pmat.rsRaw
  1use crate::hal::layouts::{
  2    Backend, MatZnxToRef, Scratch, VecZnxDftToMut, VecZnxDftToRef, VmpPMatOwned, VmpPMatToMut, VmpPMatToRef,
  3};
  4
  5pub trait VmpPMatAlloc<B: Backend> {
  6    fn vmp_pmat_alloc(&self, n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> VmpPMatOwned<B>;
  7}
  8
  9pub trait VmpPMatAllocBytes {
 10    fn vmp_pmat_alloc_bytes(&self, n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize;
 11}
 12
 13pub trait VmpPMatFromBytes<B: Backend> {
 14    fn vmp_pmat_from_bytes(
 15        &self,
 16        n: usize,
 17        rows: usize,
 18        cols_in: usize,
 19        cols_out: usize,
 20        size: usize,
 21        bytes: Vec<u8>,
 22    ) -> VmpPMatOwned<B>;
 23}
 24
 25pub trait VmpPrepareTmpBytes {
 26    fn vmp_prepare_tmp_bytes(&self, n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize;
 27}
 28
 29pub trait VmpPMatPrepare<B: Backend> {
 30    fn vmp_prepare<R, A>(&self, res: &mut R, a: &A, scratch: &mut Scratch<B>)
 31    where
 32        R: VmpPMatToMut<B>,
 33        A: MatZnxToRef;
 34}
 35
 36pub trait VmpApplyTmpBytes {
 37    fn vmp_apply_tmp_bytes(
 38        &self,
 39        n: usize,
 40        res_size: usize,
 41        a_size: usize,
 42        b_rows: usize,
 43        b_cols_in: usize,
 44        b_cols_out: usize,
 45        b_size: usize,
 46    ) -> usize;
 47}
 48
 49pub trait VmpApply<B: Backend> {
 50    /// Applies the vector matrix product [crate::hal::layouts::VecZnxDft] x [crate::hal::layouts::VmpPMat].
 51    ///
 52    /// A vector matrix product numerically equivalent to a sum of [crate::hal::api::SvpApply],
 53    /// where each [crate::hal::layouts::SvpPPol] is a limb of the input [crate::hal::layouts::VecZnx] in DFT,
 54    /// and each vector a [crate::hal::layouts::VecZnxDft] (row) of the [crate::hal::layouts::VmpPMat].
 55    ///
 56    /// As such, given an input [crate::hal::layouts::VecZnx] of `i` size and a [crate::hal::layouts::VmpPMat] of `i` rows and
 57    /// `j` size, the output is a [crate::hal::layouts::VecZnx] of `j` size.
 58    ///
 59    /// If there is a mismatch between the dimensions the largest valid ones are used.
 60    ///
 61    /// ```text
 62    /// |a b c d| x |e f g| = (a * |e f g| + b * |h i j| + c * |k l m|) = |n o p|
 63    ///             |h i j|
 64    ///             |k l m|
 65    /// ```
 66    /// where each element is a [crate::hal::layouts::VecZnxDft].
 67    ///
 68    /// # Arguments
 69    ///
 70    /// * `c`: the output of the vector matrix product, as a [crate::hal::layouts::VecZnxDft].
 71    /// * `a`: the left operand [crate::hal::layouts::VecZnxDft] of the vector matrix product.
 72    /// * `b`: the right operand [crate::hal::layouts::VmpPMat] of the vector matrix product.
 73    /// * `buf`: scratch space, the size can be obtained with [VmpApplyTmpBytes::vmp_apply_tmp_bytes].
 74    fn vmp_apply<R, A, C>(&self, res: &mut R, a: &A, b: &C, scratch: &mut Scratch<B>)
 75    where
 76        R: VecZnxDftToMut<B>,
 77        A: VecZnxDftToRef<B>,
 78        C: VmpPMatToRef<B>;
 79}
 80
 81pub trait VmpApplyAddTmpBytes {
 82    fn vmp_apply_add_tmp_bytes(
 83        &self,
 84        n: usize,
 85        res_size: usize,
 86        a_size: usize,
 87        b_rows: usize,
 88        b_cols_in: usize,
 89        b_cols_out: usize,
 90        b_size: usize,
 91    ) -> usize;
 92}
 93
 94pub trait VmpApplyAdd<B: Backend> {
 95    fn vmp_apply_add<R, A, C>(&self, res: &mut R, a: &A, b: &C, scale: usize, scratch: &mut Scratch<B>)
 96    where
 97        R: VecZnxDftToMut<B>,
 98        A: VecZnxDftToRef<B>,
 99        C: VmpPMatToRef<B>;
100}