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
//! Enhanced trait implementations for memory allocators
//!
//! This module provides implementations of the new memory management trait hierarchy,
//! integrating with the existing allocator infrastructure while providing enhanced
//! functionality for the refactored NumRS2 architecture.

use crate::error::{NumRs2Error, Result};
use crate::traits::{
    AllocationFrequency, AllocationLifetime, AllocationRequirements, AllocationStats,
    AllocationStrategy, ArrayAllocator, MemoryAllocator as NewMemoryAllocator,
    SpecializedAllocator, StrategyStats, ThreadingRequirements,
};
use std::alloc::Layout;
use std::collections::HashMap;
use std::ptr::NonNull;
use std::sync::{Arc, Mutex};

// Bridge the old and new allocator traits
use super::strategy::{MemoryAllocator as OldMemoryAllocator, StandardAllocator};
use super::{AlignedAllocator, ArenaAllocator, PoolAllocator};

// Type alias for complex allocator cache type
type AllocatorCache =
    Arc<Mutex<HashMap<String, Box<dyn SpecializedAllocator<Error = NumRs2Error>>>>>;

// =============================================================================
// BRIDGE IMPLEMENTATIONS FOR EXISTING ALLOCATORS
// =============================================================================

/// Wrapper to bridge old MemoryAllocator trait to new enhanced trait system
#[derive(Debug, Clone)]
pub struct EnhancedAllocatorBridge<T: OldMemoryAllocator> {
    inner: T,
    stats: Arc<Mutex<AllocationStats>>,
}

impl<T: OldMemoryAllocator + std::fmt::Debug> EnhancedAllocatorBridge<T> {
    pub fn new(allocator: T) -> Self {
        Self {
            inner: allocator,
            stats: Arc::new(Mutex::new(AllocationStats::default())),
        }
    }
}

impl<T: OldMemoryAllocator + std::fmt::Debug> NewMemoryAllocator for EnhancedAllocatorBridge<T> {
    type Error = NumRs2Error;

    fn allocate(&self, layout: Layout) -> Result<NonNull<u8>> {
        let ptr = self.inner.allocate_layout(layout).ok_or_else(|| {
            NumRs2Error::AllocationFailed(format!("Failed to allocate {} bytes", layout.size()))
        })?;

        // Update statistics
        if let Ok(mut stats) = self.stats.lock() {
            stats.bytes_allocated += layout.size();
            stats.active_allocations += 1;
            stats.allocation_count += 1;
            if stats.bytes_allocated - stats.bytes_deallocated > stats.peak_usage {
                stats.peak_usage = stats.bytes_allocated - stats.bytes_deallocated;
            }
        }

        Ok(ptr)
    }

    unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) -> Result<()> {
        self.inner.deallocate(ptr, layout);

        // Update statistics
        if let Ok(mut stats) = self.stats.lock() {
            stats.bytes_deallocated += layout.size();
            stats.active_allocations = stats.active_allocations.saturating_sub(1);
            stats.deallocation_count += 1;
        }

        Ok(())
    }

    unsafe fn reallocate(
        &self,
        ptr: NonNull<u8>,
        old_layout: Layout,
        new_layout: Layout,
    ) -> Result<NonNull<u8>> {
        // Simple implementation: allocate new, copy, deallocate old
        let new_ptr = self.allocate(new_layout)?;

        let copy_size = std::cmp::min(old_layout.size(), new_layout.size());
        std::ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_ptr(), copy_size);

        self.deallocate(ptr, old_layout)?;

        Ok(new_ptr)
    }

    fn statistics(&self) -> Option<AllocationStats> {
        self.stats.lock().ok().map(|stats| stats.clone())
    }

    fn supports_layout(&self, _layout: Layout) -> bool {
        true // Most allocators support arbitrary layouts
    }

    fn preferred_alignment(&self) -> usize {
        std::mem::align_of::<usize>()
    }
}

