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 Device::get_default_mem_pool() #133

Merged
merged 1 commit into from
Dec 6, 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
42 changes: 40 additions & 2 deletions src/core/device.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::sys;
use super::{DeviceP2PAttribute, HipError, HipErrorKind, HipResult, PCIBusId, Result};
use super::{DeviceP2PAttribute, HipError, HipErrorKind, HipResult, MemPool, PCIBusId, Result};
use semver::Version;
use std::ffi::CStr;
use std::i32;
Expand Down Expand Up @@ -150,13 +150,30 @@ impl Device {
/// * There was an error retrieving the PCI bus ID
pub fn get_device_pci_bus_id(&self) -> Result<PCIBusId> {
let mut pci_bus_id = PCIBusId::new();

unsafe {
let code =
sys::hipDeviceGetPCIBusId(pci_bus_id.as_mut_ptr(), pci_bus_id.len(), self.id);
(pci_bus_id, code).to_result()
}
}

/// Gets the default memory pool associated with this device.
///
/// # Returns
/// * `Result<MemPool>` - The default memory pool for the device if successful
///
/// # Errors
/// Returns `HipError` if:
/// * The device ID is invalid
/// * The operation is not supported on this device/platform
/// * There was an error retrieving the memory pool
pub fn get_default_mem_pool(&self) -> Result<MemPool> {
let mut mem_pool = std::ptr::null_mut();
unsafe {
let code = sys::hipDeviceGetDefaultMemPool(&mut mem_pool, self.id);
(MemPool::from_raw(mem_pool), code).to_result()
}
}
}

/// Free Functions
Expand Down Expand Up @@ -300,6 +317,27 @@ pub fn get_device_by_pci_bus_id(mut pci_bus_id: PCIBusId) -> Result<Device> {
mod tests {
use super::*;

#[test]
fn test_get_default_mem_pool() {
let device = Device::new(0);
let result = device.get_default_mem_pool();

// The operation might not be supported on all devices/platforms
match result {
Ok(mem_pool) => {
println!("Successfully retrieved default memory pool");
assert!(!mem_pool.is_null());
}
Err(e) => {
// Check if the error is "not supported" which is acceptable
if e.kind != HipErrorKind::NotSupported {
panic!("Unexpected error getting default memory pool: {:?}", e);
}
println!("Memory pools not supported on this device/platform");
}
}
}

#[test]
fn test_get_device_by_pci_bus_id() {
let device = Device::new(0);
Expand Down
27 changes: 27 additions & 0 deletions src/core/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,33 @@ impl<T> Drop for MemoryPointer<T> {
}
}

/// Represents a HIP memory pool handle
#[derive(Debug)]
pub struct MemPool {
handle: sys::hipMemPool_t,
}

impl MemPool {
/// Creates a new MemPool from a raw handle
pub(crate) fn from_raw(handle: sys::hipMemPool_t) -> Self {
MemPool { handle }
}

/// Returns true if the memory pool handle is null
pub fn is_null(&self) -> bool {
self.handle.is_null()
}

/// Gets the raw handle to the memory pool
pub fn handle(&self) -> sys::hipMemPool_t {
self.handle
}
}

// Implement Send and Sync since MemPool can be safely shared between threads
unsafe impl Send for MemPool {}
unsafe impl Sync for MemPool {}

#[repr(u32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MemoryCopyKind {
Expand Down
2 changes: 2 additions & 0 deletions src/core/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub enum HipErrorKind {
Deinitialized = 4,
InvalidDevice = 101,
FileNotFound = 301,
NotSupported = 801,
Unknown = 999,
}

Expand All @@ -37,6 +38,7 @@ impl HipErrorKind {
4 => HipErrorKind::Deinitialized,
101 => HipErrorKind::InvalidDevice,
301 => HipErrorKind::FileNotFound,
801 => HipErrorKind::NotSupported,
_ => HipErrorKind::Unknown,
}
}
Expand Down