scirs2-linalg 0.4.2

Linear algebra module for SciRS2 (scirs2-linalg)
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
//! GPU-accelerated matrix operations: core types and error definitions
//!
//! This module provides a pure-Rust simulated GPU abstraction that models the
//! GPU programming model (buffers, capabilities, backends) without requiring
//! any C/Fortran dependencies or hardware-specific feature flags.  When real
//! OxiBLAS GPU backends become available they can slot in behind the same
//! public API.
//!
//! ## Design
//!
//! - [`GpuBackendKind`]: selects between the CPU fallback, the pure-Rust
//!   simulated-GPU tile engine, or the OxiBLAS GPU backend (future).
//! - [`GpuMatrixBuffer`]: a CPU-backed representation of a matrix "on device",
//!   tracking shape metadata alongside the flat data vector.
//! - [`GpuError`] / [`GpuResult`]: error type hierarchy for all GPU operations.
//! - [`GpuCapabilities`]: describes the (simulated) device capabilities.
//! - [`GpuMatrixConfig`]: tuning knobs for GEMM and dispatch.

use std::fmt;

// ─── Backend selection ────────────────────────────────────────────────────────

/// Selects the compute backend for GPU-style matrix operations.
///
/// The enum is `#[non_exhaustive]` so future backends (real OxiBLAS GPU,
/// Vulkan compute, WebGPU, …) can be added without a breaking change.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum GpuBackendKind {
    /// Pure CPU path — uses the 3-level cache-blocked GEMM from `gpu_gemm`.
    /// Always available and chosen automatically for small matrices.
    Cpu,
    /// Pure-Rust simulated GPU — models the tiled GEMM execution pattern
    /// (32×32 tiles) on top of Vecs.  Useful for testing the GPU code path
    /// without hardware and as a functional reference.
    Simulated,
    /// OxiBLAS GPU backend.  Activated when the `gpu` feature flag is enabled
    /// and a compatible OxiBLAS GPU runtime is present.  Falls back silently to
    /// `Simulated` when the runtime is absent.
    OxiBlasGpu,
}

impl Default for GpuBackendKind {
    fn default() -> Self {
        Self::Simulated
    }
}

impl fmt::Display for GpuBackendKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Cpu => write!(f, "CPU"),
            Self::Simulated => write!(f, "Simulated-GPU"),
            Self::OxiBlasGpu => write!(f, "OxiBLAS-GPU"),
        }
    }
}

// ─── Configuration ────────────────────────────────────────────────────────────

/// Tuning configuration for GPU-style matrix operations.
///
/// All fields have sensible defaults via [`Default`].
#[derive(Clone, Debug)]
pub struct GpuMatrixConfig {
    /// Which backend to use.  Defaults to [`GpuBackendKind::Simulated`].
    pub backend: GpuBackendKind,
    /// Tile edge length for the tiled GEMM kernel (both row and column tiles).
    /// Must be ≥ 1.  Larger tiles improve cache reuse up to a point; 32 is a
    /// good default for simulated execution.
    pub tile_size: usize,
    /// Hint that the workload may benefit from tensor-core style accumulation.
    /// Currently advisory only; reserved for future OxiBLAS GPU integration.
    pub use_tensor_cores: bool,
    /// Soft limit on device memory the operation is allowed to use, in MiB.
    /// Zero means "no limit".
    pub memory_limit_mb: u64,
    /// Minimum `m * n * k` product below which [`GpuBackendKind::Cpu`] is
    /// chosen even when `backend` is set to `Simulated` or `OxiBlasGpu`.
    pub gpu_threshold: usize,
}

impl Default for GpuMatrixConfig {
    fn default() -> Self {
        Self {
            backend: GpuBackendKind::default(),
            tile_size: 32,
            use_tensor_cores: false,
            memory_limit_mb: 0,
            gpu_threshold: 1_000_000, // 10³ elements per side
        }
    }
}

// ─── GPU buffer ───────────────────────────────────────────────────────────────

/// A CPU-backed matrix buffer that models a GPU device allocation.
///
/// Data is stored in row-major (C) order as a flat `Vec<T>`.  The type
/// intentionally does not implement `Copy` — callers should pass references
/// or use [`clone`](GpuMatrixBuffer::clone) explicitly.
#[derive(Clone, Debug)]
pub struct GpuMatrixBuffer<T> {
    /// Flat row-major data: element (i, j) is at `data[i * cols + j]`.
    pub(crate) data: Vec<T>,
    /// Number of rows.
    pub rows: usize,
    /// Number of columns.
    pub cols: usize,
}

impl<T: Clone + Default> GpuMatrixBuffer<T> {
    /// Allocate a zeroed buffer of shape `rows × cols`.
    pub fn zeros(rows: usize, cols: usize) -> Self {
        Self {
            data: vec![T::default(); rows * cols],
            rows,
            cols,
        }
    }

