/home/runner/work/HiCR/HiCR/include/hicr/frontends/channel/fixedSize/spsc/consumer.hpp Source File

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/frontends/channel/fixedSize/spsc/consumer.hpp Source File
HiCR
consumer.hpp
Go to the documentation of this file.
1/*
2 * Copyright 2025 Huawei Technologies Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
24#pragma once
25
26#include <hicr/core/definitions.hpp>
29#include <utility>
30
31namespace HiCR::channel::fixedSize::SPSC
32{
33
41{
42 private:
43
48 const std::shared_ptr<HiCR::GlobalMemorySlot> _tokenBuffer;
49
54 const std::shared_ptr<HiCR::GlobalMemorySlot> _producerCoordinationBuffer;
55
56 public:
57
73 Consumer(CommunicationManager &coordinationCommunicationManager,
74 CommunicationManager &payloadCommunicationManager,
75 const std::shared_ptr<GlobalMemorySlot> &tokenBuffer,
76 const std::shared_ptr<LocalMemorySlot> &internalCoordinationBuffer,
77 std::shared_ptr<GlobalMemorySlot> producerCoordinationBuffer,
78 const size_t tokenSize,
79 const size_t capacity)
80 : channel::fixedSize::Base(coordinationCommunicationManager, payloadCommunicationManager, internalCoordinationBuffer, tokenSize, capacity),
81 _tokenBuffer(tokenBuffer),
82 _producerCoordinationBuffer(std::move(producerCoordinationBuffer))
83
84 {
85 // Checking whether the memory slot is local. This backend only supports local data transfers
86 if (tokenBuffer->getSourceLocalMemorySlot() == nullptr)
87 HICR_THROW_LOGIC("The passed token buffer slot was not created locally (it must be to be used internally by the channel implementation)\n");
88
89 // Checking that the provided token exchange buffer has the right size
90 auto requiredTokenBufferSize = getTokenBufferSize(getTokenSize(), capacity);
91 auto providedTokenBufferSize = tokenBuffer->getSourceLocalMemorySlot()->getSize();
92 if (providedTokenBufferSize < requiredTokenBufferSize)
94 "Attempting to create a channel with a token data buffer size (%lu) smaller than the required size (%lu).\n", providedTokenBufferSize, requiredTokenBufferSize);
95 }
96 ~Consumer() = default;
97
121 __INLINE__ size_t peek(const size_t pos = 0)
122 {
123 // Check if the requested position exceeds the capacity of the channel
124 if (pos >= getCircularBuffer()->getCapacity())
125 HICR_THROW_LOGIC("Attempting to peek for a token with position (%lu), which is beyond than the channel capacity (%lu)", pos, getCircularBuffer()->getCapacity());
126
127 // Make sure receiver queues are occasionally processed
130
131 // Updating channel depth
132 updateDepth();
133
134 // Check if there are enough tokens in the buffer to satisfy the request
135 if (pos >= getCircularBuffer()->getDepth())
136 HICR_THROW_RUNTIME("Attempting to peek position (%lu) but not enough tokens (%lu) are in the buffer", pos, getCircularBuffer()->getDepth());
137
138 // Calculating buffer position
139 const size_t bufferPos = (getCircularBuffer()->getTailPosition() + pos) % getCircularBuffer()->getCapacity();
140
141 // Succeeded in pushing the token(s)
142 return bufferPos;
143 }
144
158 __INLINE__ void pop(const size_t n = 1)
159 {
160 if (n > getCircularBuffer()->getCapacity())
161 HICR_THROW_LOGIC("Attempting to pop (%lu) tokens, which is larger than the channel capacity (%lu)", n, getCircularBuffer()->getCapacity());
162
163 // Updating channel depth
164 updateDepth();
165
166 // If the exchange buffer does not have n tokens pushed, reject operation
167 if (n > getCircularBuffer()->getDepth())
168 HICR_THROW_RUNTIME("Attempting to pop (%lu) tokens, which is more than the number of current tokens in the channel (%lu)", n, getCircularBuffer()->getDepth());
169
170 // Advancing tail (removes elements from the circular buffer)
171 getCircularBuffer()->advanceTail(n);
172
173 const auto coordBuffElemSize = sizeof(_HICR_CHANNEL_COORDINATION_BUFFER_ELEMENT_TYPE);
174 auto coordinationCommunicationManager = getCoordinationCommunicationManager();
175
176 // Notifying producer(s) of buffer liberation
177 coordinationCommunicationManager->memcpy(_producerCoordinationBuffer,
178 _HICR_CHANNEL_TAIL_ADVANCE_COUNT_IDX * coordBuffElemSize,
180 _HICR_CHANNEL_TAIL_ADVANCE_COUNT_IDX * coordBuffElemSize,
181 coordBuffElemSize);
182 coordinationCommunicationManager->fence(getCoordinationBuffer(), 1, 0);
183 }
184
190 __INLINE__ void updateDepth() {}
191
198 [[nodiscard]] __INLINE__ std::shared_ptr<GlobalMemorySlot> getTokenBuffer() const { return _tokenBuffer; }
199};
200
201} // namespace HiCR::channel::fixedSize::SPSC
Definition communicationManager.hpp:54
virtual __INLINE__ void flushReceived()
Definition communicationManager.hpp:469
__INLINE__ auto getCoordinationBuffer() const
Definition base.hpp:235
__INLINE__ size_t getDepth() const noexcept
Definition base.hpp:141
__INLINE__ size_t getTokenSize() const noexcept
Definition base.hpp:84
static __INLINE__ size_t getTokenBufferSize(const size_t tokenSize, const size_t capacity) noexcept
Definition base.hpp:123
__INLINE__ CommunicationManager * getPayloadCommunicationManager() const
Definition base.hpp:223
__INLINE__ CommunicationManager * getCoordinationCommunicationManager() const
Definition base.hpp:229
__INLINE__ auto getCircularBuffer() const noexcept
Definition base.hpp:167
Definition base.hpp:42
Consumer(CommunicationManager &coordinationCommunicationManager, CommunicationManager &payloadCommunicationManager, const std::shared_ptr< GlobalMemorySlot > &tokenBuffer, const std::shared_ptr< LocalMemorySlot > &internalCoordinationBuffer, std::shared_ptr< GlobalMemorySlot > producerCoordinationBuffer, const size_t tokenSize, const size_t capacity)
Definition consumer.hpp:73
__INLINE__ size_t peek(const size_t pos=0)
Definition consumer.hpp:121
__INLINE__ void pop(const size_t n=1)
Definition consumer.hpp:158
__INLINE__ void updateDepth()
Definition consumer.hpp:190
__INLINE__ std::shared_ptr< GlobalMemorySlot > getTokenBuffer() const
Definition consumer.hpp:198
Provides a failure model and corresponding exception classes.
#define HICR_THROW_RUNTIME(...)
Definition exceptions.hpp:74
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67
Provides base functionality for a fixed-size MPSC channel over HiCR.