Skip to main content

singe_cusolver/
params.rs

1#[allow(unused_imports)]
2use crate::error::Status;
3
4use std::ptr;
5
6use crate::{
7    error::{Error, Result},
8    sys, try_ffi,
9    types::{AlgorithmMode, Function},
10};
11
12#[derive(Debug)]
13pub struct Params {
14    handle: sys::cusolverDnParams_t,
15}
16
17// cuSOLVER parameter handles store solver options and do not expose mutation
18// through shared references, so immutable sharing is allowed.
19unsafe impl Send for Params {}
20unsafe impl Sync for Params {}
21
22impl Params {
23    /// Creates and initializes the parameter structure for the cuSOLVER 64-bit interface.
24    ///
25    /// The returned [`Params`] owns the cuSOLVER parameter handle and destroys it
26    /// when dropped.
27    ///
28    /// # Errors
29    ///
30    /// Returns an error if cuSOLVER cannot allocate the parameter structure or
31    /// if it does not return a valid handle.
32    pub fn create() -> Result<Self> {
33        let mut handle = ptr::null_mut();
34        unsafe {
35            try_ffi!(sys::cusolverDnCreateParams(&raw mut handle))?;
36        }
37
38        if handle.is_null() {
39            return Err(Error::NullHandle);
40        }
41
42        Ok(Self { handle })
43    }
44
45    /// Configures the algorithm for a 64-bit cuSOLVER operation.
46    ///
47    /// # Errors
48    ///
49    /// Returns an error if `function` and `algorithm` are not a valid
50    /// combination for cuSOLVER.
51    pub fn set_adv_options(&mut self, function: Function, algorithm: AlgorithmMode) -> Result<()> {
52        unsafe {
53            try_ffi!(sys::cusolverDnSetAdvOptions(
54                self.as_raw(),
55                function.into(),
56                algorithm.into(),
57            ))?;
58        }
59        Ok(())
60    }
61
62    pub fn as_raw(&self) -> sys::cusolverDnParams_t {
63        self.handle
64    }
65}
66
67impl Drop for Params {
68    fn drop(&mut self) {
69        unsafe {
70            if let Err(err) = try_ffi!(sys::cusolverDnDestroyParams(self.handle)) {
71                #[cfg(debug_assertions)]
72                eprintln!("failed to destroy cusolver params: {err}");
73            }
74        }
75    }
76}