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

RAII Wrapper around a libfabric completion queue (fi_cq). More...

#include <CompletionQueue.hpp>

Inheritance diagram for mxl::lib::fabrics::ofi::CompletionQueue:
Collaboration diagram for mxl::lib::fabrics::ofi::CompletionQueue:

Classes

struct  Attributes
 A collection of attributes that can be used to configure the operation of newly created completion queues. More...

Public Member Functions

 CompletionQueue (CompletionQueue &&)=delete
 CompletionQueue (CompletionQueue const &)=delete
CompletionQueue & operator= (CompletionQueue &&)=delete
void operator= (CompletionQueue const &)=delete
::fid_cq const * raw () const noexcept
 Immutable accessor of the underlying fi_cq instance.
::fid_cq * raw () noexcept
 Mutable accessor of the underlying fi_cq instance.
std::optional< Completion > read ()
 Perform a non-blocking read on the completion queue.
std::optional< Completion > readBlocking (std::chrono::steady_clock::duration timeout)
 Perform a blocking read on the completion queue.
 ~CompletionQueue ()

Static Public Member Functions

static bool isWaitObjectSupportedForEFA () noexcept
 Check if wait any wait object type supported by the EFA provider for this libfabric version.
static std::shared_ptr< CompletionQueue > open (std::shared_ptr< Domain > domain, Attributes const &attr=Attributes::defaults())
 Create a new completion queue in the specified domain.

Private Member Functions

void close ()
 Releases all underlying resources. This is called from the destructor and the move-assignment operator.
 CompletionQueue (::fid_cq *raw, std::shared_ptr< Domain > domain)
 The completion queue can only exists behind a shared_ptr.
std::optional< Completion > handleReadResult (ssize_t ret, ::fi_cq_data_entry const &entry)
 Handle the result of a blocking or non-blocking read.

Private Attributes

std::shared_ptr< Domain > _domain
::fid_cq * _raw

Detailed Description

RAII Wrapper around a libfabric completion queue (fi_cq).

Definition at line 19 of file CompletionQueue.hpp.

Constructor & Destructor Documentation

◆ ~CompletionQueue()

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

Definition at line 96 of file CompletionQueue.cpp.

97 {
98 catchAndLogFabricError([this]() { close(); }, "Failed to close completion queue");
99 }
void close()
Releases all underlying resources. This is called from the destructor and the move-assignment operato...
void catchAndLogFabricError(F &&fun, std::string_view msg)

◆ CompletionQueue() [1/3]

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

◆ CompletionQueue() [2/3]

mxl::lib::fabrics::ofi::CompletionQueue::CompletionQueue ( CompletionQueue && )
delete

◆ CompletionQueue() [3/3]

mxl::lib::fabrics::ofi::CompletionQueue::CompletionQueue ( ::fid_cq * raw,
std::shared_ptr< Domain > domain )
private

The completion queue can only exists behind a shared_ptr.

Some events need to carry a reference to the completion queue. To prevent the queue from being released before all these events have be released, they own a shared_ptr to the queue they have originated from.

Definition at line 91 of file CompletionQueue.cpp.

92 : _raw(raw)
93 , _domain(std::move(domain))
94 {}
::fid_cq * raw() noexcept
Mutable accessor of the underlying fi_cq instance.

Member Function Documentation

◆ close()

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

Releases all underlying resources. This is called from the destructor and the move-assignment operator.

Definition at line 111 of file CompletionQueue.cpp.

112 {
113 if (_raw)
114 {
115 MXL_DEBUG("Closing completion queue");
116
117 fiCall(::fi_close, "Failed to close completion queue", &_raw->fid);
118 _raw = nullptr;
119 }
120 }
#define MXL_DEBUG(...)
Definition Logging.hpp:34
int fiCall(F &&fun, std::string_view msg, T &&... args)
Call a libfabric function and check its return code.

◆ handleReadResult()

std::optional< Completion > mxl::lib::fabrics::ofi::CompletionQueue::handleReadResult ( ssize_t ret,
::fi_cq_data_entry const & entry )
private

Handle the result of a blocking or non-blocking read.

This takes a raw completion entry and generate a Completion instance.

Definition at line 122 of file CompletionQueue.cpp.

123 {
124 if (ret == -FI_EAGAIN)
125 {
126 // No entry available
127 return std::nullopt;
128 }
129
130 if (ret == -FI_EAVAIL)
131 {
132 // An entry is available but in the error queue
133 // Zeroed: a non-zero err_data_size makes fi_cq_readerr copy into err_data.
134 ::fi_cq_err_entry err{};
135 if (auto const rc = ::fi_cq_readerr(_raw, &err, 0); rc < 0)
136 {
137 if (rc == -FI_EAGAIN)
138 {
139 return std::nullopt;
140 }
141 throw FabricException::make(static_cast<int>(rc), "Failed to read completion error: {}", ::fi_strerror(static_cast<int>(-rc)));
142 }
143
144 return Completion{
145 Completion::Error{err, this->shared_from_this()}
146 };
147 }
148
149 if (ret < 0)
150 {
151 throw FabricException::make(ret, "Failed to read completion from queue: {}", ::fi_strerror(ret));
152 }
153
154 return Completion{Completion::Data{entry}};
155 }
static FabricException make(int fiErrno, fmt::format_string< T... > fmt, T &&... args)
Create a new exception object.