    /// Create a buffer from existing row-major flat data.
    ///
    /// # Errors
    ///
    /// Returns [`GpuError::DimensionMismatch`] when `data.len() != rows * cols`.
    pub fn from_slice(data: &[T], rows: usize, cols: usize) -> GpuResult<Self> {
        let expected = rows.checked_mul(cols).ok_or(GpuError::OutOfMemory {
            requested_bytes: u64::MAX,
            available_bytes: 0,
        })?;
        if data.len() != expected {
            return Err(GpuError::DimensionMismatch {
                expected,
                got: data.len(),
                context: "GpuMatrixBuffer::from_slice: data length mismatch".to_string(),
            });
        }
        Ok(Self {
            data: data.to_vec(),
            rows,
            cols,
        })
    }

    /// Number of elements stored in the buffer.
    #[inline]
    pub fn len(&self) -> usize {
        self.data.len()
    }

    /// Returns `true` if the buffer contains no elements.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    /// Immutable view of the flat row-major data.
    #[inline]
    pub fn as_slice(&self) -> &[T] {
        &self.data
    }

    /// Mutable view of the flat row-major data.
    #[inline]
    pub fn as_mut_slice(&mut self) -> &mut [T] {
        &mut self.data
    }

    /// Read element at position `(row, col)`.
    ///
    /// # Errors
    ///
    /// Returns [`GpuError::DimensionMismatch`] if either index is out of range.
    #[inline]
    pub fn get(&self, row: usize, col: usize) -> GpuResult<&T> {
        if row >= self.rows || col >= self.cols {
            return Err(GpuError::DimensionMismatch {
                expected: self.rows * self.cols,
                got: row * self.cols + col,
                context: format!(
                    "GpuMatrixBuffer::get index ({row},{col}) out of bounds for {}×{}",
                    self.rows, self.cols
                ),
            });
        }
        Ok(&self.data[row * self.cols + col])
    }

    /// Mutable reference to element at `(row, col)`.
    ///
    /// # Errors
    ///
    /// Returns [`GpuError::DimensionMismatch`] if either index is out of range.
    #[inline]
    pub fn get_mut(&mut self, row: usize, col: usize) -> GpuResult<&mut T> {
        if row >= self.rows || col >= self.cols {
            return Err(GpuError::DimensionMismatch {
                expected: self.rows * self.cols,
                got: row * self.cols + col,
                context: format!(
                    "GpuMatrixBuffer::get_mut index ({row},{col}) out of bounds for {}×{}",
                    self.rows, self.cols
                ),
            });
        }
        Ok(&mut self.data[row * self.cols + col])
    }
}

// ─── Error types ─────────────────────────────────────────────────────────────

/// Error type for GPU-style matrix operations.
///
/// `#[non_exhaustive]` — new variants may be added in future releases.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum GpuError {
    /// The operation would exceed available (simulated) device memory.
    OutOfMemory {
        /// Number of bytes the operation requested.
        requested_bytes: u64,
        /// Number of bytes currently available.
        available_bytes: u64,
    },
    /// The requested operation is not supported by the selected backend.
    UnsupportedOperation {
        /// Human-readable description of what was attempted.
        operation: String,
        /// Which backend rejected the operation.
        backend: String,
    },
    /// No suitable GPU backend could be found or initialised.
    BackendUnavailable {
        /// Human-readable reason.
        reason: String,
    },
    /// Matrix or vector dimension mismatch.
    DimensionMismatch {
        /// Expected size / product.
        expected: usize,
        /// Actual size / product received.
        got: usize,
        /// Context string for debugging.
        context: String,
    },
    /// An integer overflow occurred while computing a buffer size.
    SizeOverflow {
        /// Description of what overflowed.
        detail: String,
    },
}

impl fmt::Display for GpuError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::OutOfMemory {
                requested_bytes,
                available_bytes,
            } => write!(
                f,
                "GPU out-of-memory: requested {requested_bytes} bytes, \
                 only {available_bytes} bytes available"
            ),
            Self::UnsupportedOperation { operation, backend } => {
                write!(
                    f,
                    "GPU unsupported operation '{operation}' on backend '{backend}'"
                )
            }
            Self::BackendUnavailable { reason } => {
                write!(f, "GPU backend unavailable: {reason}")
            }
            Self::DimensionMismatch {
                expected,
                got,
                context,
            } => write!(
                f,
                "GPU dimension mismatch (expected {expected}, got {got}): {context}"
            ),
            Self::SizeOverflow { detail } => {
                write!(f, "GPU size overflow: {detail}")
            }
        }
    }
}

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

/// Convenience `Result` alias for GPU operations.
pub type GpuResult<T> = Result<T, GpuError>;

