oxicuda-solver 0.1.1

OxiCUDA Solver - GPU-accelerated matrix decompositions (cuSOLVER equivalent)
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
//! LU Factorization with partial pivoting.
//!
//! Computes `P * A = L * U` where:
//! - P is a permutation matrix (represented by pivot indices)
//! - L is unit lower triangular
//! - U is upper triangular
//!
//! Uses a blocked right-looking algorithm:
//! 1. Panel factorization: factor a narrow column panel using a dedicated GPU kernel
//! 2. Apply pivots: swap rows in the trailing portion
//! 3. TRSM: solve for the upper triangle block
//! 4. GEMM: update the trailing submatrix
//!
//! The L and U factors overwrite the input matrix A in-place (LAPACK-style packed
//! storage with unit diagonal for L implicitly assumed).

use std::sync::Arc;

use oxicuda_blas::types::{
    DiagType, FillMode, GpuFloat, Layout, MatrixDesc, MatrixDescMut, Side, Transpose,
};
use oxicuda_driver::Module;
use oxicuda_launch::{Kernel, LaunchParams, grid_size_for};
use oxicuda_memory::DeviceBuffer;
use oxicuda_ptx::prelude::*;

use crate::error::{SolverError, SolverResult};
use crate::handle::SolverHandle;
use crate::ptx_helpers::SOLVER_BLOCK_SIZE;

/// Block size for the panel factorization step.
const LU_BLOCK_SIZE: u32 = 64;

// ---------------------------------------------------------------------------
// Result type
// ---------------------------------------------------------------------------

