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

feat: support cursor for lmr #114

Merged
merged 3 commits into from
Sep 1, 2023
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
9 changes: 7 additions & 2 deletions examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ async fn send_data_to_server(rdma: &Rdma) -> io::Result<()> {
async fn send_data_with_imm_to_server(rdma: &Rdma) -> io::Result<()> {
// alloc 8 bytes local memory
let mut lmr = rdma.alloc_local_mr(Layout::new::<[u8; 8]>())?;
// write data into lmr
let _num = lmr.as_mut_slice().write(&[1_u8; 8])?;
{
// use cursor to append data to lmr
// use this in a scope to drop mr_cursor automatically
let mut mr_cursor = lmr.as_mut_slice_cursor();
let _num = mr_cursor.write(&[1_u8; 4])?;
let _num = mr_cursor.write(&[1_u8; 4])?;
}
// send data and imm to the remote end
rdma.send_with_imm(&lmr, 1_u32).await?;
Ok(())
Expand Down
64 changes: 64 additions & 0 deletions src/memory_region/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use sealed::sealed;
use std::{
alloc::{dealloc, Layout},
fmt::Debug,
io::Cursor,
ops::Range,
slice,
sync::Arc,
Expand Down Expand Up @@ -91,6 +92,37 @@ pub unsafe trait LocalMrReadAccess: MrAccess {
)
}

/// Get the memory region as slice until it is readable warppered with cursor
/// user can use cursor to read or write data without maintaining the index manually
///
/// If this mr is being used in RDMA ops, the thread may be blocked
#[inline]
#[allow(clippy::as_conversions)]
fn as_slice_cursor(&self) -> MappedRwLockReadGuard<Cursor<&[u8]>> {
// SAFETY: memory of this mr should have been initialized
MappedRwLockReadGuard::map(self.as_ptr(), |ptr| unsafe {
Cursor::new(slice::from_raw_parts(ptr, self.length()))
})
}

/// Try to get the memory region as slice warppered with cursor
/// user can use cursor to read or write data without maintaining the index manually
///
/// Return `None` if this mr is being used in RDMA ops without blocking thread
#[allow(clippy::as_conversions)]
#[inline]
fn try_as_slice_cursor(&self) -> Option<MappedRwLockReadGuard<Cursor<&[u8]>>> {
self.try_as_ptr().map_or_else(
|| None,
|guard| {
// SAFETY: memory of this mr should have been initialized
return Some(MappedRwLockReadGuard::map(guard, |ptr| unsafe {
Cursor::new(slice::from_raw_parts(ptr, self.length()))
}));
},
)
}

/// Get the memory region as slice without lock
///
/// # Safety
Expand Down Expand Up @@ -275,6 +307,38 @@ pub unsafe trait LocalMrWriteAccess: MrAccess + LocalMrReadAccess {
)
}

/// Try to get the memory region as mutable slice warppered with cursor
/// user can use cursor to read or write data without maintaining the index manually
///
/// If this mr is being used in RDMA ops, the thread may be blocked
#[inline]
#[allow(clippy::as_conversions)]
fn as_mut_slice_cursor(&mut self) -> MappedRwLockWriteGuard<Cursor<&mut [u8]>> {
let len = self.length();
// SAFETY: memory of this mr should have been initialized
MappedRwLockWriteGuard::map(self.as_mut_ptr(), |ptr| unsafe {
Cursor::new(slice::from_raw_parts_mut(ptr, len))
})
}

/// Try to get the memory region as mutable slice warppered with cursor
/// user can use cursor to read or write data without maintaining the index manually
///
/// Return `None` if this mr is being used in RDMA ops without blocking thread
#[inline]
#[allow(clippy::as_conversions)]
fn try_as_mut_slice_cursor(&mut self) -> Option<MappedRwLockWriteGuard<Cursor<&mut [u8]>>> {
self.try_as_mut_ptr().map_or_else(
|| None,
|guard| {
// SAFETY: memory of this mr should have been initialized
return Some(MappedRwLockWriteGuard::map(guard, |ptr| unsafe {
Cursor::new(slice::from_raw_parts_mut(ptr, self.length()))
}));
},
)
}

/// Get the memory region as mut slice without lock
///
/// # Safety
Expand Down