torsh-backend 0.1.2

Backend abstraction layer for ToRSh
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
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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
//! CPU Memory Management with NUMA awareness

// Framework infrastructure - components designed for future use
#![allow(dead_code)]
use crate::buffer::{generate_buffer_id, BufferHandle};
use crate::memory::{MemoryManager, MemoryPool, MemoryStats, PoolStats};
use crate::{Buffer, BufferDescriptor, Device};
use torsh_core::device::DeviceType;
use torsh_core::error::{Result, TorshError};

#[cfg(feature = "std")]
use std::collections::HashMap;
#[cfg(feature = "std")]
use std::sync::{Arc, Mutex};

#[cfg(not(feature = "std"))]
use alloc::{collections::BTreeMap as HashMap, sync::Arc};
#[cfg(not(feature = "std"))]
use spin::Mutex;

#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, vec::Vec};

use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Instant;

/// NUMA node information
#[derive(Debug, Clone)]
pub struct NumaNode {
    /// Node ID
    pub id: u32,
    /// Available memory in bytes
    pub available_memory: u64,
    /// Memory bandwidth in GB/s
    pub memory_bandwidth: f64,
    /// CPU cores associated with this node
    pub cpu_cores: Vec<u32>,
    /// Distance to other NUMA nodes
    pub distances: HashMap<u32, u32>,
}

/// NUMA topology information
#[derive(Debug, Clone)]
pub struct NumaTopology {
    /// Available NUMA nodes
    pub nodes: HashMap<u32, NumaNode>,
    /// Current thread's preferred NUMA node
    pub current_node: u32,
    /// Whether NUMA is available on this system
    pub numa_available: bool,
}

/// Memory access pattern tracking
#[derive(Debug)]
pub struct MemoryAccessPattern {
    /// Access frequency counter
    pub access_count: AtomicU64,
    /// Last access time
    pub last_access: Instant,
    /// Access pattern (sequential, random, etc.)
    pub pattern_type: AccessPatternType,
    /// Preferred NUMA node for this allocation
    pub preferred_node: u32,
}

impl Clone for MemoryAccessPattern {
    fn clone(&self) -> Self {
        Self {
            access_count: AtomicU64::new(self.access_count.load(Ordering::Relaxed)),
            last_access: self.last_access,
            pattern_type: self.pattern_type,
            preferred_node: self.preferred_node,
        }
    }
}

/// Types of memory access patterns
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AccessPatternType {
    Sequential,
    Random,
    Strided,
    Temporal,
    Unknown,
}

/// Memory allocation strategy
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NumaAllocationStrategy {
    /// Allocate on local NUMA node
    Local,
    /// Allocate on preferred node
    Preferred(u32),
    /// Interleave across all nodes
    Interleaved,
    /// Best fit based on available memory
    BestFit,
    /// Round-robin across nodes
    RoundRobin,
}

/// CPU memory manager implementation
#[derive(Debug, Clone)]
pub struct CpuMemoryManager {
    pools: Arc<Mutex<HashMap<usize, CpuMemoryPool>>>,
    stats: Arc<Mutex<MemoryStats>>,
    numa_topology: Arc<Mutex<NumaTopology>>,
    #[allow(dead_code)]
    numa_pools: Arc<Mutex<HashMap<(usize, u32), CpuMemoryPool>>>, // (size_class, numa_node) -> pool
    allocation_strategy: Arc<Mutex<NumaAllocationStrategy>>,
    access_patterns: Arc<Mutex<HashMap<usize, MemoryAccessPattern>>>,
    round_robin_counter: Arc<Mutex<u32>>,
}

impl CpuMemoryManager {
    /// Create a new CPU memory manager
    pub fn new() -> Self {
        let numa_topology = Self::detect_numa_topology();
        Self {
            pools: Arc::new(Mutex::new(HashMap::new())),
            stats: Arc::new(Mutex::new(MemoryStats::default())),
            numa_topology: Arc::new(Mutex::new(numa_topology)),
            numa_pools: Arc::new(Mutex::new(HashMap::new())),
            allocation_strategy: Arc::new(Mutex::new(NumaAllocationStrategy::Local)),
            access_patterns: Arc::new(Mutex::new(HashMap::new())),
            round_robin_counter: Arc::new(Mutex::new(0)),
        }
    }

    /// Detect NUMA topology of the system
    fn detect_numa_topology() -> NumaTopology {
        let mut numa_topology = NumaTopology {
            nodes: HashMap::new(),
            current_node: 0,
            numa_available: false,
        };

        // Try to detect NUMA nodes
        #[cfg(target_os = "linux")]
        {
            numa_topology.numa_available = Self::detect_linux_numa(&mut numa_topology);
        }

        #[cfg(not(target_os = "linux"))]
        {
            // Fallback: create a single node
            numa_topology.nodes.insert(
                0,
                NumaNode {
                    id: 0,
                    available_memory: 8 * 1024 * 1024 * 1024, // 8GB
                    memory_bandwidth: 50.0,
                    cpu_cores: (0..num_cpus::get() as u32).collect(),
                    distances: HashMap::new(),
                },
            );
        }

        numa_topology
    }

