Skip to main content

baracuda_cusparse_sys/
lib.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2//! Raw FFI + dynamic loader for NVIDIA cuSPARSE (generic API subset).
3//!
4//! `baracuda-cusparse` wraps this with a safe, typed API. Use this
5//! crate directly only if you need a function that the safe layer
6//! hasn't wrapped yet (in which case please file a bug).
7
8#![allow(
9    non_camel_case_types,
10    non_snake_case,
11    non_upper_case_globals,
12    reason = "names mirror the vendor C API (FFI bindings)"
13)]
14#![warn(missing_debug_implementations)]
15
16use core::ffi::{c_int, c_void};
17use std::sync::OnceLock;
18
19use baracuda_core::{Library, LoaderError, platform};
20use baracuda_cuda_sys::runtime::cudaStream_t;
21use baracuda_types::CudaStatus;
22
23// ---- handles --------------------------------------------------------------
24
25/// Opaque cuSPARSE handle.
26pub type cusparseHandle_t = *mut c_void;
27/// Opaque sparse-matrix descriptor (CSR / CSC / COO / BSR).
28pub type cusparseSpMatDescr_t = *mut c_void;
29/// Opaque dense-matrix descriptor.
30pub type cusparseDnMatDescr_t = *mut c_void;
31/// Opaque dense-vector descriptor.
32pub type cusparseDnVecDescr_t = *mut c_void;
33/// Opaque SpGEMM intermediate-state descriptor.
34pub type cusparseSpGEMMDescr_t = *mut c_void;
35/// Opaque SpSV intermediate-state descriptor.
36pub type cusparseSpSVDescr_t = *mut c_void;
37/// Opaque SpSM intermediate-state descriptor.
38pub type cusparseSpSMDescr_t = *mut c_void;
39/// Opaque legacy-API matrix descriptor.
40pub type cusparseMatDescr_t = *mut c_void;
41
42// ---- enums ----------------------------------------------------------------
43
44/// Transpose selector for cuSPARSE routines (matches cuBLAS values).
45#[repr(i32)]
46#[derive(Copy, Clone, Debug, Eq, PartialEq)]
47pub enum cusparseOperation_t {
48    /// No transpose.
49    N = 0,
50    /// Transpose.
51    T = 1,
52    /// Conjugate transpose.
53    C = 2,
54}
55
56/// Index-element dtype for sparse-matrix offsets and column indices.
57#[repr(i32)]
58#[derive(Copy, Clone, Debug, Eq, PartialEq)]
59pub enum cusparseIndexType_t {
60    /// Unsigned 16-bit indices.
61    I16U = 1,
62    /// Signed 32-bit indices.
63    I32I = 2,
64    /// Signed 64-bit indices.
65    I64I = 3,
66}
67
68/// Zero- vs one-based indexing for sparse-matrix index arrays.
69#[repr(i32)]
70#[derive(Copy, Clone, Debug, Eq, PartialEq)]
71pub enum cusparseIndexBase_t {
72    /// Zero-based indexing.
73    Zero = 0,
74    /// One-based indexing (Fortran convention).
75    One = 1,
76}
77
78/// Row-major vs column-major dense storage order.
79#[repr(i32)]
80#[derive(Copy, Clone, Debug, Eq, PartialEq)]
81pub enum cusparseOrder_t {
82    /// Row-major dense storage.
83    Row = 1,
84    /// Column-major dense storage.
85    Col = 2,
86}
87
88/// Algorithm selector for `cusparseSpMV`.
89#[repr(i32)]
90#[derive(Copy, Clone, Debug, Eq, PartialEq)]
91pub enum cusparseSpMVAlg_t {
92    /// Driver-chosen default.
93    Default = 0,
94    /// CSR algorithm 1 (deterministic).
95    CsrAlg1 = 2,
96    /// CSR algorithm 2 (higher throughput).
97    CsrAlg2 = 3,
98    /// COO algorithm 1.
99    CooAlg1 = 1,
100    /// COO algorithm 2.
101    CooAlg2 = 4,
102}
103
104/// Algorithm selector for `cusparseSpMM`.
105#[repr(i32)]
106#[derive(Copy, Clone, Debug, Eq, PartialEq)]
107pub enum cusparseSpMMAlg_t {
108    /// Driver-chosen default.
109    Default = 0,
110    /// COO algorithm 1.
111    CooAlg1 = 1,
112    /// CSR algorithm 1.
113    CsrAlg1 = 2,
114    /// COO algorithm 2.
115    CooAlg2 = 3,
116    /// COO algorithm 3.
117    CooAlg3 = 4,
118    /// CSR algorithm 2.
119    CsrAlg2 = 5,
120    /// CSR algorithm 3.
121    CsrAlg3 = 6,
122    /// BSR algorithm.
123    Bsr = 7,
124    /// CSR algorithm 4.
125    CsrAlg4 = 8,
126}
127
128/// Algorithm selector for `cusparseSpGEMM`.
129#[repr(i32)]
130#[derive(Copy, Clone, Debug, Eq, PartialEq)]
131pub enum cusparseSpGEMMAlg_t {
132    /// Driver-chosen default.
133    Default = 0,
134    /// SpGEMM algorithm 1.
135    Alg1 = 1,
136    /// SpGEMM algorithm 2.
137    Alg2 = 2,
138    /// SpGEMM algorithm 3.
139    Alg3 = 3,
140    /// Memory-saving default for CSR inputs.
141    CsrMemoryDefault = 4,
142}
143
144/// Algorithm selector for `cusparseSpSV`.
145#[repr(i32)]
146#[derive(Copy, Clone, Debug, Eq, PartialEq)]
147pub enum cusparseSpSVAlg_t {
148    /// Driver-chosen default.
149    Default = 0,
150}
151
152/// Algorithm selector for `cusparseSpSM`.
153#[repr(i32)]
154#[derive(Copy, Clone, Debug, Eq, PartialEq)]
155pub enum cusparseSpSMAlg_t {
156    /// Driver-chosen default.
157    Default = 0,
158}
159
160/// Algorithm selector for `cusparseSDDMM`.
161#[repr(i32)]
162#[derive(Copy, Clone, Debug, Eq, PartialEq)]
163pub enum cusparseSDDMMAlg_t {
164    /// Driver-chosen default.
165    Default = 0,
166}
167
168/// Algorithm selector for CSR-to-CSC conversion.
169#[repr(i32)]
170#[derive(Copy, Clone, Debug, Eq, PartialEq)]
171pub enum cusparseCsr2CscAlg_t {
172    /// CSR-to-CSC algorithm 1.
173    Alg1 = 1,
174    /// CSR-to-CSC algorithm 2.
175    Alg2 = 2,
176}
177
178impl cusparseCsr2CscAlg_t {
179    /// Alias for `cusparseCsr2CscAlg_t::Alg1`.
180    pub const DEFAULT: Self = Self::Alg1;
181}
182
183/// Triangular fill mode for sparse triangular routines.
184#[repr(i32)]
185#[derive(Copy, Clone, Debug, Eq, PartialEq)]
186pub enum cusparseFillMode_t {
187    /// Lower-triangular.
188    Lower = 0,
189    /// Upper-triangular.
190    Upper = 1,
191}
192
193/// Diagonal-unit selector for sparse triangular routines.
194#[repr(i32)]
195#[derive(Copy, Clone, Debug, Eq, PartialEq)]
196pub enum cusparseDiagType_t {
197    /// Non-unit diagonal.
198    NonUnit = 0,
199    /// Unit diagonal.
200    Unit = 1,
201}
202
203/// Attribute key for `cusparseSpMatSetAttribute` / `*GetAttribute`.
204#[repr(i32)]
205#[derive(Copy, Clone, Debug, Eq, PartialEq)]
206pub enum cusparseSpMatAttribute_t {
207    /// Fill-mode attribute (lower / upper triangular).
208    FillMode = 0,
209    /// Diagonal-type attribute (unit / non-unit).
210    DiagType = 1,
211}
212
213/// `cudaDataType` values used by cuSPARSE / cuSOLVER's generic APIs. Only
214/// the subset we actually use at v0.1.
215#[repr(i32)]
216#[derive(Copy, Clone, Debug, Eq, PartialEq)]
217pub enum cudaDataType {
218    /// 32-bit real (`f32`).
219    R_32F = 0,
220    /// 64-bit real (`f64`).
221    R_64F = 1,
222    /// 16-bit real (IEEE half / `f16`).
223    R_16F = 2,
224    /// 32-bit complex (`Complex<f32>`).
225    C_32F = 4,
226    /// 64-bit complex (`Complex<f64>`).
227    C_64F = 5,
228    /// 16-bit real bfloat16.
229    R_16BF = 14,
230}
231
232// ---- status ---------------------------------------------------------------
233
234/// Status / error code returned by cuSPARSE FFI calls.
235#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
236#[repr(transparent)]
237pub struct cusparseStatus_t(pub i32);
238
239impl cusparseStatus_t {
240    /// Status: success.
241    pub const SUCCESS: Self = Self(0);
242    /// Status: not initialized.
243    pub const NOT_INITIALIZED: Self = Self(1);
244    /// Status: alloc failed.
245    pub const ALLOC_FAILED: Self = Self(2);
246    /// Status: invalid value.
247    pub const INVALID_VALUE: Self = Self(3);
248    /// Status: arch mismatch.
249    pub const ARCH_MISMATCH: Self = Self(4);
250    /// Status: mapping error.
251    pub const MAPPING_ERROR: Self = Self(5);
252    /// Status: execution failed.
253    pub const EXECUTION_FAILED: Self = Self(6);
254    /// Status: internal error.
255    pub const INTERNAL_ERROR: Self = Self(7);
256    /// Status: matrix type not supported.
257    pub const MATRIX_TYPE_NOT_SUPPORTED: Self = Self(8);
258    /// Status: zero pivot.
259    pub const ZERO_PIVOT: Self = Self(9);
260    /// Status: not supported.
261    pub const NOT_SUPPORTED: Self = Self(10);
262    /// Status: insufficient resources.
263    pub const INSUFFICIENT_RESOURCES: Self = Self(11);
264
265    /// Returns `true` when this is the success status code.
266    pub const fn is_success(self) -> bool {
267        self.0 == 0
268    }
269}
270
271impl CudaStatus for cusparseStatus_t {
272    fn code(self) -> i32 {
273        self.0
274    }
275    fn name(self) -> &'static str {
276        match self.0 {
277            0 => "CUSPARSE_STATUS_SUCCESS",
278            1 => "CUSPARSE_STATUS_NOT_INITIALIZED",
279            2 => "CUSPARSE_STATUS_ALLOC_FAILED",
280            3 => "CUSPARSE_STATUS_INVALID_VALUE",
281            4 => "CUSPARSE_STATUS_ARCH_MISMATCH",
282            6 => "CUSPARSE_STATUS_EXECUTION_FAILED",
283            7 => "CUSPARSE_STATUS_INTERNAL_ERROR",
284            10 => "CUSPARSE_STATUS_NOT_SUPPORTED",
285            _ => "CUSPARSE_STATUS_UNRECOGNIZED",
286        }
287    }
288    fn description(self) -> &'static str {
289        match self.0 {
290            0 => "success",
291            1 => "cuSPARSE handle not initialized",
292            2 => "allocation failed",
293            3 => "invalid argument",
294            6 => "GPU execution failed",
295            10 => "operation not supported",
296            _ => "unrecognized cuSPARSE status code",
297        }
298    }
299    fn is_success(self) -> bool {
300        cusparseStatus_t::is_success(self)
301    }
302    fn library(self) -> &'static str {
303        "cusparse"
304    }
305}
306
307// ---- function-pointer types ----------------------------------------------
308
309/// cuSPARSE: create a cuSPARSE handle. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
310pub type PFN_cusparseCreate =
311    unsafe extern "C" fn(handle: *mut cusparseHandle_t) -> cusparseStatus_t;
312/// cuSPARSE: destroy a cuSPARSE handle. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
313pub type PFN_cusparseDestroy = unsafe extern "C" fn(handle: cusparseHandle_t) -> cusparseStatus_t;
314/// cuSPARSE: bind a CUDA stream to a cuSPARSE handle. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
315pub type PFN_cusparseSetStream =
316    unsafe extern "C" fn(handle: cusparseHandle_t, stream: cudaStream_t) -> cusparseStatus_t;
317/// cuSPARSE: return the cuSPARSE library version. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
318pub type PFN_cusparseGetVersion =
319    unsafe extern "C" fn(handle: cusparseHandle_t, version: *mut c_int) -> cusparseStatus_t;
320
321/// cuSPARSE: create a CSR sparse-matrix descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
322pub type PFN_cusparseCreateCsr = unsafe extern "C" fn(
323    sp_mat: *mut cusparseSpMatDescr_t,
324    rows: i64,
325    cols: i64,
326    nnz: i64,
327    csr_row_offsets: *mut c_void,
328    csr_col_ind: *mut c_void,
329    csr_values: *mut c_void,
330    csr_row_offsets_type: cusparseIndexType_t,
331    csr_col_ind_type: cusparseIndexType_t,
332    idx_base: cusparseIndexBase_t,
333    value_type: cudaDataType,
334) -> cusparseStatus_t;
335/// cuSPARSE: destroy a sparse-matrix descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
336pub type PFN_cusparseDestroySpMat =
337    unsafe extern "C" fn(descr: cusparseSpMatDescr_t) -> cusparseStatus_t;
338
339/// cuSPARSE: create a dense-vector descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
340pub type PFN_cusparseCreateDnVec = unsafe extern "C" fn(
341    descr: *mut cusparseDnVecDescr_t,
342    size: i64,
343    values: *mut c_void,
344    value_type: cudaDataType,
345) -> cusparseStatus_t;
346/// cuSPARSE: destroy a dense-vector descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
347pub type PFN_cusparseDestroyDnVec =
348    unsafe extern "C" fn(descr: cusparseDnVecDescr_t) -> cusparseStatus_t;
349
350/// cuSPARSE: workspace-size query for sparse matrix-vector multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
351pub type PFN_cusparseSpMV_bufferSize = unsafe extern "C" fn(
352    handle: cusparseHandle_t,
353    op: cusparseOperation_t,
354    alpha: *const c_void,
355    mat_a: cusparseSpMatDescr_t,
356    vec_x: cusparseDnVecDescr_t,
357    beta: *const c_void,
358    vec_y: cusparseDnVecDescr_t,
359    compute_type: cudaDataType,
360    alg: cusparseSpMVAlg_t,
361    buffer_size: *mut usize,
362) -> cusparseStatus_t;
363
364/// cuSPARSE: sparse matrix-vector multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
365pub type PFN_cusparseSpMV = unsafe extern "C" fn(
366    handle: cusparseHandle_t,
367    op: cusparseOperation_t,
368    alpha: *const c_void,
369    mat_a: cusparseSpMatDescr_t,
370    vec_x: cusparseDnVecDescr_t,
371    beta: *const c_void,
372    vec_y: cusparseDnVecDescr_t,
373    compute_type: cudaDataType,
374    alg: cusparseSpMVAlg_t,
375    external_buffer: *mut c_void,
376) -> cusparseStatus_t;
377
378// ---- CSC / COO / BSR / Dense descriptors ---------------------------------
379
380/// cuSPARSE: create a CSC sparse-matrix descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
381pub type PFN_cusparseCreateCsc = unsafe extern "C" fn(
382    sp_mat: *mut cusparseSpMatDescr_t,
383    rows: i64,
384    cols: i64,
385    nnz: i64,
386    csc_col_offsets: *mut c_void,
387    csc_row_ind: *mut c_void,
388    csc_values: *mut c_void,
389    csc_col_offsets_type: cusparseIndexType_t,
390    csc_row_ind_type: cusparseIndexType_t,
391    idx_base: cusparseIndexBase_t,
392    value_type: cudaDataType,
393) -> cusparseStatus_t;
394
395/// cuSPARSE: create a COO sparse-matrix descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
396pub type PFN_cusparseCreateCoo = unsafe extern "C" fn(
397    sp_mat: *mut cusparseSpMatDescr_t,
398    rows: i64,
399    cols: i64,
400    nnz: i64,
401    coo_row_ind: *mut c_void,
402    coo_col_ind: *mut c_void,
403    coo_values: *mut c_void,
404    coo_idx_type: cusparseIndexType_t,
405    idx_base: cusparseIndexBase_t,
406    value_type: cudaDataType,
407) -> cusparseStatus_t;
408
409/// cuSPARSE: create a BSR sparse-matrix descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
410pub type PFN_cusparseCreateBsr = unsafe extern "C" fn(
411    sp_mat: *mut cusparseSpMatDescr_t,
412    brows: i64,
413    bcols: i64,
414    bnnz: i64,
415    row_block_dim: i64,
416    col_block_dim: i64,
417    bsr_row_offsets: *mut c_void,
418    bsr_col_ind: *mut c_void,
419    bsr_values: *mut c_void,
420    bsr_row_offsets_type: cusparseIndexType_t,
421    bsr_col_ind_type: cusparseIndexType_t,
422    idx_base: cusparseIndexBase_t,
423    value_type: cudaDataType,
424    order: cusparseOrder_t,
425) -> cusparseStatus_t;
426
427/// cuSPARSE: create a dense-matrix descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
428pub type PFN_cusparseCreateDnMat = unsafe extern "C" fn(
429    descr: *mut cusparseDnMatDescr_t,
430    rows: i64,
431    cols: i64,
432    ld: i64,
433    values: *mut c_void,
434    value_type: cudaDataType,
435    order: cusparseOrder_t,
436) -> cusparseStatus_t;
437
438/// cuSPARSE: destroy a dense-matrix descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
439pub type PFN_cusparseDestroyDnMat =
440    unsafe extern "C" fn(descr: cusparseDnMatDescr_t) -> cusparseStatus_t;
441
442/// cuSPARSE: query rows, cols, and nnz of a sparse-matrix descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
443pub type PFN_cusparseSpMatGetSize = unsafe extern "C" fn(
444    sp_mat: cusparseSpMatDescr_t,
445    rows: *mut i64,
446    cols: *mut i64,
447    nnz: *mut i64,
448) -> cusparseStatus_t;
449
450/// cuSPARSE: set an attribute (fill mode, diag type) on a sparse-matrix descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
451pub type PFN_cusparseSpMatSetAttribute = unsafe extern "C" fn(
452    sp_mat: cusparseSpMatDescr_t,
453    attribute: cusparseSpMatAttribute_t,
454    data: *const c_void,
455    data_size: usize,
456) -> cusparseStatus_t;
457
458/// cuSPARSE: replace the row/column/value pointers on a CSR descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
459pub type PFN_cusparseCsrSetPointers = unsafe extern "C" fn(
460    sp_mat: cusparseSpMatDescr_t,
461    csr_row_offsets: *mut c_void,
462    csr_col_ind: *mut c_void,
463    csr_values: *mut c_void,
464) -> cusparseStatus_t;
465
466/// cuSPARSE: replace the column/row/value pointers on a CSC descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
467pub type PFN_cusparseCscSetPointers = unsafe extern "C" fn(
468    sp_mat: cusparseSpMatDescr_t,
469    csc_col_offsets: *mut c_void,
470    csc_row_ind: *mut c_void,
471    csc_values: *mut c_void,
472) -> cusparseStatus_t;
473
474/// cuSPARSE: replace the row/column/value pointers on a COO descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
475pub type PFN_cusparseCooSetPointers = unsafe extern "C" fn(
476    sp_mat: cusparseSpMatDescr_t,
477    coo_row_ind: *mut c_void,
478    coo_col_ind: *mut c_void,
479    coo_values: *mut c_void,
480) -> cusparseStatus_t;
481
482// ---- SpMM (sparse × dense = dense) ---------------------------------------
483
484/// cuSPARSE: workspace-size query for sparse matrix × dense matrix multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
485pub type PFN_cusparseSpMM_bufferSize = unsafe extern "C" fn(
486    handle: cusparseHandle_t,
487    op_a: cusparseOperation_t,
488    op_b: cusparseOperation_t,
489    alpha: *const c_void,
490    mat_a: cusparseSpMatDescr_t,
491    mat_b: cusparseDnMatDescr_t,
492    beta: *const c_void,
493    mat_c: cusparseDnMatDescr_t,
494    compute_type: cudaDataType,
495    alg: cusparseSpMMAlg_t,
496    buffer_size: *mut usize,
497) -> cusparseStatus_t;
498
499/// cuSPARSE: preprocess stage of sparse matrix × dense matrix multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
500pub type PFN_cusparseSpMM_preprocess = unsafe extern "C" fn(
501    handle: cusparseHandle_t,
502    op_a: cusparseOperation_t,
503    op_b: cusparseOperation_t,
504    alpha: *const c_void,
505    mat_a: cusparseSpMatDescr_t,
506    mat_b: cusparseDnMatDescr_t,
507    beta: *const c_void,
508    mat_c: cusparseDnMatDescr_t,
509    compute_type: cudaDataType,
510    alg: cusparseSpMMAlg_t,
511    external_buffer: *mut c_void,
512) -> cusparseStatus_t;
513
514/// cuSPARSE: sparse matrix × dense matrix multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
515pub type PFN_cusparseSpMM = unsafe extern "C" fn(
516    handle: cusparseHandle_t,
517    op_a: cusparseOperation_t,
518    op_b: cusparseOperation_t,
519    alpha: *const c_void,
520    mat_a: cusparseSpMatDescr_t,
521    mat_b: cusparseDnMatDescr_t,
522    beta: *const c_void,
523    mat_c: cusparseDnMatDescr_t,
524    compute_type: cudaDataType,
525    alg: cusparseSpMMAlg_t,
526    external_buffer: *mut c_void,
527) -> cusparseStatus_t;
528
529// ---- SpGEMM (sparse × sparse = sparse) -----------------------------------
530
531/// cuSPARSE: create an opaque descriptor for sparse matrix × sparse matrix multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
532pub type PFN_cusparseSpGEMM_createDescr =
533    unsafe extern "C" fn(descr: *mut cusparseSpGEMMDescr_t) -> cusparseStatus_t;
534/// cuSPARSE: destroy an opaque descriptor for sparse matrix × sparse matrix multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
535pub type PFN_cusparseSpGEMM_destroyDescr =
536    unsafe extern "C" fn(descr: cusparseSpGEMMDescr_t) -> cusparseStatus_t;
537
538/// cuSPARSE: work-estimation stage of sparse matrix × sparse matrix multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
539pub type PFN_cusparseSpGEMM_workEstimation = unsafe extern "C" fn(
540    handle: cusparseHandle_t,
541    op_a: cusparseOperation_t,
542    op_b: cusparseOperation_t,
543    alpha: *const c_void,
544    mat_a: cusparseSpMatDescr_t,
545    mat_b: cusparseSpMatDescr_t,
546    beta: *const c_void,
547    mat_c: cusparseSpMatDescr_t,
548    compute_type: cudaDataType,
549    alg: cusparseSpGEMMAlg_t,
550    descr: cusparseSpGEMMDescr_t,
551    buffer_size1: *mut usize,
552    external_buffer1: *mut c_void,
553) -> cusparseStatus_t;
554
555/// cuSPARSE: compute stage of sparse matrix × sparse matrix multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
556pub type PFN_cusparseSpGEMM_compute = unsafe extern "C" fn(
557    handle: cusparseHandle_t,
558    op_a: cusparseOperation_t,
559    op_b: cusparseOperation_t,
560    alpha: *const c_void,
561    mat_a: cusparseSpMatDescr_t,
562    mat_b: cusparseSpMatDescr_t,
563    beta: *const c_void,
564    mat_c: cusparseSpMatDescr_t,
565    compute_type: cudaDataType,
566    alg: cusparseSpGEMMAlg_t,
567    descr: cusparseSpGEMMDescr_t,
568    buffer_size2: *mut usize,
569    external_buffer2: *mut c_void,
570) -> cusparseStatus_t;
571
572/// cuSPARSE: copy stage of sparse matrix × sparse matrix multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
573pub type PFN_cusparseSpGEMM_copy = unsafe extern "C" fn(
574    handle: cusparseHandle_t,
575    op_a: cusparseOperation_t,
576    op_b: cusparseOperation_t,
577    alpha: *const c_void,
578    mat_a: cusparseSpMatDescr_t,
579    mat_b: cusparseSpMatDescr_t,
580    beta: *const c_void,
581    mat_c: cusparseSpMatDescr_t,
582    compute_type: cudaDataType,
583    alg: cusparseSpGEMMAlg_t,
584    descr: cusparseSpGEMMDescr_t,
585) -> cusparseStatus_t;
586
587// ---- SpSV (sparse triangular solve, vector) ------------------------------
588
589/// cuSPARSE: create an opaque descriptor for sparse triangular linear solve (single right-hand side). See <https://docs.nvidia.com/cuda/cusparse/index.html>.
590pub type PFN_cusparseSpSV_createDescr =
591    unsafe extern "C" fn(descr: *mut cusparseSpSVDescr_t) -> cusparseStatus_t;
592/// cuSPARSE: destroy an opaque descriptor for sparse triangular linear solve (single right-hand side). See <https://docs.nvidia.com/cuda/cusparse/index.html>.
593pub type PFN_cusparseSpSV_destroyDescr =
594    unsafe extern "C" fn(descr: cusparseSpSVDescr_t) -> cusparseStatus_t;
595
596/// cuSPARSE: workspace-size query for sparse triangular linear solve (single right-hand side). See <https://docs.nvidia.com/cuda/cusparse/index.html>.
597pub type PFN_cusparseSpSV_bufferSize = unsafe extern "C" fn(
598    handle: cusparseHandle_t,
599    op_a: cusparseOperation_t,
600    alpha: *const c_void,
601    mat_a: cusparseSpMatDescr_t,
602    vec_x: cusparseDnVecDescr_t,
603    vec_y: cusparseDnVecDescr_t,
604    compute_type: cudaDataType,
605    alg: cusparseSpSVAlg_t,
606    descr: cusparseSpSVDescr_t,
607    buffer_size: *mut usize,
608) -> cusparseStatus_t;
609
610/// cuSPARSE: analysis stage of sparse triangular linear solve (single right-hand side). See <https://docs.nvidia.com/cuda/cusparse/index.html>.
611pub type PFN_cusparseSpSV_analysis = unsafe extern "C" fn(
612    handle: cusparseHandle_t,
613    op_a: cusparseOperation_t,
614    alpha: *const c_void,
615    mat_a: cusparseSpMatDescr_t,
616    vec_x: cusparseDnVecDescr_t,
617    vec_y: cusparseDnVecDescr_t,
618    compute_type: cudaDataType,
619    alg: cusparseSpSVAlg_t,
620    descr: cusparseSpSVDescr_t,
621    external_buffer: *mut c_void,
622) -> cusparseStatus_t;
623
624/// cuSPARSE: solve stage of sparse triangular linear solve (single right-hand side). See <https://docs.nvidia.com/cuda/cusparse/index.html>.
625pub type PFN_cusparseSpSV_solve = unsafe extern "C" fn(
626    handle: cusparseHandle_t,
627    op_a: cusparseOperation_t,
628    alpha: *const c_void,
629    mat_a: cusparseSpMatDescr_t,
630    vec_x: cusparseDnVecDescr_t,
631    vec_y: cusparseDnVecDescr_t,
632    compute_type: cudaDataType,
633    alg: cusparseSpSVAlg_t,
634    descr: cusparseSpSVDescr_t,
635) -> cusparseStatus_t;
636
637// ---- SpSM (sparse triangular solve, matrix) ------------------------------
638
639/// cuSPARSE: create an opaque descriptor for sparse triangular linear solve (multiple right-hand sides). See <https://docs.nvidia.com/cuda/cusparse/index.html>.
640pub type PFN_cusparseSpSM_createDescr =
641    unsafe extern "C" fn(descr: *mut cusparseSpSMDescr_t) -> cusparseStatus_t;
642/// cuSPARSE: destroy an opaque descriptor for sparse triangular linear solve (multiple right-hand sides). See <https://docs.nvidia.com/cuda/cusparse/index.html>.
643pub type PFN_cusparseSpSM_destroyDescr =
644    unsafe extern "C" fn(descr: cusparseSpSMDescr_t) -> cusparseStatus_t;
645
646/// cuSPARSE: workspace-size query for sparse triangular linear solve (multiple right-hand sides). See <https://docs.nvidia.com/cuda/cusparse/index.html>.
647pub type PFN_cusparseSpSM_bufferSize = unsafe extern "C" fn(
648    handle: cusparseHandle_t,
649    op_a: cusparseOperation_t,
650    op_b: cusparseOperation_t,
651    alpha: *const c_void,
652    mat_a: cusparseSpMatDescr_t,
653    mat_b: cusparseDnMatDescr_t,
654    mat_c: cusparseDnMatDescr_t,
655    compute_type: cudaDataType,
656    alg: cusparseSpSMAlg_t,
657    descr: cusparseSpSMDescr_t,
658    buffer_size: *mut usize,
659) -> cusparseStatus_t;
660
661/// cuSPARSE: analysis stage of sparse triangular linear solve (multiple right-hand sides). See <https://docs.nvidia.com/cuda/cusparse/index.html>.
662pub type PFN_cusparseSpSM_analysis = unsafe extern "C" fn(
663    handle: cusparseHandle_t,
664    op_a: cusparseOperation_t,
665    op_b: cusparseOperation_t,
666    alpha: *const c_void,
667    mat_a: cusparseSpMatDescr_t,
668    mat_b: cusparseDnMatDescr_t,
669    mat_c: cusparseDnMatDescr_t,
670    compute_type: cudaDataType,
671    alg: cusparseSpSMAlg_t,
672    descr: cusparseSpSMDescr_t,
673    external_buffer: *mut c_void,
674) -> cusparseStatus_t;
675
676/// cuSPARSE: solve stage of sparse triangular linear solve (multiple right-hand sides). See <https://docs.nvidia.com/cuda/cusparse/index.html>.
677pub type PFN_cusparseSpSM_solve = unsafe extern "C" fn(
678    handle: cusparseHandle_t,
679    op_a: cusparseOperation_t,
680    op_b: cusparseOperation_t,
681    alpha: *const c_void,
682    mat_a: cusparseSpMatDescr_t,
683    mat_b: cusparseDnMatDescr_t,
684    mat_c: cusparseDnMatDescr_t,
685    compute_type: cudaDataType,
686    alg: cusparseSpSMAlg_t,
687    descr: cusparseSpSMDescr_t,
688) -> cusparseStatus_t;
689
690// ---- SDDMM (sampled dense-dense matmul) ----------------------------------
691
692/// cuSPARSE: workspace-size query for sampled dense-dense matrix multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
693pub type PFN_cusparseSDDMM_bufferSize = unsafe extern "C" fn(
694    handle: cusparseHandle_t,
695    op_a: cusparseOperation_t,
696    op_b: cusparseOperation_t,
697    alpha: *const c_void,
698    mat_a: cusparseDnMatDescr_t,
699    mat_b: cusparseDnMatDescr_t,
700    beta: *const c_void,
701    mat_c: cusparseSpMatDescr_t,
702    compute_type: cudaDataType,
703    alg: cusparseSDDMMAlg_t,
704    buffer_size: *mut usize,
705) -> cusparseStatus_t;
706
707/// cuSPARSE: preprocess stage of sampled dense-dense matrix multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
708pub type PFN_cusparseSDDMM_preprocess = unsafe extern "C" fn(
709    handle: cusparseHandle_t,
710    op_a: cusparseOperation_t,
711    op_b: cusparseOperation_t,
712    alpha: *const c_void,
713    mat_a: cusparseDnMatDescr_t,
714    mat_b: cusparseDnMatDescr_t,
715    beta: *const c_void,
716    mat_c: cusparseSpMatDescr_t,
717    compute_type: cudaDataType,
718    alg: cusparseSDDMMAlg_t,
719    external_buffer: *mut c_void,
720) -> cusparseStatus_t;
721
722/// cuSPARSE: sampled dense-dense matrix multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
723pub type PFN_cusparseSDDMM = unsafe extern "C" fn(
724    handle: cusparseHandle_t,
725    op_a: cusparseOperation_t,
726    op_b: cusparseOperation_t,
727    alpha: *const c_void,
728    mat_a: cusparseDnMatDescr_t,
729    mat_b: cusparseDnMatDescr_t,
730    beta: *const c_void,
731    mat_c: cusparseSpMatDescr_t,
732    compute_type: cudaDataType,
733    alg: cusparseSDDMMAlg_t,
734    external_buffer: *mut c_void,
735) -> cusparseStatus_t;
736
737// ---- CSR ↔ CSC conversion -------------------------------------------------
738
739/// cuSPARSE: workspace-size query for `cusparseCsr2cscEx2`. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
740pub type PFN_cusparseCsr2cscEx2_bufferSize = unsafe extern "C" fn(
741    handle: cusparseHandle_t,
742    m: c_int,
743    n: c_int,
744    nnz: c_int,
745    csr_val: *const c_void,
746    csr_row_ptr: *const c_int,
747    csr_col_ind: *const c_int,
748    csc_val: *mut c_void,
749    csc_col_ptr: *mut c_int,
750    csc_row_ind: *mut c_int,
751    value_type: cudaDataType,
752    copy_values: c_int,
753    idx_base: cusparseIndexBase_t,
754    alg: cusparseCsr2CscAlg_t,
755    buffer_size: *mut usize,
756) -> cusparseStatus_t;
757
758/// cuSPARSE: convert a CSR matrix to CSC (extended API). See <https://docs.nvidia.com/cuda/cusparse/index.html>.
759pub type PFN_cusparseCsr2cscEx2 = unsafe extern "C" fn(
760    handle: cusparseHandle_t,
761    m: c_int,
762    n: c_int,
763    nnz: c_int,
764    csr_val: *const c_void,
765    csr_row_ptr: *const c_int,
766    csr_col_ind: *const c_int,
767    csc_val: *mut c_void,
768    csc_col_ptr: *mut c_int,
769    csc_row_ind: *mut c_int,
770    value_type: cudaDataType,
771    copy_values: c_int,
772    idx_base: cusparseIndexBase_t,
773    alg: cusparseCsr2CscAlg_t,
774    buffer: *mut c_void,
775) -> cusparseStatus_t;
776
777// ---- Sparse↔Dense conversion ---------------------------------------------
778
779/// cuSPARSE: workspace-size query for sparse-to-dense conversion. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
780pub type PFN_cusparseSparseToDense_bufferSize = unsafe extern "C" fn(
781    handle: cusparseHandle_t,
782    mat_a: cusparseSpMatDescr_t,
783    mat_b: cusparseDnMatDescr_t,
784    alg: c_int,
785    buffer_size: *mut usize,
786) -> cusparseStatus_t;
787
788/// cuSPARSE: materialize a sparse matrix into a dense one. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
789pub type PFN_cusparseSparseToDense = unsafe extern "C" fn(
790    handle: cusparseHandle_t,
791    mat_a: cusparseSpMatDescr_t,
792    mat_b: cusparseDnMatDescr_t,
793    alg: c_int,
794    external_buffer: *mut c_void,
795) -> cusparseStatus_t;
796
797/// cuSPARSE: workspace-size query for dense-to-sparse conversion. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
798pub type PFN_cusparseDenseToSparse_bufferSize = unsafe extern "C" fn(
799    handle: cusparseHandle_t,
800    mat_a: cusparseDnMatDescr_t,
801    mat_b: cusparseSpMatDescr_t,
802    alg: c_int,
803    buffer_size: *mut usize,
804) -> cusparseStatus_t;
805
806/// cuSPARSE: analysis stage for dense-to-sparse conversion. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
807pub type PFN_cusparseDenseToSparse_analysis = unsafe extern "C" fn(
808    handle: cusparseHandle_t,
809    mat_a: cusparseDnMatDescr_t,
810    mat_b: cusparseSpMatDescr_t,
811    alg: c_int,
812    external_buffer: *mut c_void,
813) -> cusparseStatus_t;
814
815/// cuSPARSE: execute stage for dense-to-sparse conversion. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
816pub type PFN_cusparseDenseToSparse_convert = unsafe extern "C" fn(
817    handle: cusparseHandle_t,
818    mat_a: cusparseDnMatDescr_t,
819    mat_b: cusparseSpMatDescr_t,
820    alg: c_int,
821    external_buffer: *mut c_void,
822) -> cusparseStatus_t;
823
824// ---- Axpby / Gather / Scatter / Rot (sparse BLAS L1) --------------------
825
826/// cuSPARSE: scaled vector addition (alpha*x + beta*y → y). See <https://docs.nvidia.com/cuda/cusparse/index.html>.
827pub type PFN_cusparseAxpby = unsafe extern "C" fn(
828    handle: cusparseHandle_t,
829    alpha: *const c_void,
830    vec_x: cusparseDnVecDescr_t,
831    beta: *const c_void,
832    vec_y: cusparseDnVecDescr_t,
833) -> cusparseStatus_t;
834
835/// cuSPARSE: gather dense entries into a sparse vector. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
836pub type PFN_cusparseGather = unsafe extern "C" fn(
837    handle: cusparseHandle_t,
838    vec_y: cusparseDnVecDescr_t,
839    vec_x: cusparseDnVecDescr_t,
840) -> cusparseStatus_t;
841
842/// cuSPARSE: scatter sparse vector entries into a dense vector. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
843pub type PFN_cusparseScatter = unsafe extern "C" fn(
844    handle: cusparseHandle_t,
845    vec_x: cusparseDnVecDescr_t,
846    vec_y: cusparseDnVecDescr_t,
847) -> cusparseStatus_t;
848
849/// cuSPARSE: Givens-rotation on sparse and dense vector pair. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
850pub type PFN_cusparseRot = unsafe extern "C" fn(
851    handle: cusparseHandle_t,
852    c: *const c_void,
853    s: *const c_void,
854    vec_x: cusparseDnVecDescr_t,
855    vec_y: cusparseDnVecDescr_t,
856) -> cusparseStatus_t;
857
858// ---- loader --------------------------------------------------------------
859
860fn cusparse_candidates() -> Vec<String> {
861    platform::versioned_library_candidates("cusparse", &["13", "12", "11"])
862}
863
864macro_rules! cusparse_fns {
865    ($($(#[$m:meta])* $name:ident as $sym:literal : $pfn:ty);* $(;)?) => {
866        /// Loaded cuSPARSE shared library plus a per-symbol `OnceLock` of function pointers.
867        pub struct Cusparse {
868            lib: Library,
869            $($name: OnceLock<$pfn>,)*
870        }
871        impl core::fmt::Debug for Cusparse {
872            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
873                f.debug_struct("Cusparse").field("lib", &self.lib).finish_non_exhaustive()
874            }
875        }
876        impl Cusparse {
877            $(
878                $(#[$m])*
879                pub fn $name(&self) -> Result<$pfn, LoaderError> {
880                    if let Some(&p) = self.$name.get() { return Ok(p); }
881                    let raw: *mut () = unsafe { self.lib.raw_symbol($sym)? };
882                    let p: $pfn = unsafe { core::mem::transmute_copy::<*mut (), $pfn>(&raw) };
883                    let _ = self.$name.set(p);
884                    Ok(p)
885                }
886            )*
887            fn empty(lib: Library) -> Self {
888                Self { lib, $($name: OnceLock::new(),)* }
889            }
890        }
891    };
892}
893
894cusparse_fns! {
895    /// cuSPARSE: create a cuSPARSE handle. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
896    cusparse_create as "cusparseCreate": PFN_cusparseCreate;
897    /// cuSPARSE: destroy a cuSPARSE handle. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
898    cusparse_destroy as "cusparseDestroy": PFN_cusparseDestroy;
899    /// cuSPARSE: bind a CUDA stream to a cuSPARSE handle. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
900    cusparse_set_stream as "cusparseSetStream": PFN_cusparseSetStream;
901    /// cuSPARSE: return the cuSPARSE library version. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
902    cusparse_get_version as "cusparseGetVersion": PFN_cusparseGetVersion;
903    // Sparse-matrix descriptors
904    /// cuSPARSE: create a CSR sparse-matrix descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
905    cusparse_create_csr as "cusparseCreateCsr": PFN_cusparseCreateCsr;
906    /// cuSPARSE: create a CSC sparse-matrix descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
907    cusparse_create_csc as "cusparseCreateCsc": PFN_cusparseCreateCsc;
908    /// cuSPARSE: create a COO sparse-matrix descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
909    cusparse_create_coo as "cusparseCreateCoo": PFN_cusparseCreateCoo;
910    /// cuSPARSE: create a BSR sparse-matrix descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
911    cusparse_create_bsr as "cusparseCreateBsr": PFN_cusparseCreateBsr;
912    /// cuSPARSE: destroy a sparse-matrix descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
913    cusparse_destroy_sp_mat as "cusparseDestroySpMat": PFN_cusparseDestroySpMat;
914    /// cuSPARSE: query rows, cols, and nnz of a sparse-matrix descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
915    cusparse_sp_mat_get_size as "cusparseSpMatGetSize": PFN_cusparseSpMatGetSize;
916    /// cuSPARSE: set an attribute (fill mode, diag type) on a sparse-matrix descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
917    cusparse_sp_mat_set_attribute as "cusparseSpMatSetAttribute": PFN_cusparseSpMatSetAttribute;
918    /// cuSPARSE: replace the row/column/value pointers on a CSR descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
919    cusparse_csr_set_pointers as "cusparseCsrSetPointers": PFN_cusparseCsrSetPointers;
920    /// cuSPARSE: replace the column/row/value pointers on a CSC descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
921    cusparse_csc_set_pointers as "cusparseCscSetPointers": PFN_cusparseCscSetPointers;
922    /// cuSPARSE: replace the row/column/value pointers on a COO descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
923    cusparse_coo_set_pointers as "cusparseCooSetPointers": PFN_cusparseCooSetPointers;
924    // Dense descriptors
925    /// cuSPARSE: create a dense-vector descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
926    cusparse_create_dn_vec as "cusparseCreateDnVec": PFN_cusparseCreateDnVec;
927    /// cuSPARSE: destroy a dense-vector descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
928    cusparse_destroy_dn_vec as "cusparseDestroyDnVec": PFN_cusparseDestroyDnVec;
929    /// cuSPARSE: create a dense-matrix descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
930    cusparse_create_dn_mat as "cusparseCreateDnMat": PFN_cusparseCreateDnMat;
931    /// cuSPARSE: destroy a dense-matrix descriptor. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
932    cusparse_destroy_dn_mat as "cusparseDestroyDnMat": PFN_cusparseDestroyDnMat;
933    // SpMV
934    /// cuSPARSE: workspace-size query for sparse matrix-vector multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
935    cusparse_spmv_buffer_size as "cusparseSpMV_bufferSize": PFN_cusparseSpMV_bufferSize;
936    /// cuSPARSE: sparse matrix-vector multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
937    cusparse_spmv as "cusparseSpMV": PFN_cusparseSpMV;
938    // SpMM
939    /// cuSPARSE: workspace-size query for sparse matrix × dense matrix multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
940    cusparse_spmm_buffer_size as "cusparseSpMM_bufferSize": PFN_cusparseSpMM_bufferSize;
941    /// cuSPARSE: preprocess stage of sparse matrix × dense matrix multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
942    cusparse_spmm_preprocess as "cusparseSpMM_preprocess": PFN_cusparseSpMM_preprocess;
943    /// cuSPARSE: sparse matrix × dense matrix multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
944    cusparse_spmm as "cusparseSpMM": PFN_cusparseSpMM;
945    // SpGEMM
946    /// cuSPARSE: create an opaque descriptor for sparse matrix × sparse matrix multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
947    cusparse_spgemm_create_descr as "cusparseSpGEMM_createDescr": PFN_cusparseSpGEMM_createDescr;
948    /// cuSPARSE: destroy an opaque descriptor for sparse matrix × sparse matrix multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
949    cusparse_spgemm_destroy_descr as "cusparseSpGEMM_destroyDescr": PFN_cusparseSpGEMM_destroyDescr;
950    /// cuSPARSE: work-estimation stage of sparse matrix × sparse matrix multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
951    cusparse_spgemm_work_estimation as "cusparseSpGEMM_workEstimation": PFN_cusparseSpGEMM_workEstimation;
952    /// cuSPARSE: compute stage of sparse matrix × sparse matrix multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
953    cusparse_spgemm_compute as "cusparseSpGEMM_compute": PFN_cusparseSpGEMM_compute;
954    /// cuSPARSE: copy stage of sparse matrix × sparse matrix multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
955    cusparse_spgemm_copy as "cusparseSpGEMM_copy": PFN_cusparseSpGEMM_copy;
956    // SpSV
957    /// cuSPARSE: create an opaque descriptor for sparse triangular linear solve (single right-hand side). See <https://docs.nvidia.com/cuda/cusparse/index.html>.
958    cusparse_spsv_create_descr as "cusparseSpSV_createDescr": PFN_cusparseSpSV_createDescr;
959    /// cuSPARSE: destroy an opaque descriptor for sparse triangular linear solve (single right-hand side). See <https://docs.nvidia.com/cuda/cusparse/index.html>.
960    cusparse_spsv_destroy_descr as "cusparseSpSV_destroyDescr": PFN_cusparseSpSV_destroyDescr;
961    /// cuSPARSE: workspace-size query for sparse triangular linear solve (single right-hand side). See <https://docs.nvidia.com/cuda/cusparse/index.html>.
962    cusparse_spsv_buffer_size as "cusparseSpSV_bufferSize": PFN_cusparseSpSV_bufferSize;
963    /// cuSPARSE: analysis stage of sparse triangular linear solve (single right-hand side). See <https://docs.nvidia.com/cuda/cusparse/index.html>.
964    cusparse_spsv_analysis as "cusparseSpSV_analysis": PFN_cusparseSpSV_analysis;
965    /// cuSPARSE: solve stage of sparse triangular linear solve (single right-hand side). See <https://docs.nvidia.com/cuda/cusparse/index.html>.
966    cusparse_spsv_solve as "cusparseSpSV_solve": PFN_cusparseSpSV_solve;
967    // SpSM
968    /// cuSPARSE: create an opaque descriptor for sparse triangular linear solve (multiple right-hand sides). See <https://docs.nvidia.com/cuda/cusparse/index.html>.
969    cusparse_spsm_create_descr as "cusparseSpSM_createDescr": PFN_cusparseSpSM_createDescr;
970    /// cuSPARSE: destroy an opaque descriptor for sparse triangular linear solve (multiple right-hand sides). See <https://docs.nvidia.com/cuda/cusparse/index.html>.
971    cusparse_spsm_destroy_descr as "cusparseSpSM_destroyDescr": PFN_cusparseSpSM_destroyDescr;
972    /// cuSPARSE: workspace-size query for sparse triangular linear solve (multiple right-hand sides). See <https://docs.nvidia.com/cuda/cusparse/index.html>.
973    cusparse_spsm_buffer_size as "cusparseSpSM_bufferSize": PFN_cusparseSpSM_bufferSize;
974    /// cuSPARSE: analysis stage of sparse triangular linear solve (multiple right-hand sides). See <https://docs.nvidia.com/cuda/cusparse/index.html>.
975    cusparse_spsm_analysis as "cusparseSpSM_analysis": PFN_cusparseSpSM_analysis;
976    /// cuSPARSE: solve stage of sparse triangular linear solve (multiple right-hand sides). See <https://docs.nvidia.com/cuda/cusparse/index.html>.
977    cusparse_spsm_solve as "cusparseSpSM_solve": PFN_cusparseSpSM_solve;
978    // SDDMM
979    /// cuSPARSE: workspace-size query for sampled dense-dense matrix multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
980    cusparse_sddmm_buffer_size as "cusparseSDDMM_bufferSize": PFN_cusparseSDDMM_bufferSize;
981    /// cuSPARSE: preprocess stage of sampled dense-dense matrix multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
982    cusparse_sddmm_preprocess as "cusparseSDDMM_preprocess": PFN_cusparseSDDMM_preprocess;
983    /// cuSPARSE: sampled dense-dense matrix multiplication. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
984    cusparse_sddmm as "cusparseSDDMM": PFN_cusparseSDDMM;
985    // CSR ↔ CSC
986    /// cuSPARSE: workspace-size query for `cusparseCsr2cscEx2`. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
987    cusparse_csr2csc_ex2_buffer_size as "cusparseCsr2cscEx2_bufferSize": PFN_cusparseCsr2cscEx2_bufferSize;
988    /// cuSPARSE: convert a CSR matrix to CSC (extended API). See <https://docs.nvidia.com/cuda/cusparse/index.html>.
989    cusparse_csr2csc_ex2 as "cusparseCsr2cscEx2": PFN_cusparseCsr2cscEx2;
990    // Sparse ↔ Dense
991    /// cuSPARSE: workspace-size query for sparse-to-dense conversion. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
992    cusparse_sparse_to_dense_buffer_size as "cusparseSparseToDense_bufferSize": PFN_cusparseSparseToDense_bufferSize;
993    /// cuSPARSE: materialize a sparse matrix into a dense one. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
994    cusparse_sparse_to_dense as "cusparseSparseToDense": PFN_cusparseSparseToDense;
995    /// cuSPARSE: workspace-size query for dense-to-sparse conversion. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
996    cusparse_dense_to_sparse_buffer_size as "cusparseDenseToSparse_bufferSize": PFN_cusparseDenseToSparse_bufferSize;
997    /// cuSPARSE: analysis stage for dense-to-sparse conversion. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
998    cusparse_dense_to_sparse_analysis as "cusparseDenseToSparse_analysis": PFN_cusparseDenseToSparse_analysis;
999    /// cuSPARSE: execute stage for dense-to-sparse conversion. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
1000    cusparse_dense_to_sparse_convert as "cusparseDenseToSparse_convert": PFN_cusparseDenseToSparse_convert;
1001    // Sparse BLAS L1
1002    /// cuSPARSE: scaled vector addition (alpha*x + beta*y → y). See <https://docs.nvidia.com/cuda/cusparse/index.html>.
1003    cusparse_axpby as "cusparseAxpby": PFN_cusparseAxpby;
1004    /// cuSPARSE: gather dense entries into a sparse vector. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
1005    cusparse_gather as "cusparseGather": PFN_cusparseGather;
1006    /// cuSPARSE: scatter sparse vector entries into a dense vector. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
1007    cusparse_scatter as "cusparseScatter": PFN_cusparseScatter;
1008    /// cuSPARSE: Givens-rotation on sparse and dense vector pair. See <https://docs.nvidia.com/cuda/cusparse/index.html>.
1009    cusparse_rot as "cusparseRot": PFN_cusparseRot;
1010}
1011
1012/// Lazy-load the cuSPARSE shared library and return its function-pointer table.
1013pub fn cusparse() -> Result<&'static Cusparse, LoaderError> {
1014    static CUSPARSE: OnceLock<Cusparse> = OnceLock::new();
1015    if let Some(c) = CUSPARSE.get() {
1016        return Ok(c);
1017    }
1018    let candidates: Vec<&'static str> = cusparse_candidates()
1019        .into_iter()
1020        .map(|s| Box::leak(s.into_boxed_str()) as &'static str)
1021        .collect();
1022    let candidates_leaked: &'static [&'static str] = Box::leak(candidates.into_boxed_slice());
1023    let lib = Library::open("cusparse", candidates_leaked)?;
1024    let c = Cusparse::empty(lib);
1025    let _ = CUSPARSE.set(c);
1026    Ok(CUSPARSE.get().expect("OnceLock set or lost race"))
1027}