Memory Handle#
2026-03-31
6 min read time
The ucxx::MemoryHandle class wraps a UCP memory handle (ucp_mem_h). Memory handles
are used to register memory regions for remote memory access (RMA) operations. A memory
handle can either allocate new memory or map an existing buffer for RMA.
#include <ucxx/memory_handle.h>
namespace ucxx
-
class MemoryHandle : public ucxx::Component#
Component holding a UCP memory handle.
The UCP layer provides RMA (Remote Memory Access) to memory handles that it controls in form of
ucp_mem_hobject, this class encapsulates that object and provides methods to simplify its handling.Public Functions
- ucp_mem_h getHandle(
Get the underlying
ucp_mem_hhandle.Lifetime of the
ucp_mem_hhandle is managed by theucxx::MemoryHandleobject and its ownership is non-transferrable. Once theucxx::MemoryHandleis destroyed the memory is unmapped and the handle is not valid anymore, it is the user’s responsibility to ensure the owner’s lifetime while using the handle.// memoryHandle is `std::shared_ptr<ucxx::MemoryHandle>` ucp_mem_h ucpMemoryHandle = memoryHandle->getHandle();
- Returns:
The underlying
ucp_mem_hhandle.
- size_t getSize(
Get the size of the memory allocation.
Get the size of the memory allocation, which is at least the number of bytes specified with the
sizeargument passed tocreateMemoryHandle().// memoryHandle is `std::shared_ptr<ucxx::MemoryHandle>` auto memorySize = memoryHandle->getSize();
- Returns:
The size of the memory allocation.
- uint64_t getBaseAddress(
Get the base address of the memory allocation.
Get the base address of the memory allocation, which is going to be used as the remote address to put or get memory via the
ucxx::Endpoint::memPut()orucxx::Endpoint::memGet()methods.// memoryHandle is `std::shared_ptr<ucxx::MemoryHandle>` auto memoryBase Address = memoryHandle->getBaseAddress();
- Returns:
The base address of the memory allocation.
- ucs_memory_type_t getMemoryType(
Get the memory type of the allocation.
Get the memory type of the allocation, which indicates whether the memory is host memory or device memory (i.e., CUDA).
- Returns:
The memory type of the allocation.
- std::shared_ptr<RemoteKey> createRemoteKey(
Create a remote key for the memory allocation.
Create a remote key that can be used by a remote endpoint to access this memory allocation. The remote key is required for remote memory access operations.
- Returns:
A shared pointer to the created remote key.
Friends
- std::shared_ptr<Context> context,
- const size_t size,
- void *buffer,
- const ucs_memory_type_t memoryType
Constructor for
shared_ptr<ucxx::MemoryHandle>.The constructor for a
shared_ptr<ucxx::MemoryHandle>object, mapping a memory buffer with UCP to provide RMA (Remote Memory Access) to.The allocation requires a
sizeand abuffer. Thebufferprovided may be either anullptr, in which case UCP will allocate a new memory region for it, or an already existing allocation, in which case UCP will only map it for RMA and it’s the caller’s responsibility to keepbufferalive until this object is destroyed. When the UCP allocatesbuffer(i.e., when the value passed isnullptr), the actual size of the allocation may be larger than requested, and can later be found calling thegetSize()method, if a preallocated buffer is passedgetSize()will return the same value specified forsize.// `context` is `std::shared_ptr<ucxx::Context>` // Allocate a 128-byte buffer with UCP. auto memoryHandle = context->createMemoryHandle(128, nullptr); // Equivalent to line above // auto memoryHandle = ucxx::createMemoryHandle(context, 128, nullptr); // Map an existing 128-byte buffer with UCP. size_t allocationSize = 128; auto buffer = new uint8_t[allocationSize]; auto memoryHandleFromBuffer = context->createMemoryHandle( allocationSize * sizeof(*buffer), reinterpret_cast<void*>(buffer) ); // Equivalent to line above // auto memoryHandleFromBuffer = ucxx::createMemoryHandle( // context, allocationSize * sizeof(*buffer), reinterpret_cast<void*>(buffer) // );
- Throws:
- Parameters:
context – [in] parent context where to map memory.
size – [in] the minimum size of the memory allocation
buffer – [in] the pointer to an existing allocation or
nullptrto allocate a new memory region.memoryType – [in] the type of memory the handle points to.
- Returns:
The
shared_ptr<ucxx::MemoryHandle>object
Factory Functions#
#include <ucxx/constructors.h>
- std::shared_ptr<Context> context,
- const size_t size,
- void *buffer = nullptr,
- const ucs_memory_type_t memoryType = UCS_MEMORY_TYPE_HOST
The constructor for a
shared_ptr<ucxx::MemoryHandle>object, mapping a memory buffer with UCP to provide RMA (Remote Memory Access) to.The allocation requires a
sizeand abuffer. Thebufferprovided may be either anullptr, in which case UCP will allocate a new memory region for it, or an already existing allocation, in which case UCP will only map it for RMA and it’s the caller’s responsibility to keepbufferalive until this object is destroyed. When the UCP allocatesbuffer(i.e., when the value passed isnullptr), the actual size of the allocation may be larger than requested, and can later be found calling thegetSize()method, if a preallocated buffer is passedgetSize()will return the same value specified forsize.// `context` is `std::shared_ptr<ucxx::Context>` // Allocate a 128-byte buffer with UCP. auto memoryHandle = context->createMemoryHandle(128, nullptr); // Equivalent to line above // auto memoryHandle = ucxx::createMemoryHandle(context, 128, nullptr); // Map an existing 128-byte buffer with UCP. size_t allocationSize = 128; auto buffer = new uint8_t[allocationSize]; auto memoryHandleFromBuffer = context->createMemoryHandle( allocationSize * sizeof(*buffer), reinterpret_cast<void*>(buffer) ); // Equivalent to line above // auto memoryHandleFromBuffer = ucxx::createMemoryHandle( // context, allocationSize * sizeof(*buffer), reinterpret_cast<void*>(buffer) // );
- Throws:
- Parameters:
context – [in] parent context where to map memory.
size – [in] the minimum size of the memory allocation
buffer – [in] the pointer to an existing allocation or
nullptrto allocate a new memory region.memoryType – [in] the type of memory the handle points to.
- Returns:
The
shared_ptr<ucxx::MemoryHandle>object