optirs-gpu 0.3.2

OptiRS GPU acceleration and multi-GPU optimization
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
// ROCm backend for GPU memory management
//
// This module provides AMD ROCm/HIP-specific memory management functionality,
// including device memory allocation, HIP streams, and performance optimization
// features specific to AMD GPUs.
//
// # This is a host-memory simulation, not real ROCm
//
// `optirs-gpu` is Pure Rust with no FFI dependencies by default, and this
// crate ships no ROCm/HIP runtime bindings. There is therefore no real
// `hipMalloc` underneath this module: "device", "host", "coarse-grained",
// "fine-grained" and "host-visible" memory are all the *same* system-heap
// allocation (see `sim_alloc`/`sim_dealloc` below), and `RocmDeviceProperties`
// /`RocmStats` are example numbers, not a query of real hardware. This
// module models the ROCm memory-management *API shape* for testing that
// shape in isolation; treat every allocation as host memory and every
// device number as illustrative.
//
// This extends to data movement: `memcpy` and `memcpy_async` copy **zero
// bytes**. They build a `HipOperation` record, hand it to
// `HipStreamManager::execute_operation` (which returns immediately and never
// dereferences `src_ptr`/`dst_ptr`), and increment
// `RocmStats::memory_transfers` — that counter says "this many `memcpy`
// calls were made," not "this many bytes moved." An earlier revision of
// this module also injected a `std::thread::sleep` here to imitate transfer
// latency by operation kind; that fake timing has been removed, so the
// distinction between `MemcpyHostToDevice`/`MemcpyDeviceToHost`/
// `MemcpyDeviceToDevice`/`MemcpyAsync` no longer affects anything
// observable. `RocmStats::stream_operations` and `RocmStats::kernel_launches`
// are declared for API-shape completeness but nothing in this module ever
// increments them — read a `0` there as "not tracked," not "none occurred."

use std::collections::HashMap;
use std::ffi::c_void;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

/// Byte alignment every simulated allocation below uses.
const SIM_ALLOC_ALIGN: usize = 256;

/// Allocate `size` bytes through the system allocator, 256-byte aligned,
/// without the two ways the naive `std::alloc::alloc(Layout::from_size_align_unchecked(size,
/// 256))` this module used to call was undefined behaviour: a zero-size
/// layout is unsound to pass to `GlobalAlloc::alloc`, and a real allocation
/// failure returns null, which must never be treated as valid memory. See
/// `cuda_backend::sim_alloc` for the full rationale (this mirrors it).
///
/// The payload is prefixed with one `SIM_ALLOC_ALIGN`-byte header recording
/// the requested size, so [`sim_dealloc`] can reconstruct the exact `Layout`
/// this function used — the `Layout::from_size_align_unchecked(1, 1)` this
/// module used at free time was a mismatched-layout deallocation, itself
/// unconditionally undefined behaviour.
fn sim_alloc(size: usize) -> Result<*mut c_void, RocmError> {
    if size == 0 {
        return Ok(SIM_ALLOC_ALIGN as *mut c_void);
    }
    let total = SIM_ALLOC_ALIGN.checked_add(size).ok_or_else(|| {
        RocmError::OutOfMemory(format!(
            "{size}-byte request overflows the allocator's size limit"
        ))
    })?;
    let layout = std::alloc::Layout::from_size_align(total, SIM_ALLOC_ALIGN)
        .map_err(|e| RocmError::OutOfMemory(format!("invalid allocation layout: {e}")))?;
    // SAFETY: `layout` has non-zero size (checked above) and a valid
    // (power-of-two) alignment constructed by `Layout::from_size_align`.
    let base = unsafe { std::alloc::alloc(layout) };
    if base.is_null() {
        return Err(RocmError::OutOfMemory(format!(
            "allocator returned null for a {size}-byte request"
        )));
    }
    // SAFETY: `base` is non-null and `layout`'s size is at least
    // `SIM_ALLOC_ALIGN + size >= SIM_ALLOC_ALIGN >= size_of::<usize>()`, so
    // writing one `usize` at the start of the block is in-bounds.
    unsafe { (base as *mut usize).write(size) };
    // SAFETY: `base` was allocated with `total = SIM_ALLOC_ALIGN + size`
    // bytes, so offsetting by `SIM_ALLOC_ALIGN` stays within (or one past)
    // the allocation.
    Ok(unsafe { base.add(SIM_ALLOC_ALIGN) } as *mut c_void)
}

