MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
MemoryRegion.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Contributors to the Media eXchange Layer project.
2//
3// SPDX-License-Identifier: Apache-2.0
4
5#include "MemoryRegion.hpp"
6#include <cstdint>
7#include <random>
8#include <sys/mman.h>
9#include <sys/uio.h>
11#include <rdma/fabric.h>
12#include <rdma/fi_domain.h>
13#include "Domain.hpp"
14#include "Exception.hpp"
15#include "Region.hpp"
16
18{
19
20 void setDeviceId(::fi_mr_attr& attr, Region::Location loc)
21 {
22 switch (loc.iface())
23 {
24 case FI_HMEM_CUDA:
25 {
26 attr.device.cuda = loc.id();
27 return;
28 }
29
30 case FI_HMEM_SYSTEM:
31 case FI_HMEM_ROCR:
32 case FI_HMEM_ZE:
33 case FI_HMEM_NEURON:
34 case FI_HMEM_SYNAPSEAI: return;
35 }
36 }
37
38 MemoryRegion MemoryRegion::reg(Domain& domain, Region const& region, std::uint64_t access)
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)
78 {}
79 };
80
81 return {raw};
82 }
83
85 {
86 catchAndLogFabricError([this]() { close(); }, "Failed to close memory region");
87 }
88
90 : _raw(other._raw)
91 {
92 other._raw = nullptr;
93 }
94
96 {
97 close();
98
99 _raw = other._raw;
100 other._raw = nullptr;
101
102 return *this;
103 }
104
105 void* MemoryRegion::desc() const noexcept
106 {
107 return fi_mr_desc(_raw);
108 }
109
110 std::uint64_t MemoryRegion::rkey() const noexcept
111 {
112 return fi_mr_key(_raw);
113 }
114
116 : _raw(raw)
117 {}
118
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 }
128
129}
#define MXL_DEBUG(...)
Definition Logging.hpp:34
RAII Wrapper around a libfabric domain (fi_domain).
Definition Domain.hpp:24
::fid_domain * raw() noexcept
Accessor for the underlying fi_domain instance.
Definition Domain.cpp:119
void operator=(MemoryRegion const &)=delete
static MemoryRegion reg(Domain &domain, Region const &region, std::uint64_t access)
Register a memory region with a specified domain and access permissions.
void close()
Internal method to release the memory region.
::fid_mr * raw() noexcept
Access the underlying raw fid_mr pointer.
void * desc() const noexcept
Get the descriptor associated with the memory region.
std::uint64_t rkey() const noexcept
Get the remote key (rkey) associated with the memory region.
MemoryRegion(MemoryRegion const &)=delete
Represent the location of a memory region.
Definition Region.hpp:29
std::string toString() const noexcept
Generate a string representation of the memory location.
Definition Region.cpp:66
std::uint64_t id() const noexcept
Return the device id. For host location 0 is returned.
Definition Region.cpp:33
bool isHost() const noexcept
Return true if the memory location is on host.
Definition Region.cpp:61
::fi_hmem_iface iface() const noexcept
Convert the current location to libfabric "iface" representation.
Definition Region.cpp:47
Represent a memory region (unregistered).
Definition Region.hpp:24
::iovec const * asIovec() const noexcept
Return the underlying iovec structure representing the memory region.
Definition Region.cpp:77
void setDeviceId(::fi_mr_attr &attr, Region::Location loc)
void catchAndLogFabricError(F &&fun, std::string_view msg)
int fiCall(F &&fun, std::string_view msg, T &&... args)
Call a libfabric function and check its return code.
gInfo flags
Mark the grain as invalid.