    /// Detect NUMA topology on Linux systems
    #[cfg(target_os = "linux")]
    fn detect_linux_numa(numa_topology: &mut NumaTopology) -> bool {
        use std::fs;
        use std::path::Path;

        let numa_path = Path::new("/sys/devices/system/node");
        if !numa_path.exists() {
            return false;
        }

        let mut numa_available = false;

        // Read NUMA nodes
        if let Ok(entries) = fs::read_dir(numa_path) {
            for entry in entries.flatten() {
                let name = entry.file_name();
                let name_str = name.to_string_lossy();

                if let Some(stripped) = name_str.strip_prefix("node") {
                    if let Ok(node_id) = stripped.parse::<u32>() {
                        numa_available = true;

                        // Get memory information
                        let meminfo_path = entry.path().join("meminfo");
                        let available_memory =
                            if let Ok(content) = fs::read_to_string(&meminfo_path) {
                                Self::parse_numa_meminfo(&content)
                            } else {
                                1024 * 1024 * 1024 // 1GB default
                            };

                        // Get CPU list
                        let cpulist_path = entry.path().join("cpulist");
                        let cpu_cores = if let Ok(content) = fs::read_to_string(&cpulist_path) {
                            Self::parse_cpu_list(&content)
                        } else {
                            Vec::new()
                        };

                        // Get distances
                        let distance_path = entry.path().join("distance");
                        let distances = if let Ok(content) = fs::read_to_string(&distance_path) {
                            Self::parse_numa_distances(&content)
                        } else {
                            HashMap::new()
                        };

                        numa_topology.nodes.insert(
                            node_id,
                            NumaNode {
                                id: node_id,
                                available_memory,
                                memory_bandwidth: 50.0, // Default bandwidth
                                cpu_cores,
                                distances,
                            },
                        );
                    }
                }
            }
        }

        // Set current node
        numa_topology.current_node = Self::get_current_numa_node();

        numa_available
    }

    /// Parse NUMA memory information
    #[cfg(target_os = "linux")]
    fn parse_numa_meminfo(content: &str) -> u64 {
        for line in content.lines() {
            if line.starts_with("Node") && line.contains("MemTotal:") {
                let parts: Vec<&str> = line.split_whitespace().collect();
                if parts.len() >= 3 {
                    if let Ok(kb) = parts[2].parse::<u64>() {
                        return kb * 1024; // Convert KB to bytes
                    }
                }
            }
        }
        1024 * 1024 * 1024 // 1GB default
    }

    /// Parse CPU list from NUMA node
    #[cfg(target_os = "linux")]
    fn parse_cpu_list(content: &str) -> Vec<u32> {
        let mut cpu_cores = Vec::new();
        let content = content.trim();

        for range in content.split(',') {
            if let Some(dash_pos) = range.find('-') {
                // Range format: "0-3"
                let start = range[..dash_pos].parse::<u32>().unwrap_or(0);
                let end = range[dash_pos + 1..].parse::<u32>().unwrap_or(0);
                for cpu in start..=end {
                    cpu_cores.push(cpu);
                }
            } else {
                // Single CPU
                if let Ok(cpu) = range.parse::<u32>() {
                    cpu_cores.push(cpu);
                }
            }
        }

        cpu_cores
    }

    /// Parse NUMA distance information
    #[cfg(target_os = "linux")]
    fn parse_numa_distances(content: &str) -> HashMap<u32, u32> {
        let mut distances = HashMap::new();
        let content = content.trim();

        for (node_id, distance_str) in content.split_whitespace().enumerate() {
            if let Ok(distance) = distance_str.parse::<u32>() {
                distances.insert(node_id as u32, distance);
            }
        }

        distances
    }

    /// Get current thread's NUMA node
    fn get_current_numa_node() -> u32 {
        #[cfg(target_os = "linux")]
        {
            // Try to get current CPU and map it to NUMA node
            if let Ok(cpu) = std::fs::read_to_string("/proc/self/stat") {
                // Parse the CPU field from /proc/self/stat
                let fields: Vec<&str> = cpu.split_whitespace().collect();
                if fields.len() > 38 {
                    if let Ok(cpu_id) = fields[38].parse::<u32>() {
                        return Self::cpu_to_numa_node(cpu_id);
                    }
                }
            }
        }

        0 // Default to node 0
    }

    /// Map CPU ID to NUMA node
    #[cfg(target_os = "linux")]
    fn cpu_to_numa_node(cpu_id: u32) -> u32 {
        use std::fs;
        use std::path::Path;

        let cpu_path_str = format!("/sys/devices/system/cpu/cpu{}/node", cpu_id);
        let cpu_path = Path::new(&cpu_path_str);
        if let Ok(content) = fs::read_to_string(cpu_path) {
            content.trim().parse::<u32>().unwrap_or(0)
        } else {
            0
        }
    }

    /// Set NUMA allocation strategy
    pub fn set_allocation_strategy(&self, strategy: NumaAllocationStrategy) -> Result<()> {
        let mut allocation_strategy = self.allocation_strategy.lock().map_err(|_| {
            TorshError::AllocationError("Failed to acquire allocation strategy lock".to_string())
        })?;
        *allocation_strategy = strategy;
        Ok(())
    }

    /// Get current allocation strategy
    pub fn get_allocation_strategy(&self) -> Result<NumaAllocationStrategy> {
        let allocation_strategy = self.allocation_strategy.lock().map_err(|_| {
            TorshError::AllocationError("Failed to acquire allocation strategy lock".to_string())
        })?;
        Ok(*allocation_strategy)
    }

