scirs2-core 0.4.3

Core utilities and common functionality for SciRS2 (scirs2-core)
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
//! Buffer pool and arena memory management for GPU operations.
//!
//! This submodule provides core pool-based allocation types: [`BufferPool`],
//! [`BufferAllocator`], [`MemoryArena`], and associated error/result types.

use crate::gpu::{GpuBuffer, GpuDataType, GpuError};
use std::collections::{BTreeMap, HashMap, VecDeque};
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use thiserror::Error;

#[derive(Error, Debug)]
pub enum MemoryError {
    /// Out of memory
    #[error("Out of memory: {0}")]
    OutOfMemory(String),

    /// Invalid allocation size
    #[error("Invalid allocation size: {0}")]
    InvalidSize(usize),

    /// Buffer not found
    #[error("Buffer not found: {0}")]
    BufferNotFound(u64),

    /// Pool is full
    #[error("Pool is full")]
    PoolFull,

    /// Fragmentation threshold exceeded
    #[error("Fragmentation threshold exceeded: {0:.2}%")]
    FragmentationExceeded(f64),

    /// GPU error
    #[error("GPU error: {0}")]
    GpuError(#[from] GpuError),
}

/// Result type for memory operations
pub type MemoryResult<T> = Result<T, MemoryError>;

/// Buffer handle for tracking allocated buffers
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct BufferHandle(u64);

impl BufferHandle {
    /// Create a new buffer handle
    fn new() -> Self {
        static COUNTER: AtomicU64 = AtomicU64::new(1);
        Self(COUNTER.fetch_add(1, Ordering::Relaxed))
    }

    /// Get the raw handle value
    pub fn raw(&self) -> u64 {
        self.0
    }
}

/// Buffer metadata for pool management
#[derive(Debug, Clone)]
struct BufferMetadata {
    handle: BufferHandle,
    size: usize,
    allocated_at: Instant,
    last_used: Instant,
    use_count: usize,
    is_pinned: bool,
}

impl BufferMetadata {
    fn new(handle: BufferHandle, size: usize) -> Self {
        let now = Instant::now();
        Self {
            handle,
            size,
            allocated_at: now,
            last_used: now,
            use_count: 0,
            is_pinned: false,
        }
    }

    fn mark_used(&mut self) {
        self.last_used = Instant::now();
        self.use_count += 1;
    }

    fn age(&self) -> Duration {
        self.allocated_at.elapsed()
    }

    fn idle_time(&self) -> Duration {
        self.last_used.elapsed()
    }
}

/// Buffer pool for reusing GPU buffers
#[derive(Debug)]
pub struct BufferPool<T: GpuDataType> {
    // Size-stratified pools (size -> list of available buffers)
    pools: Arc<Mutex<BTreeMap<usize, VecDeque<(BufferHandle, Arc<GpuBuffer<T>>)>>>>,
    // Active buffers being used
    active_buffers: Arc<Mutex<HashMap<BufferHandle, BufferMetadata>>>,
    // Statistics
    total_allocated: Arc<AtomicUsize>,
    total_reused: Arc<AtomicUsize>,
    max_pool_size: usize,
    eviction_policy: EvictionPolicy,
}

/// Buffer eviction policy
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EvictionPolicy {
    /// Least Recently Used
    Lru,
    /// Least Frequently Used
    Lfu,
    /// First In First Out
    Fifo,
}

impl<T: GpuDataType> BufferPool<T> {
    /// Create a new buffer pool
    pub fn new() -> Self {
        Self::with_capacity_and_policy(1024, EvictionPolicy::Lru)
    }

    /// Create a new buffer pool with capacity
    pub fn with_capacity(max_pool_size: usize) -> Self {
        Self::with_capacity_and_policy(max_pool_size, EvictionPolicy::Lru)
    }

    /// Create a new buffer pool with capacity and eviction policy
    pub fn with_capacity_and_policy(max_pool_size: usize, eviction_policy: EvictionPolicy) -> Self {
        Self {
            pools: Arc::new(Mutex::new(BTreeMap::new())),
            active_buffers: Arc::new(Mutex::new(HashMap::new())),
            total_allocated: Arc::new(AtomicUsize::new(0)),
            total_reused: Arc::new(AtomicUsize::new(0)),
            max_pool_size,
            eviction_policy,
        }
    }