/// Free a pointer returned by [`sim_alloc`]. A no-op for a null pointer or
/// the zero-size sentinel — neither was ever allocated.
///
/// Only ever called from this module (it is not `pub`) with a pointer
/// `sim_alloc` returned that has not already been freed — the unsafety of
/// the pointer arithmetic below is contained to that invariant, matching
/// this module's existing style of confining `unsafe` to the raw
/// `std::alloc` calls rather than marking `free()`'s public wrapper unsafe.
fn sim_dealloc(ptr: *mut c_void) {
    if ptr.is_null() || (ptr as usize) == SIM_ALLOC_ALIGN {
        return;
    }
    // SAFETY: by this function's contract `ptr` came from `sim_alloc`.
    let base = unsafe { (ptr as *mut u8).sub(SIM_ALLOC_ALIGN) };
    // SAFETY: `sim_alloc` wrote a `usize` at `base` before returning.
    let size = unsafe { (base as *const usize).read() };
    if let Ok(layout) = std::alloc::Layout::from_size_align(SIM_ALLOC_ALIGN + size, SIM_ALLOC_ALIGN)
    {
        // SAFETY: `layout` is exactly the layout `sim_alloc` allocated
        // `base` with.
        unsafe { std::alloc::dealloc(base, layout) };
    }
}

/// ROCm memory backend implementation
pub struct RocmMemoryBackend {
    /// Backend configuration
    config: RocmConfig,
    /// Device properties
    device_properties: RocmDeviceProperties,
    /// Active HIP contexts
    contexts: HashMap<u32, HipContext>,
    /// Memory pools
    memory_pools: HashMap<RocmMemoryType, RocmMemoryPool>,
    /// Statistics
    stats: RocmStats,
    /// Stream management
    stream_manager: HipStreamManager,
}

/// ROCm backend configuration
#[derive(Debug, Clone)]
pub struct RocmConfig {
    /// Device ID to use
    pub device_id: u32,
    /// Enable coarse-grained memory
    pub enable_coarse_memory: bool,
    /// Enable fine-grained memory
    pub enable_fine_memory: bool,
    /// Enable memory pools
    pub enable_memory_pools: bool,
    /// Enable async memory operations
    pub enable_async_ops: bool,
    /// Memory pool growth size
    pub pool_growth_size: usize,
    /// Enable host-visible device memory
    pub enable_host_visible: bool,
    /// Enable device coherent memory
    pub enable_device_coherent: bool,
    /// Maximum number of streams
    pub max_streams: u32,
    /// Enable GPU memory profiling
    pub enable_profiling: bool,
}

impl Default for RocmConfig {
    fn default() -> Self {
        Self {
            device_id: 0,
            enable_coarse_memory: true,
            enable_fine_memory: true,
            enable_memory_pools: true,
            enable_async_ops: true,
            pool_growth_size: 64 * 1024 * 1024, // 64MB
            enable_host_visible: true,
            enable_device_coherent: false,
            max_streams: 16,
            enable_profiling: false,
        }
    }
}

/// ROCm device properties
#[derive(Debug, Clone)]
pub struct RocmDeviceProperties {
    pub device_id: u32,
    pub name: String,
    pub arch: String,
    pub gcn_arch_name: String,
    pub total_global_memory: usize,
    pub local_memory_size: usize,
    pub max_work_group_size: u32,
    pub max_work_item_dimensions: u32,
    pub max_work_item_sizes: [u32; 3],
    pub compute_units: u32,
    pub wavefront_size: u32,
    pub memory_clock_frequency: u32,
    pub memory_bus_width: u32,
    pub l2_cache_size: usize,
    pub max_constant_buffer_size: usize,
    pub pci_bus_id: u32,
    pub pci_device_id: u32,
    pub supports_cooperative_launch: bool,
    pub supports_dynamic_parallelism: bool,
}

/// ROCm memory types
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum RocmMemoryType {
    Device,
    Host,
    HostVisible,
    DeviceCoherent,
    CoarseGrained,
    FineGrained,
}

/// HIP context for managing device state
pub struct HipContext {
    /// Context handle (simulated)
    pub handle: *mut c_void,
    /// Device ID
    pub device_id: u32,
    /// Context flags
    pub flags: HipContextFlags,
    /// Creation time
    pub created_at: Instant,
    /// Active streams
    pub streams: Vec<HipStream>,
    /// Device memory info
    pub memory_info: HipMemoryInfo,
}

/// HIP context creation flags
#[derive(Debug, Clone)]
pub struct HipContextFlags {
    pub sched_auto: bool,
    pub sched_spin: bool,
    pub sched_yield: bool,
    pub sched_blocking_sync: bool,
    pub map_host: bool,
}

impl Default for HipContextFlags {
    fn default() -> Self {
        Self {
            sched_auto: true,
            sched_spin: false,
            sched_yield: false,
            sched_blocking_sync: false,
            map_host: false,
        }
    }
}

/// HIP memory information
#[derive(Debug, Clone)]
pub struct HipMemoryInfo {
    pub total_memory: usize,
    pub free_memory: usize,
    pub used_memory: usize,
    pub coarse_memory: usize,
    pub fine_memory: usize,
}