    /// Select NUMA node for allocation based on strategy
    fn select_numa_node(&self, size: usize) -> Result<u32> {
        let strategy = self.get_allocation_strategy()?;
        let numa_topology = self.numa_topology.lock().map_err(|_| {
            TorshError::AllocationError("Failed to acquire NUMA topology lock".to_string())
        })?;

        if !numa_topology.numa_available {
            return Ok(0); // Default to node 0 if NUMA is not available
        }

        match strategy {
            NumaAllocationStrategy::Local => Ok(numa_topology.current_node),
            NumaAllocationStrategy::Preferred(node_id) => {
                if numa_topology.nodes.contains_key(&node_id) {
                    Ok(node_id)
                } else {
                    Ok(numa_topology.current_node)
                }
            }
            NumaAllocationStrategy::Interleaved => {
                // Select node based on address interleaving
                let node_count = numa_topology.nodes.len() as u32;
                Ok((size / 4096) as u32 % node_count) // Page-based interleaving
            }
            NumaAllocationStrategy::BestFit => {
                // Find node with most available memory
                let mut best_node = numa_topology.current_node;
                let mut best_memory = 0;

                for (node_id, node) in &numa_topology.nodes {
                    if node.available_memory > best_memory {
                        best_memory = node.available_memory;
                        best_node = *node_id;
                    }
                }

                Ok(best_node)
            }
            NumaAllocationStrategy::RoundRobin => {
                let mut counter = self.round_robin_counter.lock().map_err(|_| {
                    TorshError::AllocationError(
                        "Failed to acquire round-robin counter lock".to_string(),
                    )
                })?;

                let node_count = numa_topology.nodes.len() as u32;
                let selected_node = *counter % node_count;
                *counter = (*counter + 1) % node_count;

                Ok(selected_node)
            }
        }
    }

    /// Allocate memory on specific NUMA node
    fn allocate_on_numa_node(
        &self,
        size: usize,
        alignment: usize,
        numa_node: u32,
    ) -> Result<*mut u8> {
        #[cfg(target_os = "linux")]
        {
            // NUMA allocation is disabled to avoid linking dependencies
            // In a production version, this would check for libnuma availability
            // and use numa_alloc_onnode if available
            let _ = numa_node; // Acknowledge the parameter
        }

        // Fallback to regular allocation
        let layout = std::alloc::Layout::from_size_align(size, alignment)
            .map_err(|e| TorshError::AllocationError(format!("Invalid layout: {}", e)))?;

        let ptr = unsafe { std::alloc::alloc(layout) };
        if ptr.is_null() {
            return Err(TorshError::AllocationError(format!(
                "Failed to allocate {} bytes on NUMA node {}",
                size, numa_node
            )));
        }

        Ok(ptr)
    }

    /// Prefetch memory to improve access patterns
    pub fn prefetch_memory(
        &self,
        ptr: *mut u8,
        size: usize,
        pattern: AccessPatternType,
    ) -> Result<()> {
        match pattern {
            AccessPatternType::Sequential => {
                // Prefetch sequential pages
                self.prefetch_sequential(ptr, size)
            }
            AccessPatternType::Random => {
                // Prefetch random access patterns
                self.prefetch_random(ptr, size)
            }
            AccessPatternType::Strided => {
                // Prefetch strided access patterns
                self.prefetch_strided(ptr, size)
            }
            AccessPatternType::Temporal => {
                // Prefetch for temporal locality
                self.prefetch_temporal(ptr, size)
            }
            AccessPatternType::Unknown => {
                // Use default prefetching strategy
                self.prefetch_default(ptr, size)
            }
        }
    }

    /// Prefetch sequential memory pattern
    fn prefetch_sequential(&self, ptr: *mut u8, size: usize) -> Result<()> {
        let page_size = 4096;
        let pages = (size + page_size - 1) / page_size;

        for i in 0..pages {
            let page_ptr = unsafe { ptr.add(i * page_size) };
            self.prefetch_page(page_ptr);
        }

        Ok(())
    }

    /// Prefetch random memory pattern
    fn prefetch_random(&self, ptr: *mut u8, size: usize) -> Result<()> {
        // For random access, prefetch a few strategic pages
        let page_size = 4096;
        let pages = (size + page_size - 1) / page_size;

        // Prefetch first, middle, and last pages
        self.prefetch_page(ptr);
        if pages > 1 {
            let mid_ptr = unsafe { ptr.add((pages / 2) * page_size) };
            self.prefetch_page(mid_ptr);
        }
        if pages > 2 {
            let last_ptr = unsafe { ptr.add((pages - 1) * page_size) };
            self.prefetch_page(last_ptr);
        }

        Ok(())
    }

    /// Prefetch strided memory pattern
    fn prefetch_strided(&self, ptr: *mut u8, size: usize) -> Result<()> {
        let page_size = 4096;
        let stride = page_size * 2; // Every other page
        let mut offset = 0;

        while offset < size {
            let page_ptr = unsafe { ptr.add(offset) };
            self.prefetch_page(page_ptr);
            offset += stride;
        }

        Ok(())
    }

    /// Prefetch temporal memory pattern
    fn prefetch_temporal(&self, ptr: *mut u8, size: usize) -> Result<()> {
        // For temporal locality, prefetch the entire region
        self.prefetch_sequential(ptr, size)
    }

    /// Default prefetch strategy
    fn prefetch_default(&self, ptr: *mut u8, size: usize) -> Result<()> {
        // Use sequential prefetching as default
        self.prefetch_sequential(ptr, size)
    }

    /// Prefetch a single page
    fn prefetch_page(&self, _ptr: *mut u8) {
        #[cfg(target_arch = "x86_64")]
        {
            unsafe {
                // Use Intel prefetch instructions
                std::arch::x86_64::_mm_prefetch(_ptr as *const i8, std::arch::x86_64::_MM_HINT_T0);
            }
        }

        #[cfg(target_arch = "aarch64")]
        {
            // Prefetch instructions are currently unstable in Rust
            // TODO: Re-enable when stabilized (see issue #117217)
            // unsafe {
            //     std::arch::aarch64::_prefetch(
            //         ptr as *const i8,
            //         std::arch::aarch64::_PREFETCH_READ,
            //         std::arch::aarch64::_PREFETCH_LOCALITY3,
            //     );
            // }
        }

        #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
        {
            // No prefetch instruction available
            let _ = ptr;
        }
    }

