metrale_core/
device.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! 2026-09-25: GB10 hardware constants (`sm121`) and the cudarc device handle `MetraleDevice`.
4//!
5//! `sm121` compiles on every feature set; `MetraleDevice` only with the `cuda`
6//! feature.
7//!
8//! Owner: core.
9//! Invariants: none beyond the types.
10
11/// 2026-09-25: GB10 (SM 12.1) hardware constants. No code in the workspace
12/// reads them; `NUM_SMS` and `MEMORY_BW_GBS` match `sm_count` and
13/// `memory_bandwidth_gbps` in `kernels/gb10/HARDWARE.toml`.
14pub mod sm121 {
15    pub const NUM_SMS: u32 = 48;
16
17    /// 2026-09-25: Shared memory per SM, in bytes.
18    pub const SMEM_PER_SM: usize = 99 * 1024;
19
20    pub const MAX_REGS_PER_THREAD: u32 = 255;
21
22    pub const MAX_THREADS_PER_BLOCK: u32 = 1024;
23
24    pub const WARP_SIZE: u32 = 32;
25
26    /// 2026-09-25: Memory bandwidth in GB/s.
27    pub const MEMORY_BW_GBS: f64 = 273.0;
28
29    pub const COMPUTE_MAJOR: u32 = 12;
30    pub const COMPUTE_MINOR: u32 = 1;
31}
32
33#[cfg(feature = "cuda")]
34mod cuda_impl {
35    use cudarc::driver::CudaContext;
36    use std::sync::Arc;
37
38    use crate::error::{MetraleError, Result};
39
40    /// 2026-09-25: A cudarc `CudaContext` and the GPU ordinal it was created on.
41    #[derive(Clone)]
42    pub struct MetraleDevice {
43        pub ctx: Arc<CudaContext>,
44        pub ordinal: usize,
45    }
46
47    impl MetraleDevice {
48        /// 2026-09-25: Create a CUDA context on GPU `ordinal`; a driver failure
49        /// is returned as `MetraleError::CudaDriver`.
50        pub fn new(ordinal: usize) -> Result<Self> {
51            let ctx = CudaContext::new(ordinal).map_err(MetraleError::CudaDriver)?;
52            Ok(Self { ctx, ordinal })
53        }
54    }
55}
56
57#[cfg(feature = "cuda")]
58pub use cuda_impl::MetraleDevice;