numrs2 0.3.3

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
Documentation
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//! Aligned memory allocator for SIMD and cache-efficient operations
//!
//! This module provides allocators that ensure memory is aligned to specific
//! boundaries for optimal performance with SIMD and cache operations.

use std::alloc::{alloc, alloc_zeroed, dealloc, Layout};
use std::mem;
use std::ptr::NonNull;

/// Alignment configuration for memory allocations
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AlignmentConfig {
    /// Alignment in bytes (must be a power of 2)
    pub alignment: usize,
    /// Whether to zero-initialize allocated memory
    pub zero_init: bool,
}

impl Default for AlignmentConfig {
    fn default() -> Self {
        Self {
            alignment: 64, // Default to cache line size on most CPUs
            zero_init: false,
        }
    }
}

/// Common alignment values
impl AlignmentConfig {
    /// Create a new alignment configuration
    pub fn new(alignment: usize, zero_init: bool) -> Self {
        assert!(
            alignment.is_power_of_two(),
            "Alignment must be a power of 2"
        );
        Self {
            alignment,
            zero_init,
        }
    }

    /// Default cache line alignment (64 bytes)
    pub fn cache_line() -> Self {
        Self {
            alignment: 64,
            zero_init: false,
        }
    }

    /// SIMD-friendly alignment (16 bytes for 128-bit SIMD)
    pub fn simd_128() -> Self {
        Self {
            alignment: 16,
            zero_init: false,
        }
    }

    /// SIMD-friendly alignment (32 bytes for 256-bit SIMD)
    pub fn simd_256() -> Self {
        Self {
            alignment: 32,
            zero_init: false,
        }
    }

    /// SIMD-friendly alignment (64 bytes for 512-bit SIMD)
    pub fn simd_512() -> Self {
        Self {
            alignment: 64,
            zero_init: false,
        }
    }

    /// Page-aligned memory (4KB on most systems)
    pub fn page() -> Self {
        Self {
            alignment: 4096,
            zero_init: false,
        }
    }

    /// Zero-initialized variant
    pub fn zeroed(mut self) -> Self {
        self.zero_init = true;
        self
    }
}

/// An allocator for aligned memory
#[derive(Debug)]
pub struct AlignedAllocator {
    config: AlignmentConfig,
}

impl AlignedAllocator {
    /// Create a new aligned allocator with the given configuration
    pub fn new(config: AlignmentConfig) -> Self {
        Self { config }
    }

    /// Allocate memory with the configured alignment
    pub fn allocate(&self, size: usize) -> Option<NonNull<u8>> {
        if size == 0 {
            return None;
        }

        let layout = Layout::from_size_align(size, self.config.alignment).ok()?;

        unsafe {
            let ptr = if self.config.zero_init {
                alloc_zeroed(layout)
            } else {
                alloc(layout)
            };

            NonNull::new(ptr)
        }
    }

    /// Allocate memory for the given type with appropriate alignment
    pub fn allocate_for_type<T>(&self) -> Option<NonNull<T>> {
        let size = mem::size_of::<T>();
        let align = mem::align_of::<T>().max(self.config.alignment);

        if size == 0 {
            return None;
        }

        let layout = Layout::from_size_align(size, align).ok()?;

        unsafe {
            let ptr = if self.config.zero_init {
                alloc_zeroed(layout)
            } else {
                alloc(layout)
            };

            NonNull::new(ptr as *mut T)
        }
    }

    /// Allocate an array of elements with the configured alignment
    pub fn allocate_array<T>(&self, count: usize) -> Option<NonNull<T>> {
        let size = mem::size_of::<T>().checked_mul(count)?;
        let align = mem::align_of::<T>().max(self.config.alignment);

        if size == 0 {
            return None;
        }

        let layout = Layout::from_size_align(size, align).ok()?;

        unsafe {
            let ptr = if self.config.zero_init {
                alloc_zeroed(layout)
            } else {
                alloc(layout)
            };

            NonNull::new(ptr as *mut T)
        }
    }

    /// Deallocate memory that was allocated with this allocator
    ///
    /// # Safety
    ///
    /// - The pointer must have been allocated by this allocator
    /// - The size and alignment must match the original allocation
    pub unsafe fn deallocate(&self, ptr: NonNull<u8>, size: usize) {
        let layout = Layout::from_size_align_unchecked(size, self.config.alignment);
        dealloc(ptr.as_ptr(), layout);
    }

    /// Deallocate an array that was allocated with this allocator
    ///
    /// # Safety
    ///
    /// - The pointer must have been allocated by this allocator
    /// - The type and count must match the original allocation
    pub unsafe fn deallocate_array<T>(&self, ptr: NonNull<T>, count: usize) {
        let size = mem::size_of::<T>() * count;
        let align = mem::align_of::<T>().max(self.config.alignment);
        let layout = Layout::from_size_align_unchecked(size, align);
        dealloc(ptr.as_ptr() as *mut u8, layout);
    }

    /// Get the alignment used by this allocator
    pub fn alignment(&self) -> usize {
        self.config.alignment
    }