    /// Track memory access pattern
    pub fn track_access(&self, ptr: *mut u8, access_type: AccessPatternType) -> Result<()> {
        let mut patterns = self.access_patterns.lock().map_err(|_| {
            TorshError::AllocationError("Failed to acquire access patterns lock".to_string())
        })?;

        if let Some(pattern) = patterns.get_mut(&(ptr as usize)) {
            pattern.access_count.fetch_add(1, Ordering::Relaxed);
            pattern.last_access = Instant::now();
            pattern.pattern_type = access_type;
        }

        Ok(())
    }

    /// Get memory access statistics
    pub fn get_access_stats(&self, ptr: *mut u8) -> Result<Option<MemoryAccessPattern>> {
        let patterns = self.access_patterns.lock().map_err(|_| {
            TorshError::AllocationError("Failed to acquire access patterns lock".to_string())
        })?;

        Ok(patterns.get(&(ptr as usize)).cloned())
    }

    /// Optimize memory layout based on access patterns
    pub fn optimize_layout(&self) -> Result<()> {
        let patterns = self.access_patterns.lock().map_err(|_| {
            TorshError::AllocationError("Failed to acquire access patterns lock".to_string())
        })?;

        // Analyze access patterns and suggest NUMA node migrations
        for (_ptr_val, pattern) in patterns.iter() {
            let access_count = pattern.access_count.load(Ordering::Relaxed);
            let current_node = Self::get_current_numa_node();

            // If access count is high and preferred node is different, consider migration
            if access_count > 1000 && pattern.preferred_node != current_node {
                // Log suggestion for migration (implementation would depend on specific needs)
                #[cfg(feature = "tracing")]
                tracing::info!(
                    "High access count {} for allocation {:?}, consider migrating to node {}",
                    access_count,
                    _ptr_val,
                    pattern.preferred_node
                );
            }
        }

        Ok(())
    }

    /// Create a new CPU memory manager with config
    pub fn with_config(config: crate::MemoryPoolConfig) -> Self {
        let numa_topology = Self::detect_numa_topology();
        let manager = Self {
            pools: Arc::new(Mutex::new(HashMap::new())),
            stats: Arc::new(Mutex::new(MemoryStats::default())),
            numa_topology: Arc::new(Mutex::new(numa_topology)),
            numa_pools: Arc::new(Mutex::new(HashMap::new())),
            allocation_strategy: Arc::new(Mutex::new(NumaAllocationStrategy::Local)),
            access_patterns: Arc::new(Mutex::new(HashMap::new())),
            round_robin_counter: Arc::new(Mutex::new(0)),
        };

        // Configure NUMA strategy based on config
        if let Some(strategy) = config.numa_strategy {
            let _ = manager.set_allocation_strategy(strategy);
        }

        manager
    }

    /// Calculate size class for a given size (power of 2 rounding)
    fn calculate_size_class(size: usize) -> usize {
        if size <= 64 {
            64
        } else {
            size.next_power_of_two()
        }
    }
}

impl MemoryManager for CpuMemoryManager {
    fn allocate(&mut self, descriptor: &BufferDescriptor) -> Result<Buffer> {
        let size = descriptor.size;
        let alignment = descriptor.alignment.unwrap_or(std::mem::align_of::<u8>());
        let size_class = Self::calculate_size_class(size);

        // Select NUMA node for allocation
        let numa_node = self.select_numa_node(size)?;

        // Try NUMA-aware allocation first
        let ptr = if let Ok(numa_ptr) = self.allocate_on_numa_node(size, alignment, numa_node) {
            // Track access pattern for NUMA-allocated memory
            let mut patterns = self.access_patterns.lock().map_err(|_| {
                TorshError::AllocationError("Failed to acquire access patterns lock".to_string())
            })?;

            let pattern = MemoryAccessPattern {
                access_count: AtomicU64::new(0),
                last_access: Instant::now(),
                pattern_type: AccessPatternType::Unknown,
                preferred_node: numa_node,
            };

            patterns.insert(numa_ptr as usize, pattern);
            numa_ptr
        } else {
            // Fallback to regular pool allocation
            {
                let mut pools = self.pools.lock().map_err(|_| {
                    TorshError::AllocationError("Failed to acquire pools lock".to_string())
                })?;

                pools
                    .entry(size_class)
                    .or_insert_with(|| CpuMemoryPool::new(size_class));
            }

            let mut pools = self.pools.lock().map_err(|_| {
                TorshError::AllocationError("Failed to acquire pools lock".to_string())
            })?;

            let pool = pools
                .get_mut(&size_class)
                .expect("size_class should exist in pools");
            pool.allocate(size, alignment)
                .map_err(|e| TorshError::AllocationError(e.to_string()))?
        };

        // Update statistics
        let mut stats = self
            .stats
            .lock()
            .map_err(|_| TorshError::AllocationError("Failed to acquire stats lock".to_string()))?;
        stats.allocated_memory += size;
        stats.peak_memory = stats.peak_memory.max(stats.allocated_memory);
        stats.total_allocations += 1;
        stats.active_allocations += 1;

        Ok(Buffer::new(
            generate_buffer_id(),
            self.device().clone(),
            size,
            descriptor.usage,
            descriptor.clone(),
            BufferHandle::Cpu { ptr, size },
        ))
    }