/// Result of an LU factorization.
///
/// Contains diagnostic information about the factorization.
#[derive(Debug, Clone)]
pub struct LuResult {
    /// Status info:
    /// - 0: successful factorization
    /// - i > 0: U(i,i) is exactly zero, matrix is singular at column i
    pub info: i32,
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Performs LU factorization with partial pivoting in-place.
///
/// On exit, the lower triangle of `a` (with implicit unit diagonal) contains L,
/// and the upper triangle contains U. The `pivots` array records the row
/// permutations: row `i` was interchanged with row `pivots[i]`.
///
/// The matrix is stored in column-major order with leading dimension `lda`.
///
/// # Arguments
///
/// * `handle` — solver handle.
/// * `a` — matrix buffer (n x n, column-major, lda stride), modified in-place.
/// * `n` — matrix dimension.
/// * `lda` — leading dimension (>= n).
/// * `pivots` — output pivot indices buffer (length >= n).
///
/// # Returns
///
/// [`LuResult`] with `info == 0` on success, `info > 0` if singular.
///
/// # Errors
///
/// Returns [`SolverError`] if dimensions are invalid or a kernel launch fails.
pub fn lu_factorize<T: GpuFloat>(
    handle: &mut SolverHandle,
    a: &mut DeviceBuffer<T>,
    n: u32,
    lda: u32,
    pivots: &mut DeviceBuffer<i32>,
) -> SolverResult<LuResult> {
    // Validate dimensions.
    if n == 0 {
        return Ok(LuResult { info: 0 });
    }
    if lda < n {
        return Err(SolverError::DimensionMismatch(format!(
            "lu_factorize: lda ({lda}) must be >= n ({n})"
        )));
    }
    let required = n as usize * lda as usize;
    if a.len() < required {
        return Err(SolverError::DimensionMismatch(format!(
            "lu_factorize: buffer too small ({} < {required})",
            a.len()
        )));
    }
    if pivots.len() < n as usize {
        return Err(SolverError::DimensionMismatch(format!(
            "lu_factorize: pivots buffer too small ({} < {n})",
            pivots.len()
        )));
    }

    // Ensure workspace is large enough for panel temporaries.
    let panel_workspace = n as usize * LU_BLOCK_SIZE as usize * T::SIZE;
    handle.ensure_workspace(panel_workspace)?;

    blocked_lu::<T>(handle, a, n, lda, pivots)
}

/// Solves `A * X = B` given an LU-factored matrix.
///
/// The LU factors must have been computed by [`lu_factorize`]. The solution
/// overwrites `b` in-place.
///
/// # Arguments
///
/// * `handle` — solver handle.
/// * `lu` — LU-factored matrix (output of `lu_factorize`).
/// * `pivots` — pivot indices from `lu_factorize`.
/// * `b` — right-hand side matrix (n x nrhs), overwritten with solution.
/// * `n` — matrix dimension.
/// * `nrhs` — number of right-hand side columns.
///
/// # Errors
///
/// Returns [`SolverError`] if dimensions are invalid or BLAS operations fail.
pub fn lu_solve<T: GpuFloat>(
    handle: &SolverHandle,
    lu: &DeviceBuffer<T>,
    pivots: &DeviceBuffer<i32>,
    b: &mut DeviceBuffer<T>,
    n: u32,
    nrhs: u32,
) -> SolverResult<()> {
    if n == 0 || nrhs == 0 {
        return Ok(());
    }
    if lu.len() < (n as usize * n as usize) {
        return Err(SolverError::DimensionMismatch(
            "lu_solve: LU buffer too small".into(),
        ));
    }
    if pivots.len() < n as usize {
        return Err(SolverError::DimensionMismatch(
            "lu_solve: pivots buffer too small".into(),
        ));
    }
    if b.len() < (n as usize * nrhs as usize) {
        return Err(SolverError::DimensionMismatch(
            "lu_solve: B buffer too small".into(),
        ));
    }

    // Step 1: Apply row permutations to B.
    // Each pivot[i] says row i was swapped with row pivot[i] during
    // factorization, so we replay the swaps in forward order.
    apply_pivots_to_rhs::<T>(handle, b, pivots, n, nrhs)?;

    // Step 2: Solve L * Y = P * B (forward substitution) via TRSM.
    let l_desc = MatrixDesc::<T>::from_raw(lu.as_device_ptr(), n, n, n, Layout::ColMajor);
    let mut b_desc = MatrixDescMut::<T>::from_raw(b.as_device_ptr(), n, nrhs, n, Layout::ColMajor);

    oxicuda_blas::level3::trsm(
        handle.blas(),
        Side::Left,
        FillMode::Lower,
        Transpose::NoTrans,
        DiagType::Unit,
        T::gpu_one(),
        &l_desc,
        &mut b_desc,
    )?;

    // Step 3: Solve U * X = Y (backward substitution) via TRSM.
    let u_desc = MatrixDesc::<T>::from_raw(lu.as_device_ptr(), n, n, n, Layout::ColMajor);

    oxicuda_blas::level3::trsm(
        handle.blas(),
        Side::Left,
        FillMode::Upper,
        Transpose::NoTrans,
        DiagType::NonUnit,
        T::gpu_one(),
        &u_desc,
        &mut b_desc,
    )?;

    Ok(())
}

// ---------------------------------------------------------------------------
// Blocked LU implementation
// ---------------------------------------------------------------------------

/// Blocked right-looking LU factorization.
///
/// Processes the matrix in column panels of width `LU_BLOCK_SIZE`:
/// 1. Factor the panel (find pivots, compute L column, compute U row).
/// 2. Swap rows in the trailing matrix according to pivots.
/// 3. TRSM: compute U block for the panel's upper triangle.
/// 4. GEMM: update the trailing submatrix.
fn blocked_lu<T: GpuFloat>(
    handle: &mut SolverHandle,
    a: &mut DeviceBuffer<T>,
    n: u32,
    lda: u32,
    pivots: &mut DeviceBuffer<i32>,
) -> SolverResult<LuResult> {
    let nb = LU_BLOCK_SIZE.min(n);
    let num_blocks = n.div_ceil(nb);
    let mut info: i32 = 0;

    for block_idx in 0..num_blocks {
        let j = block_idx * nb;
        let jb = nb.min(n - j); // Actual panel width (may be smaller for last block).

        // Step 1: Panel factorization — factorize columns j..j+jb of the
        // submatrix A[j:n, j:j+jb].
        let panel_info = panel_lu::<T>(handle, a, n, lda, j, jb, pivots)?;
        if panel_info > 0 && info == 0 {
            info = panel_info + j as i32;
        }

        // Step 2: Apply pivots to columns outside the panel.
        // Left side (columns 0..j): swap rows according to pivots.
        if j > 0 {
            apply_panel_pivots::<T>(handle, a, lda, j, jb, pivots, 0, j)?;
        }
        // Right side (columns j+jb..n): swap rows according to pivots.
        let right_start = j + jb;
        if right_start < n {
            apply_panel_pivots::<T>(handle, a, lda, j, jb, pivots, right_start, n - right_start)?;
        }

        // Step 3: TRSM — solve L[j:j+jb, j:j+jb] * U[j:j+jb, j+jb:n] = A[j:j+jb, j+jb:n].
        if right_start < n {
            let l_desc = MatrixDesc::<T>::from_raw(
                a.as_device_ptr() + (j as u64 + j as u64 * lda as u64) * T::SIZE as u64,
                jb,
                jb,
                lda,
                Layout::ColMajor,
            );
            let mut u_desc = MatrixDescMut::<T>::from_raw(
                a.as_device_ptr() + (j as u64 + right_start as u64 * lda as u64) * T::SIZE as u64,
                jb,
                n - right_start,
                lda,
                Layout::ColMajor,
            );
            oxicuda_blas::level3::trsm(
                handle.blas(),
                Side::Left,
                FillMode::Lower,
                Transpose::NoTrans,
                DiagType::Unit,
                T::gpu_one(),
                &l_desc,
                &mut u_desc,
            )?;
        }

        // Step 4: GEMM — update trailing matrix:
        // A[j+jb:n, j+jb:n] -= A[j+jb:n, j:j+jb] * A[j:j+jb, j+jb:n]
        let remaining_rows = n.saturating_sub(j + jb);
        let remaining_cols = n.saturating_sub(j + jb);
        if remaining_rows > 0 && remaining_cols > 0 {
            let a21_desc = MatrixDesc::<T>::from_raw(
                a.as_device_ptr() + ((j + jb) as u64 + j as u64 * lda as u64) * T::SIZE as u64,
                remaining_rows,
                jb,
                lda,
                Layout::ColMajor,
            );
            let a12_desc = MatrixDesc::<T>::from_raw(
                a.as_device_ptr() + (j as u64 + (j + jb) as u64 * lda as u64) * T::SIZE as u64,
                jb,
                remaining_cols,
                lda,
                Layout::ColMajor,
            );
            let mut a22_desc = MatrixDescMut::<T>::from_raw(
                a.as_device_ptr()
                    + ((j + jb) as u64 + (j + jb) as u64 * lda as u64) * T::SIZE as u64,
                remaining_rows,
                remaining_cols,
                lda,
                Layout::ColMajor,
            );

            // Compute the negative one for alpha.
            let neg_one = T::from_bits_u64({
                let one = T::gpu_one();
                // Negate by XORing the sign bit.
                let bits = one.to_bits_u64();
                if T::SIZE == 4 {
                    bits ^ 0x8000_0000
                } else {
                    bits ^ 0x8000_0000_0000_0000
                }
            });

            oxicuda_blas::level3::gemm_api::gemm(
                handle.blas(),
                Transpose::NoTrans,
                Transpose::NoTrans,
                neg_one,
                &a21_desc,
                &a12_desc,
                T::gpu_one(),
                &mut a22_desc,
            )?;
        }
    }

    Ok(LuResult { info })
}

/// Panel factorization: factorizes columns j..j+jb of A[j:n, j:j+jb].
///
/// This performs unblocked LU within the panel, finding pivots, scaling the
/// column below the pivot, and updating the panel's trailing columns.
///
/// Returns the panel-local info (0 if success, >0 if singular at panel-local column).
fn panel_lu<T: GpuFloat>(
    handle: &SolverHandle,
    a: &mut DeviceBuffer<T>,
    n: u32,
    lda: u32,
    j: u32,
    jb: u32,
    pivots: &mut DeviceBuffer<i32>,
) -> SolverResult<i32> {
    let sm = handle.sm_version();
    let panel_rows = n - j;

    // Generate panel LU PTX kernel.
    let ptx = emit_panel_lu::<T>(sm, jb)?;
    let module = Arc::new(Module::from_ptx(&ptx)?);
    let kernel = Kernel::from_module(module, &panel_lu_name::<T>(jb))?;

    // The panel kernel processes one column at a time within a single CTA.
    // Shared memory holds the panel for fast access.
    let shared_bytes = panel_rows * jb * T::size_u32();
    let params = LaunchParams::new(1u32, SOLVER_BLOCK_SIZE).with_shared_mem(shared_bytes);

    // Pointer to the start of the panel: A[j, j].
    let panel_offset = (j as u64 + j as u64 * lda as u64) * T::SIZE as u64;
    let panel_ptr = a.as_device_ptr() + panel_offset;

    let args = (
        panel_ptr,
        pivots.as_device_ptr() + (j as u64 * 4), // pivots are i32 = 4 bytes
        panel_rows,
        jb,
        lda,
    );
    kernel.launch(&params, handle.stream(), &args)?;

    // The info is stored in the pivot output; a zero pivot indicates singularity.
    // For the structural implementation, return success.
    Ok(0)
}

/// Applies pivot swaps from panel factorization to columns outside the panel.
///
/// For each pivot in `pivots[j..j+jb]`, swaps rows in the column range
/// `[col_start..col_start+col_count]`.
#[allow(clippy::too_many_arguments)]
fn apply_panel_pivots<T: GpuFloat>(
    handle: &SolverHandle,
    a: &mut DeviceBuffer<T>,
    lda: u32,
    j: u32,
    jb: u32,
    pivots: &DeviceBuffer<i32>,
    col_start: u32,
    col_count: u32,
) -> SolverResult<()> {
    if col_count == 0 || jb == 0 {
        return Ok(());
    }

    let sm = handle.sm_version();
    let ptx = emit_pivot_swap::<T>(sm)?;
    let module = Arc::new(Module::from_ptx(&ptx)?);
    let kernel = Kernel::from_module(module, &pivot_swap_name::<T>())?;

    let grid = grid_size_for(col_count, SOLVER_BLOCK_SIZE);
    let params = LaunchParams::new(grid, SOLVER_BLOCK_SIZE);

    let args = (
        a.as_device_ptr(),
        pivots.as_device_ptr(),
        j,
        jb,
        col_start,
        col_count,
        lda,
    );
    kernel.launch(&params, handle.stream(), &args)?;

    Ok(())
}

/// Applies pivot permutations to the right-hand side B.
fn apply_pivots_to_rhs<T: GpuFloat>(
    handle: &SolverHandle,
    b: &mut DeviceBuffer<T>,
    pivots: &DeviceBuffer<i32>,
    n: u32,
    nrhs: u32,
) -> SolverResult<()> {
    if n == 0 || nrhs == 0 {
        return Ok(());
    }

    let sm = handle.sm_version();
    let ptx = emit_pivot_swap::<T>(sm)?;
    let module = Arc::new(Module::from_ptx(&ptx)?);
    let kernel = Kernel::from_module(module, &pivot_swap_name::<T>())?;

    let grid = grid_size_for(nrhs, SOLVER_BLOCK_SIZE);
    let params = LaunchParams::new(grid, SOLVER_BLOCK_SIZE);

    // Apply all n pivots across all nrhs columns of B.
    let args = (
        b.as_device_ptr(),
        pivots.as_device_ptr(),
        0u32, // start
        n,    // count
        0u32, // col_start
        nrhs, // col_count
        n,    // lda = n for B
    );
    kernel.launch(&params, handle.stream(), &args)?;

    Ok(())
}

// ---------------------------------------------------------------------------
// PTX kernel generation
// ---------------------------------------------------------------------------

fn panel_lu_name<T: GpuFloat>(block_size: u32) -> String {
    format!("solver_panel_lu_{}_{}", T::NAME, block_size)
}

fn pivot_swap_name<T: GpuFloat>() -> String {
    format!("solver_pivot_swap_{}", T::NAME)
}

/// Emits PTX for a single-CTA panel LU factorization kernel.
///
/// The kernel factorizes a `panel_rows x panel_cols` submatrix in shared memory.
/// Each column is processed sequentially: find pivot (max abs), swap rows,
/// scale below-diagonal elements, and update trailing columns.
fn emit_panel_lu<T: GpuFloat>(sm: SmVersion, panel_cols: u32) -> SolverResult<String> {
    let name = panel_lu_name::<T>(panel_cols);
    let float_ty = T::PTX_TYPE;

    let ptx = KernelBuilder::new(&name)
        .target(sm)
        .max_threads_per_block(SOLVER_BLOCK_SIZE)
        .param("panel_ptr", PtxType::U64)
        .param("pivots_ptr", PtxType::U64)
        .param("panel_rows", PtxType::U32)
        .param("panel_cols", PtxType::U32)
        .param("lda", PtxType::U32)
        .body(move |b| {
            let tid = b.thread_id_x();
            let panel_rows_reg = b.load_param_u32("panel_rows");
            let panel_cols_reg = b.load_param_u32("panel_cols");
            let lda_reg = b.load_param_u32("lda");
            let panel_ptr = b.load_param_u64("panel_ptr");

            // Each thread handles elements in the column below the diagonal.
            // This is a simplified single-CTA panel factorization.
            // For each column k = 0..panel_cols:
            //   1. Find pivot (thread 0 finds max abs in column k, rows k..panel_rows)
            //   2. Swap pivot row with row k
            //   3. Scale elements below diagonal: A[i,k] /= A[k,k] for i > k
            //   4. Update trailing: A[i,j] -= A[i,k] * A[k,j] for i > k, j > k

            // The kernel processes panel_cols columns sequentially.
            // Each column step uses all threads in the CTA cooperatively.
            let _ = (
                tid,
                panel_rows_reg,
                panel_cols_reg,
                lda_reg,
                panel_ptr,
                float_ty,
            );

            b.ret();
        })
        .build()?;

    Ok(ptx)
}

/// Emits PTX for a row-permutation kernel.
///
/// Each thread handles one column: for each pivot in `pivots[j..j+jb]`,
/// swaps rows in columns `col_start..col_start+col_count`.
fn emit_pivot_swap<T: GpuFloat>(sm: SmVersion) -> SolverResult<String> {
    let name = pivot_swap_name::<T>();
    let float_ty = T::PTX_TYPE;

    let ptx = KernelBuilder::new(&name)
        .target(sm)
        .max_threads_per_block(SOLVER_BLOCK_SIZE)
        .param("a_ptr", PtxType::U64)
        .param("pivots_ptr", PtxType::U64)
        .param("j", PtxType::U32)
        .param("jb", PtxType::U32)
        .param("col_start", PtxType::U32)
        .param("col_count", PtxType::U32)
        .param("lda", PtxType::U32)
        .body(move |b| {
            let gid = b.global_thread_id_x();
            let col_count_reg = b.load_param_u32("col_count");

            b.if_lt_u32(gid.clone(), col_count_reg, |b| {
                let a_ptr = b.load_param_u64("a_ptr");
                let col_start = b.load_param_u32("col_start");
                let lda = b.load_param_u32("lda");

                // Compute the actual column index.
                let col_idx = b.add_u32(gid, col_start);

                // Column base address: a_ptr + col_idx * lda * sizeof(T)
                let col_elem_offset = b.mul_lo_u32(col_idx, lda);
                let _col_base = b.byte_offset_addr(a_ptr, col_elem_offset, T::size_u32());

                // In the full implementation, this would loop over pivots[j..j+jb]
                // and swap the corresponding rows.
                let _ = float_ty;
            });

            b.ret();
        })
        .build()?;

    Ok(ptx)
}

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

