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 CudaDevice::name() and result::device::get_name() #252

Merged
merged 3 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
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
22 changes: 20 additions & 2 deletions src/driver/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@ pub mod device {
sys::{self, lib},
DriverError,
};
use core::ffi::c_int;
use std::mem::MaybeUninit;
use std::{
ffi::{c_int, CStr},
mem::MaybeUninit,
string::String,
};

/// Get a device for a specific ordinal.
/// See [cuDeviceGet() docs](https://docs.nvidia.com/cuda/cuda-driver-api/group__CUDA__DEVICE.html#group__CUDA__DEVICE_1g8bdd1cc7201304b01357b8034f6587cb).
Expand Down Expand Up @@ -148,6 +151,21 @@ pub mod device {
.result()?;
Ok(value.assume_init())
}

/// Get name of the device.
///
/// See [cuda docs](https://docs.nvidia.com/cuda/cuda-driver-api/group__CUDA__DEVICE.html#group__CUDA__DEVICE_1gef75aa30df95446a845f2a7b9fffbb7f)
pub fn get_name(dev: sys::CUdevice) -> Result<String, DriverError> {
const BUF_SIZE: usize = 128;
let mut buf = [0u8; BUF_SIZE];
unsafe {
lib()
.cuDeviceGetName(buf.as_mut_ptr() as _, BUF_SIZE as _, dev)
.result()?;
}
let name = CStr::from_bytes_until_nul(&buf).expect("No null byte was present");
Ok(String::from_utf8_lossy(name.to_bytes()).into())
}
}

pub mod function {
Expand Down
5 changes: 5 additions & 0 deletions src/driver/safe/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ impl CudaDevice {
self.ordinal
}

/// Get the name of this device.
pub fn name(&self) -> Result<String, result::DriverError> {
result::device::get_name(self.cu_device)
}

/// Get the underlying [sys::CUdevice] of this [CudaDevice].
///
/// # Safety
Expand Down
Loading