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, 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
 65impl<D: AsMut<[u8]> + AsRef<[u8]>> VecZnxDft<D, FFT64>{
 66    pub fn set_size(&mut self, size: usize){
 67        assert!(size <= self.data.as_ref().len() / (self.n * self.cols()));
 68        self.size = size
 69    }
 70}
 71
 72pub(crate) fn bytes_of_vec_znx_dft<B: Backend>(module: &Module<B>, cols: usize, size: usize) -> usize {
 73    unsafe { vec_znx_dft::bytes_of_vec_znx_dft(module.ptr, size as u64) as usize * cols }
 74}
 75
 76impl<D: From<Vec<u8>>, B: Backend> VecZnxDft<D, B> {
 77    pub(crate) fn new(module: &Module<B>, cols: usize, size: usize) -> Self {
 78        let data = alloc_aligned::<u8>(bytes_of_vec_znx_dft(module, cols, size));
 79        Self {
 80            data: data.into(),
 81            n: module.n(),
 82            cols,
 83            size,
 84            _phantom: PhantomData,
 85        }
 86    }
 87
 88    pub(crate) fn new_from_bytes(module: &Module<B>, cols: usize, size: usize, bytes: impl Into<Vec<u8>>) -> Self {
 89        let data: Vec<u8> = bytes.into();
 90        assert!(data.len() == bytes_of_vec_znx_dft(module, cols, size));
 91        Self {
 92            data: data.into(),
 93            n: module.n(),
 94            cols,
 95            size,
 96            _phantom: PhantomData,
 97        }
 98    }
 99}
100
101pub type VecZnxDftOwned<B> = VecZnxDft<Vec<u8>, B>;
102
103impl<D, B: Backend> VecZnxDft<D, B> {
104    pub(crate) fn from_data(data: D, n: usize, cols: usize, size: usize) -> Self {
105        Self {
106            data,
107            n,
108            cols,
109            size,
110            _phantom: PhantomData,
111        }
112    }
113}
114
115pub trait VecZnxDftToRef<B: Backend> {
116    fn to_ref(&self) -> VecZnxDft<&[u8], B>;
117}
118
119impl<D, B: Backend> VecZnxDftToRef<B> for VecZnxDft<D, B>
120where
121    D: AsRef<[u8]>,
122    B: Backend,
123{
124    fn to_ref(&self) -> VecZnxDft<&[u8], B> {
125        VecZnxDft {
126            data: self.data.as_ref(),
127            n: self.n,
128            cols: self.cols,
129            size: self.size,
130            _phantom: std::marker::PhantomData,
131        }
132    }
133}
134
135pub trait VecZnxDftToMut<B: Backend> {
136    fn to_mut(&mut self) -> VecZnxDft<&mut [u8], B>;
137}
138
139impl<D, B: Backend> VecZnxDftToMut<B> for VecZnxDft<D, B>
140where
141    D: AsRef<[u8]> + AsMut<[u8]>,
142    B: Backend,
143{
144    fn to_mut(&mut self) -> VecZnxDft<&mut [u8], B> {
145        VecZnxDft {
146            data: self.data.as_mut(),
147            n: self.n,
148            cols: self.cols,
149            size: self.size,
150            _phantom: std::marker::PhantomData,
151        }
152    }
153}
154
155impl<D: AsRef<[u8]>> fmt::Display for VecZnxDft<D, FFT64> {
156    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157        writeln!(
158            f,
159            "VecZnxDft(n={}, cols={}, size={})",
160            self.n, self.cols, self.size
161        )?;
162
163        for col in 0..self.cols {
164            writeln!(f, "Column {}:", col)?;
165            for size in 0..self.size {
166                let coeffs = self.at(col, size);
167                write!(f, "  Size {}: [", size)?;
168
169                let max_show = 100;
170                let show_count = coeffs.len().min(max_show);
171
172                for (i, &coeff) in coeffs.iter().take(show_count).enumerate() {
173                    if i > 0 {
174                        write!(f, ", ")?;
175                    }
176                    write!(f, "{}", coeff)?;
177                }
178
179                if coeffs.len() > max_show {
180                    write!(f, ", ... ({} more)", coeffs.len() - max_show)?;
181                }
182
183                writeln!(f, "]")?;
184            }
185        }
186        Ok(())
187    }
188}