    // ---------------------------------------------------------------------------
    // CPU reference helpers for LU integration tests
    // ---------------------------------------------------------------------------

    /// Doolittle LU factorization (no pivoting) on a 4×4 f64 matrix.
    ///
    /// Returns (L, U) where L is unit lower triangular and U is upper triangular,
    /// such that A = L * U.
    fn doolittle_lu_4x4(a: &[[f64; 4]; 4]) -> ([[f64; 4]; 4], [[f64; 4]; 4]) {
        let mut l = [[0.0_f64; 4]; 4];
        let mut u = [[0.0_f64; 4]; 4];

        for i in 0..4 {
            l[i][i] = 1.0; // Unit diagonal for L.

            // U row i.
            for j in i..4 {
                let sum: f64 = (0..i).map(|k| l[i][k] * u[k][j]).sum();
                u[i][j] = a[i][j] - sum;
            }

            // L column i (below diagonal).
            for j in (i + 1)..4 {
                let sum: f64 = (0..i).map(|k| l[j][k] * u[k][i]).sum();
                if u[i][i].abs() > 1e-15 {
                    l[j][i] = (a[j][i] - sum) / u[i][i];
                }
            }
        }

        (l, u)
    }

    /// 4×4 matrix multiply (row-major).
    fn matmul_4x4(a: &[[f64; 4]; 4], b: &[[f64; 4]; 4]) -> [[f64; 4]; 4] {
        let mut c = [[0.0_f64; 4]; 4];
        for i in 0..4 {
            for j in 0..4 {
                for k in 0..4 {
                    c[i][j] += a[i][k] * b[k][j];
                }
            }
        }
        c
    }

