poulpy/backend/src/mat_znx_dft.rs
backend/src/mat_znx_dft.rsRaw
1use crate::znx_base::ZnxInfos;
2use crate::{Backend, DataView, DataViewMut, FFT64, Module, ZnxSliceSize, ZnxView, alloc_aligned};
3use std::marker::PhantomData;
4
5/// Vector Matrix Product Prepared Matrix: a vector of [VecZnx],
6/// stored as a 3D matrix in the DFT domain in a single contiguous array.
7/// Each col of the [MatZnxDft] can be seen as a collection of [VecZnxDft].
8///
9/// [MatZnxDft] is used to permform a vector matrix product between a [VecZnx]/[VecZnxDft] and a [MatZnxDft].
10/// See the trait [MatZnxDftOps] for additional information.
11pub struct MatZnxDft<D, B: Backend> {
12 data: D,
13 n: usize,
14 size: usize,
15 rows: usize,
16 cols_in: usize,
17 cols_out: usize,
18 _phantom: PhantomData<B>,
19}
20
21impl<D, B: Backend> ZnxInfos for MatZnxDft<D, B> {
22 fn cols(&self) -> usize {
23 self.cols_in
24 }
25
26 fn rows(&self) -> usize {
27 self.rows
28 }
29
30 fn n(&self) -> usize {
31 self.n
32 }
33
34 fn size(&self) -> usize {
35 self.size
36 }
37}
38
39impl<D> ZnxSliceSize for MatZnxDft<D, FFT64> {
40 fn sl(&self) -> usize {
41 self.n() * self.cols_out()
42 }
43}
44
45impl<D, B: Backend> DataView for MatZnxDft<D, B> {
46 type D = D;
47 fn data(&self) -> &Self::D {
48 &self.data
49 }
50}
51
52impl<D, B: Backend> DataViewMut for MatZnxDft<D, B> {
53 fn data_mut(&mut self) -> &mut Self::D {
54 &mut self.data
55 }
56}
57
58impl<D: AsRef<[u8]>> ZnxView for MatZnxDft<D, FFT64> {
59 type Scalar = f64;
60}
61
62impl<D, B: Backend> MatZnxDft<D, B> {
63 pub fn cols_in(&self) -> usize {
64 self.cols_in
65 }
66
67 pub fn cols_out(&self) -> usize {
68 self.cols_out
69 }
70}
71
72impl<D: From<Vec<u8>>, B: Backend> MatZnxDft<D, B> {
73 pub(crate) fn bytes_of(module: &Module<B>, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize {
74 unsafe {
75 crate::ffi::vmp::bytes_of_vmp_pmat(
76 module.ptr,
77 (rows * cols_in) as u64,
78 (size * cols_out) as u64,
79 ) as usize
80 }
81 }
82
83 pub(crate) fn new(module: &Module<B>, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> Self {
84 let data: Vec<u8> = alloc_aligned(Self::bytes_of(module, rows, cols_in, cols_out, size));
85 Self {
86 data: data.into(),
87 n: module.n(),
88 size,
89 rows,
90 cols_in,
91 cols_out,
92 _phantom: PhantomData,
93 }
94 }
95
96 pub(crate) fn new_from_bytes(
97 module: &Module<B>,
98 rows: usize,
99 cols_in: usize,
100 cols_out: usize,
101 size: usize,
102 bytes: impl Into<Vec<u8>>,
103 ) -> Self {
104 let data: Vec<u8> = bytes.into();
105 assert!(data.len() == Self::bytes_of(module, rows, cols_in, cols_out, size));
106 Self {
107 data: data.into(),
108 n: module.n(),
109 size,
110 rows,
111 cols_in,
112 cols_out,
113 _phantom: PhantomData,
114 }
115 }
116}
117
118impl<D: AsRef<[u8]>> MatZnxDft<D, FFT64> {
119 /// Returns a copy of the backend array at index (i, j) of the [MatZnxDft].
120 ///
121 /// # Arguments
122 ///
123 /// * `row`: row index (i).
124 /// * `col`: col index (j).
125 #[allow(dead_code)]
126 fn at(&self, row: usize, col: usize) -> Vec<f64> {
127 let n: usize = self.n();
128
129 let mut res: Vec<f64> = alloc_aligned(n);
130
131 if n < 8 {
132 res.copy_from_slice(&self.raw()[(row + col * self.rows()) * n..(row + col * self.rows()) * (n + 1)]);
133 } else {
134 (0..n >> 3).for_each(|blk| {
135 res[blk * 8..(blk + 1) * 8].copy_from_slice(&self.at_block(row, col, blk)[..8]);
136 });
137 }
138
139 res
140 }
141
142 #[allow(dead_code)]
143 fn at_block(&self, row: usize, col: usize, blk: usize) -> &[f64] {
144 let nrows: usize = self.rows();
145 let nsize: usize = self.size();
146 if col == (nsize - 1) && (nsize & 1 == 1) {
147 &self.raw()[blk * nrows * nsize * 8 + col * nrows * 8 + row * 8..]
148 } else {
149 &self.raw()[blk * nrows * nsize * 8 + (col / 2) * (2 * nrows) * 8 + row * 2 * 8 + (col % 2) * 8..]
150 }
151 }
152}
153
154pub type MatZnxDftOwned<B> = MatZnxDft<Vec<u8>, B>;
155pub type MatZnxDftMut<'a, B> = MatZnxDft<&'a mut [u8], B>;
156pub type MatZnxDftRef<'a, B> = MatZnxDft<&'a [u8], B>;
157
158pub trait MatZnxToRef<B: Backend> {
159 fn to_ref(&self) -> MatZnxDft<&[u8], B>;
160}
161
162impl<D, B: Backend> MatZnxToRef<B> for MatZnxDft<D, B>
163where
164 D: AsRef<[u8]>,
165 B: Backend,
166{
167 fn to_ref(&self) -> MatZnxDft<&[u8], B> {
168 MatZnxDft {
169 data: self.data.as_ref(),
170 n: self.n,
171 rows: self.rows,
172 cols_in: self.cols_in,
173 cols_out: self.cols_out,
174 size: self.size,
175 _phantom: std::marker::PhantomData,
176 }
177 }
178}
179
180pub trait MatZnxToMut<B: Backend> {
181 fn to_mut(&mut self) -> MatZnxDft<&mut [u8], B>;
182}
183
184impl<D, B: Backend> MatZnxToMut<B> for MatZnxDft<D, B>
185where
186 D: AsRef<[u8]> + AsMut<[u8]>,
187 B: Backend,
188{
189 fn to_mut(&mut self) -> MatZnxDft<&mut [u8], B> {
190 MatZnxDft {
191 data: self.data.as_mut(),
192 n: self.n,
193 rows: self.rows,
194 cols_in: self.cols_in,
195 cols_out: self.cols_out,
196 size: self.size,
197 _phantom: std::marker::PhantomData,
198 }
199 }
200}
201
202impl<D, B: Backend> MatZnxDft<D, B> {
203 pub(crate) fn from_data(data: D, n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> Self {
204 Self {
205 data,
206 n,
207 rows,
208 cols_in,
209 cols_out,
210 size,
211 _phantom: PhantomData,
212 }
213 }
214}