/// HIP stream for asynchronous operations
pub struct HipStream {
    /// Stream handle (simulated)
    pub handle: *mut c_void,
    /// Stream ID
    pub id: u32,
    /// Stream priority
    pub priority: i32,
    /// Stream flags
    pub flags: HipStreamFlags,
    /// Creation time
    pub created_at: Instant,
    /// Operations queue
    pub operations: std::collections::VecDeque<HipOperation>,
}

/// HIP stream flags
#[derive(Debug, Clone)]
pub struct HipStreamFlags {
    pub default: bool,
    pub non_blocking: bool,
    pub per_thread: bool,
}

impl Default for HipStreamFlags {
    fn default() -> Self {
        Self {
            default: true,
            non_blocking: false,
            per_thread: false,
        }
    }
}

/// HIP asynchronous operation
#[derive(Debug, Clone)]
pub struct HipOperation {
    pub op_type: HipOperationType,
    pub src_ptr: Option<*mut c_void>,
    pub dst_ptr: Option<*mut c_void>,
    pub size: usize,
    pub timestamp: Instant,
}

/// Types of HIP operations
#[derive(Debug, Clone)]
pub enum HipOperationType {
    MemcpyHostToDevice,
    MemcpyDeviceToHost,
    MemcpyDeviceToDevice,
    MemcpyAsync,
    MemsetAsync,
    KernelLaunch,
    EventRecord,
    EventSynchronize,
}

/// ROCm memory pool
pub struct RocmMemoryPool {
    /// Memory type
    memory_type: RocmMemoryType,
    /// Current size
    current_size: usize,
    /// Maximum size
    max_size: usize,
    /// Used size
    used_size: usize,
    /// Free blocks
    free_blocks: std::collections::VecDeque<RocmMemoryBlock>,
    /// Allocated blocks
    allocated_blocks: HashMap<*mut c_void, RocmMemoryBlock>,
    /// Memory attributes
    attributes: RocmMemoryAttributes,
}

/// ROCm memory block
#[derive(Debug, Clone)]
pub struct RocmMemoryBlock {
    pub ptr: *mut c_void,
    pub size: usize,
    pub memory_type: RocmMemoryType,
    pub allocated_at: Instant,
    pub last_access: Option<Instant>,
    pub ref_count: u32,
    pub agent_accessible: bool,
}

/// ROCm memory attributes
#[derive(Debug, Clone)]
pub struct RocmMemoryAttributes {
    pub is_coarse_grained: bool,
    pub is_fine_grained: bool,
    pub is_host_accessible: bool,
    pub is_device_accessible: bool,
    pub is_coherent: bool,
    pub numa_node: Option<u32>,
}

impl Default for RocmMemoryAttributes {
    fn default() -> Self {
        Self {
            is_coarse_grained: true,
            is_fine_grained: false,
            is_host_accessible: false,
            is_device_accessible: true,
            is_coherent: false,
            numa_node: None,
        }
    }
}

impl RocmMemoryPool {
    pub fn new(memory_type: RocmMemoryType, max_size: usize) -> Self {
        let attributes = match memory_type {
            RocmMemoryType::CoarseGrained => RocmMemoryAttributes {
                is_coarse_grained: true,
                is_fine_grained: false,
                is_host_accessible: false,
                is_device_accessible: true,
                is_coherent: false,
                numa_node: None,
            },
            RocmMemoryType::FineGrained => RocmMemoryAttributes {
                is_coarse_grained: false,
                is_fine_grained: true,
                is_host_accessible: true,
                is_device_accessible: true,
                is_coherent: true,
                numa_node: Some(0),
            },
            RocmMemoryType::HostVisible => RocmMemoryAttributes {
                is_coarse_grained: false,
                is_fine_grained: false,
                is_host_accessible: true,
                is_device_accessible: true,
                is_coherent: false,
                numa_node: None,
            },
            _ => RocmMemoryAttributes::default(),
        };

        Self {
            memory_type,
            current_size: 0,
            max_size,
            used_size: 0,
            free_blocks: std::collections::VecDeque::new(),
            allocated_blocks: HashMap::new(),
            attributes,
        }
    }

    /// Allocate from pool
    pub fn allocate(&mut self, size: usize) -> Result<*mut c_void, RocmError> {
        // Try to find suitable free block
        for i in 0..self.free_blocks.len() {
            if self.free_blocks[i].size >= size {
                let Some(mut block) = self.free_blocks.remove(i) else {
                    continue;
                };

                // Split block if much larger
                if block.size > size * 2 {
                    let remaining_block = RocmMemoryBlock {
                        ptr: unsafe { block.ptr.add(size) },
                        size: block.size - size,
                        memory_type: block.memory_type.clone(),
                        allocated_at: block.allocated_at,
                        last_access: None,
                        ref_count: 0,
                        agent_accessible: block.agent_accessible,
                    };
                    self.free_blocks.push_back(remaining_block);
                    block.size = size;
                }

                block.last_access = Some(Instant::now());
                block.ref_count = 1;

                let ptr = block.ptr;
                self.allocated_blocks.insert(ptr, block);
                self.used_size += size;

                return Ok(ptr);
            }
        }

        // Need to allocate new memory
        if self.current_size + size > self.max_size {
            return Err(RocmError::OutOfMemory(
                "Pool size limit exceeded".to_string(),
            ));
        }

        let ptr = self.hip_malloc(size)?;
        let block = RocmMemoryBlock {
            ptr,
            size,
            memory_type: self.memory_type.clone(),
            allocated_at: Instant::now(),
            last_access: Some(Instant::now()),
            ref_count: 1,
            agent_accessible: self.attributes.is_device_accessible,
        };

        self.allocated_blocks.insert(ptr, block);
        self.current_size += size;
        self.used_size += size;

        Ok(ptr)
    }

