MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
ProtocolIngressRMA.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
8#include "DataLayout.hpp"
9#include "Exception.hpp"
10#include "ImmData.hpp"
11#include "Region.hpp"
12
14{
15 //
16 // RMAGrainIngressProtocol implementations below
18 : _regions{std::move(regions)}
19 {}
20
21 std::vector<RemoteRegion> RMAGrainIngressProtocol::registerMemory(std::shared_ptr<Domain> domain)
22 {
24 {
25 throw Exception::invalidState("Memory is already registered.");
26 }
27
28 domain->registerRegions(_regions, FI_REMOTE_WRITE);
30
31 return domain->remoteRegions();
32 }
33
34 std::optional<TargetInfoBounceBufferInfo> RMAGrainIngressProtocol::bounceBufferInfo() const
35 {
36 return std::nullopt;
37 }
38
40 {
41 if (endpoint.domain()->usingRecvBufForCqData())
42 {
43 endpoint.recv(immDataRegion());
44 }
45 }
46
47 std::optional<Target::ReadResult> RMAGrainIngressProtocol::read(Endpoint const& endpoint, Completion const& completion)
48 {
49 auto completionData = completion.tryData();
50 if (!completionData)
51 {
52 return {};
53 }
54
56 {
57 endpoint.recv(_immDataBuffer->toLocalRegion());
58 }
59
60 auto immData = completionData->data();
61 if (!immData)
62 {
63 return {};
64 }
65
66 auto [slot, slice] = ImmDataGrain{static_cast<std::uint32_t>(*immData)}.unpack();
67
68 // Set the number of valid slices in the grain header. This information is received through the immediate data and must be updated
69 // in the local shared memory in the case of partial writes.
70 setValidSlicesForGrain(_regions, slot, slice);
71
72 // Get the actual grain index from the grain header in share memory. This was written in the first RMA write.
73 auto grainIndex = getGrainIndexInRingSlot(_regions, slot);
74
75 return std::make_optional<Target::GrainReadResult>(grainIndex);
76 }
77
79 {
80 return true;
81 }
82
84 {
85 return false;
86 }
87
90
92 {
93 if (!_immDataBuffer)
94 {
95 _immDataBuffer.emplace();
96 }
97
98 return _immDataBuffer->toLocalRegion();
99 }
100
101 //
102 // RMASampleIngressProtocol implementations below
103 RMASampleIngressProtocol::RMASampleIngressProtocol(Region region, DataLayout::Continuous const& layout, std::uint32_t maxSyncBatchSize)
104 : _bounceBuffer{makeAudioBounceBuffer(layout, maxSyncBatchSize)}
105 , _region{region}
106 {}
107
108 std::vector<RemoteRegion> RMASampleIngressProtocol::registerMemory(std::shared_ptr<Domain> domain)
109 {
111 {
112 throw Exception::invalidState("Memory is already registered.");
113 }
114
115 domain->registerRegions(_bounceBuffer.getRegions(), FI_REMOTE_WRITE);
116 _isMemoryRegistered = true;
117
118 return domain->remoteRegions();
119 }
120
121 std::optional<TargetInfoBounceBufferInfo> RMASampleIngressProtocol::bounceBufferInfo() const
122 {
123 auto const entrySize = _bounceBuffer.entrySize(); // All entries have the same size, we can just take the size of the first one
124 auto const entryCount = _bounceBuffer.entryCount();
125
126 return TargetInfoBounceBufferInfo{.entryCount = entryCount, .entrySize = entrySize};
127 }
128
130 {
131 if (endpoint.domain()->usingRecvBufForCqData())
132 {
133 endpoint.recv(immDataRegion());
134 }
135 }
136
137 std::optional<Target::ReadResult> RMASampleIngressProtocol::read(Endpoint const& endpoint, Completion const& completion)
138 {
139 auto completionData = completion.tryData();
140 if (!completionData)
141 {
142 return {};
143 }
144
145 if (_immDataBuffer)
146 {
147 endpoint.recv(immDataRegion());
148 }
149
150 auto const immData = completionData->data();
151 if (!immData)
152 {
153 throw Exception::invalidState("Received a completion without immediate data.");
154 }
155
156 auto const header = _bounceBuffer.unpack(*immData, _region);
157 if (header.count == 0)
158 {
159 return {};
160 }
161 return std::make_optional<Target::SampleReadResult>(header.headIndex, header.count);
162 }
163
165 {
166 return false;
167 }
168
170 {
171 return true;
172 }
173
176
178 {
179 if (!_immDataBuffer)
180 {
181 _immDataBuffer.emplace();
182 }
183
184 return _immDataBuffer->toLocalRegion();
185 }
186
188 {
189 auto const oneSampleSize = layout.sampleSize * layout.channelCount;
190 auto const entrySize = oneSampleSize * maxSyncBatchSize;
191 auto const historySize = layout.bufferLength * oneSampleSize;
192
193 auto const entryCount = (historySize + entrySize - 1U) / entrySize; // ceil(historySize / entrySize)
194
195 MXL_INFO("Creating audio bounce buffer with entry size {} bytes and entry count {}, maxSyncBatchSize {} historySize {} bufferLength {}",
196 entrySize,
197 entryCount,
198 maxSyncBatchSize,
199 historySize,
200 layout.bufferLength);
201 return {entryCount, entrySize, layout};
202 }
203
204}
#define MXL_INFO(...)
Definition Logging.hpp:45
A bounce buffer for audio samples.
Type to represent a completion entry from a CompletionQueue.
std::optional< Data > tryData() const noexcept
Attempt to access the data variant. Returns std::nullopt if this is an error entry.
RAII Wrapper around a libfabric endpoint (fi_ep).
Definition Endpoint.hpp:29
std::shared_ptr< Domain > domain() const
Get the domain that the endpoint has beed created on.
Definition Endpoint.cpp:302
void recv(LocalRegion region) const
Push a recv work request to the endpoint work queue.
Definition Endpoint.cpp:398
static Exception invalidState(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_INVALID_STATE exception.
Definition Exception.hpp:59
Immediate data representation for discrete flow transfers.
Definition ImmData.hpp:15
Unpacked unpack() const noexcept
Unpack the immediate data into ring buffer index and slice index.
Definition ImmData.cpp:22
virtual std::optional< Target::ReadResult > read(Endpoint const &endpoint, Completion const &completion) override
virtual bool canReadGrains() const noexcept override
This protocol can read grains, but not samples, since it is designed for remote writes of grain buffe...
std::optional< Target::ImmediateDataLocation > _immDataBuffer
virtual std::optional< TargetInfoBounceBufferInfo > bounceBufferInfo() const override
RMAGrainIngressProtocol(std::vector< Region > regions)
virtual bool canReadSamples() const noexcept override
This protocol cannot read samples, since it is designed for remote writes of grain buffers.
virtual std::vector< RemoteRegion > registerMemory(std::shared_ptr< Domain > domain) override
Register local memory regions used in this protocol.
virtual void start(Endpoint const &endpoint) override
Start receiving.
virtual std::optional< Target::ReadResult > read(Endpoint const &endpoint, Completion const &completion) override
virtual bool canReadGrains() const noexcept override
This protocol cannot read grains, since it is designed for remote writes of audio samples.
virtual std::optional< TargetInfoBounceBufferInfo > bounceBufferInfo() const override
AudioBounceBuffer makeAudioBounceBuffer(DataLayout::Continuous const &layout, std::uint32_t maxSyncBatchSize)
Helper function to create an AudioBounceBuffer based on the given audio data layout and maximum synch...
std::optional< Target::ImmediateDataLocation > _immDataBuffer
virtual bool canReadSamples() const noexcept override
This protocol can read samples, since it is designed for remote writes of audio samples.
RMASampleIngressProtocol(Region region, DataLayout::Continuous const &dataLayout, std::uint32_t maxSyncBatchSize)
virtual std::vector< RemoteRegion > registerMemory(std::shared_ptr< Domain > domain) override
Register local memory regions used in this protocol.
virtual void start(Endpoint const &endpoint) override
Start receiving.
Represent a memory region (unregistered).
Definition Region.hpp:24
std::uint64_t getGrainIndexInRingSlot(std::vector< Region > const &regions, std::uint16_t slot)
Definition Region.cpp:208
void setValidSlicesForGrain(std::vector< Region > const &regions, std::uint16_t slot, std::uint16_t validSlices)
Definition Region.cpp:218
Continuous layout variant of DataLayout.
Represent a source memory region used for for data transfer.
Information about the bounce buffer on the target, if the target uses one.