atomr-accel-cuda 0.10.0

GPU acceleration via the actor model. Wraps NVIDIA CUDA libraries (cuBLAS, cuDNN, cuFFT, cuRAND, cuSOLVER, cuSPARSE, cuTENSOR, cuBLASLt, NVRTC, NCCL) as supervised atomr actors with generation-validated buffers and a uniform async surface.
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
//! Batched cuSOLVER ops.
//!
//! cuSOLVER ships native batched variants for Cholesky
//! (`Spotrf/DpotrfBatched`) and Jacobi SVD
//! (`Sgesvdj/DgesvdjBatched`). The batched LU live on the cuBLAS side
//! (`cublasSgetrf/DgetrfBatched`); we adopt them into `SolverActor` for
//! API symmetry — each request takes a contiguous strided block of
//! `batch_size × n × n` matrices and stages an array-of-pointers on
//! demand.
//!
//! All three requests share the same input shape:
//! - `a: GpuRef<T>` — `batch_size × m × n × stride` packed
//!   column-major, with stride equal to `m * n` (no extra padding).
//! - `batch_size: i32` — number of independent problems.
//!
//! Per-batch info codes are read back into a host vec; any non-zero
//! entry surfaces as a `LibraryError` identifying the failing batch
//! index.

use std::ffi::c_int;
use std::sync::Arc;

use cudarc::cusolver::sys as cs;
use cudarc::driver::{DevicePtr, DevicePtrMut};
use parking_lot::Mutex;
use tokio::sync::oneshot;

use crate::dtype::SolverSupported;
use crate::error::GpuError;
use crate::gpu_ref::GpuRef;
use crate::kernel::envelope;
use crate::sys::cusolver::{status_to_result, SolverScalar, LIB};

use super::workspace::{check_info_array, ensure_workspace_bytes, lwork_bytes};
use super::{SolverCells, SolverDispatch, Uplo};

// =====================================================================
// LU batched (`cublasSgetrfBatched` / `cublasDgetrfBatched`)
// =====================================================================

pub struct GetrfBatchedRequest<T: SolverSupported> {
    /// Contiguous `batch_size × n × n` column-major buffer.
    pub a: GpuRef<T>,
    /// Square problem size.
    pub n: i32,
    /// Number of independent problems.
    pub batch_size: i32,
    /// Pivot indices: `batch_size * n` `i32` entries.
    pub ipiv: GpuRef<i32>,
    pub reply: oneshot::Sender<Result<(), GpuError>>,
}

/// Per-dtype dispatch into `cublas[SD]getrfBatched`. The cuBLAS
/// batched LU lives outside cuSOLVER; we keep the FFI thunks
/// inline (rather than expanding `SolverScalar`) so the cuBLAS
/// symbol surface stays scoped to this module.
trait BatchedLu: SolverScalar {
    unsafe fn getrf_batched(
        handle: cudarc::cublas::sys::cublasHandle_t,
        n: c_int,
        a_array: *const *mut Self,
        lda: c_int,
        pivots: *mut c_int,
        info_array: *mut c_int,
        batch_size: c_int,
    ) -> cudarc::cublas::sys::cublasStatus_t;
}

impl BatchedLu for f32 {
    unsafe fn getrf_batched(
        handle: cudarc::cublas::sys::cublasHandle_t,
        n: c_int,
        a_array: *const *mut Self,
        lda: c_int,
        pivots: *mut c_int,
        info_array: *mut c_int,
        batch_size: c_int,
    ) -> cudarc::cublas::sys::cublasStatus_t {
        cudarc::cublas::sys::cublasSgetrfBatched(
            handle, n, a_array, lda, pivots, info_array, batch_size,
        )
    }
}

impl BatchedLu for f64 {
    unsafe fn getrf_batched(
        handle: cudarc::cublas::sys::cublasHandle_t,
        n: c_int,
        a_array: *const *mut Self,
        lda: c_int,
        pivots: *mut c_int,
        info_array: *mut c_int,
        batch_size: c_int,
    ) -> cudarc::cublas::sys::cublasStatus_t {
        cudarc::cublas::sys::cublasDgetrfBatched(
            handle, n, a_array, lda, pivots, info_array, batch_size,
        )
    }
}

impl<T> SolverDispatch for GetrfBatchedRequest<T>
where
    T: SolverSupported + SolverScalar + BatchedLu,
{
    fn dispatch(self: Box<Self>, cells: SolverCells<'_>) {
        let GetrfBatchedRequest {
            a,
            n,
            batch_size,
            ipiv,
            reply,
        } = *self;
        run_getrf_batched::<T>(cells, a, n, batch_size, ipiv, reply);
    }

    fn dispatch_mock(self: Box<Self>) {
        let _ = self.reply.send(Err(GpuError::Unrecoverable(
            "SolverActor in mock mode".into(),
        )));
    }
}

