poulpy/base2k/src/vec_znx_dft.rs
base2k/src/vec_znx_dft.rsRaw
1use std::marker::PhantomData;
2
3use crate::ffi::vec_znx_dft;
4use crate::znx_base::{ZnxAlloc, ZnxInfos};
5use crate::{Backend, DataView, DataViewMut, FFT64, Module, ZnxView, alloc_aligned};
6
7const VEC_ZNX_DFT_ROWS: usize = 1;
8
9pub struct VecZnxDft<D, B> {
10 data: D,
11 n: usize,
12 cols: usize,
13 size: usize,
14 _phantom: PhantomData<B>,
15}
16
17impl<D, B> ZnxInfos for VecZnxDft<D, B> {
18 fn cols(&self) -> usize {
19 self.cols
20 }
21
22 fn rows(&self) -> usize {
23 1
24 }
25
26 fn n(&self) -> usize {
27 self.n
28 }
29
30 fn size(&self) -> usize {
31 self.size
32 }
33
34 fn sl(&self) -> usize {
35 self.cols() * self.n()
36 }
37}
38
39impl<D, B> DataView for VecZnxDft<D, B> {
40 type D = D;
41 fn data(&self) -> &Self::D {
42 &self.data
43 }
44}
45
46impl<D, B> DataViewMut for VecZnxDft<D, B> {
47 fn data_mut(&self) -> &mut Self::D {
48 &mut self.data
49 }
50}
51
52impl<D: AsRef<[u8]>> ZnxView for VecZnxDft<D, FFT64> {
53 type Scalar = f64;
54}
55
56impl<D: From<Vec<u8>>, B: Backend> VecZnxDft<D, B> {
57 pub(crate) fn bytes_of(module: &Module<B>, cols: usize, size: usize) -> usize {
58 unsafe { vec_znx_dft::bytes_of_vec_znx_dft(module.ptr, size as u64) as usize * cols }
59 }
60
61 pub(crate) fn new(module: &Module<B>, cols: usize, size: usize) -> Self {
62 let data = alloc_aligned::<u8>(Self::bytes_of(module, cols, size));
63 Self {
64 data: data.into(),
65 n: module.n(),
66 cols,
67 size,
68 _phantom: PhantomData,
69 }
70 }
71
72 pub(crate) fn new_from_bytes(module: &Module<B>, cols: usize, size: usize, bytes: impl Into<Vec<u8>>) -> Self {
73 let data: Vec<u8> = bytes.into();
74 assert!(data.len() == Self::bytes_of(module, cols, size));
75 Self {
76 data: data.into(),
77 n: module.n(),
78 cols,
79 size,
80 _phantom: PhantomData,
81 }
82 }
83}
84
85pub type VecZnxDftOwned<B> = VecZnxDft<Vec<u8>, B>;
86
87// impl<B: Backend> ZnxAlloc<B> for VecZnxDft<B> {
88// type Scalar = u8;
89
90// fn from_bytes_borrow(module: &Module<B>, _rows: usize, cols: usize, size: usize, bytes: &mut [u8]) -> Self {
91// debug_assert_eq!(bytes.len(), Self::bytes_of(module, _rows, cols, size));
92// Self {
93// inner: ZnxBase::from_bytes_borrow(module.n(), VEC_ZNX_DFT_ROWS, cols, size, bytes),
94// _marker: PhantomData,
95// }
96// }
97
98// fn bytes_of(module: &Module<B>, _rows: usize, cols: usize, size: usize) -> usize {
99// debug_assert_eq!(
100// _rows, VEC_ZNX_DFT_ROWS,
101// "rows != {} not supported for VecZnxDft",
102// VEC_ZNX_DFT_ROWS
103// );
104// unsafe { vec_znx_dft::bytes_of_vec_znx_dft(module.ptr, size as u64) as usize * cols }
105// }
106// }
107
108// impl VecZnxDft<FFT64> {
109// pub fn print(&self, n: usize, col: usize) {
110// (0..self.size()).for_each(|i| println!("{}: {:?}", i, &self.at(col, i)[..n]));
111// }
112// }
113
114// impl<B: Backend> VecZnxDft<B> {
115// /// Cast a [VecZnxDft] into a [VecZnxBig].
116// /// The returned [VecZnxBig] shares the backing array
117// /// with the original [VecZnxDft].
118// pub fn alias_as_vec_znx_big(&mut self) -> VecZnxBig<B> {
119// assert!(
120// self.data().len() == 0,
121// "cannot alias VecZnxDft into VecZnxBig if it owns the data"
122// );
123// VecZnxBig::<B> {
124// inner: ZnxBase {
125// data: Vec::new(),
126// ptr: self.ptr(),
127// n: self.n(),
128// rows: self.rows(),
129// cols: self.cols(),
130// size: self.size(),
131// },
132// _marker: PhantomData,
133// }
134// }
135// }