    /// Free back to pool
    pub fn free(&mut self, ptr: *mut c_void) -> Result<(), RocmError> {
        if let Some(block) = self.allocated_blocks.remove(&ptr) {
            self.used_size -= block.size;

            // Add to free blocks
            self.free_blocks.push_back(RocmMemoryBlock {
                ptr: block.ptr,
                size: block.size,
                memory_type: block.memory_type,
                allocated_at: block.allocated_at,
                last_access: None,
                ref_count: 0,
                agent_accessible: block.agent_accessible,
            });

            // Try to coalesce adjacent blocks
            self.coalesce_free_blocks();

            Ok(())
        } else {
            Err(RocmError::InvalidPointer(
                "Pointer not found in pool".to_string(),
            ))
        }
    }

    fn coalesce_free_blocks(&mut self) {
        // Sort free blocks by address
        let mut blocks: Vec<RocmMemoryBlock> = self.free_blocks.drain(..).collect();
        blocks.sort_by_key(|block| block.ptr as usize);

        let mut coalesced = Vec::new();
        let mut current_block: Option<RocmMemoryBlock> = None;

        for block in blocks {
            match current_block.take() {
                None => current_block = Some(block),
                Some(mut prev_block) => {
                    let prev_end = prev_block.ptr as usize + prev_block.size;
                    let block_start = block.ptr as usize;

                    if prev_end == block_start && prev_block.memory_type == block.memory_type {
                        // Coalesce blocks
                        prev_block.size += block.size;
                        current_block = Some(prev_block);
                    } else {
                        coalesced.push(prev_block);
                        current_block = Some(block);
                    }
                }
            }
        }

        if let Some(block) = current_block {
            coalesced.push(block);
        }

        self.free_blocks = coalesced.into();
    }

    fn hip_malloc(&self, size: usize) -> Result<*mut c_void, RocmError> {
        // Simulate HIP memory allocation
        match self.memory_type {
            RocmMemoryType::Device => sim_alloc(size), // hipMalloc equivalent
            RocmMemoryType::Host => sim_alloc(size),   // hipMallocHost equivalent
            RocmMemoryType::CoarseGrained => sim_alloc(size), // coarse-grained device memory
            RocmMemoryType::FineGrained => sim_alloc(size), // fine-grained system memory
            RocmMemoryType::HostVisible => sim_alloc(size), // host-visible device memory
            _ => Err(RocmError::UnsupportedOperation(
                "Unsupported memory type for allocation".to_string(),
            )),
        }
    }
}

/// HIP stream manager
pub struct HipStreamManager {
    /// Available streams
    streams: Vec<HipStream>,
    /// Stream pool for reuse
    stream_pool: std::collections::VecDeque<HipStream>,
    /// Next stream ID
    next_stream_id: u32,
    /// Configuration
    config: HipStreamConfig,
}

/// Stream manager configuration
#[derive(Debug, Clone)]
pub struct HipStreamConfig {
    pub default_priority: i32,
    pub enable_priorities: bool,
    pub max_operations_per_stream: usize,
}

impl Default for HipStreamConfig {
    fn default() -> Self {
        Self {
            default_priority: 0,
            enable_priorities: true,
            max_operations_per_stream: 1000,
        }
    }
}

impl HipStreamManager {
    pub fn new(config: HipStreamConfig) -> Self {
        Self {
            streams: Vec::new(),
            stream_pool: std::collections::VecDeque::new(),
            next_stream_id: 0,
            config,
        }
    }

    /// Create new stream
    /// Create new stream
    ///
    /// Reuses a previously [`Self::destroy_stream`]d stream from
    /// `stream_pool` when one is available (its operation queue is cleared
    /// and it is given a fresh ID) instead of always allocating a new one.
    pub fn create_stream(&mut self, priority: Option<i32>) -> Result<u32, RocmError> {
        let stream_id = self.next_stream_id;
        self.next_stream_id += 1;

        let mut stream = self.stream_pool.pop_front().unwrap_or_else(|| HipStream {
            handle: std::ptr::null_mut(), // Would be actual HIP stream
            id: stream_id,
            priority: priority.unwrap_or(self.config.default_priority),
            flags: HipStreamFlags::default(),
            created_at: Instant::now(),
            operations: std::collections::VecDeque::new(),
        });
        stream.id = stream_id;
        stream.priority = priority.unwrap_or(self.config.default_priority);
        stream.created_at = Instant::now();
        stream.operations.clear();

        self.streams.push(stream);
        Ok(stream_id)
    }

