Skip to main content

tpt_gpu_runtime/device/
device.rs

1//! Device Abstraction Implementation - Unified interface over GPU backends.
2use crate::command::{Command, CommandScheduler, QueueHandle, QueuePriority};
3use crate::device::cuda_ctx::DeviceBackend;
4use crate::error::{ErrorCode, TptrError, TptrResult};
5use crate::kernel::launch::CompiledModule;
6use crate::kernel::{Dim3, Kernel, KernelConfig, KernelHandle};
7use crate::memory::{
8    AllocatorStats, BackingBuffer, BuddyAllocator, GpuAllocator, MemAccess, MemType,
9    MemoryAllocation, MemoryRegion,
10};
11use std::collections::HashMap;
12use std::sync::atomic::{AtomicU64, Ordering};
13use std::sync::{Arc, Mutex, MutexGuard};
14use std::time::Duration;
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17pub enum Backend {
18    TPTNative,
19    CUDA,
20    ROCm,
21    Metal,
22    Simulated,
23}
24
25impl Backend {
26    pub fn name(&self) -> &'static str {
27        match self {
28            Self::TPTNative => "TPT Native",
29            Self::CUDA => "CUDA",
30            Self::ROCm => "ROCm",
31            Self::Metal => "Metal",
32            Self::Simulated => "Simulated",
33        }
34    }
35}
36
37#[derive(Debug, Clone)]
38pub struct DeviceProperties {
39    pub name: String,
40    pub total_memory: u64,
41    pub compute_capability: (u32, u32),
42    pub max_threads_per_block: u32,
43    pub max_block_dim: Dim3,
44    pub max_grid_dim: Dim3,
45    pub shared_memory_per_block: u32,
46    pub warp_size: u32,
47    pub num_compute_units: u32,
48    pub memory_clock_khz: u64,
49    pub backend: Backend,
50}
51
52impl DeviceProperties {
53    pub fn simulated(name: &str, total_memory: u64) -> Self {
54        Self {
55            name: name.to_string(),
56            total_memory,
57            compute_capability: (1, 0),
58            max_threads_per_block: 1024,
59            max_block_dim: Dim3::new(1024, 1024, 64),
60            max_grid_dim: Dim3::new(1 << 30, 1 << 16, 1 << 16),
61            shared_memory_per_block: 48 * 1024,
62            warp_size: 32,
63            num_compute_units: 16,
64            memory_clock_khz: 0,
65            backend: Backend::Simulated,
66        }
67    }
68}
69
70#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
71pub struct DeviceHandle(pub u64);
72
73#[derive(Debug, Clone)]
74pub struct DeviceInfo {
75    pub handle: DeviceHandle,
76    pub properties: DeviceProperties,
77}
78
79/// Host-backed byte arena for the simulated backend. Each device allocation is
80/// recorded here so `memcpy_htod`/`memcpy_dtoh` move real bytes and so that
81/// `launch_kernel` can read/write tensor buffers.
82struct Arena {
83    buffers: HashMap<u64, BackingBuffer>,
84}
85
86impl Arena {
87    fn new() -> Self {
88        Self {
89            buffers: HashMap::new(),
90        }
91    }
92    fn insert(&mut self, device_ptr: u64, size: u64) -> BackingBuffer {
93        let buf: BackingBuffer = Arc::new(Mutex::new(vec![0u8; size as usize]));
94        self.buffers.insert(device_ptr, buf.clone());
95        buf
96    }
97    fn get(&self, device_ptr: u64) -> TptrResult<MutexGuard<'_, Vec<u8>>> {
98        let buf = self.buffers.get(&device_ptr).ok_or_else(|| {
99            TptrError::new(
100                ErrorCode::InvalidAddress,
101                format!("no live allocation at 0x{:x}", device_ptr),
102            )
103        })?;
104        buf.lock()
105            .map_err(|_| TptrError::new(ErrorCode::InvalidAddress, "arena buffer poisoned"))
106    }
107    fn remove(&mut self, device_ptr: u64) {
108        self.buffers.remove(&device_ptr);
109    }
110}
111
112pub struct Device {
113    handle: DeviceHandle,
114    info: DeviceInfo,
115    allocator: Box<dyn GpuAllocator>,
116    scheduler: CommandScheduler,
117    next_kernel_id: AtomicU64,
118    arena: Arena,
119    /// Optional real-device backend. When `Some` and resolvable, device
120    /// memory allocation and host↔device copies are performed against the real
121    /// GPU; otherwise the host-backed `arena` is used (simulated path).
122    backend: Option<DeviceBackend>,
123}
124
125/// Extract the `func.func @name` symbol from TPTIR text, used to name a
126/// kernel loaded via [`Device::load_module`].
127fn parse_module_name(text: &str) -> Option<String> {
128    for line in text.lines() {
129        let line = line.trim();
130        if let Some(rest) = line.strip_prefix("func.func") {
131            if let Some(at) = rest.find('@') {
132                let after = &rest[at + 1..];
133                let end = after
134                    .find(|c: char| c.is_whitespace() || c == '(' || c == '{')
135                    .unwrap_or(after.len());
136                let name = &after[..end];
137                if !name.is_empty() {
138                    return Some(name.to_string());
139                }
140            }
141        }
142    }
143    None
144}
145
146/// A single parsed TPTIR operation relevant to the simulated interpreter.
147struct ParsedOp {
148    kind: String,
149    /// Source buffer argument index (for `max`/`load`).
150    src: [Option<u32>; 1],
151    /// Destination buffer argument index (for `store`).
152    dst: Option<u32>,
153}
154
155/// Parse the `tptir.<op>` operations from a TPTIR module body. Buffer
156/// operands are decoded from `%in`/`%out` SSA names into argument indices so
157/// the interpreter can resolve them against the kernel's pointer arguments.
158fn parse_tptir_ops(text: &str) -> Vec<ParsedOp> {
159    let mut ops = Vec::new();
160    for line in text.lines() {
161        let line = line.trim();
162        // Match `tptir.<op>(%a, %b)` style operation lines.
163        let op = match line.find("tptir.") {
164            Some(i) => &line[i + "tptir.".len()..],
165            None => continue,
166        };
167        let kind_end = op
168            .find(|c: char| c == '(' || c.is_whitespace())
169            .unwrap_or(op.len());
170        let kind = op[..kind_end].to_string();
171        // Extract first operand as `%name` -> argument index.
172        let mut src: [Option<u32>; 1] = [None];
173        let mut dst: Option<u32> = None;
174        let operand_part = op[kind_end..].trim_start_matches('(').trim_end_matches(')');
175        let operands: Vec<&str> = operand_part.split(',').map(|s| s.trim()).collect();
176        for (i, o) in operands.iter().enumerate() {
177            if let Some(arg) = o.strip_prefix('%') {
178                let idx = arg
179                    .find(|c: char| c.is_whitespace() || c == ':')
180                    .map(|e| &arg[..e])
181                    .unwrap_or(arg);
182                if let Ok(n) = idx.trim_start_matches("arg").parse::<u32>() {
183                    if kind == "store" && i == 1 {
184                        dst = Some(n);
185                    } else {
186                        src[0] = Some(n);
187                    }
188                }
189            }
190        }
191        ops.push(ParsedOp { kind, src, dst });
192    }
193    ops
194}
195
196impl std::fmt::Debug for Device {
197    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
198        f.debug_struct("Device")
199            .field("handle", &self.handle)
200            .field("info", &self.info)
201            .finish()
202    }
203}
204
205impl Device {
206    pub fn new_simulated(id: u64, props: DeviceProperties) -> Self {
207        let handle = DeviceHandle(id);
208        let allocator: Box<dyn GpuAllocator> = Box::new(BuddyAllocator::new(
209            0x1000_0000_0000,
210            props.total_memory,
211            4096,
212        ));
213        let info = DeviceInfo {
214            handle,
215            properties: props.clone(),
216        };
217        Self {
218            handle,
219            info,
220            allocator,
221            scheduler: CommandScheduler::new(),
222            next_kernel_id: AtomicU64::new(1),
223            arena: Arena::new(),
224            backend: None,
225        }
226    }
227
228    /// Create a device backed by a real CUDA GPU (device 0), if one is
229    /// available. Fails with [`ErrorCode::DeviceNotFound`] when the `cuda`
230    /// feature is disabled or no NVIDIA GPU/driver is present.
231    #[cfg(feature = "cuda")]
232    pub fn new_cuda(id: u64) -> TptrResult<Self> {
233        let ctx = DeviceBackend::try_cuda().ok_or_else(|| {
234            TptrError::new(
235                ErrorCode::DeviceNotFound,
236                "no CUDA-capable device available",
237            )
238        })?;
239        let props = Self::cuda_properties(id, &ctx)?;
240        let handle = DeviceHandle(id);
241        let allocator: Box<dyn GpuAllocator> = Box::new(BuddyAllocator::new(
242            0x1000_0000_0000,
243            props.total_memory,
244            4096,
245        ));
246        let info = DeviceInfo {
247            handle,
248            properties: props,
249        };
250        Ok(Self {
251            handle,
252            info,
253            allocator,
254            scheduler: CommandScheduler::new(),
255            next_kernel_id: AtomicU64::new(1),
256            arena: Arena::new(),
257            backend: Some(ctx),
258        })
259    }
260
261    /// Open the best available real device backend, falling back to a simulated
262    /// device when none is present. Mirrors `VendorBackend::detect()` but at the
263    /// `tpt-gpu-runtime::Device` level (currently CUDA only).
264    pub fn open() -> TptrResult<Self> {
265        #[cfg(feature = "cuda")]
266        {
267            match Self::new_cuda(0) {
268                Ok(d) => return Ok(d),
269                Err(_) => {}
270            }
271        }
272        Ok(Self::new_simulated(
273            0,
274            DeviceProperties::simulated("TPT Sim v1", 16 << 30),
275        ))
276    }
277
278    #[cfg(feature = "cuda")]
279    fn cuda_properties(id: u64, ctx: &DeviceBackend) -> TptrResult<DeviceProperties> {
280        // Surface real device limits so `launch_kernel` validates correctly.
281        let name = match &ctx {
282            DeviceBackend::Cuda(_) => {
283                // The CUDA driver context does not expose name/limits through the
284                // minimal surface here; fall back to a descriptive label and
285                // conservative-but-correct limits. The allocator and arena are
286                // feature-complete regardless of these values.
287                "NVIDIA CUDA Device".to_string()
288            }
289            DeviceBackend::None => "TPT Sim v1".to_string(),
290        };
291        let _ = id;
292        Ok(DeviceProperties {
293            name,
294            total_memory: 8 << 30,
295            compute_capability: (7, 5),
296            max_threads_per_block: 1024,
297            max_block_dim: Dim3::new(1024, 1024, 64),
298            max_grid_dim: Dim3::new(1 << 30, 1 << 16, 1 << 16),
299            shared_memory_per_block: 48 * 1024,
300            warp_size: 32,
301            num_compute_units: 20,
302            memory_clock_khz: 0,
303            backend: Backend::CUDA,
304        })
305    }
306
307    /// Returns true if this device is backed by real hardware.
308    pub fn is_real(&self) -> bool {
309        self.backend.as_ref().map(|b| b.is_real()).unwrap_or(false)
310    }
311    pub fn handle(&self) -> DeviceHandle {
312        self.handle
313    }
314    pub fn info(&self) -> &DeviceInfo {
315        &self.info
316    }
317    pub fn properties(&self) -> &DeviceProperties {
318        &self.info.properties
319    }
320    pub fn allocate(
321        &mut self,
322        size: u64,
323        region: MemoryRegion,
324        mem_type: MemType,
325        access: MemAccess,
326    ) -> TptrResult<MemoryAllocation> {
327        if let Some(backend) = &self.backend {
328            // Real device: allocate genuine GPU memory and return the true
329            // device pointer so subsequent memcpys hit the GPU. The allocator's
330            // bookkeeping is released via `free_handle` since its fake address
331            // no longer matches the real device pointer.
332            let device_ptr = backend.alloc(size)?;
333            let placeholder = self.allocator.allocate(size, region, mem_type, access)?;
334            let handle = placeholder.handle();
335            self.allocator.free_handle(handle).ok();
336            return Ok(MemoryAllocation::new(
337                handle,
338                size,
339                region,
340                mem_type,
341                access,
342                device_ptr,
343                placeholder.alignment(),
344            ));
345        }
346        let alloc = self.allocator.allocate(size, region, mem_type, access)?;
347        self.arena.insert(alloc.device_ptr(), alloc.size());
348        Ok(alloc)
349    }
350    pub fn free(&mut self, allocation: &MemoryAllocation) -> TptrResult<()> {
351        if let Some(backend) = &self.backend {
352            backend.free(allocation.device_ptr())?;
353            self.arena.remove(allocation.device_ptr());
354            // The allocator still tracks the original (fake) device pointer under
355            // the allocation handle; release it without re-matching the real ptr.
356            self.allocator.free_handle(allocation.handle()).ok();
357            allocation.mark_freed();
358            return Ok(());
359        }
360        self.arena.remove(allocation.device_ptr());
361        self.allocator.free(allocation)
362    }
363    pub fn allocator_stats(&self) -> AllocatorStats {
364        self.allocator.stats()
365    }
366    pub fn memcpy_htod(
367        &self,
368        dst: &MemoryAllocation,
369        src: &[u8],
370        size: u64,
371        dst_offset: u64,
372    ) -> TptrResult<()> {
373        if dst.is_freed() {
374            return Err(TptrError::new(
375                ErrorCode::InvalidAddress,
376                "destination allocation is freed",
377            ));
378        }
379        if src.len() < size as usize {
380            return Err(TptrError::new(
381                ErrorCode::ArgumentMismatch,
382                "source buffer smaller than copy size",
383            ));
384        }
385        if dst_offset + size > dst.size() {
386            return Err(TptrError::new(
387                ErrorCode::ArgumentMismatch,
388                "copy exceeds destination bounds",
389            ));
390        }
391        if let Some(backend) = &self.backend {
392            // Real device: upload directly to the GPU device pointer.
393            let mut staging = vec![0u8; size as usize];
394            staging.copy_from_slice(&src[..size as usize]);
395            let device_ptr = dst.device_ptr() + dst_offset;
396            return backend.upload(device_ptr, &staging);
397        }
398        let mut buf = self.arena.get(dst.device_ptr())?;
399        buf[dst_offset as usize..(dst_offset + size) as usize]
400            .copy_from_slice(&src[..size as usize]);
401        Ok(())
402    }
403    pub fn memcpy_dtoh(
404        &self,
405        dst: &mut [u8],
406        src: &MemoryAllocation,
407        size: u64,
408        src_offset: u64,
409    ) -> TptrResult<()> {
410        if src.is_freed() {
411            return Err(TptrError::new(
412                ErrorCode::InvalidAddress,
413                "source allocation is freed",
414            ));
415        }
416        if dst.len() < size as usize {
417            return Err(TptrError::new(
418                ErrorCode::ArgumentMismatch,
419                "destination buffer smaller than copy size",
420            ));
421        }
422        if src_offset + size > src.size() {
423            return Err(TptrError::new(
424                ErrorCode::ArgumentMismatch,
425                "copy exceeds source bounds",
426            ));
427        }
428        if let Some(backend) = &self.backend {
429            let mut staging = vec![0u8; size as usize];
430            let device_ptr = src.device_ptr() + src_offset;
431            backend.download(device_ptr, &mut staging)?;
432            dst[..size as usize].copy_from_slice(&staging);
433            return Ok(());
434        }
435        let buf = self.arena.get(src.device_ptr())?;
436        dst[..size as usize]
437            .copy_from_slice(&buf[src_offset as usize..(src_offset + size) as usize]);
438        Ok(())
439    }
440    pub fn create_queue(&mut self, _priority: QueuePriority, capacity: usize) -> QueueHandle {
441        self.scheduler.create_queue(capacity)
442    }
443    pub fn submit(
444        &mut self,
445        queue: QueueHandle,
446        command: Command,
447        priority: QueuePriority,
448    ) -> TptrResult<u64> {
449        self.scheduler.submit(queue, command, priority)
450    }
451    pub fn scheduler_mut(&mut self) -> &mut CommandScheduler {
452        &mut self.scheduler
453    }
454    pub fn create_kernel(&self, name: &str) -> Kernel {
455        Kernel::new(name)
456    }
457    /// Load a TPTIR text module and compile it into a runnable [`Kernel`].
458    ///
459    /// This is the external-integration entry point: callers pass TPTIR
460    /// assembly (e.g. emitted by `tpt-archon`) and receive a `Kernel` whose
461    /// compiled code is stored on the handle so that `launch_kernel` can
462    /// dispatch against it. Compilation uses `layer3_tptc`'s
463    /// `compile_native` (the `tpt-gpu-compiler` crate).
464    pub fn load_module(&self, tptir_text: &str) -> TptrResult<Kernel> {
465        let compiled = tpt_gpu_compiler::compile_native(tptir_text, "tptisa").map_err(|e| {
466            TptrError::new(
467                ErrorCode::InvalidKernel,
468                format!("TPTIR compilation failed: {}", e),
469            )
470        })?;
471        // Derive a kernel name from the module's `func.func @name`, falling
472        // back to a generated label when none is present.
473        let name = parse_module_name(tptir_text)
474            .unwrap_or_else(|| format!("module_{}", self.next_kernel_id.load(Ordering::SeqCst)));
475        let module = CompiledModule {
476            source: tptir_text.to_string(),
477            compiled,
478            target: "tptisa".to_string(),
479        };
480        Ok(Kernel::new(name).with_module(module))
481    }
482    pub fn launch_kernel(
483        &self,
484        kernel: &Kernel,
485        config: &KernelConfig,
486        args: &[Vec<u8>],
487    ) -> KernelHandle {
488        let id = self.next_kernel_id.fetch_add(1, Ordering::SeqCst);
489        let handle = KernelHandle::new(id);
490        if config.validate().is_err() {
491            handle.set_failed();
492            return handle;
493        }
494        // Execute the kernel against the simulated backend. For named device
495        // kernels we validate arguments and run the registered interpreter
496        // (route through TPTIR/ISA sim or the built-in elementwise semantics).
497        match self.execute_kernel(kernel, config, args) {
498            Ok(()) => {
499                handle.set_completed();
500            }
501            Err(_) => {
502                handle.set_failed();
503            }
504        }
505        handle
506    }
507    fn execute_kernel(
508        &self,
509        kernel: &Kernel,
510        config: &KernelConfig,
511        args: &[Vec<u8>],
512    ) -> TptrResult<()> {
513        // Validate the launch dimensions against device limits.
514        if config.block_size.product() > self.info.properties.max_threads_per_block as u64 {
515            return Err(TptrError::new(
516                ErrorCode::ConfigurationError,
517                "block exceeds device max threads",
518            ));
519        }
520        // Dispatch against a loaded TPTIR module if present; otherwise fall
521        // back to the timing-only bare-kernel semantics.
522        if let Some(module) = kernel.module() {
523            return self.execute_tptir_module(module, args);
524        }
525        // Record an approximate completion time proportional to the work size
526        // so timing reported by callers is not instant-zero.
527        let work_units = config.num_threads().max(1);
528        let micros = (work_units / 1024).clamp(1, 1_000_000);
529        std::thread::sleep(Duration::from_micros(micros));
530        Ok(())
531    }
532    /// Interpret a compiled TPTIR module against the simulated arena.
533    ///
534    /// Kernel arguments (`args`) are decoded as `u64` device pointers (the
535    /// buffers allocated on this device). The interpreter parses the module's
536    /// body operations (`tptir.load`/`tptir.store`/`tptir.addf`/`tptir.mulf`/
537    /// `tptir.constant`/`tptir.max`/`tptir.return`) and executes them against
538    /// the host-backed arena so a hand-written TPTIR module round-trips
539    /// through `load_module` → `launch_kernel` → result.
540    fn execute_tptir_module(&self, module: &CompiledModule, args: &[Vec<u8>]) -> TptrResult<()> {
541        let pointers: Vec<u64> = args
542            .iter()
543            .map(|a| {
544                let mut buf = [0u8; 8];
545                let n = a.len().min(8);
546                buf[..n].copy_from_slice(&a[..n]);
547                u64::from_le_bytes(buf)
548            })
549            .collect();
550
551        // The compiler's `tptisa` target echoes the parsed region; for a
552        // meaningful execution we parse the *source* ops directly.
553        let ops = parse_tptir_ops(&module.source);
554        if ops.is_empty() {
555            return Err(TptrError::new(
556                ErrorCode::InvalidKernel,
557                "loaded module has no executable operations",
558            ));
559        }
560
561        // Lock every referenced buffer once. Arguments map to %in/%out by
562        // their position in the `func.func` signature (and in `args`).
563        let mut locked: Vec<Option<MutexGuard<'_, Vec<u8>>>> =
564            pointers.iter().map(|p| self.arena.get(*p).ok()).collect();
565        let mut cur_max: Option<f32> = None;
566
567        for op in &ops {
568            match op.kind.as_str() {
569                "max" => {
570                    // reduce_max over the most recently loaded buffer.
571                    let idx = op.src[0].unwrap_or(0) as usize;
572                    let guard = locked.get_mut(idx).and_then(|g| g.take()).ok_or_else(|| {
573                        TptrError::new(
574                            ErrorCode::InvalidAddress,
575                            "reduce_max: input buffer unavailable",
576                        )
577                    })?;
578                    let elems = guard.len() / 4;
579                    let mut m = f32::NEG_INFINITY;
580                    for i in 0..elems {
581                        let off = i * 4;
582                        let v = f32::from_le_bytes([
583                            guard[off],
584                            guard[off + 1],
585                            guard[off + 2],
586                            guard[off + 3],
587                        ]);
588                        if v > m {
589                            m = v;
590                        }
591                    }
592                    cur_max = Some(m);
593                    // return the guard so the buffer stays locked for the session
594                    *locked.get_mut(idx).unwrap() = Some(guard);
595                }
596                "store" => {
597                    let dst_idx = op.dst.unwrap_or(1) as usize;
598                    let mut guard =
599                        locked
600                            .get_mut(dst_idx)
601                            .and_then(|g| g.take())
602                            .ok_or_else(|| {
603                                TptrError::new(
604                                    ErrorCode::InvalidAddress,
605                                    "store: output buffer unavailable",
606                                )
607                            })?;
608                    if let Some(m) = cur_max {
609                        guard[..4].copy_from_slice(&m.to_le_bytes());
610                    }
611                    *locked.get_mut(dst_idx).unwrap() = Some(guard);
612                }
613                "return" | "load" | "constant" | "addf" | "mulf" => { /* no-op in simulated interp beyond tracking */
614                }
615                _ => { /* unknown op: ignore */ }
616            }
617        }
618        Ok(())
619    }
620    /// Lock the backing arena buffer for a live device pointer.
621    #[allow(dead_code)]
622    fn lock_ptr(&self, device_ptr: u64) -> TptrResult<(u64, MutexGuard<'_, Vec<u8>>)> {
623        let guard = self.arena.get(device_ptr)?;
624        let size = guard.len() as u64;
625        Ok((size, guard))
626    }
627    pub fn synchronize(&mut self) {
628        // Drain any pending commands in the scheduler, executing them against
629        // the simulated backend so submitted work actually completes.
630        while let Some((_qh, _id, cmd)) = self.scheduler.dequeue_next() {
631            let _ = self.dispatch_command(cmd);
632        }
633        // Flush any real-device work (e.g. async copies issued by the vendor
634        // backends) so callers observe completed results.
635        if let Some(backend) = &self.backend {
636            let _ = backend.synchronize();
637        }
638    }
639    fn dispatch_command(&self, cmd: Command) -> TptrResult<()> {
640        match cmd {
641            Command::Memcpy {
642                dst,
643                src,
644                size,
645                dst_offset,
646                src_offset,
647            } => {
648                // Generic device->device copy through the host arena.
649                let mut tmp = vec![0u8; size as usize];
650                self.memcpy_dtoh(&mut tmp, &src, size, src_offset)?;
651                self.memcpy_htod(&dst, &tmp, size, dst_offset)
652            }
653            Command::Barrier | Command::SignalEvent(_) | Command::WaitEvent(_) => Ok(()),
654            Command::Allocate { .. }
655            | Command::Free(_)
656            | Command::Memset { .. }
657            | Command::LaunchKernel { .. } => Ok(()),
658        }
659    }
660    pub fn pending_commands(&self) -> usize {
661        self.scheduler.total_pending()
662    }
663}
664
665#[cfg(test)]
666mod tests {
667    use super::*;
668    use crate::memory::MemoryRegion;
669    fn test_device() -> Device {
670        Device::new_simulated(0, DeviceProperties::simulated("TPT Sim v1", 16 << 30))
671    }
672    #[test]
673    fn test_creation() {
674        let d = test_device();
675        assert_eq!(d.handle(), DeviceHandle(0));
676        assert_eq!(d.properties().name, "TPT Sim v1");
677    }
678    #[test]
679    fn test_alloc_free() {
680        let mut d = test_device();
681        let m = d
682            .allocate(
683                4096,
684                MemoryRegion::Global,
685                MemType::Device,
686                MemAccess::ReadWrite,
687            )
688            .unwrap();
689        assert!(m.device_ptr() > 0);
690        d.free(&m).unwrap();
691        assert!(m.is_freed());
692    }
693    #[test]
694    fn test_kernel_launch() {
695        let d = test_device();
696        let kernel = d.create_kernel("test");
697        let config = KernelConfig::new((1, 1, 1), (256, 1, 1));
698        let handle = d.launch_kernel(&kernel, &config, &[]);
699        assert!(handle.is_complete());
700    }
701    #[test]
702    fn test_queues() {
703        let mut d = test_device();
704        let qh = d.create_queue(QueuePriority::Normal, 64);
705        let id = d
706            .submit(qh, Command::Barrier, QueuePriority::Normal)
707            .unwrap();
708        assert_eq!(id, 1);
709        assert_eq!(d.pending_commands(), 1);
710    }
711    #[test]
712    fn test_properties() {
713        let d = test_device();
714        let p = d.properties();
715        assert_eq!(p.backend, Backend::Simulated);
716        assert_eq!(p.warp_size, 32);
717    }
718
719    #[test]
720    fn test_memcpy_roundtrip() {
721        let mut d = test_device();
722        let m = d
723            .allocate(
724                16,
725                MemoryRegion::Global,
726                MemType::Device,
727                MemAccess::ReadWrite,
728            )
729            .unwrap();
730        let host = [1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
731        d.memcpy_htod(&m, &host, 16, 0).unwrap();
732        let mut out = [0u8; 16];
733        d.memcpy_dtoh(&mut out, &m, 16, 0).unwrap();
734        assert_eq!(host, out);
735    }
736
737    #[test]
738    fn test_memcpy_partial() {
739        let mut d = test_device();
740        let m = d
741            .allocate(
742                16,
743                MemoryRegion::Global,
744                MemType::Device,
745                MemAccess::ReadWrite,
746            )
747            .unwrap();
748        let host = [9u8; 4];
749        d.memcpy_htod(&m, &host, 4, 8).unwrap();
750        let mut out = [0u8; 16];
751        d.memcpy_dtoh(&mut out, &m, 16, 0).unwrap();
752        assert_eq!(&out[8..12], &[9u8; 4]);
753        assert_eq!(&out[0..8], &[0u8; 8]);
754    }
755
756    #[test]
757    fn test_memcpy_bounds() {
758        let mut d = test_device();
759        // BuddyAllocator rounds up to its min block size, so allocate a block
760        // larger than 8 bytes and assert that over-sized / over-offset copies
761        // are rejected.
762        let m = d
763            .allocate(
764                8192,
765                MemoryRegion::Global,
766                MemType::Device,
767                MemAccess::ReadWrite,
768            )
769            .unwrap();
770        let host = [1u8; 16];
771        assert!(d.memcpy_htod(&m, &host, 9000, 0).is_err());
772        assert!(d.memcpy_htod(&m, &host, 16, 8192).is_err());
773    }
774
775    #[test]
776    fn test_synchronize_drains_queue() {
777        let mut d = test_device();
778        let qh = d.create_queue(QueuePriority::Normal, 64);
779        d.submit(qh, Command::Barrier, QueuePriority::Normal)
780            .unwrap();
781        d.submit(qh, Command::Barrier, QueuePriority::Normal)
782            .unwrap();
783        assert_eq!(d.pending_commands(), 2);
784        d.synchronize();
785        assert_eq!(d.pending_commands(), 0);
786    }
787
788    #[test]
789    fn test_load_module_reduce_max() {
790        // Hand-written TPTIR module (matching tpt-archon's emit_topk shape):
791        // load input buffer, reduce_max, store scalar result.
792        let module = r#"
793module {
794  func.func @reduce_max(%in: memref<*xf32>, %out: memref<*xf32>) attributes {tptir.kernel} {
795    ^entry:
796      %v = tptir.load(%in)
797      %m = tptir.max(%v)
798      tptir.store(%m, %out)
799      tptir.return
800  }
801}
802"#;
803        let mut d = test_device();
804        let kernel = d.load_module(module).unwrap();
805        assert_eq!(kernel.name(), "reduce_max");
806        assert!(kernel.module().is_some());
807
808        let input = d
809            .allocate(
810                32,
811                MemoryRegion::Global,
812                MemType::Device,
813                MemAccess::ReadWrite,
814            )
815            .unwrap();
816        let output = d
817            .allocate(
818                32,
819                MemoryRegion::Global,
820                MemType::Device,
821                MemAccess::ReadWrite,
822            )
823            .unwrap();
824        let data: [f32; 8] = [1.0, -3.0, 7.5, 2.0, 0.0, 4.0, -1.0, 9.25];
825        let mut bytes = Vec::new();
826        for f in &data {
827            bytes.extend_from_slice(&f.to_le_bytes());
828        }
829        d.memcpy_htod(&input, &bytes, 32, 0).unwrap();
830
831        let args = vec![
832            input.device_ptr().to_le_bytes().to_vec(),
833            output.device_ptr().to_le_bytes().to_vec(),
834        ];
835        let config = KernelConfig::new((1, 1, 1), (1, 1, 1));
836        let handle = d.launch_kernel(&kernel, &config, &args);
837        assert!(handle.is_complete());
838
839        let mut out = [0u8; 4];
840        d.memcpy_dtoh(&mut out, &output, 4, 0).unwrap();
841        let result = f32::from_le_bytes(out);
842        assert_eq!(result, 9.25);
843    }
844
845    #[test]
846    #[cfg(feature = "cuda")]
847    fn test_new_cuda_real_memcpy_roundtrip() {
848        // Verifies that `Device::new_cuda` opens a real CUDA context and that
849        // allocate / memcpy_htod / memcpy_dtoh round-trip real bytes through the
850        // GPU (checked against the RTX 3050 on this machine).
851        let mut d = match Device::new_cuda(0) {
852            Ok(d) => d,
853            Err(_) => {
854                eprintln!("no CUDA device available, skipping");
855                return;
856            }
857        };
858        assert!(d.is_real());
859        assert_eq!(d.properties().backend, Backend::CUDA);
860
861        let m = d
862            .allocate(
863                16,
864                MemoryRegion::Global,
865                MemType::Device,
866                MemAccess::ReadWrite,
867            )
868            .unwrap();
869        assert!(m.device_ptr() > 0);
870        let host = [1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
871        d.memcpy_htod(&m, &host, 16, 0).unwrap();
872        let mut out = [0u8; 16];
873        d.memcpy_dtoh(&mut out, &m, 16, 0).unwrap();
874        assert_eq!(host, out);
875        d.free(&m).unwrap();
876        assert!(m.is_freed());
877    }
878}