    fn deallocate(&mut self, buffer: &Buffer) -> Result<()> {
        let size_class = Self::calculate_size_class(buffer.size);

        // Extract pointer from buffer handle
        let ptr = match &buffer.handle {
            BufferHandle::Cpu { ptr, .. } => *ptr,
            _ => {
                return Err(TorshError::AllocationError(
                    "Invalid buffer handle type for CPU backend".to_string(),
                ))
            }
        };

        // Deallocate from the pool
        {
            let mut pools = self.pools.lock().map_err(|_| {
                TorshError::AllocationError("Failed to acquire pools lock".to_string())
            })?;

            if let Some(pool) = pools.get_mut(&size_class) {
                pool.deallocate(ptr, buffer.size)
                    .map_err(|e| TorshError::AllocationError(e.to_string()))?;
            } else {
                return Err(TorshError::InvalidArgument(
                    "No pool found for deallocating buffer".to_string(),
                ));
            }
        }

        // Update statistics
        let mut stats = self
            .stats
            .lock()
            .map_err(|_| TorshError::AllocationError("Failed to acquire stats lock".to_string()))?;
        stats.allocated_memory = stats.allocated_memory.saturating_sub(buffer.size);
        stats.total_deallocations += 1;
        stats.active_allocations = stats.active_allocations.saturating_sub(1);

        Ok(())
    }

    fn stats(&self) -> MemoryStats {
        self.stats
            .lock()
            .expect("lock should not be poisoned")
            .clone()
    }

    fn garbage_collect(&mut self) -> Result<usize> {
        // For CPU memory, we don't need explicit garbage collection
        Ok(0)
    }

    fn set_pool(&mut self, _pool: Box<dyn MemoryPool>) -> Result<()> {
        // Simple implementation - we manage our own pools
        Ok(())
    }

    fn device(&self) -> &Device {
        use crate::device::DeviceInfo;
        use std::sync::OnceLock;
        static CPU_DEVICE: OnceLock<Device> = OnceLock::new();
        CPU_DEVICE.get_or_init(|| Device {
            id: 0,
            device_type: DeviceType::Cpu,
            name: "CPU".to_string(),
            info: DeviceInfo {
                vendor: "CPU".to_string(),
                driver_version: "CPU Backend 1.0".to_string(),
                total_memory: 8 * 1024 * 1024 * 1024, // 8GB typical
                available_memory: 8 * 1024 * 1024 * 1024,
                compute_units: 1,
                max_work_group_size: u32::MAX as usize,
                max_work_group_dimensions: vec![u32::MAX as usize, 1, 1],
                clock_frequency_mhz: 3000, // 3GHz typical
                memory_bandwidth_gbps: 50.0,
                peak_gflops: 100.0,
                features: Vec::new(),
                properties: Vec::new(),
            },
        })
    }

    fn allocate_raw(&mut self, size: usize, alignment: usize) -> Result<*mut u8> {
        let size_class = Self::calculate_size_class(size);

        // Select NUMA node for allocation
        let numa_node = self.select_numa_node(size)?;

        // Try NUMA-aware allocation first
        if let Ok(numa_ptr) = self.allocate_on_numa_node(size, alignment, numa_node) {
            // Track access pattern for NUMA-allocated memory
            let mut patterns = self.access_patterns.lock().map_err(|_| {
                TorshError::AllocationError("Failed to acquire access patterns lock".to_string())
            })?;

            let pattern = MemoryAccessPattern {
                access_count: AtomicU64::new(0),
                last_access: Instant::now(),
                pattern_type: AccessPatternType::Unknown,
                preferred_node: numa_node,
            };

            patterns.insert(numa_ptr as usize, pattern);
            return Ok(numa_ptr);
        }

        // Fallback to regular pool allocation
        {
            let mut pools = self.pools.lock().map_err(|_| {
                TorshError::AllocationError("Failed to acquire pools lock".to_string())
            })?;

            pools
                .entry(size_class)
                .or_insert_with(|| CpuMemoryPool::new(size_class));
        }

        let mut pools = self
            .pools
            .lock()
            .map_err(|_| TorshError::AllocationError("Failed to acquire pools lock".to_string()))?;

        let pool = pools
            .get_mut(&size_class)
            .expect("size_class should exist in pools");
        pool.allocate(size, alignment)
            .map_err(|e| TorshError::AllocationError(e.to_string()))
    }

    fn deallocate_raw(&mut self, ptr: *mut u8, size: usize) -> Result<()> {
        let size_class = Self::calculate_size_class(size);

        // Clean up access pattern tracking
        {
            let mut patterns = self.access_patterns.lock().map_err(|_| {
                TorshError::AllocationError("Failed to acquire access patterns lock".to_string())
            })?;
            patterns.remove(&(ptr as usize));
        }

        let mut pools = self
            .pools
            .lock()
            .map_err(|_| TorshError::AllocationError("Failed to acquire pools lock".to_string()))?;

        if let Some(pool) = pools.get_mut(&size_class) {
            pool.deallocate(ptr, size)
                .map_err(|e| TorshError::AllocationError(e.to_string()))
        } else {
            // If no pool found, this might be NUMA-allocated memory
            // Use std::alloc::dealloc as fallback (same as allocate_on_numa_node)
            let alignment = if size <= 8 { 8 } else { 16 }; // Default alignment
            if let Ok(layout) = std::alloc::Layout::from_size_align(size, alignment) {
                unsafe {
                    std::alloc::dealloc(ptr, layout);
                }
                Ok(())
            } else {
                Err(TorshError::InvalidArgument(
                    "Invalid layout for deallocating memory".to_string(),
                ))
            }
        }
    }

    fn supports_unified_memory(&self) -> bool {
        // CPU memory is essentially "unified" since it's all system memory
        true
    }

    fn allocate_unified(&mut self, size: usize) -> Result<*mut u8> {
        // For CPU, unified memory is just regular allocation
        self.allocate_raw(size, std::mem::align_of::<u8>())
    }

    fn deallocate_unified(&mut self, ptr: *mut u8, size: usize) -> Result<()> {
        // For CPU, unified memory deallocation is just regular deallocation
        self.deallocate_raw(ptr, size)
    }