    /// Destroy stream
    ///
    /// Returns the stream to `stream_pool` for [`Self::create_stream`] to
    /// reuse instead of dropping it outright.
    pub fn destroy_stream(&mut self, stream_id: u32) -> Result<(), RocmError> {
        if let Some(pos) = self.streams.iter().position(|s| s.id == stream_id) {
            let stream = self.streams.remove(pos);
            self.stream_pool.push_back(stream);
            Ok(())
        } else {
            Err(RocmError::InvalidStream("Stream not found".to_string()))
        }
    }

    /// Add operation to stream
    pub fn add_operation(
        &mut self,
        stream_id: u32,
        operation: HipOperation,
    ) -> Result<(), RocmError> {
        if let Some(stream) = self.streams.iter_mut().find(|s| s.id == stream_id) {
            if stream.operations.len() >= self.config.max_operations_per_stream {
                return Err(RocmError::StreamFull(
                    "Stream operation queue is full".to_string(),
                ));
            }

            stream.operations.push_back(operation);
            Ok(())
        } else {
            Err(RocmError::InvalidStream("Stream not found".to_string()))
        }
    }

    /// Synchronize stream
    pub fn synchronize_stream(&mut self, stream_id: u32) -> Result<(), RocmError> {
        // First, collect all operations from the stream
        let mut operations = Vec::new();
        if let Some(stream) = self.streams.iter_mut().find(|s| s.id == stream_id) {
            while let Some(operation) = stream.operations.pop_front() {
                operations.push(operation);
            }
        } else {
            return Err(RocmError::InvalidStream("Stream not found".to_string()));
        }

        // Now execute all operations
        for operation in operations {
            self.execute_operation(operation)?;
        }

        Ok(())
    }

    fn execute_operation(&self, _operation: HipOperation) -> Result<(), RocmError> {
        // Host-memory simulation (see module docs): there is no real ROCm/HIP
        // device to transfer to or from, so no data movement happens here.
        // This used to also inject an artificial `std::thread::sleep` per
        // operation type to mimic device-transfer latency; that fake timing
        // has been removed rather than left as an undisclosed simulated
        // number, so callers now see the true (near-zero) cost of this
        // simulation instead of a fabricated one.
        Ok(())
    }
}

/// ROCm statistics
#[derive(Debug, Clone, Default)]
pub struct RocmStats {
    pub total_allocations: u64,
    pub total_deallocations: u64,
    pub bytes_allocated: u64,
    pub bytes_deallocated: u64,
    pub device_memory_used: usize,
    pub host_memory_used: usize,
    pub coarse_grained_used: usize,
    pub fine_grained_used: usize,
    pub stream_operations: u64,
    pub kernel_launches: u64,
    pub memory_transfers: u64,
    pub average_allocation_time: Duration,
    pub peak_memory_usage: usize,
}

impl RocmMemoryBackend {
    /// Create new ROCm backend
    pub fn new(config: RocmConfig) -> Result<Self, RocmError> {
        // Initialize ROCm device
        let device_properties = Self::query_device_properties(config.device_id)?;

        // Create memory pools
        let mut memory_pools = HashMap::new();
        if config.enable_memory_pools {
            let pool_size = device_properties.total_global_memory / 4; // Use 1/4 of total memory

            memory_pools.insert(
                RocmMemoryType::Device,
                RocmMemoryPool::new(RocmMemoryType::Device, pool_size),
            );
            memory_pools.insert(
                RocmMemoryType::Host,
                RocmMemoryPool::new(RocmMemoryType::Host, pool_size),
            );

            if config.enable_coarse_memory {
                memory_pools.insert(
                    RocmMemoryType::CoarseGrained,
                    RocmMemoryPool::new(RocmMemoryType::CoarseGrained, pool_size),
                );
            }

            if config.enable_fine_memory {
                memory_pools.insert(
                    RocmMemoryType::FineGrained,
                    RocmMemoryPool::new(RocmMemoryType::FineGrained, pool_size / 2),
                );
            }

            if config.enable_host_visible {
                memory_pools.insert(
                    RocmMemoryType::HostVisible,
                    RocmMemoryPool::new(RocmMemoryType::HostVisible, pool_size / 4),
                );
            }
        }

        let stream_manager = HipStreamManager::new(HipStreamConfig::default());

        Ok(Self {
            config,
            device_properties,
            contexts: HashMap::new(),
            memory_pools,
            stats: RocmStats::default(),
            stream_manager,
        })
    }