impl<T: OldMemoryAllocator + std::fmt::Debug> SpecializedAllocator for EnhancedAllocatorBridge<T> {
    fn allocation_error(&self, msg: &str) -> Self::Error {
        NumRs2Error::AllocationFailed(msg.to_string())
    }
}

// =============================================================================
// SPECIALIZED ALLOCATOR IMPLEMENTATIONS
// =============================================================================

/// High-performance allocator specifically designed for numerical arrays
#[derive(Debug, Clone)]
pub struct NumericalArrayAllocator {
    inner: EnhancedAllocatorBridge<StandardAllocator>,
    alignment_preference: usize,
}

impl Default for NumericalArrayAllocator {
    fn default() -> Self {
        Self::new()
    }
}

impl NumericalArrayAllocator {
    pub fn new() -> Self {
        Self {
            inner: EnhancedAllocatorBridge::new(StandardAllocator),
            alignment_preference: 32, // 256-bit SIMD alignment
        }
    }

    pub fn with_alignment(alignment: usize) -> Self {
        Self {
            inner: EnhancedAllocatorBridge::new(StandardAllocator),
            alignment_preference: alignment,
        }
    }
}

impl NewMemoryAllocator for NumericalArrayAllocator {
    type Error = NumRs2Error;

    fn allocate(&self, layout: Layout) -> Result<NonNull<u8>> {
        // For numerical data, prefer aligned allocation
        let aligned_layout = Layout::from_size_align(
            layout.size(),
            std::cmp::max(layout.align(), self.alignment_preference),
        )
        .map_err(|_| {
            NumRs2Error::AllocationFailed("Invalid layout for numerical array".to_string())
        })?;

        self.inner.allocate(aligned_layout)
    }

    unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) -> Result<()> {
        self.inner.deallocate(ptr, layout)
    }

    unsafe fn reallocate(
        &self,
        ptr: NonNull<u8>,
        old_layout: Layout,
        new_layout: Layout,
    ) -> Result<NonNull<u8>> {
        self.inner.reallocate(ptr, old_layout, new_layout)
    }

    fn statistics(&self) -> Option<AllocationStats> {
        self.inner.statistics()
    }

    fn supports_layout(&self, layout: Layout) -> bool {
        // We support layouts that can be aligned to our preference
        layout.align() <= self.alignment_preference
    }

    fn preferred_alignment(&self) -> usize {
        self.alignment_preference
    }
}

impl SpecializedAllocator for NumericalArrayAllocator {
    fn allocation_error(&self, msg: &str) -> Self::Error {
        NumRs2Error::AllocationFailed(msg.to_string())
    }
}

impl ArrayAllocator for NumericalArrayAllocator {
    type Error = NumRs2Error;

    fn allocate_array<T>(&self, len: usize) -> std::result::Result<NonNull<T>, Self::Error> {
        let size = len * std::mem::size_of::<T>();
        let alignment = std::cmp::max(std::mem::align_of::<T>(), self.alignment_preference);
        let layout = Layout::from_size_align(size, alignment)
            .map_err(|_| NumRs2Error::AllocationFailed("Invalid array layout".to_string()))?;

        self.allocate(layout).map(|ptr| ptr.cast::<T>())
    }

    fn allocate_simd_aligned<T>(
        &self,
        len: usize,
        alignment: usize,
    ) -> std::result::Result<NonNull<T>, Self::Error> {
        let size = len * std::mem::size_of::<T>();
        let layout = Layout::from_size_align(size, alignment)
            .map_err(|_| NumRs2Error::AllocationFailed("Invalid SIMD layout".to_string()))?;

        self.allocate(layout).map(|ptr| ptr.cast::<T>())
    }
}

// =============================================================================
// INTELLIGENT ALLOCATION STRATEGY
// =============================================================================

/// Intelligent allocation strategy that selects the best allocator based on requirements
#[derive(Debug)]
pub struct IntelligentAllocationStrategy {
    stats: Arc<Mutex<StrategyStats>>,
    allocator_cache: AllocatorCache,
}