    /// Check if allocations are zero-initialized
    pub fn is_zero_initialized(&self) -> bool {
        self.config.zero_init
    }

    /// Create a new aligned array and initialize it with the given values
    pub fn create_array<T: Copy>(&self, values: &[T]) -> Option<NonNull<T>> {
        let ptr = self.allocate_array::<T>(values.len())?;

        unsafe {
            std::ptr::copy_nonoverlapping(values.as_ptr(), ptr.as_ptr(), values.len());
        }

        Some(ptr)
    }

    /// Create a reference to the aligned array
    ///
    /// # Safety
    ///
    /// - The pointer must have been allocated by this allocator
    /// - The count must match the original allocation
    /// - The memory must be properly initialized
    pub unsafe fn as_slice<T>(&self, ptr: NonNull<T>, count: usize) -> &[T] {
        std::slice::from_raw_parts(ptr.as_ptr(), count)
    }

    /// Create a mutable reference to the aligned array
    ///
    /// # Safety
    ///
    /// - The pointer must have been allocated by this allocator
    /// - The count must match the original allocation
    /// - The memory must be properly initialized
    pub unsafe fn as_mut_slice<T>(&mut self, ptr: NonNull<T>, count: usize) -> &mut [T] {
        std::slice::from_raw_parts_mut(ptr.as_ptr(), count)
    }
}

/// Safe wrapper for aligned memory allocation
pub struct AlignedBox<T> {
    ptr: NonNull<T>,
    allocator: AlignedAllocator,
}

impl<T> AlignedBox<T> {
    /// Create a new aligned box with the given value
    pub fn new(value: T, alignment: usize) -> Option<Self> {
        let config = AlignmentConfig::new(alignment, false);
        let allocator = AlignedAllocator::new(config);

        let ptr = allocator.allocate_for_type::<T>()?;

        unsafe {
            std::ptr::write(ptr.as_ptr(), value);
        }

        Some(Self { ptr, allocator })
    }

    /// Get a reference to the contained value
    pub fn get(&self) -> &T {
        unsafe { self.ptr.as_ref() }
    }

    /// Get a mutable reference to the contained value
    pub fn get_mut(&mut self) -> &mut T {
        unsafe { self.ptr.as_mut() }
    }

    /// Convert into the contained value
    pub fn into_inner(self) -> T {
        // Read the value
        let value = unsafe { std::ptr::read(self.ptr.as_ptr()) };

        // Prevent the destructor from running
        std::mem::forget(self);

        value
    }

    /// Get the alignment of this allocation
    pub fn alignment(&self) -> usize {
        self.allocator.alignment()
    }
}

impl<T> Drop for AlignedBox<T> {
    fn drop(&mut self) {
        unsafe {
            // Drop the contained value
            std::ptr::drop_in_place(self.ptr.as_ptr());

            // Deallocate the memory
            self.allocator.deallocate(
                NonNull::new_unchecked(self.ptr.as_ptr() as *mut u8),
                mem::size_of::<T>(),
            );
        }
    }
}

impl<T: Clone> Clone for AlignedBox<T> {
    fn clone(&self) -> Self {
        let value = self.get().clone();
        Self::new(value, self.allocator.alignment())
            .expect("aligned allocation for clone should succeed")
    }
}

/// Safe wrapper for aligned arrays
pub struct AlignedVec<T> {
    ptr: NonNull<T>,
    len: usize,
    allocator: AlignedAllocator,
}

impl<T> AlignedVec<T> {
    /// Create a new aligned vector with the given capacity
    pub fn with_capacity(capacity: usize, alignment: usize) -> Option<Self> {
        if capacity == 0 {
            return None;
        }

        let config = AlignmentConfig::new(alignment, false);
        let allocator = AlignedAllocator::new(config);

        let ptr = allocator.allocate_array::<T>(capacity)?;

        Some(Self {
            ptr,
            len: 0,
            allocator,
        })
    }

    /// Create a new zero-initialized aligned vector with the given capacity
    pub fn with_capacity_zeroed(capacity: usize, alignment: usize) -> Option<Self>
    where
        T: Copy + Default,
    {
        if capacity == 0 {
            return None;
        }

        let config = AlignmentConfig::new(alignment, true);
        let allocator = AlignedAllocator::new(config);

        let ptr = allocator.allocate_array::<T>(capacity)?;

        Some(Self {
            ptr,
            len: capacity,
            allocator,
        })
    }

    /// Push a value to the end of the vector
    ///
    /// Returns false if there's no more capacity
    pub fn push(&mut self, value: T) -> bool {
        if self.len >= self.capacity() {
            return false;
        }

        unsafe {
            std::ptr::write(self.ptr.as_ptr().add(self.len), value);
        }

        self.len += 1;
        true
    }

    /// Get the length of the vector
    pub fn len(&self) -> usize {
        self.len
    }

    /// Check if the vector is empty
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Get the current capacity of the vector
    pub fn capacity(&self) -> usize {
        // For simplicity, we just use length as capacity
        // A real implementation would track capacity separately
        self.len
    }

    /// Get a reference to the vector as a slice
    pub fn as_slice(&self) -> &[T] {
        unsafe { std::slice::from_raw_parts(self.ptr.as_ptr(), self.len) }
    }