    /// Query device properties
    fn query_device_properties(device_id: u32) -> Result<RocmDeviceProperties, RocmError> {
        // Simulate querying ROCm device properties
        Ok(RocmDeviceProperties {
            device_id,
            name: format!("AMD GPU {}", device_id),
            arch: "gfx906".to_string(), // Vega architecture
            gcn_arch_name: "Vega20".to_string(),
            total_global_memory: 16 * 1024 * 1024 * 1024, // 16GB
            local_memory_size: 64 * 1024,                 // 64KB
            max_work_group_size: 1024,
            max_work_item_dimensions: 3,
            max_work_item_sizes: [1024, 1024, 1024],
            compute_units: 64,
            wavefront_size: 64,
            memory_clock_frequency: 1000000, // 1 GHz
            memory_bus_width: 4096,
            l2_cache_size: 4 * 1024 * 1024,      // 4MB
            max_constant_buffer_size: 64 * 1024, // 64KB
            pci_bus_id: 0x03,
            pci_device_id: 0x66AF,
            supports_cooperative_launch: true,
            supports_dynamic_parallelism: false,
        })
    }

    /// Allocate device memory
    pub fn allocate(
        &mut self,
        size: usize,
        memory_type: RocmMemoryType,
    ) -> Result<*mut c_void, RocmError> {
        let start_time = Instant::now();

        let ptr = if self.config.enable_memory_pools {
            if let Some(pool) = self.memory_pools.get_mut(&memory_type) {
                pool.allocate(size)?
            } else {
                return Err(RocmError::UnsupportedMemoryType(
                    "Memory type not supported".to_string(),
                ));
            }
        } else {
            // Direct allocation
            self.direct_allocate(size, memory_type.clone())?
        };

        // Update statistics
        self.stats.total_allocations += 1;
        self.stats.bytes_allocated += size as u64;

        match memory_type {
            RocmMemoryType::Device => self.stats.device_memory_used += size,
            RocmMemoryType::Host => self.stats.host_memory_used += size,
            RocmMemoryType::CoarseGrained => self.stats.coarse_grained_used += size,
            RocmMemoryType::FineGrained => self.stats.fine_grained_used += size,
            _ => {}
        }

        let allocation_time = start_time.elapsed();
        let total_time = self.stats.average_allocation_time.as_nanos() as u64
            * (self.stats.total_allocations - 1)
            + allocation_time.as_nanos() as u64;
        self.stats.average_allocation_time =
            Duration::from_nanos(total_time / self.stats.total_allocations);

        let current_usage = self.stats.device_memory_used
            + self.stats.host_memory_used
            + self.stats.coarse_grained_used
            + self.stats.fine_grained_used;
        if current_usage > self.stats.peak_memory_usage {
            self.stats.peak_memory_usage = current_usage;
        }

        Ok(ptr)
    }

    fn direct_allocate(
        &self,
        size: usize,
        memory_type: RocmMemoryType,
    ) -> Result<*mut c_void, RocmError> {
        // Simulate direct HIP allocation
        match memory_type {
            RocmMemoryType::Device => sim_alloc(size), // hipMalloc
            RocmMemoryType::Host => sim_alloc(size),   // hipMallocHost
            RocmMemoryType::CoarseGrained => sim_alloc(size), // coarse-grained device memory
            RocmMemoryType::FineGrained => sim_alloc(size), // fine-grained system memory
            _ => Err(RocmError::UnsupportedMemoryType(
                "Unsupported memory type".to_string(),
            )),
        }
    }

    /// Free device memory
    pub fn free(&mut self, ptr: *mut c_void, memory_type: RocmMemoryType) -> Result<(), RocmError> {
        if self.config.enable_memory_pools {
            if let Some(pool) = self.memory_pools.get_mut(&memory_type) {
                pool.free(ptr)?;
            } else {
                return Err(RocmError::UnsupportedMemoryType(
                    "Memory type not supported".to_string(),
                ));
            }
        } else {
            // Direct deallocation. `ptr` was returned by `sim_alloc` via
            // `hip_malloc`/`direct_allocate` above, and this is the first
            // time it is freed.
            sim_dealloc(ptr);
        }

        self.stats.total_deallocations += 1;
        Ok(())
    }

    /// Copy memory
    pub fn memcpy(
        &mut self,
        dst: *mut c_void,
        src: *const c_void,
        size: usize,
        kind: RocmMemcpyKind,
    ) -> Result<(), RocmError> {
        let operation = HipOperation {
            op_type: match kind {
                RocmMemcpyKind::HostToDevice => HipOperationType::MemcpyHostToDevice,
                RocmMemcpyKind::DeviceToHost => HipOperationType::MemcpyDeviceToHost,
                RocmMemcpyKind::DeviceToDevice => HipOperationType::MemcpyDeviceToDevice,
                RocmMemcpyKind::HostToHost => HipOperationType::MemcpyAsync,
            },
            src_ptr: Some(src as *mut c_void),
            dst_ptr: Some(dst),
            size,
            timestamp: Instant::now(),
        };

        // Execute synchronously for now
        self.stream_manager.execute_operation(operation)?;
        self.stats.memory_transfers += 1;

        Ok(())
    }