/// Build a contiguous `Vec<*mut T>` of per-batch starting pointers and
/// upload it as a `CudaSlice<u64>` (raw pointer values). We use the
/// stream's `memcpy_htod` since the device pointer table is unique to
/// this launch and short-lived.
fn upload_pointer_table<T>(
    stream: &Arc<cudarc::driver::CudaStream>,
    base: *mut T,
    batch: i32,
    n: i32,
) -> Result<cudarc::driver::CudaSlice<u64>, GpuError> {
    let count = batch.max(0) as usize;
    let stride_bytes = (n.max(0) as usize) * (n.max(0) as usize) * std::mem::size_of::<T>();
    let mut ptrs = Vec::with_capacity(count);
    for i in 0..count {
        let p = (base as usize).saturating_add(i * stride_bytes);
        ptrs.push(p as u64);
    }
    let mut buf = stream
        .alloc_zeros::<u64>(count.max(1))
        .map_err(|e| GpuError::OutOfMemory(format!("ptr table ({count}): {e}")))?;
    stream
        .memcpy_htod(&ptrs, &mut buf)
        .map_err(|e| GpuError::lib(LIB, format!("upload ptr table: {e}")))?;
    Ok(buf)
}

fn run_getrf_batched<T: SolverScalar + BatchedLu>(
    cells: SolverCells<'_>,
    a: GpuRef<T>,
    n: i32,
    batch_size: i32,
    ipiv: GpuRef<i32>,
    reply: oneshot::Sender<Result<(), GpuError>>,
) {
    let SolverCells {
        stream, completion, ..
    } = cells;

    let a_slice = match a.access() {
        Ok(s) => s.clone(),
        Err(e) => {
            let _ = reply.send(Err(e));
            return;
        }
    };
    let ipiv_slice = match ipiv.access() {
        Ok(s) => s.clone(),
        Err(e) => {
            let _ = reply.send(Err(e));
            return;
        }
    };
    let mut a_owned = match Arc::try_unwrap(a_slice) {
        Ok(s) => s,
        Err(_) => {
            let _ = reply.send(Err(GpuError::Unrecoverable(
                "GetrfBatched a has multiple live references".into(),
            )));
            return;
        }
    };
    let mut ipiv_owned = match Arc::try_unwrap(ipiv_slice) {
        Ok(s) => s,
        Err(_) => {
            let _ = reply.send(Err(GpuError::Unrecoverable(
                "GetrfBatched ipiv has multiple live references".into(),
            )));
            return;
        }
    };

    // Lazy cuBLAS handle: cuSOLVER doesn't ship batched LU, so we
    // bind cuBLAS into the cuSOLVER actor's stream just for this
    // op. Created/destroyed locally so it doesn't outlive the
    // launch. cudarc 0.19's `CudaBlas::new` pins the handle to the
    // supplied stream — exactly what we need.
    let blas = match cudarc::cublas::CudaBlas::new(stream.clone()) {
        Ok(b) => b,
        Err(e) => {
            let _ = reply.send(Err(GpuError::lib(LIB, format!("CudaBlas::new: {e}"))));
            return;
        }
    };
    let blas_handle = *blas.handle();

    // Build pointer table.
    let (a_base_ptr, _g_base) = a_owned.device_ptr_mut(stream);
    let ptr_table = match upload_pointer_table::<T>(stream, a_base_ptr as *mut T, batch_size, n) {
        Ok(t) => t,
        Err(e) => {
            let _ = reply.send(Err(e));
            return;
        }
    };
    drop(_g_base);

    // info array: one int per batch entry, allocated fresh.
    let info_array = match stream.alloc_zeros::<i32>(batch_size.max(1) as usize) {
        Ok(b) => b,
        Err(e) => {
            let _ = reply.send(Err(GpuError::OutOfMemory(format!(
                "GetrfBatched info: {e}"
            ))));
            return;
        }
    };

    a.record_write(stream);
    ipiv.record_write(stream);

    let stream_for_check = stream.clone();
    envelope::run_kernel(LIB, stream, completion, (), reply, move || {
        let (ptrs_dev, _gp) = ptr_table.device_ptr(&stream_for_check);
        let (ipiv_ptr, _gpiv) = ipiv_owned.device_ptr_mut(&stream_for_check);
        let (info_ptr, _ginfo) = info_array.device_ptr(&stream_for_check);
        let status = unsafe {
            T::getrf_batched(
                blas_handle,
                n,
                ptrs_dev as *const *mut T,
                n,
                ipiv_ptr as *mut c_int,
                info_ptr as *mut c_int,
                batch_size,
            )
        };
        drop((_gp, _gpiv, _ginfo));
        if status != cudarc::cublas::sys::cublasStatus_t::CUBLAS_STATUS_SUCCESS {
            return Err(GpuError::lib(LIB, format!("getrfBatched: {status:?}")));
        }
        check_info_array(
            &info_array,
            &stream_for_check,
            "getrfBatched",
            batch_size.max(0) as usize,
        )?;
        // Keep blas + table alive until completion.
        Ok((a_owned, ipiv_owned, ptr_table, info_array, blas))
    });
}