    /// Get a mutable reference to the vector as a slice
    pub fn as_mut_slice(&mut self) -> &mut [T] {
        unsafe { std::slice::from_raw_parts_mut(self.ptr.as_ptr(), self.len) }
    }

    /// Get the alignment of this vector
    pub fn alignment(&self) -> usize {
        self.allocator.alignment()
    }
}

impl<T> Drop for AlignedVec<T> {
    fn drop(&mut self) {
        unsafe {
            // Drop all elements
            for i in 0..self.len {
                std::ptr::drop_in_place(self.ptr.as_ptr().add(i));
            }

            // Deallocate the memory
            self.allocator.deallocate_array(self.ptr, self.len);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_aligned_allocator_basic() {
        // Test with 16-byte alignment
        let config = AlignmentConfig::new(16, false);
        let allocator = AlignedAllocator::new(config);

        // Allocate some memory
        let ptr = allocator.allocate(100).expect("Allocation should succeed");

        // Check alignment
        assert_eq!(
            ptr.as_ptr() as usize % 16,
            0,
            "Pointer should be 16-byte aligned"
        );

        // Deallocate
        unsafe {
            allocator.deallocate(ptr, 100);
        }

        // Test with larger alignment
        let config = AlignmentConfig::new(4096, false);
        let allocator = AlignedAllocator::new(config);

        let ptr = allocator.allocate(100).expect("Allocation should succeed");
        assert_eq!(
            ptr.as_ptr() as usize % 4096,
            0,
            "Pointer should be 4096-byte aligned"
        );

        unsafe {
            allocator.deallocate(ptr, 100);
        }
    }

    #[test]
    fn test_aligned_allocator_zero_init() {
        // Create a zero-initialized allocator
        let config = AlignmentConfig::new(64, true);
        let allocator = AlignedAllocator::new(config);

        // Allocate memory for an array of 10 integers
        let ptr = allocator
            .allocate_array::<i32>(10)
            .expect("Allocation should succeed");

        // Check that it's properly zero-initialized
        unsafe {
            let slice = std::slice::from_raw_parts(ptr.as_ptr(), 10);
            for &value in slice {
                assert_eq!(value, 0, "Value should be zero-initialized");
            }

            // Deallocate
            allocator.deallocate_array(ptr, 10);
        }
    }

    #[test]
    fn test_aligned_box() {
        // Create an aligned box with a value
        let mut aligned_box = AlignedBox::new(42i32, 16).expect("Allocation should succeed");

        // Check alignment
        let ptr_addr = aligned_box.get() as *const i32 as usize;
        assert_eq!(ptr_addr % 16, 0, "AlignedBox should be 16-byte aligned");

        // Access the value
        assert_eq!(*aligned_box.get(), 42);

        // Modify the value
        *aligned_box.get_mut() = 84;
        assert_eq!(*aligned_box.get(), 84);

        // Extract the value
        let value = aligned_box.into_inner();
        assert_eq!(value, 84);
    }

    #[test]
    fn test_aligned_vec() {
        // Create an aligned vector with capacity
        let mut vec =
            AlignedVec::<i32>::with_capacity_zeroed(5, 64).expect("Allocation should succeed");

        // Check initial state
        assert_eq!(vec.len(), 5);
        assert!(!vec.is_empty());

        // Initialize values
        for i in 0..5 {
            vec.as_mut_slice()[i] = i as i32;
        }

        // Check state after initialization
        assert_eq!(vec.len(), 5);
        assert!(!vec.is_empty());

        // Check contents
        let slice = vec.as_slice();
        assert_eq!(slice, &[0, 1, 2, 3, 4]);

        // Modify contents
        let mut_slice = vec.as_mut_slice();
        for item in mut_slice.iter_mut() {
            *item *= 2;
        }

        // Check modified contents
        assert_eq!(vec.as_slice(), &[0, 2, 4, 6, 8]);
    }

    #[test]
    fn test_aligned_vec_zeroed() {
        // Create a zero-initialized vector
        let vec =
            AlignedVec::<i32>::with_capacity_zeroed(10, 32).expect("Allocation should succeed");

        // Check that it's properly zero-initialized
        assert_eq!(vec.len(), 10);
        for &value in vec.as_slice() {
            assert_eq!(value, 0, "Value should be zero-initialized");
        }

        // Check alignment
        assert_eq!(vec.alignment(), 32);
    }

    #[test]
    fn test_alignment_configs() {
        let cache_config = AlignmentConfig::cache_line();
        assert_eq!(cache_config.alignment, 64);
        assert!(!cache_config.zero_init);

        let simd_config = AlignmentConfig::simd_256();
        assert_eq!(simd_config.alignment, 32);
        assert!(!simd_config.zero_init);

        let page_config = AlignmentConfig::page();
        assert_eq!(page_config.alignment, 4096);
        assert!(!page_config.zero_init);

        let zeroed_config = AlignmentConfig::simd_128().zeroed();
        assert_eq!(zeroed_config.alignment, 16);
        assert!(zeroed_config.zero_init);
    }
}