Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add safe methods set_pointer_mode and get_pointer_mode #291

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/cublas/safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,37 @@ impl CudaBlas {
None => result::set_stream(self.handle, self.device.stream as *mut _),
}
}

/// Set the handle's pointer mode.
/// ref: <https://docs.nvidia.com/cuda/cublas/#cublassetpointermode>
///
/// Some cublas functions require the pointer mode to be set to `cublasPointerMode_t::CUBLAS_POINTER_MODE_DEVICE`
/// when passing a device memory result buffer into the function, such as `cublas<t>asum()`.
/// Otherwise the operation will panic with `SIGSEGV: invalid memory reference`,
/// or one has to use a host memory reference, which has performance implications.
pub fn set_pointer_mode(
&self,
pointer_mode: sys::cublasPointerMode_t,
) -> Result<(), CublasError> {
unsafe {
sys::lib()
.cublasSetPointerMode_v2(self.handle, pointer_mode)
.result()?;
}
Ok(())
}

/// Get the handle's current pointer mode.
/// ref: <https://docs.nvidia.com/cuda/cublas/#cublasgetpointermode>
pub fn get_pointer_mode(&self) -> Result<sys::cublasPointerMode_t, CublasError> {
unsafe {
let mut mode = ::core::mem::MaybeUninit::uninit();
sys::lib()
.cublasGetPointerMode_v2(self.handle, mode.as_mut_ptr())
.result()?;
Ok(mode.assume_init())
}
}
}

impl Drop for CudaBlas {
Expand Down Expand Up @@ -865,4 +896,23 @@ mod tests {
}
}
}

#[test]
fn cublas_pointer_mode() {
let dev = CudaDevice::new(0).unwrap();
let blas = CudaBlas::new(dev.clone()).unwrap();
assert_eq!(
blas.get_pointer_mode().unwrap(),
sys::cublasPointerMode_t::CUBLAS_POINTER_MODE_HOST,
"The default pointer mode uses host pointers"
);

blas.set_pointer_mode(sys::cublasPointerMode_t::CUBLAS_POINTER_MODE_DEVICE)
.unwrap();
assert_eq!(
blas.get_pointer_mode().unwrap(),
sys::cublasPointerMode_t::CUBLAS_POINTER_MODE_DEVICE,
"We have set the mode to use device pointers"
);
}
}
Loading