    /// Allocate a buffer from the pool or create a new one
    pub fn allocate(&self, size: usize) -> MemoryResult<(BufferHandle, Arc<GpuBuffer<T>>)> {
        if size == 0 {
            return Err(MemoryError::InvalidSize(size));
        }

        // Try to reuse an existing buffer
        let mut pools = self.pools.lock().map_err(|_| {
            MemoryError::GpuError(GpuError::Other("Failed to lock pools".to_string()))
        })?;

        // Look for a buffer of the exact size or slightly larger
        let reusable = pools
            .range_mut(size..)
            .next()
            .and_then(|(_, buffers)| buffers.pop_front());

        if let Some((handle, buffer)) = reusable {
            self.total_reused.fetch_add(1, Ordering::Relaxed);

            let mut active = self.active_buffers.lock().map_err(|_| {
                MemoryError::GpuError(GpuError::Other("Failed to lock active buffers".to_string()))
            })?;

            if let Some(metadata) = active.get_mut(&handle) {
                metadata.mark_used();
            } else {
                let metadata = BufferMetadata::new(handle, size);
                active.insert(handle, metadata);
            }

            return Ok((handle, buffer));
        }

        drop(pools);

        // No reusable buffer found, allocate a new one
        // Note: In a real implementation, this would call the GPU backend
        // For now, we create a placeholder buffer
        self.allocate_new_buffer(size)
    }

    /// Allocate a new buffer
    fn allocate_new_buffer(&self, size: usize) -> MemoryResult<(BufferHandle, Arc<GpuBuffer<T>>)> {
        self.total_allocated.fetch_add(1, Ordering::Relaxed);

        // In a real implementation, this would use the GPU backend to allocate
        // For now, we create a dummy buffer handle
        let handle = BufferHandle::new();

        // Since we can't create a real GpuBuffer without a backend,
        // we'll need to modify this in practice to use the actual GPU context
        // For the type system, we'll return an error indicating this needs backend support
        Err(MemoryError::GpuError(GpuError::Other(
            "Buffer allocation requires GPU backend context".to_string(),
        )))
    }

    /// Return a buffer to the pool
    pub fn deallocate(&self, handle: BufferHandle, buffer: Arc<GpuBuffer<T>>) -> MemoryResult<()> {
        let mut active = self.active_buffers.lock().map_err(|_| {
            MemoryError::GpuError(GpuError::Other("Failed to lock active buffers".to_string()))
        })?;

        let metadata = active
            .remove(&handle)
            .ok_or(MemoryError::BufferNotFound(handle.raw()))?;

        drop(active);

        let mut pools = self.pools.lock().map_err(|_| {
            MemoryError::GpuError(GpuError::Other("Failed to lock pools".to_string()))
        })?;

        let pool = pools.entry(metadata.size).or_insert_with(VecDeque::new);

        if pool.len() >= self.max_pool_size {
            // Pool is full, apply eviction policy
            self.evict_buffer(pool)?;
        }

        pool.push_back((handle, buffer));
        Ok(())
    }

    /// Evict a buffer based on the eviction policy
    fn evict_buffer(
        &self,
        pool: &mut VecDeque<(BufferHandle, Arc<GpuBuffer<T>>)>,
    ) -> MemoryResult<()> {
        match self.eviction_policy {
            EvictionPolicy::Lru | EvictionPolicy::Lfu | EvictionPolicy::Fifo => {
                // For FIFO and LRU, remove the front
                pool.pop_front();
                Ok(())
            }
        }
    }

    /// Get pool statistics
    pub fn statistics(&self) -> BufferPoolStatistics {
        let pools = self.pools.lock().expect("Failed to lock pools");
        let active = self
            .active_buffers
            .lock()
            .expect("Failed to lock active buffers");

        let total_pooled: usize = pools.values().map(|v| v.len()).sum();
        let total_active = active.len();

        BufferPoolStatistics {
            total_allocated: self.total_allocated.load(Ordering::Relaxed),
            total_reused: self.total_reused.load(Ordering::Relaxed),
            pooled_buffers: total_pooled,
            active_buffers: total_active,
            pool_size_distribution: pools
                .iter()
                .map(|(size, buffers)| (*size, buffers.len()))
                .collect(),
        }
    }

    /// Clear the pool
    pub fn clear(&self) -> MemoryResult<()> {
        let mut pools = self.pools.lock().map_err(|_| {
            MemoryError::GpuError(GpuError::Other("Failed to lock pools".to_string()))
        })?;
        pools.clear();
        Ok(())
    }

    /// Get the total number of pooled buffers
    pub fn pooled_count(&self) -> MemoryResult<usize> {
        let pools = self.pools.lock().map_err(|_| {
            MemoryError::GpuError(GpuError::Other("Failed to lock pools".to_string()))
        })?;
        Ok(pools.values().map(|v| v.len()).sum())
    }
}

impl<T: GpuDataType> Default for BufferPool<T> {
    fn default() -> Self {
        Self::new()
    }
}

/// Buffer pool statistics
#[derive(Debug, Clone)]
pub struct BufferPoolStatistics {
    pub total_allocated: usize,
    pub total_reused: usize,
    pub pooled_buffers: usize,
    pub active_buffers: usize,
    pub pool_size_distribution: Vec<(usize, usize)>,
}

/// Buffer allocator with size stratification
#[derive(Debug)]
pub struct BufferAllocator {
    // Size classes (powers of 2)
    size_classes: Vec<usize>,
    // Memory statistics
    allocated_bytes: Arc<AtomicUsize>,
    freed_bytes: Arc<AtomicUsize>,
    peak_usage: Arc<AtomicUsize>,
    // Fragmentation tracking
    fragmentation_threshold: f64,
}

impl BufferAllocator {
    /// Create a new buffer allocator
    pub fn new() -> Self {
        Self::with_size_classes(Self::default_size_classes())
    }