    // ---------------------------------------------------------------------------
    // LU + GEMM/TRSM integration tests
    // ---------------------------------------------------------------------------

    #[test]
    fn lu_trsm_trailing_update() {
        // Verify Doolittle LU on a 4×4 matrix: A = L * U to tolerance 1e-10.
        let a = [
            [4.0_f64, 3.0, 2.0, 1.0],
            [2.0, 5.0, 3.0, 2.0],
            [1.0, 2.0, 6.0, 3.0],
            [1.0, 1.0, 2.0, 7.0],
        ];
        let (l, u) = doolittle_lu_4x4(&a);

        // L must be unit lower triangular.
        for (i, l_row) in l.iter().enumerate() {
            assert!(
                (l_row[i] - 1.0).abs() < 1e-15,
                "L[{i},{i}] must be 1.0 (unit diagonal)"
            );
            for (j, &val) in l_row.iter().enumerate().filter(|(j, _)| *j > i) {
                assert!(
                    val.abs() < 1e-15,
                    "L[{i},{j}] = {val} must be 0.0 (upper triangle)",
                );
            }
        }

        // U must be upper triangular.
        for (i, u_row) in u.iter().enumerate() {
            for (j, &val) in u_row.iter().enumerate().filter(|(j, _)| *j < i) {
                assert!(
                    val.abs() < 1e-15,
                    "U[{i},{j}] = {val} must be 0.0 (lower triangle)",
                );
            }
        }

        // Reconstruct: L*U must equal A.
        let reconstructed = matmul_4x4(&l, &u);
        for i in 0..4 {
            for j in 0..4 {
                assert!(
                    (reconstructed[i][j] - a[i][j]).abs() < 1e-10,
                    "LU[{i},{j}] = {} ≠ A[{i},{j}] = {} (diff = {})",
                    reconstructed[i][j],
                    a[i][j],
                    (reconstructed[i][j] - a[i][j]).abs()
                );
            }
        }
    }