// ─── Capabilities ─────────────────────────────────────────────────────────────

/// Simulated GPU device capabilities.
#[derive(Clone, Debug)]
pub struct GpuCapabilities {
    /// Whether the device supports IEEE 754 half-precision (FP16).
    pub has_fp16: bool,
    /// Whether the device exposes tensor-core style mixed-precision units.
    pub has_tensor_cores: bool,
    /// Simulated video RAM in gigabytes.
    pub vram_gb: f64,
    /// Human-readable device name.
    pub name: String,
    /// Number of simulated compute units (analogous to CUDA SMs).
    pub compute_units: u32,
    /// Preferred warp / wavefront width (elements processed together).
    pub warp_size: u32,
    /// Maximum total elements in a single kernel launch.
    pub max_buffer_elements: usize,
}

impl Default for GpuCapabilities {
    fn default() -> Self {
        detect_gpu_capabilities()
    }
}

/// Return a [`GpuCapabilities`] struct describing the simulated device.
///
/// In the absence of real hardware the values are fixed at reasonable
/// mid-range defaults that reflect a typical discrete GPU.
pub fn detect_gpu_capabilities() -> GpuCapabilities {
    GpuCapabilities {
        has_fp16: true,
        has_tensor_cores: false, // Tensor-core emulation not yet implemented
        vram_gb: 8.0,
        name: "SciRS2 Simulated GPU (OxiBLAS backend)".to_string(),
        compute_units: 64,
        warp_size: 32,
        max_buffer_elements: 1 << 30, // 1 billion elements
    }
}

// ─── Tests ────────────────────────────────────────────────────────────────────

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

    #[test]
    fn test_gpu_backend_kind_default() {
        assert_eq!(GpuBackendKind::default(), GpuBackendKind::Simulated);
    }

    #[test]
    fn test_gpu_matrix_config_default() {
        let cfg = GpuMatrixConfig::default();
        assert_eq!(cfg.tile_size, 32);
        assert!(!cfg.use_tensor_cores);
        assert_eq!(cfg.memory_limit_mb, 0);
        assert_eq!(cfg.gpu_threshold, 1_000_000);
    }

    #[test]
    fn test_gpu_buffer_zeros() {
        let buf = GpuMatrixBuffer::<f64>::zeros(3, 4);
        assert_eq!(buf.rows, 3);
        assert_eq!(buf.cols, 4);
        assert_eq!(buf.len(), 12);
        assert!(buf.as_slice().iter().all(|&v| v == 0.0));
    }

    #[test]
    fn test_gpu_buffer_from_slice_ok() {
        let data: Vec<f64> = (0..6).map(|i| i as f64).collect();
        let buf = GpuMatrixBuffer::from_slice(&data, 2, 3).unwrap();
        assert_eq!(buf.rows, 2);
        assert_eq!(buf.cols, 3);
        assert_eq!(*buf.get(0, 2).unwrap(), 2.0);
        assert_eq!(*buf.get(1, 0).unwrap(), 3.0);
    }

    #[test]
    fn test_gpu_buffer_from_slice_mismatch() {
        let data = vec![1.0_f64; 5];
        let result = GpuMatrixBuffer::from_slice(&data, 2, 3);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            GpuError::DimensionMismatch { .. }
        ));
    }

    #[test]
    fn test_gpu_buffer_get_out_of_bounds() {
        let buf = GpuMatrixBuffer::<f64>::zeros(2, 2);
        assert!(buf.get(2, 0).is_err());
        assert!(buf.get(0, 2).is_err());
    }

    #[test]
    fn test_gpu_error_display_out_of_memory() {
        let err = GpuError::OutOfMemory {
            requested_bytes: 1024,
            available_bytes: 512,
        };
        let msg = err.to_string();
        assert!(msg.contains("out-of-memory"));
        assert!(msg.contains("1024"));
    }

    #[test]
    fn test_gpu_error_display_dimension_mismatch() {
        let err = GpuError::DimensionMismatch {
            expected: 6,
            got: 4,
            context: "test".to_string(),
        };
        let msg = err.to_string();
        assert!(msg.contains("dimension mismatch"));
        assert!(msg.contains("6"));
        assert!(msg.contains("4"));
    }

    #[test]
    fn test_detect_gpu_capabilities() {
        let caps = detect_gpu_capabilities();
        assert!(caps.vram_gb > 0.0);
        assert!(caps.compute_units > 0);
        assert!(!caps.name.is_empty());
    }

    #[test]
    fn test_gpu_backend_kind_display() {
        assert_eq!(GpuBackendKind::Cpu.to_string(), "CPU");
        assert_eq!(GpuBackendKind::Simulated.to_string(), "Simulated-GPU");
        assert_eq!(GpuBackendKind::OxiBlasGpu.to_string(), "OxiBLAS-GPU");
    }
}