singe-cusolver 0.1.0-alpha.5

Safe Rust wrappers for the NVIDIA cuSOLVER dense and sparse solver library.
Documentation
#[allow(unused_imports)]
use crate::error::Status;

use std::ptr;

use crate::{
    error::{Error, Result},
    sys, try_ffi,
    types::{AlgorithmMode, Function},
};

#[derive(Debug)]
pub struct Params {
    handle: sys::cusolverDnParams_t,
}

// cuSOLVER parameter handles store solver options and do not expose mutation
// through shared references, so immutable sharing is allowed.
unsafe impl Send for Params {}
unsafe impl Sync for Params {}

impl Params {
    /// Creates and initializes the parameter structure for the cuSOLVER 64-bit interface.
    ///
    /// The returned [`Params`] owns the cuSOLVER parameter handle and destroys it
    /// when dropped.
    ///
    /// # Errors
    ///
    /// Returns an error if cuSOLVER cannot allocate the parameter structure or
    /// if it does not return a valid handle.
    pub fn create() -> Result<Self> {
        let mut handle = ptr::null_mut();
        unsafe {
            try_ffi!(sys::cusolverDnCreateParams(&raw mut handle))?;
        }

        if handle.is_null() {
            return Err(Error::NullHandle);
        }

        Ok(Self { handle })
    }

    /// Configures the algorithm for a 64-bit cuSOLVER operation.
    ///
    /// # Errors
    ///
    /// Returns an error if `function` and `algorithm` are not a valid
    /// combination for cuSOLVER.
    pub fn set_adv_options(&mut self, function: Function, algorithm: AlgorithmMode) -> Result<()> {
        unsafe {
            try_ffi!(sys::cusolverDnSetAdvOptions(
                self.as_raw(),
                function.into(),
                algorithm.into(),
            ))?;
        }
        Ok(())
    }

    pub fn as_raw(&self) -> sys::cusolverDnParams_t {
        self.handle
    }
}

impl Drop for Params {
    fn drop(&mut self) {
        unsafe {
            if let Err(err) = try_ffi!(sys::cusolverDnDestroyParams(self.handle)) {
                #[cfg(debug_assertions)]
                eprintln!("failed to destroy cusolver params: {err}");
            }
        }
    }
}