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

2 hipdevicegetname #12

Merged
merged 5 commits into from
Nov 3, 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
65 changes: 65 additions & 0 deletions src/runtime/safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::result::{HipError, HipErrorKind, HipResult, Result};
use super::sys;
use crate::types::Device;
use semver::Version;
use std::ffi::CStr;

/// Initialize the HIP runtime.
///
Expand Down Expand Up @@ -105,6 +106,18 @@ pub fn device_compute_capability(device: Device) -> Result<Version> {
}
}

/// Returns the total amount of memory on a HIP device.
///
/// # Arguments
/// * `device` - The device to query
///
/// # Returns
/// * `Result<usize>` - The total memory in bytes if successful
///
/// # Errors
/// Returns `HipError` if:
/// * The device is invalid
/// * The runtime is not initialized
pub fn device_total_mem(device: Device) -> Result<usize> {
unsafe {
let mut size: usize = 0;
Expand All @@ -113,6 +126,15 @@ pub fn device_total_mem(device: Device) -> Result<usize> {
}
}

/// Decodes a HIP version number from its internal integer representation.
///
/// The version is encoded as: major * 1_000_000 + minor * 1_000 + patch
///
/// # Arguments
/// * `version` - The encoded version number
///
/// # Returns
/// * `Version` - A semantic version with major, minor and patch components
fn decode_hip_version(version: i32) -> Version {
if version == -1 {
return Version::new(0, 0, 0);
Expand All @@ -123,6 +145,15 @@ fn decode_hip_version(version: i32) -> Version {
Version::new(major as u64, minor as u64, patch as u64)
}

/// Gets the version of the HIP runtime.
///
/// # Returns
/// * `Result<Version>` - The runtime version if successful
///
/// # Errors
/// Returns `HipError` if:
/// * The runtime is not initialized
/// * Getting the version fails
pub fn runtime_get_version() -> Result<Version> {
unsafe {
let mut version: i32 = -1;
Expand All @@ -132,10 +163,44 @@ pub fn runtime_get_version() -> Result<Version> {
}
}

/// Gets the name of a HIP device.
///
/// # Arguments
/// * `device` - The device ID to query
///
/// # Returns
/// * `Result<String>` - The device name if successful
///
/// # Errors
/// Returns `HipError` if:
/// * The device ID is invalid
/// * There was an error retrieving the device name
/// * The name string could not be converted to valid UTF-8
pub fn get_device_name(device: Device) -> Result<String> {
const buffer_size: usize = 64;
let mut buffer = vec![0i8; buffer_size];

unsafe {
let code = sys::hipDeviceGetName(buffer.as_mut_ptr(), buffer.len() as i32, device.id);
// Convert the C string to a Rust String
let c_str = CStr::from_ptr(buffer.as_ptr());
(c_str.to_string_lossy().into_owned(), code).to_result()
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_get_device_name() {
let device = Device::new(0);
let result = get_device_name(device);
assert!(result.is_ok());
let name = result.unwrap();
println!("Device name: {}", name);
}

#[test]
fn test_runtime_get_version() {
let result = runtime_get_version();
Expand Down