    fn prefetch_to_device(&self, _ptr: *mut u8, _size: usize) -> Result<()> {
        // No-op for CPU since all memory is already on the "device"
        Ok(())
    }

    fn prefetch_to_host(&self, _ptr: *mut u8, _size: usize) -> Result<()> {
        // No-op for CPU since all memory is already on the host
        Ok(())
    }

    fn set_memory_advice(
        &self,
        _ptr: *mut u8,
        _size: usize,
        _advice: crate::memory::MemoryAdvice,
    ) -> Result<()> {
        // No-op for CPU since memory advice doesn't apply
        Ok(())
    }

    fn available_memory(&self) -> Result<usize> {
        // Get system memory info
        #[cfg(target_os = "linux")]
        {
            use std::fs;
            if let Ok(meminfo) = fs::read_to_string("/proc/meminfo") {
                for line in meminfo.lines() {
                    if line.starts_with("MemAvailable:") {
                        if let Some(kb_str) = line.split_whitespace().nth(1) {
                            if let Ok(kb) = kb_str.parse::<usize>() {
                                return Ok(kb * 1024); // Convert KB to bytes
                            }
                        }
                    }
                }
            }
        }

        // Fallback estimate
        Ok(4 * 1024 * 1024 * 1024) // 4GB
    }

    fn total_memory(&self) -> Result<usize> {
        // Get total system memory
        #[cfg(target_os = "linux")]
        {
            use std::fs;
            if let Ok(meminfo) = fs::read_to_string("/proc/meminfo") {
                for line in meminfo.lines() {
                    if line.starts_with("MemTotal:") {
                        if let Some(kb_str) = line.split_whitespace().nth(1) {
                            if let Ok(kb) = kb_str.parse::<usize>() {
                                return Ok(kb * 1024); // Convert KB to bytes
                            }
                        }
                    }
                }
            }
        }

        // Fallback estimate
        Ok(8 * 1024 * 1024 * 1024) // 8GB
    }

    fn synchronize(&self) -> Result<()> {
        // CPU operations are synchronous, so nothing to do
        Ok(())
    }

    fn defragment(&mut self) -> Result<crate::memory::DefragmentationResult> {
        // Simple stub implementation for CPU
        Ok(crate::memory::DefragmentationResult {
            blocks_moved: 0,
            memory_compacted: 0,
            duration_ms: 0.0,
            fragmentation_before: 0.0,
            fragmentation_after: 0.0,
            efficiency_improvement: 0.0,
            success: true,
        })
    }

    fn needs_defragmentation(&self) -> bool {
        // CPU memory pools typically don't need defragmentation
        false
    }

    fn fragmentation_info(&self) -> crate::memory::FragmentationInfo {
        // Simple implementation returning basic fragmentation info
        crate::memory::FragmentationInfo {
            overall_fragmentation: 0.1,
            external_fragmentation: 0.05,
            internal_fragmentation: 0.05,
            free_blocks: 1,
            allocated_blocks: 0,
            largest_free_block: 1024 * 1024,
            smallest_free_block: 1024,
            average_free_block: 512 * 1024,
            total_free_memory: 1024 * 1024,
            total_allocated_memory: 0,
            utilization_efficiency: 0.9,
            allocation_efficiency: 0.9,
        }
    }

    fn compact_memory(&mut self) -> Result<crate::memory::CompactionResult> {
        // Simple stub implementation for CPU
        Ok(crate::memory::CompactionResult {
            allocations_moved: 0,
            bytes_moved: 0,
            duration_ms: 0.0,
            largest_free_before: 1024 * 1024,
            largest_free_after: 1024 * 1024,
            free_blocks_before: 1,
            free_blocks_after: 1,
            success: true,
        })
    }

    fn set_defragmentation_policy(&mut self, _policy: crate::memory::DefragmentationPolicy) {
        // No-op for CPU since it doesn't need complex defragmentation
    }
}

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

/// CPU memory manager factory
pub struct CpuMemoryManagerFactory;

impl crate::memory::MemoryManagerFactory for CpuMemoryManagerFactory {
    fn create_manager(&self, _device: &Device) -> Result<Box<dyn crate::memory::MemoryManager>> {
        Ok(Box::new(CpuMemoryManager::new()))
    }

    fn backend_type(&self) -> crate::BackendType {
        crate::BackendType::Cpu
    }

    fn supports_device(&self, device: &Device) -> bool {
        device.device_type == DeviceType::Cpu
    }
}

/// CPU memory pool implementation
#[derive(Debug)]
pub struct CpuMemoryPool {
    size_class: usize,
    #[allow(clippy::arc_with_non_send_sync)]
    free_blocks: Arc<Mutex<Vec<*mut u8>>>,
    #[allow(clippy::arc_with_non_send_sync)]
    allocated_blocks: Arc<Mutex<HashMap<*mut u8, (usize, usize)>>>, // (size, alignment)
}

// SAFETY: We ensure thread safety by using Mutex and proper synchronization
unsafe impl Send for CpuMemoryPool {}
unsafe impl Sync for CpuMemoryPool {}

impl Clone for CpuMemoryPool {
    fn clone(&self) -> Self {
        Self {
            size_class: self.size_class,
            free_blocks: Arc::clone(&self.free_blocks),
            allocated_blocks: Arc::clone(&self.allocated_blocks),
        }
    }
}

impl CpuMemoryPool {
    /// Create a new CPU memory pool
    pub fn new(size_class: usize) -> Self {
        #[allow(clippy::arc_with_non_send_sync)]
        let free_blocks = Arc::new(Mutex::new(Vec::new()));
        #[allow(clippy::arc_with_non_send_sync)]
        let allocated_blocks = Arc::new(Mutex::new(HashMap::new()));

        Self {
            size_class,
            free_blocks,
            allocated_blocks,
        }
    }
}

