1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! Memory management types and helper functions for the Openqudit library.
use size_of;
use AVec;
use CACHELINE_ALIGN;
use avec;
use Zeroable;
/// A trait for types that can be stored in memory buffers.
/// An aligned memory buffer.
///
/// The memory buffer is used to store the data for matrices, vectors,
/// and other data structures. This type aliases a group of aligned
/// vectors of the unit type of a given faer entity. For faer's native
/// complex numbers this simplfies to a single vector of complex numbers.
/// For num_complex complex numbers this because two vectors, one for the
/// real part and one for the imaginary part.
///
/// # Type Parameters
///
/// * `C`: The faer entity type for the memory buffer.
///
/// # See Also
///
/// - [alloc_zeroed_memory] function to allocate a new memory buffer.
/// - [Memorable] trait for more information on trait bounds.
pub type MemoryBuffer<C: Memorable> = ;
/// A pointer to an element in a memory buffer.
///
/// The pointer is used to access elements in a memory buffer. This type
/// aliases a group of pointers to the unit type of a given faer entity.
/// For faer's native complex numbers this simplfies to a single pointer.
/// For num_complex complex numbers this becomes two pointers, one for the
/// real part and one for the imaginary part.
///
/// # Type Parameters
///
/// * `C`: The faer entity type for the memory buffer.
///
/// # See Also
///
/// - [MemoryBuffer] type alias for more information on memory buffers.
/// - [Memorable] trait for more information on trait bounds.
/// - [MemoryPointerMut] type alias for more information on mutable pointers.
/// - [MemoryPointerNonNull] type alias for more information on non-null pointers.
pub type MemoryPointer<C: Memorable> = *const C;
/// A mutable pointer to an element in a memory buffer.
///
/// The mutable pointer is used to access or modify elements in a memory
/// buffer.
///
/// # Type Parameters
///
/// * `C`: The faer entity type for the memory buffer.
///
/// # See Also
///
/// - [MemoryBuffer] type alias for more information on memory buffers.
/// - [Memorable] trait for more information on trait bounds.
/// - [MemoryPointer] type alias for more information on const pointers.
/// - [MemoryPointerNonNull] type alias for more information on non-null pointers.
pub type MemoryPointerMut<C> = *mut C;
/// A const non-null pointer to an element in a memory buffer.
///
/// # Type Parameters
///
/// * `C`: The faer entity type for the memory buffer.
///
/// # See Also
///
/// - [MemoryBuffer] type alias for more information on memory buffers.
/// - [Memorable] trait for more information on trait bounds.
/// - [MemoryPointer] type alias for more information on const pointers.
/// - [MemoryPointerMut] type alias for more information on mutable pointers.
pub type MemoryPointerNonNull<C> = NonNull;
/// Allocate a new memory buffer with the given size.
///
/// The size is the number of elements to allocate in the memory buffer,
/// not the number of bytes. The memory buffer is aligned to cachelines,
/// and the elements will be zeroed.
///
/// # Type Parameters
///
/// * `C`: The faer entity type for the memory buffer.
///
/// # Arguments
///
/// * `size` - The number of elements to allocate in the memory buffer.
///
/// # Returns
///
/// A new memory buffer with the given size.
///
/// # Panics
///
/// This function will panic if the memory requirement overflows isize.
///
/// # Example
///
/// ```
/// use qudit_core::memory::alloc_zeroed_memory;
/// let size = 10;
/// let mem = alloc_zeroed_memory::<f32>(size);
/// assert_eq!(mem.len(), size);
/// ```
///
/// # See Also
///
/// - [MemoryBuffer] type alias for more information on memory buffers.
/// Calculate column stride for a matrix with given rows and columns.
///
/// The column stride is the number of elements between the start of each column
/// in the matrix. Faer, the library qudit-core builds on, uses column-major
/// storage for matrices, so the column stride is the major stride for matrices.
/// Extra padding is added to columns to ensure that columns are aligned to
/// cachelines.
///
/// # Type Parameters
///
/// * `C`: The faer entity type for the matrix.
///
/// # Arguments
///
/// * `nrows` - The number of rows in the matrix.
///
/// * `ncols` - The number of columns in the matrix.
///
/// # Returns
///
/// The column stride for the matrix.
///
/// # Panics
///
/// This function will panic if any of the arithmetic operations overflow.
/// This should only happen for extremely large matrices.
///
/// # Example
///
/// ```
/// use qudit_core::memory::calc_col_stride;
/// let nrows = 3;
/// let ncols = 4;
/// let col_stride = calc_col_stride::<f32>(nrows, ncols);
/// assert_eq!(col_stride, 3);
///
/// let nrows = 14;
/// let ncols = 40;
/// let col_stride = calc_col_stride::<f32>(nrows, ncols);
/// assert_eq!(col_stride, 16);
/// ```
///
/// # See Also
///
/// - [calc_mat_stride] function to calculate the matrix stride.
///
/// # Notes
///
/// This function assumes that the first element starts at the beginning of
/// a cacheline but does not guarantee that matrices will end at a cacheline
/// boundary. If multiple matrices are stored in a single memory buffer, the
/// memory buffer should be aligned to cachelines, and you should calculate
/// a separate stride for the matrix dimension. See [calc_mat_stride] for
/// more information.
///
/// Additionally, this function returns a usize, while pointer arithmetic
/// is performed with isize types. Extra care should be taken to avoid
/// overflow when using the result of this function in pointer arithmetic.
///
/// This function always assumes the row stride is 1. If the row stride is
/// not 1, the column stride will be incorrect.
/// Calculate matrix stride for a tensor with given rows, columns, and column stride.
///
/// The matrix stride is the number of elements between the start of each matrix
/// in a tensor. Extra padding is added to matrices to ensure that matrices are
/// aligned to cachelines.
///
/// # Type Parameters
///
/// * `C`: The faer entity type for the matrix.
///
/// # Arguments
///
/// * `nrows` - The number of rows in the matrix.
///
/// * `ncols` - The number of columns in the matrix.
///
/// * `col_stride` - The column stride for the matrix.
///
/// # Returns
///
/// The matrix stride for the tensor.
///
/// # Example
///
/// ```
/// use qudit_core::memory::calc_mat_stride;
/// let nrows = 3;
/// let ncols = 4;
/// let col_stride = 4;
/// let mat_stride = calc_mat_stride::<f64>(nrows, ncols, col_stride);
/// assert_eq!(mat_stride, 16);
/// ```
///
/// # See Also
///
/// - [calc_col_stride] function to calculate the column stride.
/// - [MemoryBuffer] type alias for more information on memory buffers.
/// - [crate::array::Tensor] type for more information on three dimension tensors.
///
/// # Notes
///
/// This function assumes that the first element starts at the beginning
/// of a cacheline and will guarantee that matrices will end at a
/// cacheline boundary.
///
/// Additionally, this function returns a usize, while pointer arithmetic
/// is performed with isize types. Extra care should be taken to avoid
/// overflow when using the result of this function in pointer arithmetic.
///
/// This function always assumes the row stride is 1. If the row stride is
/// not 1, the column stride will be incorrect.
/// Calculates a subtensor's stride, given its size. We ensure the subtensor
/// is aligned to cachelines.
///
/// # Arguments
///
/// * `packed_subtensor_size` - The number of elements in the packed subtensor.
///
/// # Returns
///
/// * The subtensor stride, aligned to cachelines.
///
/// # Example
/// ```
/// use qudit_core::memory::calc_next_stride;
/// use qudit_core::c64;
///
/// let stride = calc_next_stride::<c64>(5);
/// let expected = 8;
/// assert_eq!(stride, expected);
/// ```