// =====================================================================
// Cholesky batched (`cusolverDn[SD]potrfBatched`)
// =====================================================================

pub struct PotrfBatchedRequest<T: SolverSupported> {
    pub a: GpuRef<T>,
    pub n: i32,
    pub batch_size: i32,
    pub uplo: Uplo,
    pub reply: oneshot::Sender<Result<(), GpuError>>,
}

impl<T> SolverDispatch for PotrfBatchedRequest<T>
where
    T: SolverSupported + SolverScalar,
{
    fn dispatch(self: Box<Self>, cells: SolverCells<'_>) {
        let PotrfBatchedRequest {
            a,
            n,
            batch_size,
            uplo,
            reply,
        } = *self;
        run_potrf_batched::<T>(cells, a, n, batch_size, uplo, reply);
    }

    fn dispatch_mock(self: Box<Self>) {
        let _ = self.reply.send(Err(GpuError::Unrecoverable(
            "SolverActor in mock mode".into(),
        )));
    }
}

fn run_potrf_batched<T: SolverScalar>(
    cells: SolverCells<'_>,
    a: GpuRef<T>,
    n: i32,
    batch_size: i32,
    uplo: Uplo,
    reply: oneshot::Sender<Result<(), GpuError>>,
) {
    let SolverCells {
        handle,
        stream,
        completion,
        ..
    } = cells;

    let a_slice = match a.access() {
        Ok(s) => s.clone(),
        Err(e) => {
            let _ = reply.send(Err(e));
            return;
        }
    };
    let mut a_owned = match Arc::try_unwrap(a_slice) {
        Ok(s) => s,
        Err(_) => {
            let _ = reply.send(Err(GpuError::Unrecoverable(
                "PotrfBatched a has multiple live references".into(),
            )));
            return;
        }
    };

    let (a_base_ptr, _g_base) = a_owned.device_ptr_mut(stream);
    let ptr_table = match upload_pointer_table::<T>(stream, a_base_ptr as *mut T, batch_size, n) {
        Ok(t) => t,
        Err(e) => {
            let _ = reply.send(Err(e));
            return;
        }
    };
    drop(_g_base);

    let info_array = match stream.alloc_zeros::<i32>(batch_size.max(1) as usize) {
        Ok(b) => b,
        Err(e) => {
            let _ = reply.send(Err(GpuError::OutOfMemory(format!(
                "PotrfBatched info: {e}"
            ))));
            return;
        }
    };

    a.record_write(stream);
    let fill = uplo.as_cusolver_fill();
    let stream_for_check = stream.clone();

    envelope::run_kernel(LIB, stream, completion, (), reply, move || {
        let h = handle.lock();
        let (ptrs_dev, _gp) = ptr_table.device_ptr(&stream_for_check);
        let (info_ptr, _ginfo) = info_array.device_ptr(&stream_for_check);
        let status = unsafe {
            T::potrf_batched(
                h.0.cu(),
                fill,
                n,
                ptrs_dev as *mut *mut T,
                n,
                info_ptr as *mut i32,
                batch_size,
            )
        };
        drop((_gp, _ginfo));
        status_to_result(status, "potrfBatched")?;
        check_info_array(
            &info_array,
            &stream_for_check,
            "potrfBatched",
            batch_size.max(0) as usize,
        )?;
        Ok((a_owned, ptr_table, info_array))
    });
}

// =====================================================================
// Batched Jacobi SVD (`cusolverDn[SD]gesvdjBatched`)
// =====================================================================

pub struct GesvdjBatchedRequest<T: SolverSupported> {
    /// Contiguous `batch_size × m × n` column-major buffer.
    pub a: GpuRef<T>,
    pub m: i32,
    pub n: i32,
    pub batch_size: i32,
    /// Singular values: `batch_size * min(m, n)` entries.
    pub s: GpuRef<T>,
    /// Left singular vectors (`batch_size × m × m`). When `None`,
    /// `jobz = NOVECTOR`.
    pub u: Option<GpuRef<T>>,
    /// Right singular vectors (`batch_size × n × n`). When `None`,
    /// `jobz = NOVECTOR`.
    pub v: Option<GpuRef<T>>,
    pub reply: oneshot::Sender<Result<(), GpuError>>,
}