◆ isWaitObjectSupportedForEFA()

bool mxl::lib::fabrics::ofi::CompletionQueue::isWaitObjectSupportedForEFA ( )
staticnoexcept

Check if wait any wait object type supported by the EFA provider for this libfabric version.

Definition at line 42 of file CompletionQueue.cpp.

43 {
44 auto const fiVersion = ::fi_version();
45
46 // check that library version >= 2.4
47 return (FI_MAJOR(fiVersion) > 2) || ((FI_MAJOR(fiVersion) == 2) && (FI_MINOR(fiVersion) >= 4));
48 }
std::uint32_t fiVersion() noexcept
Returns the libfabric version this library was compiled with.

◆ open()

std::shared_ptr< CompletionQueue > mxl::lib::fabrics::ofi::CompletionQueue::open ( std::shared_ptr< Domain > domain,
CompletionQueue::Attributes const & attr = Attributes::defaults() )
static

Create a new completion queue in the specified domain.

Definition at line 50 of file CompletionQueue.cpp.

51 {
52 ::fid_cq* cq;
53 auto cq_attr = attr.raw();
54
55 fiCall(::fi_cq_open, "Failed to open completion queue", domain->raw(), &cq_attr, &cq, nullptr);
56
57 // expose the private constructor to std::make_shared inside this function
58 struct MakeSharedEnabler : public CompletionQueue
59 {
60 MakeSharedEnabler(::fid_cq* raw, std::shared_ptr<Domain> domain)
61 : CompletionQueue(raw, domain)
62 {}
63 };
64
65 return std::make_shared<MakeSharedEnabler>(cq, domain);
66 }
CompletionQueue(CompletionQueue const &)=delete
::fi_info * raw() noexcept
Returns the raw fi_info object managed by this FabricInfo instance.

◆ operator=() [1/2]

CompletionQueue & mxl::lib::fabrics::ofi::CompletionQueue::operator= ( CompletionQueue && )
delete

◆ operator=() [2/2]

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

◆ raw() [1/2]

fid_cq const * mxl::lib::fabrics::ofi::CompletionQueue::raw ( ) const
nodiscardnoexcept

Immutable accessor of the underlying fi_cq instance.

Definition at line 106 of file CompletionQueue.cpp.

107 {
108 return _raw;
109 }

◆ raw() [2/2]

fid_cq * mxl::lib::fabrics::ofi::CompletionQueue::raw ( )
noexcept

Mutable accessor of the underlying fi_cq instance.

Definition at line 101 of file CompletionQueue.cpp.

102 {
103 return _raw;
104 }

◆ read()

std::optional< Completion > mxl::lib::fabrics::ofi::CompletionQueue::read ( )

Perform a non-blocking read on the completion queue.

If no completion event is available in the queue returns std::nullopt

Definition at line 68 of file CompletionQueue.cpp.

69 {
70 fi_cq_data_entry entry;
71
72 ssize_t ret = fi_cq_read(_raw, &entry, 1);
73
74 return handleReadResult(ret, entry);
75 }
std::optional< Completion > handleReadResult(ssize_t ret, ::fi_cq_data_entry const &entry)
Handle the result of a blocking or non-blocking read.

◆ readBlocking()

std::optional< Completion > mxl::lib::fabrics::ofi::CompletionQueue::readBlocking ( std::chrono::steady_clock::duration timeout)

Perform a blocking read on the completion queue.

This function will block until the specified timeout has elapsed, the process is signaled, or a completion is available on the queue. If no event was available after the timeout, the function returns std::nullopt. If the process is signaled while blocking on this functions, an exception will be thrown.

Definition at line 77 of file CompletionQueue.cpp.

78 {
79 auto timeoutMs = std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count();
80 if (timeoutMs == 0)
81 {
82 return read();
83 }
84
85 fi_cq_data_entry entry;
86
87 ssize_t ret = fi_cq_sread(_raw, &entry, 1, nullptr, timeoutMs);
88 return handleReadResult(ret, entry);
89 }
std::optional< Completion > read()
Perform a non-blocking read on the completion queue.

Member Data Documentation

◆ _domain

std::shared_ptr<Domain> mxl::lib::fabrics::ofi::CompletionQueue::_domain
private

The domain this queue was created into.

Definition at line 109 of file CompletionQueue.hpp.

◆ _raw

::fid_cq* mxl::lib::fabrics::ofi::CompletionQueue::_raw
private

Raw resource reference.

Definition at line 108 of file CompletionQueue.hpp.


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