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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
/// GPU memory management
/// GPUメモリ管理
use super::DeviceType;
use crate::error::{RusTorchError, RusTorchResult};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
/// GPU memory allocation information
/// GPUメモリ割り当て情報
#[derive(Debug, Clone)]
pub struct MemoryAllocation {
/// Device where memory is allocated
/// メモリが割り当てられたデバイス
pub device: DeviceType,
/// Size in bytes
/// サイズ(バイト)
pub size: usize,
/// Memory pointer (platform-specific)
/// メモリポインタ(プラットフォーム固有)
pub ptr: usize,
/// Allocation timestamp
/// 割り当てタイムスタンプ
pub timestamp: std::time::Instant,
}
/// GPU memory pool for efficient allocation
/// 効率的な割り当てのためのGPUメモリプール
pub struct GpuMemoryPool {
device: DeviceType,
total_size: usize,
allocated_size: usize,
free_blocks: Vec<(usize, usize)>, // (offset, size)
allocations: HashMap<usize, MemoryAllocation>,
// Use safe Rust memory management instead of raw pointers
memory_buffer: Option<Box<[u8]>>,
base_ptr: usize,
}
impl GpuMemoryPool {
/// Create a new GPU memory pool
/// 新しいGPUメモリプールを作成
pub fn new(device: DeviceType, size: usize) -> RusTorchResult<Self> {
let (memory_buffer, base_ptr) = match device {
DeviceType::Cpu => {
// Use safe Rust Box allocation instead of unsafe alloc
let buffer = vec![0u8; size].into_boxed_slice();
let ptr = buffer.as_ptr() as usize;
(Some(buffer), ptr)
}
DeviceType::Cuda(_) => {
#[cfg(feature = "cuda")]
{
// CUDA memory allocation would go here
// cudaMalloc equivalent
(None, 0) // Placeholder
}
#[cfg(not(feature = "cuda"))]
{
return Err(RusTorchError::gpu("CUDA not supported"));
}
}
DeviceType::Metal(_) => {
#[cfg(feature = "metal")]
{
// Metal buffer allocation would go here
(None, 0) // Placeholder
}
#[cfg(not(feature = "metal"))]
{
return Err(RusTorchError::gpu("Metal not supported"));
}
}
DeviceType::OpenCL(_) => {
#[cfg(feature = "opencl")]
{
// OpenCL buffer allocation would go here
(None, 0) // Placeholder
}
#[cfg(not(feature = "opencl"))]
{
return Err(RusTorchError::gpu("OpenCL not supported"));
}
}
#[cfg(any(
feature = "coreml",
feature = "coreml-hybrid",
feature = "coreml-fallback"
))]
DeviceType::CoreML(_) => {
return Err(RusTorchError::UnsupportedDevice(
"CoreML memory allocation not supported".to_string(),
));
}
DeviceType::Auto => {
return Err(RusTorchError::UnsupportedDevice(
"Auto memory allocation not supported".to_string(),
));
}
#[cfg(feature = "mac-hybrid")]
DeviceType::MacHybrid => {
// Use CPU allocation for MacHybrid
let buffer = vec![0u8; size].into_boxed_slice();
let ptr = buffer.as_ptr() as usize;
(Some(buffer), ptr)
}
};
if base_ptr == 0 && !matches!(device, DeviceType::Cpu) {
return Err(RusTorchError::tensor_op("Failed to allocate GPU memory"));
}
Ok(GpuMemoryPool {
device,
total_size: size,
allocated_size: 0,
free_blocks: vec![(0, size)],
allocations: HashMap::new(),
memory_buffer,
base_ptr,
})
}
/// Allocate memory from the pool with optimized alignment strategy
/// 最適化されたアライメント戦略でプールからメモリを割り当て
pub fn allocate(&mut self, size: usize) -> RusTorchResult<MemoryAllocation> {
// Advanced alignment strategy based on device type
let aligned_size = self.get_optimal_alignment(size);
// Improved best-fit allocation with coalescing prevention
let mut best_block_idx = None;
let mut best_waste_ratio = f64::INFINITY;
for (idx, &(_offset, block_size)) in self.free_blocks.iter().enumerate() {
if block_size >= aligned_size {
// Calculate waste ratio to minimize fragmentation
let waste_ratio = (block_size - aligned_size) as f64 / block_size as f64;
if waste_ratio < best_waste_ratio {
best_block_idx = Some(idx);
best_waste_ratio = waste_ratio;
// Perfect fit - no need to continue searching
if waste_ratio < 0.1 {
break;
}
}
}
}
let block_idx = best_block_idx.ok_or_else(|| {
RusTorchError::tensor_op(format!(
"No suitable free block found for size {} (aligned: {})",
size, aligned_size
))
})?;
let (offset, block_size) = self.free_blocks[block_idx];
self.free_blocks.remove(block_idx);
// If the block is larger than needed, split it
if block_size > aligned_size {
let remaining_offset = offset + aligned_size;
let remaining_size = block_size - aligned_size;
self.free_blocks.push((remaining_offset, remaining_size));
}
let ptr = self.base_ptr + offset;
let allocation = MemoryAllocation {
device: self.device,
size: aligned_size,
ptr,
timestamp: std::time::Instant::now(),
};
self.allocations.insert(ptr, allocation.clone());
self.allocated_size += aligned_size;
Ok(allocation)
}
/// Deallocate memory back to the pool
/// メモリをプールに戻す
pub fn deallocate(&mut self, ptr: usize) -> RusTorchResult<()> {
let allocation = self
.allocations
.remove(&ptr)
.ok_or_else(|| RusTorchError::tensor_op("Invalid pointer for deallocation"))?;
let offset = ptr - self.base_ptr;
let size = allocation.size;
// Add the block back to free blocks
self.free_blocks.push((offset, size));
self.allocated_size -= size;
// Merge adjacent free blocks
self.merge_free_blocks();
Ok(())
}
/// Get basic memory usage statistics (legacy interface)
/// 基本的なメモリ使用量統計を取得(レガシーインターフェース)
pub fn basic_memory_stats(&self) -> (usize, usize, usize, f32) {
let free_size = self.total_size - self.allocated_size;
let usage_percent = (self.allocated_size as f32 / self.total_size as f32) * 100.0;
(
self.total_size,
self.allocated_size,
free_size,
usage_percent,
)
}
/// Get device
/// デバイスを取得
pub fn device(&self) -> DeviceType {
self.device
}
/// Merge adjacent free blocks
/// 隣接する空きブロックをマージ
fn merge_free_blocks(&mut self) {
if self.free_blocks.len() <= 1 {
return;
}
// Sort free blocks by offset
self.free_blocks.sort_by_key(|&(offset, _)| offset);
let mut merged_blocks = Vec::new();
let mut current_block = self.free_blocks[0];
for &(offset, size) in &self.free_blocks[1..] {
let (current_offset, current_size) = current_block;
// Check if blocks are adjacent
if current_offset + current_size == offset {
// Merge blocks
current_block = (current_offset, current_size + size);
} else {
// Blocks are not adjacent, add current block and start new one
merged_blocks.push(current_block);
current_block = (offset, size);
}
}
merged_blocks.push(current_block);
self.free_blocks = merged_blocks;
}
/// Get optimal alignment for different device types and data sizes
/// 異なるデバイス種別とデータサイズに対する最適なアライメントを取得
fn get_optimal_alignment(&self, size: usize) -> usize {
let alignment = match &self.device {
DeviceType::Cpu => {
// CPU optimizations: AVX-512 requires 64-byte alignment, AVX2 requires 32-byte
if size >= 1024 * 1024 {
// Large allocations: 4KB alignment for page efficiency
Self::align_to(size, 4096)
} else if size >= 64 * 1024 {
// Medium allocations: 1KB alignment for cache line efficiency
Self::align_to(size, 1024)
} else {
// Small allocations: 64-byte alignment for SIMD operations
Self::align_to(size, 64)
}
}
DeviceType::Cuda(_) => {
// CUDA optimizations: warp size (32 threads) and memory coalescing
if size >= 1024 * 1024 {
// Large CUDA allocations: 512-byte alignment for optimal memory coalescing
Self::align_to(size, 512)
} else if size >= 32 * 1024 {
// Medium CUDA allocations: 256-byte alignment for L2 cache efficiency
Self::align_to(size, 256)
} else {
// Small CUDA allocations: 128-byte alignment for warp-level efficiency
Self::align_to(size, 128)
}
}
DeviceType::Metal(_) => {
// Metal optimizations: SIMD group size (32) and tile memory
if size >= 1024 * 1024 {
// Large Metal allocations: 1KB alignment for tile memory efficiency
Self::align_to(size, 1024)
} else if size >= 16 * 1024 {
// Medium Metal allocations: 256-byte alignment for memory bandwidth
Self::align_to(size, 256)
} else {
// Small Metal allocations: 128-byte alignment for SIMD groups
Self::align_to(size, 128)
}
}
DeviceType::OpenCL(_) => {
// OpenCL optimizations: work group and memory coalescing considerations
if size >= 1024 * 1024 {
// Large OpenCL allocations: 512-byte alignment for memory bandwidth
Self::align_to(size, 512)
} else {
// Smaller OpenCL allocations: 256-byte alignment for work group efficiency
Self::align_to(size, 256)
}
}
#[cfg(any(
feature = "coreml",
feature = "coreml-hybrid",
feature = "coreml-fallback"
))]
&DeviceType::CoreML(_) => {
// Default alignment for CoreML
Self::align_to(size, 128)
}
&DeviceType::Auto => {
// Default alignment for Auto
Self::align_to(size, 128)
}
#[cfg(feature = "mac-hybrid")]
&DeviceType::MacHybrid => {
// Default alignment for MacHybrid
Self::align_to(size, 128)
}
};
// Ensure minimum alignment for the platform
std::cmp::max(alignment, Self::get_minimum_alignment())
}
/// Align size to the specified boundary
/// 指定された境界にサイズを揃える
fn align_to(size: usize, alignment: usize) -> usize {
(size + alignment - 1) & !(alignment - 1)
}
/// Get minimum platform-specific alignment
/// プラットフォーム固有の最小アライメントを取得
fn get_minimum_alignment() -> usize {
// Use 32 bytes as minimum for modern CPUs and GPUs
32
}
/// Get current memory utilization statistics
/// 現在のメモリ使用統計を取得
pub fn memory_stats(&self) -> MemoryStats {
let fragmentation_ratio = if self.total_size > 0 {
(self.free_blocks.len() as f64) / (self.total_size as f64 / 1024.0)
} else {
0.0
};
MemoryStats {
total_size: self.total_size,
allocated_size: self.allocated_size,
free_size: self.total_size - self.allocated_size,
utilization_ratio: self.allocated_size as f64 / self.total_size as f64,
fragmentation_ratio,
num_allocations: self.allocations.len(),
num_free_blocks: self.free_blocks.len(),
}
}
}
/// Memory utilization statistics
/// メモリ使用統計#[derive(Debug, Clone)]
pub struct MemoryStats {
/// Total pool size in bytes
pub total_size: usize,
/// Currently allocated size in bytes
pub allocated_size: usize,
/// Free memory size in bytes
pub free_size: usize,
/// Utilization ratio (0.0 to 1.0)
pub utilization_ratio: f64,
/// Fragmentation ratio (lower is better)
pub fragmentation_ratio: f64,
/// Number of active allocations
pub num_allocations: usize,
/// Number of free blocks
pub num_free_blocks: usize,
}
impl Drop for GpuMemoryPool {
fn drop(&mut self) {
match self.device {
DeviceType::Cpu => {
// Memory is automatically freed when memory_buffer goes out of scope
// No manual deallocation needed - Box handles it safely
}
DeviceType::Cuda(_) => {
#[cfg(feature = "cuda")]
{
// CUDA memory deallocation would go here
// cudaFree equivalent
}
}
DeviceType::Metal(_) => {
#[cfg(feature = "metal")]
{
// Metal buffer deallocation would go here
}
}
DeviceType::OpenCL(_) => {
#[cfg(feature = "opencl")]
{
// OpenCL buffer deallocation would go here
}
}
#[cfg(any(
feature = "coreml",
feature = "coreml-hybrid",
feature = "coreml-fallback"
))]
DeviceType::CoreML(_) => {
// No manual deallocation needed for CoreML
}
DeviceType::Auto => {
// No manual deallocation needed for Auto
}
#[cfg(feature = "mac-hybrid")]
DeviceType::MacHybrid => {
// Memory is automatically freed when memory_buffer goes out of scope
// No manual deallocation needed - Box handles it safely
}
}
}
}
/// GPU memory manager for multiple devices
/// 複数デバイス用GPUメモリマネージャー
pub struct GpuMemoryManager {
pools: HashMap<DeviceType, Arc<Mutex<GpuMemoryPool>>>,
default_pool_size: usize,
}
impl GpuMemoryManager {
/// Create a new GPU memory manager
/// 新しいGPUメモリマネージャーを作成
pub fn new(default_pool_size: usize) -> Self {
GpuMemoryManager {
pools: HashMap::new(),
default_pool_size,
}
}
/// Get or create memory pool for device
/// デバイス用メモリプールを取得または作成
pub fn get_pool(&mut self, device: DeviceType) -> RusTorchResult<Arc<Mutex<GpuMemoryPool>>> {
if let Some(pool) = self.pools.get(&device) {
Ok(pool.clone())
} else {
let pool = GpuMemoryPool::new(device, self.default_pool_size)?;
let pool_arc = Arc::new(Mutex::new(pool));
self.pools.insert(device, pool_arc.clone());
Ok(pool_arc)
}
}
/// Allocate memory on specific device
/// 特定デバイスでメモリを割り当て
pub fn allocate(
&mut self,
device: DeviceType,
size: usize,
) -> RusTorchResult<MemoryAllocation> {
let pool = self.get_pool(device)?;
let mut pool_guard = pool.lock().unwrap();
pool_guard.allocate(size)
}
/// Deallocate memory
/// メモリを解放
pub fn deallocate(&mut self, allocation: &MemoryAllocation) -> RusTorchResult<()> {
if let Some(pool) = self.pools.get(&allocation.device) {
let mut pool_guard = pool.lock().unwrap();
pool_guard.deallocate(allocation.ptr)
} else {
Err(RusTorchError::tensor_op("Device pool not found"))
}
}
/// Get memory statistics for all devices (legacy format)
/// 全デバイスのメモリ統計を取得(レガシー形式)
pub fn basic_memory_stats(&self) -> HashMap<DeviceType, (usize, usize, usize, f32)> {
let mut stats = HashMap::new();
for (device, pool) in &self.pools {
if let Ok(pool_guard) = pool.lock() {
stats.insert(*device, pool_guard.basic_memory_stats());
}
}
stats
}
/// Get detailed memory statistics for all devices
/// 全デバイスの詳細メモリ統計を取得
pub fn memory_stats(&self) -> HashMap<DeviceType, MemoryStats> {
let mut stats = HashMap::new();
for (device, pool) in &self.pools {
if let Ok(pool_guard) = pool.lock() {
stats.insert(*device, pool_guard.memory_stats());
}
}
stats
}
/// Clear all pools
/// 全プールをクリア
pub fn clear(&mut self) {
self.pools.clear();
}
}
/// Data transfer operations between devices
/// デバイス間データ転送操作
pub struct DataTransfer;
impl DataTransfer {
/// Copy data from host to device
/// ホストからデバイスへデータをコピー
pub fn host_to_device<T: Copy>(
src: &[T],
dst_allocation: &MemoryAllocation,
) -> RusTorchResult<()> {
let src_size = std::mem::size_of_val(src);
if src_size > dst_allocation.size {
return Err(RusTorchError::tensor_op("Source data too large"));
}
match dst_allocation.device {
DeviceType::Cpu => {
// Direct memory copy for CPU
unsafe {
let dst_ptr = dst_allocation.ptr as *mut T;
std::ptr::copy_nonoverlapping(src.as_ptr(), dst_ptr, src.len());
}
}
DeviceType::Cuda(_) => {
#[cfg(feature = "cuda")]
{
// CUDA memory copy would go here
// cudaMemcpy equivalent
}
#[cfg(not(feature = "cuda"))]
{
return Err(RusTorchError::gpu("CUDA not supported"));
}
}
DeviceType::Metal(_) => {
#[cfg(feature = "metal")]
{
// Metal buffer copy would go here
}
#[cfg(not(feature = "metal"))]
{
return Err(RusTorchError::gpu("Metal not supported"));
}
}
DeviceType::OpenCL(_) => {
#[cfg(feature = "opencl")]
{
// OpenCL buffer copy would go here
}
#[cfg(not(feature = "opencl"))]
{
return Err(RusTorchError::gpu("OpenCL not supported"));
}
}
#[cfg(any(
feature = "coreml",
feature = "coreml-hybrid",
feature = "coreml-fallback"
))]
DeviceType::CoreML(_) => {
return Err(RusTorchError::UnsupportedDevice(
"CoreML data transfer not supported".to_string(),
));
}
DeviceType::Auto => {
return Err(RusTorchError::UnsupportedDevice(
"Auto data transfer not supported".to_string(),
));
}
#[cfg(feature = "mac-hybrid")]
DeviceType::MacHybrid => {
// Use CPU implementation for MacHybrid
unsafe {
let dst_ptr = dst_allocation.ptr as *mut T;
std::ptr::copy_nonoverlapping(src.as_ptr(), dst_ptr, src.len());
}
}
}
Ok(())
}
/// Copy data from device to host
/// デバイスからホストへデータをコピー
pub fn device_to_host<T: Copy>(
src_allocation: &MemoryAllocation,
dst: &mut [T],
) -> RusTorchResult<()> {
let dst_size = std::mem::size_of_val(dst);
if dst_size > src_allocation.size {
return Err(RusTorchError::tensor_op("Destination buffer too small"));
}
match src_allocation.device {
DeviceType::Cpu => {
// Direct memory copy for CPU
unsafe {
let src_ptr = src_allocation.ptr as *const T;
std::ptr::copy_nonoverlapping(src_ptr, dst.as_mut_ptr(), dst.len());
}
}
DeviceType::Cuda(_) => {
#[cfg(feature = "cuda")]
{
// CUDA memory copy would go here
// cudaMemcpy equivalent
}
#[cfg(not(feature = "cuda"))]
{
return Err(RusTorchError::gpu("CUDA not supported"));
}
}
DeviceType::Metal(_) => {
#[cfg(feature = "metal")]
{
// Metal buffer copy would go here
}
#[cfg(not(feature = "metal"))]
{
return Err(RusTorchError::gpu("Metal not supported"));
}
}
DeviceType::OpenCL(_) => {
#[cfg(feature = "opencl")]
{
// OpenCL buffer copy would go here
}
#[cfg(not(feature = "opencl"))]
{
return Err(RusTorchError::gpu("OpenCL not supported"));
}
}
#[cfg(any(
feature = "coreml",
feature = "coreml-hybrid",
feature = "coreml-fallback"
))]
DeviceType::CoreML(_) => {
return Err(RusTorchError::UnsupportedDevice(
"CoreML data transfer not supported".to_string(),
));
}
DeviceType::Auto => {
return Err(RusTorchError::UnsupportedDevice(
"Auto data transfer not supported".to_string(),
));
}
#[cfg(feature = "mac-hybrid")]
DeviceType::MacHybrid => {
// Use CPU implementation for MacHybrid
unsafe {
let src_ptr = src_allocation.ptr as *const T;
std::ptr::copy_nonoverlapping(src_ptr, dst.as_mut_ptr(), dst.len());
}
}
}
Ok(())
}
/// Copy data between devices
/// デバイス間でデータをコピー
pub fn device_to_device<T: Copy>(
src_allocation: &MemoryAllocation,
dst_allocation: &MemoryAllocation,
count: usize,
) -> RusTorchResult<()> {
let transfer_size = count * std::mem::size_of::<T>();
if transfer_size > src_allocation.size || transfer_size > dst_allocation.size {
return Err(RusTorchError::tensor_op("Transfer size too large"));
}
// For now, implement via host memory (not optimal but functional)
let mut temp_buffer = vec![unsafe { std::mem::zeroed::<T>() }; count];
Self::device_to_host(src_allocation, &mut temp_buffer)?;
Self::host_to_device(&temp_buffer, dst_allocation)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_memory_pool_creation() {
let pool = GpuMemoryPool::new(DeviceType::Cpu, 1024 * 1024).unwrap();
assert_eq!(pool.device(), DeviceType::Cpu);
let stats = pool.memory_stats();
assert_eq!(stats.total_size, 1024 * 1024);
assert_eq!(stats.allocated_size, 0);
assert_eq!(stats.free_size, 1024 * 1024);
assert_eq!(stats.utilization_ratio, 0.0);
}
#[test]
fn test_memory_allocation() {
let mut pool = GpuMemoryPool::new(DeviceType::Cpu, 1024 * 1024).unwrap();
let allocation = pool.allocate(1024).unwrap();
assert_eq!(allocation.device, DeviceType::Cpu);
assert_eq!(allocation.size, 1024); // Aligned to 256 bytes
let stats = pool.memory_stats();
assert_eq!(stats.allocated_size, 1024);
assert!(stats.utilization_ratio > 0.0);
}
#[test]
fn test_memory_deallocation() {
let mut pool = GpuMemoryPool::new(DeviceType::Cpu, 1024 * 1024).unwrap();
let allocation = pool.allocate(1024).unwrap();
let ptr = allocation.ptr;
pool.deallocate(ptr).unwrap();
let stats = pool.memory_stats();
assert_eq!(stats.allocated_size, 0);
assert_eq!(stats.utilization_ratio, 0.0);
}
#[test]
fn test_memory_manager() {
let mut manager = GpuMemoryManager::new(1024 * 1024);
let allocation = manager.allocate(DeviceType::Cpu, 1024).unwrap();
assert_eq!(allocation.device, DeviceType::Cpu);
manager.deallocate(&allocation).unwrap();
let stats = manager.memory_stats();
assert!(stats.contains_key(&DeviceType::Cpu));
}
#[test]
fn test_data_transfer() {
let mut pool = GpuMemoryPool::new(DeviceType::Cpu, 1024 * 1024).unwrap();
let allocation = pool.allocate(1024).unwrap();
let src_data = vec![1.0f32, 2.0, 3.0, 4.0];
DataTransfer::host_to_device(&src_data, &allocation).unwrap();
let mut dst_data = vec![0.0f32; 4];
DataTransfer::device_to_host(&allocation, &mut dst_data).unwrap();
assert_eq!(src_data, dst_data);
}
#[test]
fn test_block_merging() {
let mut pool = GpuMemoryPool::new(DeviceType::Cpu, 1024 * 1024).unwrap();
let alloc1 = pool.allocate(256).unwrap();
let alloc2 = pool.allocate(256).unwrap();
let alloc3 = pool.allocate(256).unwrap();
// Deallocate middle block first
pool.deallocate(alloc2.ptr).unwrap();
// Then deallocate adjacent blocks
pool.deallocate(alloc1.ptr).unwrap();
pool.deallocate(alloc3.ptr).unwrap();
// Should be able to allocate a large block again
let large_alloc = pool.allocate(768).unwrap();
assert!(large_alloc.size >= 768);
}
}