MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
Endpoint.hpp
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#pragma once
6
7#include <cstdint>
8#include <bit>
9#include <memory>
10#include <optional>
11#include <uuid.h>
12#include <rdma/fabric.h>
13#include <rdma/fi_endpoint.h>
14#include "Address.hpp"
15#include "AddressVector.hpp"
16#include "Completion.hpp"
17#include "CompletionQueue.hpp"
18#include "Domain.hpp"
19#include "EventQueue.hpp"
20#include "FabricInfo.hpp"
21#include "LocalRegion.hpp"
22#include "RemoteRegion.hpp"
23
25{
29 {
30 public:
34 constexpr static auto const EQReadInterval = std::chrono::milliseconds(100);
35
38 using Id = std::uintptr_t;
39
40 public:
43 [[nodiscard]]
44 static Id randomId() noexcept;
45
48 [[nodiscard]]
49 constexpr static void* idToContextValue(Id id) noexcept
50 {
51 return std::bit_cast<void*>(id);
52 }
53
56 [[nodiscard]]
57 constexpr static Id contextValueToId(void* contextValue) noexcept
58 {
59 return std::bit_cast<Endpoint::Id>(contextValue);
60 }
61
68 [[nodiscard]]
69 constexpr static Completion::Token tokenFromId(Id id) noexcept
70 {
71 static_assert(std::is_same_v<Completion::Token, Id>, "Completion token and endpoint id should be the same underlying type.");
72 return id;
73 }
74
81 [[nodiscard]]
82 constexpr static Id idFromToken(Completion::Token token) noexcept
83 {
84 static_assert(std::is_same_v<Completion::Token, Id>, "Completion token and endpoint id should be the same underlying type.");
85 return token;
86 }
87
93 static Id idFromFID(::fid_t) noexcept;
96 static Id idFromFID(::fid_ep*) noexcept;
97
103 ~Endpoint();
104
105 // Copy constructor is deleted
106 Endpoint(Endpoint const&) = delete;
107 void operator=(Endpoint const&) = delete;
108
109 // Implements proper move semantics. An endpoint in a moved-from state can no longer be used.
110 Endpoint(Endpoint&&) noexcept;
111 // Move-assigning an endpoint to another releases all resources from the moved-into endpoint and
112 Endpoint& operator=(Endpoint&&);
113
120 static Endpoint create(std::shared_ptr<Domain> domain, FabricInfoView info);
121
126 static Endpoint create(std::shared_ptr<Domain> domain);
127
133 static Endpoint create(std::shared_ptr<Domain> domain, Id id, FabricInfoView);
134
137 static Endpoint create(std::shared_ptr<Domain> domain, Id id);
138
143 [[nodiscard]]
144 Id id() const noexcept;
145
151 void bind(std::shared_ptr<EventQueue> eq);
152
158 void bind(std::shared_ptr<CompletionQueue> cq, std::uint64_t flags);
159
165 void bind(std::shared_ptr<AddressVector> av);
166
173 void enable();
174
185 void accept();
186
193 void connect(RawFabricAddress const& addr);
194
199 void shutdown();
200
203 [[nodiscard]]
205
211 [[nodiscard]]
212 std::shared_ptr<CompletionQueue> completionQueue() const;
213
219 [[nodiscard]]
220 std::shared_ptr<EventQueue> eventQueue() const;
221
227 [[nodiscard]]
228 std::shared_ptr<AddressVector> addressVector() const;
229
232 std::pair<std::optional<Completion>, std::optional<Event>> readQueues();
233
239 std::pair<std::optional<Completion>, std::optional<Event>> readQueuesBlocking(std::chrono::steady_clock::duration timeout);
240
243 [[nodiscard]]
244 std::shared_ptr<Domain> domain() const;
245
248 [[nodiscard]]
249 FabricInfoView info() const noexcept;
250
262 [[nodiscard]]
263 std::size_t write(Completion::Token token, LocalRegion const& local, RemoteRegion const& remote, ::fi_addr_t destAddr = FI_ADDR_UNSPEC,
264 std::optional<std::uint32_t> immData = std::nullopt) const;
265
281 [[nodiscard]]
282 std::size_t write(Completion::Token token, LocalRegionGroup const& localGroup, RemoteRegion const& remote,
283 ::fi_addr_t destAddr = FI_ADDR_UNSPEC, std::optional<std::uint32_t> immData = std::nullopt) const;
284
293 void recv(LocalRegion region) const;
294
297 ::fid_ep* raw() noexcept;
298
301 [[nodiscard]]
302 ::fid_ep const* raw() const noexcept;
303
304 private:
307 void writeImpl(Completion::Token token, ::iovec const* msgIov, std::size_t iovCount, void** desc, ::fi_rma_iov const* rmaIov,
308 ::fi_addr_t destAddr, std::optional<std::uint32_t> immData) const;
309
312 void close();
313
316 Endpoint(::fid_ep* raw, FabricInfoView info, std::shared_ptr<Domain> domain,
317 std::optional<std::shared_ptr<CompletionQueue>> cq = std::nullopt, std::optional<std::shared_ptr<EventQueue>> eq = std::nullopt,
318 std::optional<std::shared_ptr<AddressVector>> av = std::nullopt);
319
320 private:
321 ::fid_ep* _raw;
323 std::shared_ptr<Domain> _domain;
324
325 std::optional<std::shared_ptr<CompletionQueue>> _cq;
326 std::optional<std::shared_ptr<EventQueue>> _eq;
327 std::optional<std::shared_ptr<AddressVector>> _av;
328 };
329}
RAII Wrapper to a libfabric fi_av instance.
RAII Wrapper around a libfabric completion queue (fi_cq).
Type to represent a completion entry from a CompletionQueue.
std::uintptr_t Token
A token used to associate a completion entry with an endpoint.
RAII Wrapper around a libfabric domain (fi_domain).
Definition Domain.hpp:24
std::shared_ptr< AddressVector > addressVector() const
Get the associated addres vector.
Definition Endpoint.cpp:215
static constexpr Id idFromToken(Completion::Token token) noexcept
Convert a completion token value to an endpoint id value. Convert a completion token to an endpoint i...
Definition Endpoint.hpp:82
::fid_ep * raw() noexcept
Get the raw libfabric handle to this endpoint.
Definition Endpoint.cpp:312
static Id idFromFID(::fid_t) noexcept
Read the endpoint id from a libfabric object reference.
Definition Endpoint.cpp:41
std::shared_ptr< Domain > domain() const
Get the domain that the endpoint has beed created on.
Definition Endpoint.cpp:302
void accept()
Accept an incoming connection.
Definition Endpoint.cpp:174
static Id randomId() noexcept
Generate a new random endpoint id.
Definition Endpoint.cpp:32
void enable()
This call transitions the endpoint into an enabled state.
Definition Endpoint.cpp:169
void close()
Close the endpoint and release all resources. Called from the destructor and the move assignment oper...
Definition Endpoint.cpp:105
std::uintptr_t Id
An id type that can be used to reference this endpoint from completions and events.
Definition Endpoint.hpp:38
RawFabricAddress localAddress() const
Definition Endpoint.cpp:190
~Endpoint()
Destructor releases all resources.
Definition Endpoint.cpp:88
std::optional< std::shared_ptr< AddressVector > > _av
Definition Endpoint.hpp:327
static Endpoint create(std::shared_ptr< Domain > domain, FabricInfoView info)
Allocates (active) new endpoint.
Definition Endpoint.cpp:56
FabricInfoView info() const noexcept
Get the fabric information passed to Endpoint::create() initially.
Definition Endpoint.cpp:307
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...
Definition Endpoint.hpp:57
void connect(RawFabricAddress const &addr)
Initiate a connection to a remote (passive) endpoint.
Definition Endpoint.cpp:180
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.
Definition Endpoint.cpp:224
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.
Definition Endpoint.cpp:242
std::shared_ptr< CompletionQueue > completionQueue() const
Get the associated completion queue.
Definition Endpoint.cpp:195
std::shared_ptr< EventQueue > eventQueue() const
Get the assiociated event queue.
Definition Endpoint.cpp:205
void bind(std::shared_ptr< EventQueue > eq)
Bind the endpoint to an event queue.
Definition Endpoint.cpp:148
void recv(LocalRegion region) const
Push a recv work request to the endpoint work queue.
Definition Endpoint.cpp:398
std::shared_ptr< Domain > _domain
Definition Endpoint.hpp:323
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...
Definition Endpoint.hpp:49
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.
Definition Endpoint.cpp:343
Id id() const noexcept
Get the id of this endpoint.
Definition Endpoint.cpp:83
static constexpr auto const EQReadInterval
The minimum interval at which the event queue will be polled when doing a blocking read of both (even...
Definition Endpoint.hpp:34
std::optional< std::shared_ptr< EventQueue > > _eq
Definition Endpoint.hpp:326
std::optional< std::shared_ptr< CompletionQueue > > _cq
Definition Endpoint.hpp:325
static constexpr Completion::Token tokenFromId(Id id) noexcept
Convert an endpoint id value to a completion token value. Convert an endpoint id to a completion toke...
Definition Endpoint.hpp:69
void shutdown()
Initiate a graceful shutdown on a connection-oriented endpoint.
Definition Endpoint.cpp:185
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.
Definition Endpoint.cpp:322
RAII Wrapper around a libfabric event queue (fi_eq).
Type to represent an event entry from an EventQueue.
Definition Event.hpp:21
Non-owning view of a FabricInfo. Returned when iterating over a FabricInfoList.
Represent a scatter-gather list of source memory regions used for data transfer.
Wrapper around a libfabric endpoint address.
Definition Address.hpp:20
FabricInfo(FabricInfoView) noexcept
Create an owning FabricInfo object from a non-owning view.
Represent a source memory region used for for data transfer.
Represent a remote memory region used for data transfer.
gInfo flags
Mark the grain as invalid.