Skip to main content

singe_cusparse/
lib.rs

1//! Safe cuSPARSE wrappers for sparse descriptors and sparse GPU operations.
2//!
3//! This crate wraps cuSPARSE contexts, sparse and dense vector descriptors,
4//! sparse and dense matrix descriptors, SpMV, SpMM, SDDMM, SpGEMM, sparse
5//! triangular solve, vector-vector operations, and scalar helpers.
6
7pub mod context;
8pub mod error;
9pub mod matrix;
10pub mod operation;
11pub mod scalar;
12pub mod sddmm;
13pub mod spgemm;
14pub mod spmm;
15pub mod spmv;
16pub mod spsm;
17pub mod spsv;
18pub mod spvv;
19pub mod types;
20pub mod vector;
21
22pub(crate) mod utility;
23
24#[cfg(feature = "testing")]
25pub mod testing;
26
27use singe_core::LibraryVersion;
28use singe_cuda::types::LibraryProperty;
29use singe_cusparse_sys as sys;
30
31use crate::error::Result;
32
33/// Returns the cuSPARSE library version as a packed NVIDIA integer.
34///
35/// # Errors
36///
37/// Returns an error if cuSPARSE cannot report one of the version properties.
38pub fn version() -> Result<LibraryVersion> {
39    let major = library_property(LibraryProperty::Major)?;
40    let minor = library_property(LibraryProperty::Minor)?;
41    let patch = library_property(LibraryProperty::Patch)?;
42    Ok(LibraryVersion::new(major as _, minor as _, patch as _))
43}
44
45/// Returns the requested cuSPARSE library property.
46/// See [`LibraryProperty`] for supported properties.
47///
48/// # Errors
49///
50/// Returns an error if cuSPARSE cannot report the requested property.
51pub fn library_property(property: LibraryProperty) -> Result<i32> {
52    let mut value = 0;
53    unsafe {
54        try_ffi!(sys::cusparseGetProperty(property.into(), &raw mut value))?;
55    }
56    Ok(value)
57}
58
59#[cfg(all(test, feature = "testing"))]
60mod tests {
61    use super::*;
62    use crate::testing::setup_context;
63    use singe_cuda::error::Error as CudaError;
64
65    #[test]
66    fn it_works() -> Result<()> {
67        let ctx = match setup_context() {
68            Ok(ctx) => ctx,
69            Err(crate::error::Error::Cuda(CudaError::Cuda { message, .. }))
70                if message == "CUDA driver is a stub library" =>
71            {
72                return Ok(());
73            }
74            Err(error) => return Err(error),
75        };
76        let context_version = ctx.version()?;
77        let library_version = version()?;
78        println!("cuSPARSE version: {context_version}");
79        assert_ne!(context_version, 0);
80        assert_ne!(library_version, 0);
81        assert_ne!(library_property(LibraryProperty::Major)?, 0);
82        Ok(())
83    }
84}