    /// Create allocator with custom size classes
    pub fn with_size_classes(size_classes: Vec<usize>) -> Self {
        Self {
            size_classes,
            allocated_bytes: Arc::new(AtomicUsize::new(0)),
            freed_bytes: Arc::new(AtomicUsize::new(0)),
            peak_usage: Arc::new(AtomicUsize::new(0)),
            fragmentation_threshold: 0.3, // 30% fragmentation threshold
        }
    }

    /// Get default size classes (powers of 2 from 256 bytes to 1 GB)
    fn default_size_classes() -> Vec<usize> {
        let mut classes = Vec::new();
        let mut size = 256;
        while size <= 1024 * 1024 * 1024 {
            classes.push(size);
            size *= 2;
        }
        classes
    }

    /// Find the appropriate size class for a requested size
    pub fn size_class_for(&self, size: usize) -> usize {
        self.size_classes
            .iter()
            .find(|&&class_size| class_size >= size)
            .copied()
            .unwrap_or_else(|| {
                // Round up to next power of 2
                size.next_power_of_two()
            })
    }

    /// Record an allocation
    pub fn record_allocation(&self, size: usize) {
        let new_allocated = self.allocated_bytes.fetch_add(size, Ordering::Relaxed) + size;

        // Update peak usage
        let mut peak = self.peak_usage.load(Ordering::Relaxed);
        while new_allocated > peak {
            match self.peak_usage.compare_exchange_weak(
                peak,
                new_allocated,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(current) => peak = current,
            }
        }
    }

    /// Record a deallocation
    pub fn record_deallocation(&self, size: usize) {
        self.freed_bytes.fetch_add(size, Ordering::Relaxed);
    }

    /// Get current allocated bytes
    pub fn allocated_bytes(&self) -> usize {
        self.allocated_bytes.load(Ordering::Relaxed)
    }

    /// Get freed bytes
    pub fn freed_bytes(&self) -> usize {
        self.freed_bytes.load(Ordering::Relaxed)
    }

    /// Get current usage (allocated - freed)
    pub fn current_usage(&self) -> usize {
        self.allocated_bytes() - self.freed_bytes()
    }

    /// Get peak usage
    pub fn peak_usage(&self) -> usize {
        self.peak_usage.load(Ordering::Relaxed)
    }

    /// Calculate fragmentation ratio
    pub fn fragmentation_ratio(&self) -> f64 {
        let allocated = self.allocated_bytes() as f64;
        let current = self.current_usage() as f64;

        if allocated == 0.0 {
            return 0.0;
        }

        (allocated - current) / allocated
    }

    /// Check if defragmentation is needed
    pub fn needs_defragmentation(&self) -> bool {
        self.fragmentation_ratio() > self.fragmentation_threshold
    }

    /// Get allocator statistics
    pub fn statistics(&self) -> AllocatorStatistics {
        AllocatorStatistics {
            allocated_bytes: self.allocated_bytes(),
            freed_bytes: self.freed_bytes(),
            current_usage: self.current_usage(),
            peak_usage: self.peak_usage(),
            fragmentation_ratio: self.fragmentation_ratio(),
        }
    }
}

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

/// Allocator statistics
#[derive(Debug, Clone)]
pub struct AllocatorStatistics {
    pub allocated_bytes: usize,
    pub freed_bytes: usize,
    pub current_usage: usize,
    pub peak_usage: usize,
    pub fragmentation_ratio: f64,
}

/// Memory arena for temporary scratch space
#[derive(Debug)]
pub struct MemoryArena<T: GpuDataType> {
    // Arena buffers
    buffers: Arc<Mutex<Vec<Arc<GpuBuffer<T>>>>>,
    // Current offset in the active buffer
    current_offset: Arc<AtomicUsize>,
    // Size of each arena buffer
    buffer_size: usize,
    // Total allocated in this arena
    total_allocated: Arc<AtomicUsize>,
}

impl<T: GpuDataType> MemoryArena<T> {
    /// Create a new memory arena
    pub fn new(buffer_size: usize) -> Self {
        Self {
            buffers: Arc::new(Mutex::new(Vec::new())),
            current_offset: Arc::new(AtomicUsize::new(0)),
            buffer_size,
            total_allocated: Arc::new(AtomicUsize::new(0)),
        }
    }

