MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
mxl::lib::fabrics::ofi::MemoryRegion Class Reference

RAII wrapper around a libfabric memory region (fid_mr). More...

#include <MemoryRegion.hpp>

Public Member Functions

void * desc () const noexcept
 Get the descriptor associated with the memory region.
 MemoryRegion (MemoryRegion &&) noexcept
 MemoryRegion (MemoryRegion const &)=delete
MemoryRegion & operator= (MemoryRegion &&) noexcept
void operator= (MemoryRegion const &)=delete
::fid_mr const * raw () const noexcept
 Access the underlying raw fid_mr pointer.
::fid_mr * raw () noexcept
 Access the underlying raw fid_mr pointer.
std::uint64_t rkey () const noexcept
 Get the remote key (rkey) associated with the memory region.
 ~MemoryRegion ()

Static Public Member Functions

static MemoryRegion reg (Domain &domain, Region const &region, std::uint64_t access)
 Register a memory region with a specified domain and access permissions.

Private Member Functions

void close ()
 Internal method to release the memory region.
 MemoryRegion (::fid_mr *raw)

Private Attributes

::fid_mr * _raw

Friends

class RegisteredRegion

Detailed Description

RAII wrapper around a libfabric memory region (fid_mr).

Definition at line 19 of file MemoryRegion.hpp.

Constructor & Destructor Documentation

◆ ~MemoryRegion()

mxl::lib::fabrics::ofi::MemoryRegion::~MemoryRegion ( )

Definition at line 84 of file MemoryRegion.cpp.

85 {
86 catchAndLogFabricError([this]() { close(); }, "Failed to close memory region");
87 }
void close()
Internal method to release the memory region.
void catchAndLogFabricError(F &&fun, std::string_view msg)

◆ MemoryRegion() [1/3]

mxl::lib::fabrics::ofi::MemoryRegion::MemoryRegion ( MemoryRegion const & )
delete

◆ MemoryRegion() [2/3]

mxl::lib::fabrics::ofi::MemoryRegion::MemoryRegion ( MemoryRegion && other)
noexcept

Definition at line 89 of file MemoryRegion.cpp.

90 : _raw(other._raw)
91 {
92 other._raw = nullptr;
93 }

◆ MemoryRegion() [3/3]

mxl::lib::fabrics::ofi::MemoryRegion::MemoryRegion ( ::fid_mr * raw)
private

Definition at line 115 of file MemoryRegion.cpp.

116 : _raw(raw)
117 {}
::fid_mr * raw() noexcept
Access the underlying raw fid_mr pointer.

Member Function Documentation

◆ close()

void mxl::lib::fabrics::ofi::MemoryRegion::close ( )
private

Internal method to release the memory region.

Definition at line 119 of file MemoryRegion.cpp.

120 {
121 if (_raw)
122 {
123 MXL_DEBUG("Closing memory region with rkey={:x}", rkey());
124 fiCall(::fi_close, "Failed to close memory region", &_raw->fid);
125 _raw = nullptr;
126 }
127 }
#define MXL_DEBUG(...)
Definition Logging.hpp:34
std::uint64_t rkey() const noexcept
Get the remote key (rkey) associated with the memory region.
int fiCall(F &&fun, std::string_view msg, T &&... args)
Call a libfabric function and check its return code.

◆ desc()

void * mxl::lib::fabrics::ofi::MemoryRegion::desc ( ) const
nodiscardnoexcept

Get the descriptor associated with the memory region.

Definition at line 105 of file MemoryRegion.cpp.

106 {
107 return fi_mr_desc(_raw);
108 }

◆ operator=() [1/2]

MemoryRegion & mxl::lib::fabrics::ofi::MemoryRegion::operator= ( MemoryRegion && other)
noexcept

Definition at line 95 of file MemoryRegion.cpp.

96 {
97 close();
98
99 _raw = other._raw;
100 other._raw = nullptr;
101
102 return *this;
103 }

◆ operator=() [2/2]

void mxl::lib::fabrics::ofi::MemoryRegion::operator= ( MemoryRegion const & )
delete