    /// Asynchronous memory copy
    pub fn memcpy_async(
        &mut self,
        dst: *mut c_void,
        src: *const c_void,
        size: usize,
        kind: RocmMemcpyKind,
        stream_id: u32,
    ) -> Result<(), RocmError> {
        let operation = HipOperation {
            // Mirrors the synchronous `memcpy`'s mapping above so the queued
            // operation's recorded direction matches what the caller asked
            // for instead of always reporting a generic `MemcpyAsync`.
            op_type: match kind {
                RocmMemcpyKind::HostToDevice => HipOperationType::MemcpyHostToDevice,
                RocmMemcpyKind::DeviceToHost => HipOperationType::MemcpyDeviceToHost,
                RocmMemcpyKind::DeviceToDevice => HipOperationType::MemcpyDeviceToDevice,
                RocmMemcpyKind::HostToHost => HipOperationType::MemcpyAsync,
            },
            src_ptr: Some(src as *mut c_void),
            dst_ptr: Some(dst),
            size,
            timestamp: Instant::now(),
        };

        self.stream_manager.add_operation(stream_id, operation)?;
        Ok(())
    }

    /// Create HIP context
    pub fn create_context(&mut self, flags: HipContextFlags) -> Result<u32, RocmError> {
        let context_id = self.contexts.len() as u32;

        let memory_info = HipMemoryInfo {
            total_memory: self.device_properties.total_global_memory,
            free_memory: self.device_properties.total_global_memory - self.stats.device_memory_used,
            used_memory: self.stats.device_memory_used,
            coarse_memory: self.stats.coarse_grained_used,
            fine_memory: self.stats.fine_grained_used,
        };

        let context = HipContext {
            handle: std::ptr::null_mut(), // Would be actual HIP context
            device_id: self.config.device_id,
            flags,
            created_at: Instant::now(),
            streams: Vec::new(),
            memory_info,
        };

        self.contexts.insert(context_id, context);
        Ok(context_id)
    }

    /// Get device properties
    pub fn get_device_properties(&self) -> &RocmDeviceProperties {
        &self.device_properties
    }

    /// Get statistics
    pub fn get_stats(&self) -> &RocmStats {
        &self.stats
    }

    /// Synchronize device
    pub fn device_synchronize(&mut self) -> Result<(), RocmError> {
        // Synchronize all streams
        let stream_ids: Vec<u32> = self.stream_manager.streams.iter().map(|s| s.id).collect();
        for stream_id in stream_ids {
            self.stream_manager.synchronize_stream(stream_id)?;
        }
        Ok(())
    }

    /// Create stream
    pub fn create_stream(&mut self, priority: Option<i32>) -> Result<u32, RocmError> {
        self.stream_manager.create_stream(priority)
    }

    /// Destroy stream
    pub fn destroy_stream(&mut self, stream_id: u32) -> Result<(), RocmError> {
        self.stream_manager.destroy_stream(stream_id)
    }

    /// Query memory attributes
    ///
    /// Looks up which pool actually allocated `ptr` and returns that pool's
    /// real attributes (which vary by [`RocmMemoryType`] — see the
    /// attribute construction in [`RocmMemoryPool::new`]) instead of an
    /// unconditional default. A pointer this backend never allocated is an
    /// honest `Err`, not a guess.
    pub fn query_memory_attributes(
        &self,
        ptr: *mut c_void,
    ) -> Result<RocmMemoryAttributes, RocmError> {
        self.memory_pools
            .values()
            .find(|pool| pool.allocated_blocks.contains_key(&ptr))
            .map(|pool| pool.attributes.clone())
            .ok_or_else(|| {
                RocmError::InvalidPointer("pointer was not allocated by this backend".to_string())
            })
    }
}

// Safety: RocmMemoryBackend manages ROCm/HIP GPU memory pointers via *mut c_void.
// While raw pointers are not Send/Sync by default, it's safe to share across threads
// when protected by Arc<Mutex<>> because:
// 1. All pointers point to HIP GPU memory managed by the ROCm driver
// 2. The Mutex provides exclusive access for all mutable operations
// 3. No thread-local state is maintained
unsafe impl Send for RocmMemoryBackend {}
unsafe impl Sync for RocmMemoryBackend {}

/// ROCm memory copy kinds
#[derive(Debug, Clone)]
pub enum RocmMemcpyKind {
    HostToDevice,
    DeviceToHost,
    DeviceToDevice,
    HostToHost,
}

/// ROCm errors
#[derive(Debug, Clone)]
pub enum RocmError {
    DeviceNotFound(String),
    OutOfMemory(String),
    InvalidPointer(String),
    InvalidStream(String),
    StreamFull(String),
    UnsupportedOperation(String),
    UnsupportedMemoryType(String),
    ContextCreationFailed(String),
    KernelLaunchFailed(String),
    SynchronizationFailed(String),
    InternalError(String),
}

impl std::fmt::Display for RocmError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RocmError::DeviceNotFound(msg) => write!(f, "Device not found: {}", msg),
            RocmError::OutOfMemory(msg) => write!(f, "Out of memory: {}", msg),
            RocmError::InvalidPointer(msg) => write!(f, "Invalid pointer: {}", msg),
            RocmError::InvalidStream(msg) => write!(f, "Invalid stream: {}", msg),
            RocmError::StreamFull(msg) => write!(f, "Stream full: {}", msg),
            RocmError::UnsupportedOperation(msg) => write!(f, "Unsupported operation: {}", msg),
            RocmError::UnsupportedMemoryType(msg) => write!(f, "Unsupported memory type: {}", msg),
            RocmError::ContextCreationFailed(msg) => write!(f, "Context creation failed: {}", msg),
            RocmError::KernelLaunchFailed(msg) => write!(f, "Kernel launch failed: {}", msg),
            RocmError::SynchronizationFailed(msg) => write!(f, "Synchronization failed: {}", msg),
            RocmError::InternalError(msg) => write!(f, "Internal error: {}", msg),
        }
    }
}

impl std::error::Error for RocmError {}

/// Thread-safe ROCm backend wrapper
pub struct ThreadSafeRocmBackend {
    backend: Arc<Mutex<RocmMemoryBackend>>,
}

impl ThreadSafeRocmBackend {
    pub fn new(config: RocmConfig) -> Result<Self, RocmError> {
        let backend = RocmMemoryBackend::new(config)?;
        Ok(Self {
            backend: Arc::new(Mutex::new(backend)),
        })
    }

    pub fn allocate(
        &self,
        size: usize,
        memory_type: RocmMemoryType,
    ) -> Result<*mut c_void, RocmError> {
        let mut backend = self.backend.lock().unwrap_or_else(|e| e.into_inner());
        backend.allocate(size, memory_type)
    }

    pub fn free(&self, ptr: *mut c_void, memory_type: RocmMemoryType) -> Result<(), RocmError> {
        let mut backend = self.backend.lock().unwrap_or_else(|e| e.into_inner());
        backend.free(ptr, memory_type)
    }

    pub fn get_stats(&self) -> RocmStats {
        let backend = self.backend.lock().unwrap_or_else(|e| e.into_inner());
        backend.get_stats().clone()
    }
}

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

    /// Regression test for F26: a zero-size request must not reach
    /// `std::alloc::alloc` (unsound for a zero-size layout).
    #[test]
    fn sim_alloc_zero_size_is_a_safe_sentinel_not_a_ub_call() {
        let ptr = sim_alloc(0).expect("zero-size request must succeed");
        assert!(!ptr.is_null());
        sim_dealloc(ptr);
    }

    /// A real allocation must be readable/writable across its full size and
    /// must free through the same layout it was allocated with.
    #[test]
    fn sim_alloc_real_allocation_round_trips_and_frees_cleanly() {
        for size in [1usize, 7, 256, 4096, 1_000_003] {
            let ptr = sim_alloc(size).expect("allocation must succeed") as *mut u8;
            assert!(!ptr.is_null());
            unsafe {
                for i in 0..size {
                    ptr.add(i).write(0xAB);
                }
                for i in 0..size {
                    assert_eq!(ptr.add(i).read(), 0xAB);
                }
                sim_dealloc(ptr as *mut c_void);
            }
        }
    }

    #[test]
    fn sim_dealloc_null_is_a_no_op() {
        sim_dealloc(std::ptr::null_mut());
    }

    #[test]
    fn test_rocm_backend_creation() {
        let config = RocmConfig::default();
        let backend = RocmMemoryBackend::new(config);
        assert!(backend.is_ok());
    }

    #[test]
    fn test_memory_pool() {
        let mut pool = RocmMemoryPool::new(RocmMemoryType::CoarseGrained, 1024 * 1024);
        let ptr = pool.allocate(1024);
        assert!(ptr.is_ok());

        let ptr = ptr.expect("unwrap failed");
        let result = pool.free(ptr);
        assert!(result.is_ok());
    }

    #[test]
    fn test_hip_stream_manager() {
        let mut manager = HipStreamManager::new(HipStreamConfig::default());
        let stream_id = manager.create_stream(Some(1));
        assert!(stream_id.is_ok());

        let stream_id = stream_id.expect("unwrap failed");
        let result = manager.destroy_stream(stream_id);
        assert!(result.is_ok());
    }

    #[test]
    fn test_thread_safe_backend() {
        let config = RocmConfig::default();
        let backend = ThreadSafeRocmBackend::new(config);
        assert!(backend.is_ok());

        let backend = backend.expect("unwrap failed");
        let stats = backend.get_stats();
        assert_eq!(stats.total_allocations, 0);
    }
}