/// `gesvdjInfo_t` is non-Send by default (raw pointer); we wrap so
/// the keep-alive can ride the post-launch task.
struct GesvdjParams(cs::gesvdjInfo_t);
unsafe impl Send for GesvdjParams {}
impl Drop for GesvdjParams {
    fn drop(&mut self) {
        unsafe {
            let _ = cs::cusolverDnDestroyGesvdjInfo(self.0);
        }
    }
}

impl<T> SolverDispatch for GesvdjBatchedRequest<T>
where
    T: SolverSupported + SolverScalar,
{
    fn dispatch(self: Box<Self>, cells: SolverCells<'_>) {
        let GesvdjBatchedRequest {
            a,
            m,
            n,
            batch_size,
            s,
            u,
            v,
            reply,
        } = *self;
        run_gesvdj_batched::<T>(cells, a, m, n, batch_size, s, u, v, reply);
    }

    fn dispatch_mock(self: Box<Self>) {
        let _ = self.reply.send(Err(GpuError::Unrecoverable(
            "SolverActor in mock mode".into(),
        )));
    }
}

fn run_gesvdj_batched<T: SolverScalar>(
    cells: SolverCells<'_>,
    a: GpuRef<T>,
    m: i32,
    n: i32,
    batch_size: i32,
    s: GpuRef<T>,
    u: Option<GpuRef<T>>,
    v: Option<GpuRef<T>>,
    reply: oneshot::Sender<Result<(), GpuError>>,
) {
    let SolverCells {
        handle,
        stream,
        completion,
        workspace,
        ..
    } = cells;

    let a_slice = match a.access() {
        Ok(sl) => sl.clone(),
        Err(e) => {
            let _ = reply.send(Err(e));
            return;
        }
    };
    let s_slice = match s.access() {
        Ok(sl) => sl.clone(),
        Err(e) => {
            let _ = reply.send(Err(e));
            return;
        }
    };
    let mut a_owned = match Arc::try_unwrap(a_slice) {
        Ok(sl) => sl,
        Err(_) => {
            let _ = reply.send(Err(GpuError::Unrecoverable(
                "GesvdjBatched a has multiple live references".into(),
            )));
            return;
        }
    };
    let mut s_owned = match Arc::try_unwrap(s_slice) {
        Ok(sl) => sl,
        Err(_) => {
            let _ = reply.send(Err(GpuError::Unrecoverable(
                "GesvdjBatched s has multiple live references".into(),
            )));
            return;
        }
    };

    let mut u_owned = match u.as_ref().map(|g| g.access().map(|sl| sl.clone())) {
        Some(Ok(sl)) => match Arc::try_unwrap(sl) {
            Ok(o) => Some(o),
            Err(_) => {
                let _ = reply.send(Err(GpuError::Unrecoverable(
                    "GesvdjBatched u has multiple live references".into(),
                )));
                return;
            }
        },
        Some(Err(e)) => {
            let _ = reply.send(Err(e));
            return;
        }
        None => None,
    };
    let mut v_owned = match v.as_ref().map(|g| g.access().map(|sl| sl.clone())) {
        Some(Ok(sl)) => match Arc::try_unwrap(sl) {
            Ok(o) => Some(o),
            Err(_) => {
                let _ = reply.send(Err(GpuError::Unrecoverable(
                    "GesvdjBatched v has multiple live references".into(),
                )));
                return;
            }
        },
        Some(Err(e)) => {
            let _ = reply.send(Err(e));
            return;
        }
        None => None,
    };

    // Create gesvdj params. Defaults are fine for Phase 1.
    let mut info_handle: cs::gesvdjInfo_t = std::ptr::null_mut();
    let st = unsafe { cs::cusolverDnCreateGesvdjInfo(&mut info_handle as *mut _) };
    if let Err(e) = status_to_result(st, "CreateGesvdjInfo") {
        let _ = reply.send(Err(e));
        return;
    }
    let params = GesvdjParams(info_handle);

    // info array: batch_size + 1 (matches cuSOLVER reference; we
    // size it to batch_size for the basic check).
    let info_array = match stream.alloc_zeros::<i32>(batch_size.max(1) as usize) {
        Ok(b) => b,
        Err(e) => {
            let _ = reply.send(Err(GpuError::OutOfMemory(format!(
                "GesvdjBatched info: {e}"
            ))));
            return;
        }
    };

    let jobz = if u_owned.is_some() && v_owned.is_some() {
        cs::cusolverEigMode_t::CUSOLVER_EIG_MODE_VECTOR
    } else {
        cs::cusolverEigMode_t::CUSOLVER_EIG_MODE_NOVECTOR
    };

    // Workspace query.
    let ldu = m;
    let ldv = n;
    let mut lwork = 0i32;
    {
        let h = handle.lock();
        let (a_ptr, _ga) = a_owned.device_ptr(stream);
        let (s_ptr, _gs) = s_owned.device_ptr(stream);
        let u_ptr: *const T = match u_owned.as_ref() {
            Some(o) => {
                let (p, _g) = o.device_ptr(stream);
                p as *const T
            }
            None => std::ptr::null(),
        };
        let v_ptr: *const T = match v_owned.as_ref() {
            Some(o) => {
                let (p, _g) = o.device_ptr(stream);
                p as *const T
            }
            None => std::ptr::null(),
        };
        let status = unsafe {
            T::gesvdj_batched_buffer_size(
                h.0.cu(),
                jobz,
                m,
                n,
                a_ptr as *const T,
                m,
                s_ptr as *const T,
                u_ptr,
                ldu,
                v_ptr,
                ldv,
                &mut lwork as *mut _,
                params.0,
                batch_size,
            )
        };
        drop((_ga, _gs));
        if let Err(e) = status_to_result(status, "gesvdjBatched_bufferSize") {
            let _ = reply.send(Err(e));
            return;
        }
    }
    if let Err(e) = ensure_workspace_bytes(workspace, stream, lwork_bytes::<T>(lwork)) {
        let _ = reply.send(Err(e));
        return;
    }

    a.record_write(stream);
    s.record_write(stream);
    if let Some(g) = &u {
        g.record_write(stream);
    }
    if let Some(g) = &v {
        g.record_write(stream);
    }

    let stream_for_check = stream.clone();
    let workspace_ref: &Mutex<Option<cudarc::driver::CudaSlice<u8>>> = workspace;

    envelope::run_kernel(LIB, stream, completion, (), reply, move || {
        let h = handle.lock();
        let mut ws = workspace_ref.lock();
        let (a_ptr, _g1) = a_owned.device_ptr_mut(&stream_for_check);
        let (s_ptr, _g2) = s_owned.device_ptr_mut(&stream_for_check);
        let (u_ptr, _gu_opt): (*mut T, _) = match u_owned.as_mut() {
            Some(o) => {
                let (p, g) = o.device_ptr_mut(&stream_for_check);
                (p as *mut T, Some(g))
            }
            None => (std::ptr::null_mut(), None),
        };
        let (v_ptr, _gv_opt): (*mut T, _) = match v_owned.as_mut() {
            Some(o) => {
                let (p, g) = o.device_ptr_mut(&stream_for_check);
                (p as *mut T, Some(g))
            }
            None => (std::ptr::null_mut(), None),
        };
        let ws_slice = ws.as_mut().expect("workspace ensured");
        let (ws_ptr, _g5) = ws_slice.device_ptr_mut(&stream_for_check);
        let (info_ptr, _ginfo) = info_array.device_ptr(&stream_for_check);
        let status = unsafe {
            T::gesvdj_batched(
                h.0.cu(),
                jobz,
                m,
                n,
                a_ptr as *mut T,
                m,
                s_ptr as *mut T,
                u_ptr,
                ldu,
                v_ptr,
                ldv,
                ws_ptr as *mut T,
                lwork,
                info_ptr as *mut i32,
                params.0,
                batch_size,
            )
        };
        drop((_g1, _g2, _g5, _ginfo, _gu_opt, _gv_opt));
        status_to_result(status, "gesvdjBatched")?;
        check_info_array(
            &info_array,
            &stream_for_check,
            "gesvdjBatched",
            batch_size.max(0) as usize,
        )?;
        Ok((a_owned, s_owned, u_owned, v_owned, info_array, params))
    });
}

// =====================================================================
// Tests
// =====================================================================

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

    #[test]
    fn batched_request_round_trip() {
        fn assert_dispatch<R: SolverDispatch>() {}
        assert_dispatch::<GetrfBatchedRequest<f32>>();
        assert_dispatch::<GetrfBatchedRequest<f64>>();
        assert_dispatch::<PotrfBatchedRequest<f32>>();
        assert_dispatch::<PotrfBatchedRequest<f64>>();
        assert_dispatch::<GesvdjBatchedRequest<f32>>();
        assert_dispatch::<GesvdjBatchedRequest<f64>>();
    }
}