impl MemoryPool for CpuMemoryPool {
    fn allocate(&mut self, size: usize, alignment: usize) -> Result<*mut u8> {
        let mut free_blocks = self.free_blocks.lock().map_err(|_| {
            TorshError::AllocationError("Failed to acquire free_blocks lock".to_string())
        })?;

        // Try to reuse a free block
        if let Some(ptr) = free_blocks.pop() {
            let mut allocated_blocks = self.allocated_blocks.lock().map_err(|_| {
                TorshError::AllocationError("Failed to acquire allocated_blocks lock".to_string())
            })?;
            allocated_blocks.insert(ptr, (size, alignment));
            return Ok(ptr);
        }

        // Allocate new block
        let layout = std::alloc::Layout::from_size_align(self.size_class, alignment)
            .map_err(|e| TorshError::AllocationError(format!("Invalid layout: {}", e)))?;

        let ptr = unsafe { std::alloc::alloc(layout) };

        if ptr.is_null() {
            return Err(TorshError::AllocationError(format!(
                "Failed to allocate {} bytes",
                size
            )));
        }

        let mut allocated_blocks = self.allocated_blocks.lock().map_err(|_| {
            TorshError::AllocationError("Failed to acquire allocated_blocks lock".to_string())
        })?;
        allocated_blocks.insert(ptr, (size, alignment));

        Ok(ptr)
    }

    fn deallocate(&mut self, ptr: *mut u8, _size: usize) -> Result<()> {
        let mut allocated_blocks = self.allocated_blocks.lock().map_err(|_| {
            TorshError::AllocationError("Failed to acquire allocated_blocks lock".to_string())
        })?;

        if allocated_blocks.remove(&ptr).is_some() {
            let mut free_blocks = self.free_blocks.lock().map_err(|_| {
                TorshError::AllocationError("Failed to acquire free_blocks lock".to_string())
            })?;

            // Add to free list for reuse
            free_blocks.push(ptr);
            Ok(())
        } else {
            Err(TorshError::InvalidArgument(
                "Attempted to deallocate unknown pointer".to_string(),
            ))
        }
    }

    fn stats(&self) -> PoolStats {
        let allocated_blocks = self
            .allocated_blocks
            .lock()
            .unwrap_or_else(|_| panic!("Lock poisoned"));
        let free_blocks = self
            .free_blocks
            .lock()
            .unwrap_or_else(|_| panic!("Lock poisoned"));

        let allocated_bytes: usize = allocated_blocks.values().map(|(size, _)| *size).sum();
        let total_capacity = (allocated_blocks.len() + free_blocks.len()) * self.size_class;

        PoolStats {
            capacity: total_capacity,
            allocated: allocated_bytes,
            available: total_capacity - allocated_bytes,
            free_blocks: free_blocks.len(),
            allocated_blocks: allocated_blocks.len(),
            largest_free_block: if free_blocks.is_empty() {
                0
            } else {
                self.size_class
            },
            smallest_free_block: if free_blocks.is_empty() {
                0
            } else {
                self.size_class
            },
            average_free_block: if free_blocks.is_empty() {
                0
            } else {
                self.size_class
            },
        }
    }

    fn reset(&mut self) -> Result<()> {
        let mut allocated_blocks = self.allocated_blocks.lock().map_err(|_| {
            TorshError::AllocationError("Failed to acquire allocated_blocks lock".to_string())
        })?;
        let mut free_blocks = self.free_blocks.lock().map_err(|_| {
            TorshError::AllocationError("Failed to acquire free_blocks lock".to_string())
        })?;

        // Deallocate all blocks
        for (&ptr, &(size, alignment)) in allocated_blocks.iter() {
            let layout = std::alloc::Layout::from_size_align(size, alignment)
                .expect("layout should be valid for previously allocated block");
            unsafe {
                std::alloc::dealloc(ptr, layout);
            }
        }

        for &ptr in free_blocks.iter() {
            let layout = std::alloc::Layout::from_size_align(self.size_class, 1)
                .expect("layout should be valid for size_class");
            unsafe {
                std::alloc::dealloc(ptr, layout);
            }
        }

        allocated_blocks.clear();
        free_blocks.clear();

        Ok(())
    }

    fn capacity(&self) -> usize {
        let allocated_blocks = self
            .allocated_blocks
            .lock()
            .unwrap_or_else(|_| panic!("Lock poisoned"));
        let free_blocks = self
            .free_blocks
            .lock()
            .unwrap_or_else(|_| panic!("Lock poisoned"));
        (allocated_blocks.len() + free_blocks.len()) * self.size_class
    }

    fn available(&self) -> usize {
        let free_blocks = self
            .free_blocks
            .lock()
            .unwrap_or_else(|_| panic!("Lock poisoned"));
        free_blocks.len() * self.size_class
    }

    fn defragment(&mut self) -> Result<crate::memory::DefragmentationResult> {
        // Simple stub implementation for CPU memory pool
        Ok(crate::memory::DefragmentationResult {
            blocks_moved: 0,
            memory_compacted: 0,
            duration_ms: 0.0,
            fragmentation_before: 0.0,
            fragmentation_after: 0.0,
            efficiency_improvement: 0.0,
            success: true,
        })
    }

    fn needs_defragmentation(&self) -> bool {
        // CPU memory pools typically don't need defragmentation
        false
    }

