arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-hal/src/api/scratch.rs
poulpy-hal/src/api/scratch.rsRaw
  1use crate::{
  2    api::{ModuleN, SvpPPolBytesOf, VecZnxBigBytesOf, VecZnxDftBytesOf, VmpPMatBytesOf},
  3    layouts::{Backend, MatZnx, ScalarZnx, Scratch, SvpPPol, VecZnx, VecZnxBig, VecZnxDft, VmpPMat, Zn},
  4};
  5
  6/// Allocates a new [crate::layouts::ScratchOwned] of `size` aligned bytes.
  7pub trait ScratchOwnedAlloc<B: Backend> {
  8    fn alloc(size: usize) -> Self;
  9}
 10
 11/// Borrows a slice of bytes into a [Scratch].
 12pub trait ScratchOwnedBorrow<B: Backend> {
 13    fn borrow(&mut self) -> &mut Scratch<B>;
 14}
 15
 16/// Wrap an array of mutable borrowed bytes into a [Scratch].
 17pub trait ScratchFromBytes<B: Backend> {
 18    fn from_bytes(data: &mut [u8]) -> &mut Scratch<B>;
 19}
 20
 21/// Returns how many bytes left can be taken from the scratch.
 22pub trait ScratchAvailable {
 23    fn available(&self) -> usize;
 24}
 25
 26/// Takes a slice of bytes from a [Scratch] and return a new [Scratch] minus the taken array of bytes.
 27pub trait TakeSlice {
 28    fn take_slice<T>(&mut self, len: usize) -> (&mut [T], &mut Self);
 29}
 30
 31impl<BE: Backend> Scratch<BE>
 32where
 33    Self: TakeSlice + ScratchAvailable + ScratchFromBytes<BE>,
 34{
 35    pub fn split_at_mut(&mut self, len: usize) -> (&mut Scratch<BE>, &mut Self) {
 36        let (take_slice, rem_slice) = self.take_slice(len);
 37        (Self::from_bytes(take_slice), rem_slice)
 38    }
 39
 40    pub fn split_mut(&mut self, n: usize, len: usize) -> (Vec<&mut Scratch<BE>>, &mut Self) {
 41        assert!(self.available() >= n * len);
 42        let mut scratches: Vec<&mut Scratch<BE>> = Vec::with_capacity(n);
 43        let mut scratch: &mut Scratch<BE> = self;
 44        for _ in 0..n {
 45            let (tmp, scratch_new) = scratch.split_at_mut(len);
 46            scratch = scratch_new;
 47            scratches.push(tmp);
 48        }
 49        (scratches, scratch)
 50    }
 51}
 52
 53impl<B: Backend> ScratchTakeBasic for Scratch<B> where Self: TakeSlice + ScratchFromBytes<B> {}
 54
 55pub trait ScratchTakeBasic
 56where
 57    Self: TakeSlice,
 58{
 59    fn take_scalar_znx(&mut self, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Self) {
 60        let (take_slice, rem_slice) = self.take_slice(ScalarZnx::bytes_of(n, cols));
 61        (ScalarZnx::from_data(take_slice, n, cols), rem_slice)
 62    }
 63
 64    fn take_svp_ppol<M, B: Backend>(&mut self, module: &M, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Self)
 65    where
 66        M: SvpPPolBytesOf + ModuleN,
 67    {
 68        let (take_slice, rem_slice) = self.take_slice(module.bytes_of_svp_ppol(cols));
 69        (SvpPPol::from_data(take_slice, module.n(), cols), rem_slice)
 70    }
 71
 72    fn take_zn(&mut self, n: usize, cols: usize, size: usize) -> (Zn<&mut [u8]>, &mut Self) {
 73        let (take_slice, rem_slice) = self.take_slice(Zn::bytes_of(n, cols, size));
 74        (Zn::from_data(take_slice, n, cols, size), rem_slice)
 75    }
 76
 77    fn take_vec_znx(&mut self, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Self) {
 78        let (take_slice, rem_slice) = self.take_slice(VecZnx::bytes_of(n, cols, size));
 79        (VecZnx::from_data(take_slice, n, cols, size), rem_slice)
 80    }
 81
 82    fn take_vec_znx_big<M, B: Backend>(&mut self, module: &M, cols: usize, size: usize) -> (VecZnxBig<&mut [u8], B>, &mut Self)
 83    where
 84        M: VecZnxBigBytesOf + ModuleN,
 85    {
 86        let (take_slice, rem_slice) = self.take_slice(module.bytes_of_vec_znx_big(cols, size));
 87        (
 88            VecZnxBig::from_data(take_slice, module.n(), cols, size),
 89            rem_slice,
 90        )
 91    }
 92
 93    fn take_vec_znx_dft<M, B: Backend>(&mut self, module: &M, cols: usize, size: usize) -> (VecZnxDft<&mut [u8], B>, &mut Self)
 94    where
 95        M: VecZnxDftBytesOf + ModuleN,
 96    {
 97        let (take_slice, rem_slice) = self.take_slice(module.bytes_of_vec_znx_dft(cols, size));
 98
 99        (
100            VecZnxDft::from_data(take_slice, module.n(), cols, size),
101            rem_slice,
102        )
103    }
104
105    fn take_vec_znx_dft_slice<M, B: Backend>(
106        &mut self,
107        module: &M,
108        len: usize,
109        cols: usize,
110        size: usize,
111    ) -> (Vec<VecZnxDft<&mut [u8], B>>, &mut Self)
112    where
113        M: VecZnxDftBytesOf + ModuleN,
114    {
115        let mut scratch: &mut Self = self;
116        let mut slice: Vec<VecZnxDft<&mut [u8], B>> = Vec::with_capacity(len);
117        for _ in 0..len {
118            let (znx, new_scratch) = scratch.take_vec_znx_dft(module, cols, size);
119            scratch = new_scratch;
120            slice.push(znx);
121        }
122        (slice, scratch)
123    }
124
125    fn take_vec_znx_slice(&mut self, len: usize, n: usize, cols: usize, size: usize) -> (Vec<VecZnx<&mut [u8]>>, &mut Self) {
126        let mut scratch: &mut Self = self;
127        let mut slice: Vec<VecZnx<&mut [u8]>> = Vec::with_capacity(len);
128        for _ in 0..len {
129            let (znx, new_scratch) = scratch.take_vec_znx(n, cols, size);
130            scratch = new_scratch;
131            slice.push(znx);
132        }
133        (slice, scratch)
134    }
135
136    fn take_vmp_pmat<M, B: Backend>(
137        &mut self,
138        module: &M,
139        rows: usize,
140        cols_in: usize,
141        cols_out: usize,
142        size: usize,
143    ) -> (VmpPMat<&mut [u8], B>, &mut Self)
144    where
145        M: VmpPMatBytesOf + ModuleN,
146    {
147        let (take_slice, rem_slice) = self.take_slice(module.bytes_of_vmp_pmat(rows, cols_in, cols_out, size));
148        (
149            VmpPMat::from_data(take_slice, module.n(), rows, cols_in, cols_out, size),
150            rem_slice,
151        )
152    }
153
154    fn take_mat_znx(
155        &mut self,
156        n: usize,
157        rows: usize,
158        cols_in: usize,
159        cols_out: usize,
160        size: usize,
161    ) -> (MatZnx<&mut [u8]>, &mut Self) {
162        let (take_slice, rem_slice) = self.take_slice(MatZnx::bytes_of(n, rows, cols_in, cols_out, size));
163        (
164            MatZnx::from_data(take_slice, n, rows, cols_in, cols_out, size),
165            rem_slice,
166        )
167    }
168}