impl Default for IntelligentAllocationStrategy {
    fn default() -> Self {
        Self::new()
    }
}

impl IntelligentAllocationStrategy {
    pub fn new() -> Self {
        Self {
            stats: Arc::new(Mutex::new(StrategyStats::default())),
            allocator_cache: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    fn select_allocator_type(&self, requirements: &AllocationRequirements) -> String {
        // Intelligent selection based on requirements
        match (
            requirements.size,
            requirements.frequency,
            requirements.simd_usage,
            requirements.lifetime,
        ) {
            // Large allocations with standard system allocator
            (size, _, _, _) if size > 1_000_000 => "standard".to_string(),

            // Very small, frequent allocations work well with pool
            (size, AllocationFrequency::VeryHigh, _, AllocationLifetime::Temporary)
                if size < 8192 =>
            {
                "pool".to_string()
            }

            // Medium, frequent allocations work well with arena
            (size, AllocationFrequency::High, _, lifetime)
                if size < 65536
                    && matches!(
                        lifetime,
                        AllocationLifetime::Temporary | AllocationLifetime::ShortTerm
                    ) =>
            {
                "arena".to_string()
            }

            // SIMD operations need aligned memory
            (_, _, true, _) => "aligned".to_string(),

            // Numerical arrays get specialized treatment
            (size, freq, _, _) if size > 1024 && !matches!(freq, AllocationFrequency::VeryHigh) => {
                "numerical".to_string()
            }

            // Default to standard allocator
            _ => "standard".to_string(),
        }
    }

    fn create_allocator(
        &self,
        allocator_type: &str,
    ) -> Box<dyn SpecializedAllocator<Error = NumRs2Error>> {
        match allocator_type {
            "standard" => Box::new(EnhancedAllocatorBridge::new(StandardAllocator)),
            "pool" => Box::new(EnhancedAllocatorBridge::new(PoolAllocator::new(
                super::pool::PoolConfig::default(),
            ))),
            "arena" => Box::new(EnhancedAllocatorBridge::new(ArenaAllocator::new(
                super::arena::ArenaConfig::default(),
            ))),
            "aligned" => Box::new(EnhancedAllocatorBridge::new(AlignedAllocator::new(
                super::aligned::AlignmentConfig::default(),
            ))),
            "numerical" => Box::new(NumericalArrayAllocator::new()),
            _ => Box::new(EnhancedAllocatorBridge::new(StandardAllocator)),
        }
    }
}

impl AllocationStrategy for IntelligentAllocationStrategy {
    fn select_allocator(
        &self,
        requirements: &AllocationRequirements,
    ) -> Box<dyn SpecializedAllocator<Error = NumRs2Error>> {
        let allocator_type = self.select_allocator_type(requirements);

        // Update statistics
        if let Ok(mut stats) = self.stats.lock() {
            *stats
                .allocator_selections
                .entry(allocator_type.clone())
                .or_insert(0) += 1;
            stats.total_requests += 1;
        }

        // Check cache first
        if let Ok(mut cache) = self.allocator_cache.lock() {
            if let Some(allocator) = cache.remove(&allocator_type) {
                return allocator;
            }
        }

        // Create new allocator
        self.create_allocator(&allocator_type)
    }

    fn strategy_stats(&self) -> StrategyStats {
        self.stats
            .lock()
            .map(|stats| stats.clone())
            .unwrap_or_default()
    }
}

// =============================================================================
// HELPER FUNCTIONS FOR REQUIREMENTS DETECTION
// =============================================================================

impl AllocationRequirements {
    /// Create requirements for a typical numerical array allocation
    pub fn for_array<T>(len: usize) -> Self {
        let size = len * std::mem::size_of::<T>();
        Self {
            size,
            alignment: std::mem::align_of::<T>(),
            frequency: if size < 1024 {
                AllocationFrequency::High
            } else {
                AllocationFrequency::Medium
            },
            simd_usage: std::mem::align_of::<T>() >= 16, // Assume SIMD for well-aligned types
            lifetime: AllocationLifetime::MediumTerm,
            threading: ThreadingRequirements::MultiThreadedRead,
        }
    }

    /// Create requirements for temporary computation buffers
    pub fn for_temporary_buffer(size: usize) -> Self {
        Self {
            size,
            alignment: 8,
            frequency: AllocationFrequency::High,
            simd_usage: false,
            lifetime: AllocationLifetime::Temporary,
            threading: ThreadingRequirements::SingleThreaded,
        }
    }

    /// Create requirements for SIMD-optimized operations
    pub fn for_simd_operation<T>(len: usize, alignment: usize) -> Self {
        let size = len * std::mem::size_of::<T>();
        Self {
            size,
            alignment,
            frequency: AllocationFrequency::Medium,
            simd_usage: true,
            lifetime: AllocationLifetime::ShortTerm,
            threading: ThreadingRequirements::MultiThreadedRead,
        }
    }
}

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

    #[test]
    fn test_enhanced_allocator_bridge() {
        let allocator = EnhancedAllocatorBridge::new(StandardAllocator);

        let layout = Layout::from_size_align(1024, 8)
            .expect("Layout::from_size_align(1024, 8) should succeed");
        let ptr = allocator
            .allocate(layout)
            .expect("Allocation should succeed");

        // Check statistics
        let stats = allocator
            .statistics()
            .expect("statistics should be available");
        assert_eq!(stats.bytes_allocated, 1024);
        assert_eq!(stats.active_allocations, 1);

        unsafe {
            allocator
                .deallocate(ptr, layout)
                .expect("Deallocation should succeed");
        }

        let stats = allocator
            .statistics()
            .expect("statistics should be available");
        assert_eq!(stats.bytes_deallocated, 1024);
        assert_eq!(stats.active_allocations, 0);
    }

    #[test]
    fn test_numerical_array_allocator() {
        let allocator = NumericalArrayAllocator::new();

        // Test array allocation
        let ptr = allocator
            .allocate_array::<f64>(100)
            .expect("Array allocation should succeed");

        // Verify alignment
        assert_eq!(ptr.as_ptr() as usize % 32, 0, "Should be 32-byte aligned");

        let layout = Layout::array::<f64>(100).expect("Layout::array::<f64>(100) should succeed");
        unsafe {
            allocator
                .deallocate(ptr.cast(), layout)
                .expect("Deallocation should succeed");
        }
    }

    #[test]
    fn test_intelligent_allocation_strategy() {
        let strategy = IntelligentAllocationStrategy::new();

        // Test different requirement scenarios
        let array_req = AllocationRequirements::for_array::<f64>(1000);
        let allocator = strategy.select_allocator(&array_req);
        assert!(allocator
            .supports_layout(Layout::from_size_align(8000, 8).expect("Layout should succeed")));

        let simd_req = AllocationRequirements::for_simd_operation::<f32>(256, 32);
        let allocator = strategy.select_allocator(&simd_req);
        assert!(allocator.preferred_alignment() >= 8);

        // Check statistics
        let stats = strategy.strategy_stats();
        assert!(stats.total_requests >= 2);
    }

    #[test]
    fn test_allocation_requirements_creation() {
        let array_req = AllocationRequirements::for_array::<f64>(1000);
        assert_eq!(array_req.size, 8000);
        assert_eq!(array_req.alignment, 8);

        let simd_req = AllocationRequirements::for_simd_operation::<f32>(64, 32);
        assert_eq!(simd_req.size, 256);
        assert_eq!(simd_req.alignment, 32);
        assert!(simd_req.simd_usage);

        let temp_req = AllocationRequirements::for_temporary_buffer(512);
        assert_eq!(temp_req.size, 512);
        assert_eq!(temp_req.lifetime, AllocationLifetime::Temporary);
    }
}