    /// Allocate temporary space in the arena
    pub fn allocate_temp(&self, size: usize) -> MemoryResult<ArenaAllocation> {
        if size > self.buffer_size {
            return Err(MemoryError::InvalidSize(size));
        }

        let offset = self.current_offset.fetch_add(size, Ordering::Relaxed);

        // Check if we need a new buffer
        if offset + size > self.buffer_size {
            // Would need to allocate a new buffer in the arena
            // Reset offset and allocate from start of new buffer
            self.current_offset.store(size, Ordering::Relaxed);
            self.total_allocated
                .fetch_add(self.buffer_size, Ordering::Relaxed);

            Ok(ArenaAllocation {
                buffer_index: 0, // In practice, would track which buffer
                offset: 0,
                size,
            })
        } else {
            Ok(ArenaAllocation {
                buffer_index: 0,
                offset,
                size,
            })
        }
    }

    /// Reset the arena (deallocate all temporary allocations)
    pub fn reset(&self) {
        self.current_offset.store(0, Ordering::Relaxed);
    }

    /// Get total allocated bytes
    pub fn total_allocated(&self) -> usize {
        self.total_allocated.load(Ordering::Relaxed)
    }

    /// Get current usage
    pub fn current_usage(&self) -> usize {
        self.current_offset.load(Ordering::Relaxed)
    }
}

/// Arena allocation handle
#[derive(Debug, Clone, Copy)]
pub struct ArenaAllocation {
    buffer_index: usize,
    offset: usize,
    size: usize,
}

impl ArenaAllocation {
    /// Get the offset in the buffer
    pub fn offset(&self) -> usize {
        self.offset
    }

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

    /// Get the buffer index
    pub fn buffer_index(&self) -> usize {
        self.buffer_index
    }
}

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

    fn test_buffer_handle_creation() {
        let handle1 = BufferHandle::new();
        let handle2 = BufferHandle::new();
        assert_ne!(handle1, handle2);
    }

    #[test]
    fn test_buffer_metadata() {
        let handle = BufferHandle::new();
        let mut metadata = BufferMetadata::new(handle, 1024);

        assert_eq!(metadata.handle, handle);
        assert_eq!(metadata.size, 1024);
        assert_eq!(metadata.use_count, 0);
        assert!(!metadata.is_pinned);

        metadata.mark_used();
        assert_eq!(metadata.use_count, 1);
    }

    #[test]
    fn test_buffer_allocator_size_classes() {
        let allocator = BufferAllocator::new();

        assert_eq!(allocator.size_class_for(100), 256);
        assert_eq!(allocator.size_class_for(256), 256);
        assert_eq!(allocator.size_class_for(257), 512);
        assert_eq!(allocator.size_class_for(1000), 1024);
    }

    #[test]
    fn test_buffer_allocator_statistics() {
        let allocator = BufferAllocator::new();

        allocator.record_allocation(1024);
        allocator.record_allocation(2048);

        assert_eq!(allocator.allocated_bytes(), 3072);
        assert_eq!(allocator.current_usage(), 3072);
        assert_eq!(allocator.peak_usage(), 3072);

        allocator.record_deallocation(1024);
        assert_eq!(allocator.current_usage(), 2048);
        assert_eq!(allocator.freed_bytes(), 1024);
    }

    #[test]
    fn test_buffer_allocator_fragmentation() {
        let allocator = BufferAllocator::new();

        allocator.record_allocation(10000);
        allocator.record_deallocation(3000);

        let frag = allocator.fragmentation_ratio();
        assert!(frag > 0.0 && frag < 1.0);
    }

    #[test]
    fn test_memory_arena() {
        let arena = MemoryArena::<f32>::new(4096);

        let alloc1 = arena.allocate_temp(1024).expect("Failed to allocate");
        assert_eq!(alloc1.size(), 1024);
        assert_eq!(alloc1.offset(), 0);

        let alloc2 = arena.allocate_temp(512).expect("Failed to allocate");
        assert_eq!(alloc2.size(), 512);
        assert_eq!(alloc2.offset(), 1024);

        arena.reset();
        assert_eq!(arena.current_usage(), 0);
    }

    #[test]
    fn test_memory_arena_overflow() {
        let arena = MemoryArena::<f32>::new(1024);

        let result = arena.allocate_temp(2048);
        assert!(result.is_err());
    }

    fn test_eviction_policy() {
        let pool = BufferPool::<f32>::with_capacity_and_policy(10, EvictionPolicy::Lru);
        let stats = pool.statistics();

        assert_eq!(stats.pooled_buffers, 0);
        assert_eq!(stats.active_buffers, 0);
    }
}