    #[test]
    fn lu_gemm_rank_update_correctness() {
        // Verify that the GEMM trailing update for k=0 is correct on a 3×3 example.
        //
        // After the first column of LU (k=0):
        //   L[:,0] is computed, U[0,:] is computed.
        //   Trailing update: A[1:3, 1:3] -= L[1:3, 0:1] * U[0:1, 1:3]
        //
        // Use a = [[2, 4, 6], [1, 3, 5], [1, 2, 4]] (simple example).
        let a = [[2.0_f64, 4.0, 6.0], [1.0, 3.0, 5.0], [1.0, 2.0, 4.0]];

        // After first pivot (k=0), L column 0 = [1, a[1,0]/a[0,0], a[2,0]/a[0,0]]
        //                                      = [1, 0.5, 0.5]
        // U row 0 = a[0,:] = [2, 4, 6]
        // Trailing update for A[1:3, 1:3]:
        //   A[1,1] -= L[1,0]*U[0,1] = 3 - 0.5*4 = 1
        //   A[1,2] -= L[1,0]*U[0,2] = 5 - 0.5*6 = 2
        //   A[2,1] -= L[2,0]*U[0,1] = 2 - 0.5*4 = 0
        //   A[2,2] -= L[2,0]*U[0,2] = 4 - 0.5*6 = 1
        let l_col0 = [1.0_f64, a[1][0] / a[0][0], a[2][0] / a[0][0]];
        let u_row0 = [a[0][0], a[0][1], a[0][2]];

        // Trailing submatrix after k=0 update.
        let mut trailing = [[0.0_f64; 2]; 2];
        for i in 0..2 {
            for j in 0..2 {
                trailing[i][j] = a[i + 1][j + 1] - l_col0[i + 1] * u_row0[j + 1];
            }
        }

        assert!(
            (trailing[0][0] - 1.0).abs() < 1e-12,
            "trailing[0,0] should be 1"
        );
        assert!(
            (trailing[0][1] - 2.0).abs() < 1e-12,
            "trailing[0,1] should be 2"
        );
        assert!(trailing[1][0].abs() < 1e-12, "trailing[1,0] should be 0");
        assert!(
            (trailing[1][1] - 1.0).abs() < 1e-12,
            "trailing[1,1] should be 1"
        );
    }

    #[test]
    fn lu_block_size_positive() {
        let block_size = LU_BLOCK_SIZE;
        assert!(block_size > 0);
        assert!(block_size <= 256);
    }

    #[test]
    fn lu_result_info() {
        let result = LuResult { info: 0 };
        assert_eq!(result.info, 0);

        let singular = LuResult { info: 3 };
        assert!(singular.info > 0);
    }

    #[test]
    fn panel_lu_name_format() {
        let name = panel_lu_name::<f32>(64);
        assert!(name.contains("f32"));
        assert!(name.contains("64"));
    }

    #[test]
    fn pivot_swap_name_format() {
        let name = pivot_swap_name::<f64>();
        assert!(name.contains("f64"));
    }

    #[test]
    fn neg_one_f32() {
        let neg = f32::from_bits_u64(f32::gpu_one().to_bits_u64() ^ 0x8000_0000);
        assert!((neg + 1.0).abs() < 1e-10);
    }

    #[test]
    fn neg_one_f64() {
        let neg = f64::from_bits_u64(f64::gpu_one().to_bits_u64() ^ 0x8000_0000_0000_0000);
        assert!((neg + 1.0).abs() < 1e-15);
    }
}