◆ raw() [1/2]

::fid_mr const * mxl::lib::fabrics::ofi::MemoryRegion::raw ( ) const
nodiscardnoexcept

Access the underlying raw fid_mr pointer.

◆ raw() [2/2]

::fid_mr * mxl::lib::fabrics::ofi::MemoryRegion::raw ( )
nodiscardnoexcept

Access the underlying raw fid_mr pointer.

◆ reg()

MemoryRegion mxl::lib::fabrics::ofi::MemoryRegion::reg ( Domain & domain,
Region const & region,
std::uint64_t access )
static

Register a memory region with a specified domain and access permissions.

Parameters
domainThe domain to register the memory region with.
regionThe region to register.
accessThe access permissions for the memory region. Possible values are:
  • FI_SEND: The memory buffer may be used in outgoing message data transfers.
  • FI_RECV: The memory buffer may be used to receive inbound message transfers.
  • FI_WRITE: The memory buffer may be used as the source buffer for RMA write and atomic operations on the initiator side.
  • FI_REMOTE_READ: The memory buffer may be used as the source buffer of an RMA read operation on the target side.
  • FI_REMOTE_WRITE: The memory buffer may be used as the target buffer of an RMA write operation on the target side.
Returns
A MemoryRegion object representing the registered memory region.

Definition at line 38 of file MemoryRegion.cpp.

39 {
40 ::fid_mr* raw;
41 std::random_device rd;
42 std::mt19937_64 gen(rd());
43 std::uniform_int_distribution<uint64_t> dist(0, UINT64_MAX);
44
45 std::uint64_t flags = 0;
46 flags |= region.loc.isHost() ? 0 : FI_HMEM_DEVICE_ONLY;
47
48 ::fi_mr_attr attr{};
49 setDeviceId(attr, region.loc);
50
51 attr.mr_iov = region.asIovec();
52 attr.iov_count = 1;
53 attr.access = access;
54 attr.offset = 0; // reserved to 0
55 attr.requested_key = dist(gen); // creating a random key, but it can be ignored if FI_MR_PROV_KEY mr mode is set
56 attr.context = nullptr; // not used
57 attr.auth_key_size = 0;
58 attr.auth_key = nullptr;
59 attr.iface = region.loc.iface();
60 attr.hmem_data = nullptr;
61 attr.page_size = 4096; // not used'
62 attr.base_mr = nullptr; // not used
63 attr.sub_mr_cnt = 0; // not used
64
65 fiCall(fi_mr_regattr, "Failed to register memory region", domain.raw(), &attr, flags, &raw);
66
67 MXL_DEBUG("Registered memory region with address 0x{}, size {} and location {} -> key 0x{:x} desc {}",
68 region.base,
69 region.size,
70 region.loc.toString(),
71 raw->key,
72 raw->mem_desc);
73
74 struct MakeSharedEnabler : public MemoryRegion
75 {
76 MakeSharedEnabler(::fid_mr* raw)
77 : MemoryRegion(raw)
78 {}
79 };
80
81 return {raw};
82 }
MemoryRegion(MemoryRegion const &)=delete
void setDeviceId(::fi_mr_attr &attr, Region::Location loc)
::fi_info * raw() noexcept
Returns the raw fi_info object managed by this FabricInfo instance.
gInfo flags
Mark the grain as invalid.

◆ rkey()

std::uint64_t mxl::lib::fabrics::ofi::MemoryRegion::rkey ( ) const
nodiscardnoexcept

Get the remote key (rkey) associated with the memory region.

This the mechanism for protecting a registered region. One should be careful and should only share this secret with initiators that are allowed to access this memory region.

Definition at line 110 of file MemoryRegion.cpp.

111 {
112 return fi_mr_key(_raw);
113 }

◆ RegisteredRegion

friend class RegisteredRegion
friend

Definition at line 67 of file MemoryRegion.hpp.

Member Data Documentation

◆ _raw

::fid_mr* mxl::lib::fabrics::ofi::MemoryRegion::_raw
private

Definition at line 77 of file MemoryRegion.hpp.


The documentation for this class was generated from the following files: