arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/backend/src/vec_znx_dft.rs
backend/src/vec_znx_dft.rsRaw
  1use std::marker::PhantomData;
  2
  3use crate::ffi::vec_znx_dft;
  4use crate::znx_base::ZnxInfos;
  5use crate::{
  6    Backend, DataView, DataViewMut, FFT64, Module, VecZnxBig, ZnxSliceSize, ZnxView, ZnxViewMut, ZnxZero, alloc_aligned,
  7};
  8use std::fmt;
  9
 10pub struct VecZnxDft<D, B: Backend> {
 11    pub(crate) data: D,
 12    pub(crate) n: usize,
 13    pub(crate) cols: usize,
 14    pub(crate) size: usize,
 15    pub(crate) _phantom: PhantomData<B>,
 16}
 17
 18impl<D, B: Backend> VecZnxDft<D, B> {
 19    pub fn into_big(self) -> VecZnxBig<D, B> {
 20        VecZnxBig::<D, B>::from_data(self.data, self.n, self.cols, self.size)
 21    }
 22}
 23
 24impl<D, B: Backend> ZnxInfos for VecZnxDft<D, B> {
 25    fn cols(&self) -> usize {
 26        self.cols
 27    }
 28
 29    fn rows(&self) -> usize {
 30        1
 31    }
 32
 33    fn n(&self) -> usize {
 34        self.n
 35    }
 36
 37    fn size(&self) -> usize {
 38        self.size
 39    }
 40}
 41
 42impl<D> ZnxSliceSize for VecZnxDft<D, FFT64> {
 43    fn sl(&self) -> usize {
 44        self.n() * self.cols()
 45    }
 46}
 47
 48impl<D, B: Backend> DataView for VecZnxDft<D, B> {
 49    type D = D;
 50    fn data(&self) -> &Self::D {
 51        &self.data
 52    }
 53}
 54
 55impl<D, B: Backend> DataViewMut for VecZnxDft<D, B> {
 56    fn data_mut(&mut self) -> &mut Self::D {
 57        &mut self.data
 58    }
 59}
 60
 61impl<D: AsRef<[u8]>> ZnxView for VecZnxDft<D, FFT64> {
 62    type Scalar = f64;
 63}
 64
 65pub(crate) fn bytes_of_vec_znx_dft<B: Backend>(module: &Module<B>, cols: usize, size: usize) -> usize {
 66    unsafe { vec_znx_dft::bytes_of_vec_znx_dft(module.ptr, size as u64) as usize * cols }
 67}
 68
 69impl<D: From<Vec<u8>>, B: Backend> VecZnxDft<D, B> {
 70    pub(crate) fn new(module: &Module<B>, cols: usize, size: usize) -> Self {
 71        let data = alloc_aligned::<u8>(bytes_of_vec_znx_dft(module, cols, size));
 72        Self {
 73            data: data.into(),
 74            n: module.n(),
 75            cols,
 76            size,
 77            _phantom: PhantomData,
 78        }
 79    }
 80
 81    pub(crate) fn new_from_bytes(module: &Module<B>, cols: usize, size: usize, bytes: impl Into<Vec<u8>>) -> Self {
 82        let data: Vec<u8> = bytes.into();
 83        assert!(data.len() == bytes_of_vec_znx_dft(module, cols, size));
 84        Self {
 85            data: data.into(),
 86            n: module.n(),
 87            cols,
 88            size,
 89            _phantom: PhantomData,
 90        }
 91    }
 92}
 93
 94impl<D: AsMut<[u8]> + AsRef<[u8]>> VecZnxDft<D, FFT64>
 95where
 96    VecZnxDft<D, FFT64>: VecZnxDftToMut<FFT64>,
 97{
 98    /// Extracts the a_col-th column of 'a' and stores it on the self_col-th column [Self].
 99    pub fn extract_column<C: AsRef<[u8]>>(&mut self, self_col: usize, a: &VecZnxDft<C, FFT64>, a_col: usize)
100    where
101        VecZnxDft<C, FFT64>: VecZnxDftToRef<FFT64>,
102    {
103        #[cfg(debug_assertions)]
104        {
105            assert!(self_col < self.cols());
106            assert!(a_col < a.cols());
107        }
108
109        let min_size: usize = self.size.min(a.size());
110        let max_size: usize = self.size;
111
112        let mut self_mut: VecZnxDft<&mut [u8], FFT64> = self.to_mut();
113        let a_ref: VecZnxDft<&[u8], FFT64> = a.to_ref();
114
115        (0..min_size).for_each(|i: usize| {
116            self_mut
117                .at_mut(self_col, i)
118                .copy_from_slice(a_ref.at(a_col, i));
119        });
120
121        (min_size..max_size).for_each(|i| {
122            self_mut.zero_at(self_col, i);
123        });
124    }
125}
126
127pub type VecZnxDftOwned<B> = VecZnxDft<Vec<u8>, B>;
128
129impl<D, B: Backend> VecZnxDft<D, B> {
130    pub(crate) fn from_data(data: D, n: usize, cols: usize, size: usize) -> Self {
131        Self {
132            data,
133            n,
134            cols,
135            size,
136            _phantom: PhantomData,
137        }
138    }
139}
140
141pub trait VecZnxDftToRef<B: Backend> {
142    fn to_ref(&self) -> VecZnxDft<&[u8], B>;
143}
144
145impl<D, B: Backend> VecZnxDftToRef<B> for VecZnxDft<D, B>
146where
147    D: AsRef<[u8]>,
148    B: Backend,
149{
150    fn to_ref(&self) -> VecZnxDft<&[u8], B> {
151        VecZnxDft {
152            data: self.data.as_ref(),
153            n: self.n,
154            cols: self.cols,
155            size: self.size,
156            _phantom: std::marker::PhantomData,
157        }
158    }
159}
160
161pub trait VecZnxDftToMut<B: Backend> {
162    fn to_mut(&mut self) -> VecZnxDft<&mut [u8], B>;
163}
164
165impl<D, B: Backend> VecZnxDftToMut<B> for VecZnxDft<D, B>
166where
167    D: AsRef<[u8]> + AsMut<[u8]>,
168    B: Backend,
169{
170    fn to_mut(&mut self) -> VecZnxDft<&mut [u8], B> {
171        VecZnxDft {
172            data: self.data.as_mut(),
173            n: self.n,
174            cols: self.cols,
175            size: self.size,
176            _phantom: std::marker::PhantomData,
177        }
178    }
179}
180
181impl<D: AsRef<[u8]>> fmt::Display for VecZnxDft<D, FFT64> {
182    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
183        writeln!(
184            f,
185            "VecZnxDft(n={}, cols={}, size={})",
186            self.n, self.cols, self.size
187        )?;
188
189        for col in 0..self.cols {
190            writeln!(f, "Column {}:", col)?;
191            for size in 0..self.size {
192                let coeffs = self.at(col, size);
193                write!(f, "  Size {}: [", size)?;
194
195                let max_show = 100;
196                let show_count = coeffs.len().min(max_show);
197
198                for (i, &coeff) in coeffs.iter().take(show_count).enumerate() {
199                    if i > 0 {
200                        write!(f, ", ")?;
201                    }
202                    write!(f, "{}", coeff)?;
203                }
204
205                if coeffs.len() > max_show {
206                    write!(f, ", ... ({} more)", coeffs.len() - max_show)?;
207                }
208
209                writeln!(f, "]")?;
210            }
211        }
212        Ok(())
213    }
214}