1#![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
23pub type cusparseHandle_t = *mut c_void;
27pub type cusparseSpMatDescr_t = *mut c_void;
29pub type cusparseDnMatDescr_t = *mut c_void;
31pub type cusparseDnVecDescr_t = *mut c_void;
33pub type cusparseSpGEMMDescr_t = *mut c_void;
35pub type cusparseSpSVDescr_t = *mut c_void;
37pub type cusparseSpSMDescr_t = *mut c_void;
39pub type cusparseMatDescr_t = *mut c_void;
41
42#[repr(i32)]
46#[derive(Copy, Clone, Debug, Eq, PartialEq)]
47pub enum cusparseOperation_t {
48 N = 0,
50 T = 1,
52 C = 2,
54}
55
56#[repr(i32)]
58#[derive(Copy, Clone, Debug, Eq, PartialEq)]
59pub enum cusparseIndexType_t {
60 I16U = 1,
62 I32I = 2,
64 I64I = 3,
66}
67
68#[repr(i32)]
70#[derive(Copy, Clone, Debug, Eq, PartialEq)]
71pub enum cusparseIndexBase_t {
72 Zero = 0,
74 One = 1,
76}
77
78#[repr(i32)]
80#[derive(Copy, Clone, Debug, Eq, PartialEq)]
81pub enum cusparseOrder_t {
82 Row = 1,
84 Col = 2,
86}
87
88#[repr(i32)]
90#[derive(Copy, Clone, Debug, Eq, PartialEq)]
91pub enum cusparseSpMVAlg_t {
92 Default = 0,
94 CsrAlg1 = 2,
96 CsrAlg2 = 3,
98 CooAlg1 = 1,
100 CooAlg2 = 4,
102}
103
104#[repr(i32)]
106#[derive(Copy, Clone, Debug, Eq, PartialEq)]
107pub enum cusparseSpMMAlg_t {
108 Default = 0,
110 CooAlg1 = 1,
112 CsrAlg1 = 2,
114 CooAlg2 = 3,
116 CooAlg3 = 4,
118 CsrAlg2 = 5,
120 CsrAlg3 = 6,
122 Bsr = 7,
124 CsrAlg4 = 8,
126}
127
128#[repr(i32)]
130#[derive(Copy, Clone, Debug, Eq, PartialEq)]
131pub enum cusparseSpGEMMAlg_t {
132 Default = 0,
134 Alg1 = 1,
136 Alg2 = 2,
138 Alg3 = 3,
140 CsrMemoryDefault = 4,
142}
143
144#[repr(i32)]
146#[derive(Copy, Clone, Debug, Eq, PartialEq)]
147pub enum cusparseSpSVAlg_t {
148 Default = 0,
150}
151
152#[repr(i32)]
154#[derive(Copy, Clone, Debug, Eq, PartialEq)]
155pub enum cusparseSpSMAlg_t {
156 Default = 0,
158}
159
160#[repr(i32)]
162#[derive(Copy, Clone, Debug, Eq, PartialEq)]
163pub enum cusparseSDDMMAlg_t {
164 Default = 0,
166}
167
168#[repr(i32)]
170#[derive(Copy, Clone, Debug, Eq, PartialEq)]
171pub enum cusparseCsr2CscAlg_t {
172 Alg1 = 1,
174 Alg2 = 2,
176}
177
178impl cusparseCsr2CscAlg_t {
179 pub const DEFAULT: Self = Self::Alg1;
181}
182
183#[repr(i32)]
185#[derive(Copy, Clone, Debug, Eq, PartialEq)]
186pub enum cusparseFillMode_t {
187 Lower = 0,
189 Upper = 1,
191}
192
193#[repr(i32)]
195#[derive(Copy, Clone, Debug, Eq, PartialEq)]
196pub enum cusparseDiagType_t {
197 NonUnit = 0,
199 Unit = 1,
201}
202
203#[repr(i32)]
205#[derive(Copy, Clone, Debug, Eq, PartialEq)]
206pub enum cusparseSpMatAttribute_t {
207 FillMode = 0,
209 DiagType = 1,
211}
212
213#[repr(i32)]
216#[derive(Copy, Clone, Debug, Eq, PartialEq)]
217pub enum cudaDataType {
218 R_32F = 0,
220 R_64F = 1,
222 R_16F = 2,
224 C_32F = 4,
226 C_64F = 5,
228 R_16BF = 14,
230}
231
232#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
236#[repr(transparent)]
237pub struct cusparseStatus_t(pub i32);
238
239impl cusparseStatus_t {
240 pub const SUCCESS: Self = Self(0);
242 pub const NOT_INITIALIZED: Self = Self(1);
244 pub const ALLOC_FAILED: Self = Self(2);
246 pub const INVALID_VALUE: Self = Self(3);
248 pub const ARCH_MISMATCH: Self = Self(4);
250 pub const MAPPING_ERROR: Self = Self(5);
252 pub const EXECUTION_FAILED: Self = Self(6);
254 pub const INTERNAL_ERROR: Self = Self(7);
256 pub const MATRIX_TYPE_NOT_SUPPORTED: Self = Self(8);
258 pub const ZERO_PIVOT: Self = Self(9);
260 pub const NOT_SUPPORTED: Self = Self(10);
262 pub const INSUFFICIENT_RESOURCES: Self = Self(11);
264
265 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
307pub type PFN_cusparseCreate =
311 unsafe extern "C" fn(handle: *mut cusparseHandle_t) -> cusparseStatus_t;
312pub type PFN_cusparseDestroy = unsafe extern "C" fn(handle: cusparseHandle_t) -> cusparseStatus_t;
314pub type PFN_cusparseSetStream =
316 unsafe extern "C" fn(handle: cusparseHandle_t, stream: cudaStream_t) -> cusparseStatus_t;
317pub type PFN_cusparseGetVersion =
319 unsafe extern "C" fn(handle: cusparseHandle_t, version: *mut c_int) -> cusparseStatus_t;
320
321pub 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;
335pub type PFN_cusparseDestroySpMat =
337 unsafe extern "C" fn(descr: cusparseSpMatDescr_t) -> cusparseStatus_t;
338
339pub 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;
346pub type PFN_cusparseDestroyDnVec =
348 unsafe extern "C" fn(descr: cusparseDnVecDescr_t) -> cusparseStatus_t;
349
350pub 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
364pub 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
378pub 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
395pub 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
409pub 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
427pub 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
438pub type PFN_cusparseDestroyDnMat =
440 unsafe extern "C" fn(descr: cusparseDnMatDescr_t) -> cusparseStatus_t;
441
442pub 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
450pub 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
458pub 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
466pub 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
474pub 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
482pub 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
499pub 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
514pub 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
529pub type PFN_cusparseSpGEMM_createDescr =
533 unsafe extern "C" fn(descr: *mut cusparseSpGEMMDescr_t) -> cusparseStatus_t;
534pub type PFN_cusparseSpGEMM_destroyDescr =
536 unsafe extern "C" fn(descr: cusparseSpGEMMDescr_t) -> cusparseStatus_t;
537
538pub 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
555pub 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
572pub 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
587pub type PFN_cusparseSpSV_createDescr =
591 unsafe extern "C" fn(descr: *mut cusparseSpSVDescr_t) -> cusparseStatus_t;
592pub type PFN_cusparseSpSV_destroyDescr =
594 unsafe extern "C" fn(descr: cusparseSpSVDescr_t) -> cusparseStatus_t;
595
596pub 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
610pub 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
624pub 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
637pub type PFN_cusparseSpSM_createDescr =
641 unsafe extern "C" fn(descr: *mut cusparseSpSMDescr_t) -> cusparseStatus_t;
642pub type PFN_cusparseSpSM_destroyDescr =
644 unsafe extern "C" fn(descr: cusparseSpSMDescr_t) -> cusparseStatus_t;
645
646pub 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
661pub 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
676pub 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
690pub 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
707pub 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
722pub 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
737pub 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
758pub 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
777pub 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
788pub 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
797pub 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
806pub 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
815pub 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
824pub 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
835pub 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
842pub 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
849pub 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
858fn 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 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 as "cusparseCreate": PFN_cusparseCreate;
897 cusparse_destroy as "cusparseDestroy": PFN_cusparseDestroy;
899 cusparse_set_stream as "cusparseSetStream": PFN_cusparseSetStream;
901 cusparse_get_version as "cusparseGetVersion": PFN_cusparseGetVersion;
903 cusparse_create_csr as "cusparseCreateCsr": PFN_cusparseCreateCsr;
906 cusparse_create_csc as "cusparseCreateCsc": PFN_cusparseCreateCsc;
908 cusparse_create_coo as "cusparseCreateCoo": PFN_cusparseCreateCoo;
910 cusparse_create_bsr as "cusparseCreateBsr": PFN_cusparseCreateBsr;
912 cusparse_destroy_sp_mat as "cusparseDestroySpMat": PFN_cusparseDestroySpMat;
914 cusparse_sp_mat_get_size as "cusparseSpMatGetSize": PFN_cusparseSpMatGetSize;
916 cusparse_sp_mat_set_attribute as "cusparseSpMatSetAttribute": PFN_cusparseSpMatSetAttribute;
918 cusparse_csr_set_pointers as "cusparseCsrSetPointers": PFN_cusparseCsrSetPointers;
920 cusparse_csc_set_pointers as "cusparseCscSetPointers": PFN_cusparseCscSetPointers;
922 cusparse_coo_set_pointers as "cusparseCooSetPointers": PFN_cusparseCooSetPointers;
924 cusparse_create_dn_vec as "cusparseCreateDnVec": PFN_cusparseCreateDnVec;
927 cusparse_destroy_dn_vec as "cusparseDestroyDnVec": PFN_cusparseDestroyDnVec;
929 cusparse_create_dn_mat as "cusparseCreateDnMat": PFN_cusparseCreateDnMat;
931 cusparse_destroy_dn_mat as "cusparseDestroyDnMat": PFN_cusparseDestroyDnMat;
933 cusparse_spmv_buffer_size as "cusparseSpMV_bufferSize": PFN_cusparseSpMV_bufferSize;
936 cusparse_spmv as "cusparseSpMV": PFN_cusparseSpMV;
938 cusparse_spmm_buffer_size as "cusparseSpMM_bufferSize": PFN_cusparseSpMM_bufferSize;
941 cusparse_spmm_preprocess as "cusparseSpMM_preprocess": PFN_cusparseSpMM_preprocess;
943 cusparse_spmm as "cusparseSpMM": PFN_cusparseSpMM;
945 cusparse_spgemm_create_descr as "cusparseSpGEMM_createDescr": PFN_cusparseSpGEMM_createDescr;
948 cusparse_spgemm_destroy_descr as "cusparseSpGEMM_destroyDescr": PFN_cusparseSpGEMM_destroyDescr;
950 cusparse_spgemm_work_estimation as "cusparseSpGEMM_workEstimation": PFN_cusparseSpGEMM_workEstimation;
952 cusparse_spgemm_compute as "cusparseSpGEMM_compute": PFN_cusparseSpGEMM_compute;
954 cusparse_spgemm_copy as "cusparseSpGEMM_copy": PFN_cusparseSpGEMM_copy;
956 cusparse_spsv_create_descr as "cusparseSpSV_createDescr": PFN_cusparseSpSV_createDescr;
959 cusparse_spsv_destroy_descr as "cusparseSpSV_destroyDescr": PFN_cusparseSpSV_destroyDescr;
961 cusparse_spsv_buffer_size as "cusparseSpSV_bufferSize": PFN_cusparseSpSV_bufferSize;
963 cusparse_spsv_analysis as "cusparseSpSV_analysis": PFN_cusparseSpSV_analysis;
965 cusparse_spsv_solve as "cusparseSpSV_solve": PFN_cusparseSpSV_solve;
967 cusparse_spsm_create_descr as "cusparseSpSM_createDescr": PFN_cusparseSpSM_createDescr;
970 cusparse_spsm_destroy_descr as "cusparseSpSM_destroyDescr": PFN_cusparseSpSM_destroyDescr;
972 cusparse_spsm_buffer_size as "cusparseSpSM_bufferSize": PFN_cusparseSpSM_bufferSize;
974 cusparse_spsm_analysis as "cusparseSpSM_analysis": PFN_cusparseSpSM_analysis;
976 cusparse_spsm_solve as "cusparseSpSM_solve": PFN_cusparseSpSM_solve;
978 cusparse_sddmm_buffer_size as "cusparseSDDMM_bufferSize": PFN_cusparseSDDMM_bufferSize;
981 cusparse_sddmm_preprocess as "cusparseSDDMM_preprocess": PFN_cusparseSDDMM_preprocess;
983 cusparse_sddmm as "cusparseSDDMM": PFN_cusparseSDDMM;
985 cusparse_csr2csc_ex2_buffer_size as "cusparseCsr2cscEx2_bufferSize": PFN_cusparseCsr2cscEx2_bufferSize;
988 cusparse_csr2csc_ex2 as "cusparseCsr2cscEx2": PFN_cusparseCsr2cscEx2;
990 cusparse_sparse_to_dense_buffer_size as "cusparseSparseToDense_bufferSize": PFN_cusparseSparseToDense_bufferSize;
993 cusparse_sparse_to_dense as "cusparseSparseToDense": PFN_cusparseSparseToDense;
995 cusparse_dense_to_sparse_buffer_size as "cusparseDenseToSparse_bufferSize": PFN_cusparseDenseToSparse_bufferSize;
997 cusparse_dense_to_sparse_analysis as "cusparseDenseToSparse_analysis": PFN_cusparseDenseToSparse_analysis;
999 cusparse_dense_to_sparse_convert as "cusparseDenseToSparse_convert": PFN_cusparseDenseToSparse_convert;
1001 cusparse_axpby as "cusparseAxpby": PFN_cusparseAxpby;
1004 cusparse_gather as "cusparseGather": PFN_cusparseGather;
1006 cusparse_scatter as "cusparseScatter": PFN_cusparseScatter;
1008 cusparse_rot as "cusparseRot": PFN_cusparseRot;
1010}
1011
1012pub 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}