    fn fragmentation_info(&self) -> crate::memory::FragmentationInfo {
        let allocated_count = self
            .allocated_blocks
            .lock()
            .expect("lock should not be poisoned")
            .len();
        let free_count = self
            .free_blocks
            .lock()
            .expect("lock should not be poisoned")
            .len();

        crate::memory::FragmentationInfo {
            overall_fragmentation: 0.1,
            external_fragmentation: 0.05,
            internal_fragmentation: 0.05,
            free_blocks: free_count,
            allocated_blocks: allocated_count,
            largest_free_block: self.size_class,
            smallest_free_block: if free_count > 0 { self.size_class } else { 0 },
            average_free_block: self.size_class,
            total_free_memory: free_count * self.size_class,
            total_allocated_memory: allocated_count * self.size_class,
            utilization_efficiency: 0.9,
            allocation_efficiency: 0.9,
        }
    }

    fn compact(&mut self) -> Result<crate::memory::CompactionResult> {
        // Simple stub implementation for CPU memory pool
        Ok(crate::memory::CompactionResult {
            allocations_moved: 0,
            bytes_moved: 0,
            duration_ms: 0.0,
            largest_free_before: self.size_class,
            largest_free_after: self.size_class,
            free_blocks_before: self
                .free_blocks
                .lock()
                .expect("lock should not be poisoned")
                .len(),
            free_blocks_after: self
                .free_blocks
                .lock()
                .expect("lock should not be poisoned")
                .len(),
            success: true,
        })
    }
}

// Implement Drop for CpuMemoryPool to clean up allocated memory
impl Drop for CpuMemoryPool {
    fn drop(&mut self) {
        // Clean up any remaining allocated blocks (still in use)
        if let Ok(allocated_blocks) = self.allocated_blocks.lock() {
            for (&ptr, &(size, alignment)) in allocated_blocks.iter() {
                let layout = std::alloc::Layout::from_size_align(size, alignment)
                    .expect("layout should be valid for previously allocated block");
                unsafe {
                    std::alloc::dealloc(ptr, layout);
                }
            }
        }

        // Clean up free blocks (available for reuse)
        if let Ok(free_blocks) = self.free_blocks.lock() {
            for &ptr in free_blocks.iter() {
                // Use consistent alignment for size_class allocations
                let layout = std::alloc::Layout::from_size_align(self.size_class, 8)
                    .expect("layout should be valid for size_class");
                unsafe {
                    std::alloc::dealloc(ptr, layout);
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    // Removed unused import

    #[test]
    fn test_size_class_calculation() {
        assert_eq!(CpuMemoryManager::calculate_size_class(32), 64);
        assert_eq!(CpuMemoryManager::calculate_size_class(64), 64);
        assert_eq!(CpuMemoryManager::calculate_size_class(65), 128);
        assert_eq!(CpuMemoryManager::calculate_size_class(1000), 1024);
        assert_eq!(CpuMemoryManager::calculate_size_class(2048), 2048);
    }

    #[test]
    fn test_memory_manager_creation() {
        let manager = CpuMemoryManager::new();
        let stats = manager.stats();
        assert_eq!(stats.allocated_memory, 0);
    }

    #[test]
    fn test_unified_memory_allocation() {
        let mut manager = CpuMemoryManager::new();

        // Test unified memory support
        assert!(manager.supports_unified_memory());

        // Test unified memory allocation
        let ptr = manager
            .allocate_unified(1024)
            .expect("unified memory allocation should succeed");
        assert!(!ptr.is_null());

        // Test deallocation
        let result = manager.deallocate_unified(ptr, 1024);
        assert!(result.is_ok());
    }

    #[test]
    fn test_raw_memory_allocation() {
        let mut manager = CpuMemoryManager::new();

        // Test raw memory allocation with alignment
        let ptr = manager
            .allocate_raw(256, 16)
            .expect("raw memory allocation should succeed");
        assert!(!ptr.is_null());

        // Check alignment
        assert_eq!(ptr as usize % 16, 0);

        // Test deallocation
        let result = manager.deallocate_raw(ptr, 256);
        assert!(result.is_ok());
    }

    #[test]
    fn test_memory_info_queries() {
        let manager = CpuMemoryManager::new();

        // Test memory queries
        let total = manager
            .total_memory()
            .expect("total memory query should succeed");
        let available = manager
            .available_memory()
            .expect("available memory query should succeed");

        assert!(total > 0);
        assert!(available > 0);
        assert!(available <= total);
    }

    #[test]
    fn test_memory_operations() {
        let mut manager = CpuMemoryManager::new();

        // Test prefetch operations (should be no-ops for CPU)
        let ptr = manager
            .allocate_raw(64, 8)
            .expect("raw memory allocation should succeed");

        assert!(manager.prefetch_to_device(ptr, 64).is_ok());
        assert!(manager.prefetch_to_host(ptr, 64).is_ok());

        // Test memory advice (should be no-op for CPU)
        assert!(manager
            .set_memory_advice(ptr, 64, crate::memory::MemoryAdvice::SetReadMostly)
            .is_ok());

        // Test synchronization (should be no-op for CPU)
        assert!(manager.synchronize().is_ok());

        // Cleanup
        manager
            .deallocate_raw(ptr, 64)
            .expect("raw memory deallocation should succeed");
    }

    #[test]
    fn test_memory_manager_factory() {
        use crate::device::{Device, DeviceInfo};
        use crate::memory::MemoryManagerFactory;

        let factory = CpuMemoryManagerFactory;
        let device = Device::new(
            0,
            DeviceType::Cpu,
            "Test CPU".to_string(),
            DeviceInfo::default(),
        );

        // Test device support
        assert!(factory.supports_device(&device));
        assert_eq!(factory.backend_type(), crate::BackendType::Cpu);

        // Test manager creation
        let manager = factory.create_manager(&device);
        assert!(manager.is_ok());

        let manager = manager.expect("operation should succeed");
        assert!(manager.supports_unified_memory());
    }
}