16#include <rdma/fabric.h>
17#include <rdma/fi_cm.h>
18#include <rdma/fi_endpoint.h>
19#include <rdma/fi_errno.h>
20#include <rdma/fi_rma.h>
35 static auto mu = std::mutex{};
36 static auto engine = std::mt19937_64{std::random_device{}()};
37 auto const lock = std::scoped_lock{mu};
38 return std::uniform_int_distribution<Endpoint::Id>{}(engine);
68 struct MakeSharedEnabler :
public Endpoint
94 std::optional<std::shared_ptr<EventQueue>> eq, std::optional<std::shared_ptr<AddressVector>> av)
109#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG
110 auto id = this->
id();
113 fiCall(::fi_close,
"Failed to close endpoint", &
_raw->fid);
123 ,
_info(std::move(other._info))
124 ,
_domain(std::move(other._domain))
125 ,
_cq(std::move(other._cq))
126 ,
_eq(std::move(other._eq))
127 ,
_av(std::move(other._av))
129 other._raw =
nullptr;
137 other._raw =
nullptr;
139 _info = std::move(other._info);
140 _domain = std::move(other._domain);
141 _eq = std::move(other._eq);
142 _cq = std::move(other._cq);
143 _av = std::move(other._av);
150 fiCall(::fi_ep_bind,
"Failed to bind event queue to endpoint",
_raw, &eq->raw()->fid, 0);
157 fiCall(::fi_ep_bind,
"Failed to bind completion queue to endpoint",
_raw, &cq->raw()->fid,
flags);
164 fiCall(::fi_ep_bind,
"Failed to bind address vector to endpoint",
_raw, &av->raw()->fid, 0);
171 fiCall(::fi_enable,
"Failed to enable endpoint",
_raw);
176 auto dummy = std::uint8_t{};
177 fiCall(::fi_accept,
"Failed to accept connection",
_raw, &dummy,
sizeof(dummy));
182 fiCall(::fi_connect,
"Failed to connect endpoint",
_raw, addr.
raw(),
nullptr, 0);
187 fiCall(::fi_shutdown,
"Failed to shutdown endpoint",
_raw, 0);
226 std::optional<Completion> completion{std::nullopt};
227 std::optional<Event>
event{std::nullopt};
231 completion = (*_cq)->read();
236 event = (*_eq)->read();
239 return {completion,
event};
244 std::optional<Completion> completion{std::nullopt};
245 std::optional<Event>
event{std::nullopt};
256 return {(*_cq)->readBlocking(timeout),
event};
259 auto timeoutMs = std::chrono::duration_cast<std::remove_const_t<
decltype(
EQReadInterval)>>(timeout);
265 auto event = (*_eq)->read();
266 auto completion = (*_cq)->readBlocking(timeout);
268 return {completion,
event};
272 auto deadline = std::chrono::steady_clock::now() + timeout;
277 auto timeUntilDeadline = std::chrono::duration_cast<std::chrono::milliseconds>(deadline - std::chrono::steady_clock::now());
280 if (timeUntilDeadline <= std::chrono::milliseconds(0))
282 return {completion,
event};
286 event = (*_eq)->read();
289 return {completion,
event};
294 completion = (*_cq)->readBlocking(std::min(
EQReadInterval, timeUntilDeadline));
297 return {completion,
event};
323 ::fi_addr_t destAddr, std::optional<std::uint32_t> immData)
const
325 std::uint64_t data = immData.value_or(0);
326 std::uint64_t
flags = FI_DELIVERY_COMPLETE;
327 flags |= immData.has_value() ? FI_REMOTE_CQ_DATA : 0;
332 .iov_count = iovCount,
340 fiCall(::fi_writemsg,
"Failed to push rma write to work queue.",
_raw, &msg,
flags);
344 std::optional<std::uint32_t> immData)
const
346 std::vector<void*> descs{local.
desc};
351 writeImpl(token, &msgIov, 1, descs.data(), &rmaIov, destAddr, immData);
357 std::optional<std::uint32_t> immData)
const
359 auto remoteRmaIov = remote.
toRmaIov();
360 auto remoteOffset = std::size_t{0};
363 auto const nbWrites = (localGroup.
size() + iovLimit - 1) / iovLimit;
365 for (
auto i = std::size_t{0}; i < nbWrites; i++)
367 auto const begin = i * iovLimit;
368 auto const end = begin + std::min(iovLimit, localGroup.
size() - begin);
370 auto const localGroupSpan = localGroup.
span(begin, end);
373 if ((remoteOffset + localGroupSpan.byteSize()) > remote.
len)
378 remoteRmaIov.addr = remote.
addr + remoteOffset;
379 remoteRmaIov.len = localGroupSpan.byteSize();
381 auto const isLastWrite = (i == nbWrites - 1);
382 auto const immDataForWrite = isLastWrite ? immData : std::nullopt;
385 localGroupSpan.asIovec(),
386 localGroupSpan.size(),
387 const_cast<void**
>(localGroupSpan.desc()),
392 remoteOffset += localGroupSpan.byteSize();
401 fiCall(::fi_recv,
"Failed to push recv to work queue",
_raw, iovec.iov_base, iovec.iov_len,
nullptr, FI_ADDR_UNSPEC,
nullptr);
std::uintptr_t Token
A token used to associate a completion entry with an endpoint.
static constexpr void * tokenToContextValue(Token token) noexcept
Convert between completion tokens and context values that can be written to and read from raw libfabr...
std::shared_ptr< AddressVector > addressVector() const
Get the associated addres vector.
::fid_ep * raw() noexcept
Get the raw libfabric handle to this endpoint.
static Id idFromFID(::fid_t) noexcept
Read the endpoint id from a libfabric object reference.
std::shared_ptr< Domain > domain() const
Get the domain that the endpoint has beed created on.
void accept()
Accept an incoming connection.
static Id randomId() noexcept
Generate a new random endpoint id.
void enable()
This call transitions the endpoint into an enabled state.
void close()
Close the endpoint and release all resources. Called from the destructor and the move assignment oper...
std::uintptr_t Id
An id type that can be used to reference this endpoint from completions and events.
RawFabricAddress localAddress() const
~Endpoint()
Destructor releases all resources.
std::optional< std::shared_ptr< AddressVector > > _av
static Endpoint create(std::shared_ptr< Domain > domain, FabricInfoView info)
Allocates (active) new endpoint.
FabricInfoView info() const noexcept
Get the fabric information passed to Endpoint::create() initially.
void operator=(Endpoint const &)=delete
Endpoint(Endpoint const &)=delete
static constexpr Id contextValueToId(void *contextValue) noexcept
Convert between Ids and context values that can be written to and read from raw libfabric queue entri...
void connect(RawFabricAddress const &addr)
Initiate a connection to a remote (passive) endpoint.
std::pair< std::optional< Completion >, std::optional< Event > > readQueues()
Do a non-blocking read of both the event and completion queue associated with this endpoint.
std::pair< std::optional< Completion >, std::optional< Event > > readQueuesBlocking(std::chrono::steady_clock::duration timeout)
Do a combined blocking read of both the event and completion queue associated with this endpoint.
std::shared_ptr< CompletionQueue > completionQueue() const
Get the associated completion queue.
std::shared_ptr< EventQueue > eventQueue() const
Get the assiociated event queue.
void bind(std::shared_ptr< EventQueue > eq)
Bind the endpoint to an event queue.
void recv(LocalRegion region) const
Push a recv work request to the endpoint work queue.
std::shared_ptr< Domain > _domain
static constexpr void * idToContextValue(Id id) noexcept
Convert between Ids and context values that can be written to and read from raw libfabric queue entri...
std::size_t write(Completion::Token token, LocalRegion const &local, RemoteRegion const &remote, ::fi_addr_t destAddr=FI_ADDR_UNSPEC, std::optional< std::uint32_t > immData=std::nullopt) const
Push a remote write work request of a single contiguous buffer to the endpoint work queue.
Id id() const noexcept
Get the id of this endpoint.
static constexpr auto const EQReadInterval
The minimum interval at which the event queue will be polled when doing a blocking read of both (even...
std::optional< std::shared_ptr< EventQueue > > _eq
std::optional< std::shared_ptr< CompletionQueue > > _cq
void shutdown()
Initiate a graceful shutdown on a connection-oriented endpoint.
void writeImpl(Completion::Token token, ::iovec const *msgIov, std::size_t iovCount, void **desc, ::fi_rma_iov const *rmaIov, ::fi_addr_t destAddr, std::optional< std::uint32_t > immData) const
Internal implementation of the remote write request.
static Exception invalidArgument(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_INVALID_ARG exception.
static Exception internal(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_INTERNAL exception.
static Exception invalidState(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_INVALID_STATE exception.
Non-owning view of a FabricInfo. Returned when iterating over a FabricInfoList.
std::size_t txIovLimit() const noexcept
Return the IOV limit for this fabric info, which is the maximum number of iovecs that can be posted i...
Represent a scatter-gather list of source memory regions used for data transfer.
LocalRegionGroupSpan span(std::size_t beginIndex, std::size_t endIndex) const
Return a span representing a contiguous subset of the LocalRegionGroup.
std::size_t size() const noexcept
Wrapper around a libfabric endpoint address.
void * raw() noexcept
Pointer to the raw address data.
static RawFabricAddress fromFid(::fid_t fid, FabricInfoView info)
Retrieve the fabric address of an endpoint by passing its fid.
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.
std::filesystem::path _domain
The monitored domain.
Represent a source memory region used for for data transfer.
::iovec toIovec() const noexcept
Convert this LocalRegion to a struct iovec used by libfabric transfer functions.
Represent a remote memory region used for data transfer.
::fi_rma_iov toRmaIov() const noexcept
Convert this RemoteRegion to a struct fi_rma_iov used by libfabric RMA transfer functions.
gInfo flags
Mark the grain as invalid.