/home/runner/work/HiCR/HiCR/include/hicr/frontends/channel/variableSize/base.hpp Source File

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/frontends/channel/variableSize/base.hpp Source File
HiCR
base.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 <memory>
27#include <cstring>
28#include <hicr/core/definitions.hpp>
32#include <hicr/frontends/channel/base.hpp>
33
34namespace HiCR::channel::variableSize
35{
36
40class Base : public channel::Base
41{
42 protected:
43
59 Base(CommunicationManager &coordinationCommunicationManager,
60 CommunicationManager &payloadCommunicationManager,
61 const std::shared_ptr<LocalMemorySlot> &coordinationBufferForCounts,
62 const std::shared_ptr<LocalMemorySlot> &coordinationBufferForPayloads,
63 const size_t capacity,
64 const size_t payloadCapacity)
65 : channel::Base(coordinationCommunicationManager, payloadCommunicationManager, coordinationBufferForCounts, sizeof(size_t), capacity),
66 _coordinationBufferForCounts(coordinationBufferForCounts),
67 _coordinationBufferForPayloads(coordinationBufferForPayloads)
68 {
69 if (capacity == 0) HICR_THROW_LOGIC("Attempting to create a channel with zero capacity \n");
70
71 // Checking that the provided coordination buffers have the right size
72 auto requiredCoordinationBufferSize = 4 * sizeof(_HICR_CHANNEL_COORDINATION_BUFFER_ELEMENT_TYPE);
73 auto providedCoordinationBufferSize = coordinationBufferForPayloads->getSize() + coordinationBufferForCounts->getSize();
74 if (providedCoordinationBufferSize < requiredCoordinationBufferSize)
75 HICR_THROW_LOGIC("Attempting to create a channel with a local coordination buffer size (%lu) smaller than the required size (%lu).\n",
76 providedCoordinationBufferSize,
77 requiredCoordinationBufferSize);
78
79 // Creating internal circular buffer
80 _circularBufferForCounts = std::make_unique<channel::CircularBuffer>(
81 capacity,
82 &static_cast<_HICR_CHANNEL_COORDINATION_BUFFER_ELEMENT_TYPE *>(coordinationBufferForCounts->getPointer())[_HICR_CHANNEL_HEAD_ADVANCE_COUNT_IDX],
83 &static_cast<_HICR_CHANNEL_COORDINATION_BUFFER_ELEMENT_TYPE *>(coordinationBufferForCounts->getPointer())[_HICR_CHANNEL_TAIL_ADVANCE_COUNT_IDX]);
84
85 _circularBufferForPayloads = std::make_unique<channel::CircularBuffer>(
86 payloadCapacity,
87 &static_cast<_HICR_CHANNEL_COORDINATION_BUFFER_ELEMENT_TYPE *>(coordinationBufferForPayloads->getPointer())[_HICR_CHANNEL_HEAD_ADVANCE_COUNT_IDX],
88 &static_cast<_HICR_CHANNEL_COORDINATION_BUFFER_ELEMENT_TYPE *>(coordinationBufferForPayloads->getPointer())[_HICR_CHANNEL_TAIL_ADVANCE_COUNT_IDX]);
89 }
90
95 [[nodiscard]] __INLINE__ auto getCircularBufferForCounts() const { return _circularBufferForCounts.get(); }
96
101 [[nodiscard]] __INLINE__ auto getCircularBufferForPayloads() const { return _circularBufferForPayloads.get(); }
102
107 [[nodiscard]] __INLINE__ auto getCoordinationBufferForCounts() const { return _coordinationBufferForCounts; }
108
113 [[nodiscard]] __INLINE__ auto getCoordinationBufferForPayloads() const { return _coordinationBufferForPayloads; }
114
115 private:
116
120 std::unique_ptr<channel::CircularBuffer> _circularBufferForCounts;
124 std::unique_ptr<channel::CircularBuffer> _circularBufferForPayloads;
128 const std::shared_ptr<LocalMemorySlot> _coordinationBufferForCounts;
132 const std::shared_ptr<LocalMemorySlot> _coordinationBufferForPayloads;
133};
134
135} // namespace HiCR::channel::variableSize
Definition communicationManager.hpp:54
Definition base.hpp:70
Definition base.hpp:41
__INLINE__ auto getCircularBufferForPayloads() const
Definition base.hpp:101
Base(CommunicationManager &coordinationCommunicationManager, CommunicationManager &payloadCommunicationManager, const std::shared_ptr< LocalMemorySlot > &coordinationBufferForCounts, const std::shared_ptr< LocalMemorySlot > &coordinationBufferForPayloads, const size_t capacity, const size_t payloadCapacity)
Definition base.hpp:59
__INLINE__ auto getCoordinationBufferForPayloads() const
Definition base.hpp:113
__INLINE__ auto getCoordinationBufferForCounts() const
Definition base.hpp:107
__INLINE__ auto getCircularBufferForCounts() const
Definition base.hpp:95
Provides a definition for the base backend's communication manager class.
Provides a definition for a HiCR Global Memory Slot class.
Provides a failure model and corresponding exception classes.
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67