/home/runner/work/HiCR/HiCR/include/hicr/backends/pthreads/communicationManager.hpp Source File

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/pthreads/communicationManager.hpp Source File
HiCR
communicationManager.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 <cstring>
27#include <pthread.h>
28
32
33#include "core.hpp"
34
35namespace HiCR::backend::pthreads
36{
37
44{
45 public:
46
53 : HiCR::CommunicationManager(),
54 _core(core)
55 {}
56
61
62 __INLINE__ std::shared_ptr<HiCR::GlobalMemorySlot> getGlobalMemorySlotImpl(const HiCR::backend::hwloc::GlobalMemorySlot::tag_t tag,
64 {
65 return _core.getGlobalSlot(tag, globalKey);
66 }
67
76 __INLINE__ std::shared_ptr<HiCR::GlobalMemorySlot> promoteLocalMemorySlot(const std::shared_ptr<HiCR::LocalMemorySlot> &memorySlot, HiCR::GlobalMemorySlot::tag_t tag) override
77 {
78 // Creating new (generic) global memory slot
79 auto globalMemorySlot = std::make_shared<HiCR::backend::hwloc::GlobalMemorySlot>(tag, 0 /* key */, memorySlot);
80
81 // Returning the global memory slot
82 return globalMemorySlot;
83 }
84
90 __INLINE__ void destroyPromotedGlobalMemorySlot(const std::shared_ptr<HiCR::GlobalMemorySlot> &memorySlot) override
91 {
92 // Nothing to do here
93 }
94
95 private:
96
97 __INLINE__ void exchangeGlobalMemorySlotsImpl(const HiCR::GlobalMemorySlot::tag_t tag, const std::vector<globalKeyMemorySlotPair_t> &memorySlots) override
98 {
99 // Simply adding local memory slots to the global map
100 for (const auto &entry : memorySlots)
101 {
102 // Getting global key
103 auto globalKey = entry.first;
104
105 // Getting local memory slot to promot
106 auto memorySlot = entry.second;
107
108 // Creating new memory slot
109 auto globalMemorySlot = std::make_shared<HiCR::backend::hwloc::GlobalMemorySlot>(tag, globalKey, memorySlot);
110
111 // Push it to shared memory
112 _core.insertGlobalSlot(tag, globalKey, globalMemorySlot);
113 }
114 }
115
116 __INLINE__ void queryMemorySlotUpdatesImpl(std::shared_ptr<HiCR::LocalMemorySlot> memorySlot) override
117 {
118 // This function should check and update the abstract class for completed memcpy operations
119 }
120
127 __INLINE__ void fenceImpl(const HiCR::GlobalMemorySlot::tag_t tag) override
128 {
129 // Wait all threads to reach this point
130 _core.fence();
131
132 // Registering memory slot
133 for (const auto &[key, slot] : _core.getKeyMemorySlots(tag)) { registerGlobalMemorySlot(slot); }
134 }
135
136 __INLINE__ void memcpyImpl(const std::shared_ptr<HiCR::LocalMemorySlot> &destination,
137 const size_t dst_offset,
138 const std::shared_ptr<HiCR::LocalMemorySlot> &source,
139 const size_t src_offset,
140 const size_t size) override
141 {
142 // Getting slot pointers
143 const auto srcPtr = source->getPointer();
144 const auto dstPtr = destination->getPointer();
145
146 // Calculating actual offsets
147 const auto actualSrcPtr = (void *)(static_cast<uint8_t *>(srcPtr) + src_offset);
148 const auto actualDstPtr = (void *)(static_cast<uint8_t *>(dstPtr) + dst_offset);
149
150 // Running memcpy now
151 std::memcpy(actualDstPtr, actualSrcPtr, size);
152
153 // Increasing recv/send counters
154 increaseMessageRecvCounter(*destination);
156 }
157
164 __INLINE__ void destroyGlobalMemorySlotImpl(std::shared_ptr<HiCR::GlobalMemorySlot> memorySlot) override
165 {
166 _core.removeGlobalSlot(memorySlot->getGlobalTag(), memorySlot->getGlobalKey());
167 }
168
169 __INLINE__ void memcpyImpl(const std::shared_ptr<HiCR::GlobalMemorySlot> &destination,
170 const size_t dst_offset,
171 const std::shared_ptr<HiCR::LocalMemorySlot> &source,
172 const size_t src_offset,
173 const size_t size) override
174 {
175 // Checking whether the memory slot is local. This backend only supports local data transfers
176 if (destination->getSourceLocalMemorySlot() == nullptr) HICR_THROW_LOGIC("The passed destination memory slot is not local (required by this backend)\n");
177
178 // Executing actual memcpy
179 memcpy(destination->getSourceLocalMemorySlot(), dst_offset, source, src_offset, size);
180
181 // Increasing message received/sent counters for both memory slots
182 increaseMessageRecvCounter(*destination->getSourceLocalMemorySlot());
184 }
185
186 __INLINE__ void memcpyImpl(const std::shared_ptr<HiCR::LocalMemorySlot> &destination,
187 const size_t dst_offset,
188 const std::shared_ptr<HiCR::GlobalMemorySlot> &source,
189 const size_t src_offset,
190 const size_t size) override
191 {
192 // Checking whether the memory slot is local. This backend only supports local data transfers
193 if (source->getSourceLocalMemorySlot() == nullptr) HICR_THROW_LOGIC("The passed source memory slot is not local (required by this backend)\n");
194
195 // Executing actual memcpy
196 memcpy(destination, dst_offset, source->getSourceLocalMemorySlot(), src_offset, size);
197
198 // Increasing message received/sent counters for both memory slots
199 increaseMessageRecvCounter(*destination);
200 increaseMessageSentCounter(*source->getSourceLocalMemorySlot());
201 }
202
203 __INLINE__ bool acquireGlobalLockImpl(std::shared_ptr<HiCR::GlobalMemorySlot> memorySlot) override
204 {
205 // Getting up-casted pointer for the execution unit
206 auto m = std::dynamic_pointer_cast<hwloc::GlobalMemorySlot>(memorySlot);
207
208 // Checking whether the execution unit passed is compatible with this backend
209 if (m == nullptr) HICR_THROW_LOGIC("The passed memory slot is not supported by this backend\n");
210
211 // Locking mutex
212 return m->trylock();
213 }
214
215 __INLINE__ void releaseGlobalLockImpl(std::shared_ptr<HiCR::GlobalMemorySlot> memorySlot) override
216 {
217 // Getting up-casted pointer for the execution unit
218 auto m = std::dynamic_pointer_cast<hwloc::GlobalMemorySlot>(memorySlot);
219
220 // Checking whether the execution unit passed is compatible with this backend
221 if (m == nullptr) HICR_THROW_LOGIC("The passed memory slot is not supported by this backend\n");
222
223 // Locking mutex
224 m->unlock();
225 }
226
227 private:
228
232 Core &_core;
233};
234
235} // namespace HiCR::backend::pthreads
Provides a definition for the global memory slot class for the HWLoc backend.
Definition communicationManager.hpp:54
__INLINE__ void memcpy(const std::shared_ptr< LocalMemorySlot > &destination, size_t dst_offset, const std::shared_ptr< LocalMemorySlot > &source, size_t src_offset, size_t size)
Definition communicationManager.hpp:248
__INLINE__ void registerGlobalMemorySlot(const std::shared_ptr< GlobalMemorySlot > &memorySlot)
Definition communicationManager.hpp:495
__INLINE__ void increaseMessageRecvCounter(HiCR::LocalMemorySlot &memorySlot) noexcept
Definition communicationManager.hpp:636
__INLINE__ void increaseMessageSentCounter(HiCR::LocalMemorySlot &memorySlot) noexcept
Definition communicationManager.hpp:643
uint64_t tag_t
Definition globalMemorySlot.hpp:49
uint64_t globalKey_t
Definition globalMemorySlot.hpp:44
Definition communicationManager.hpp:44
__INLINE__ std::shared_ptr< HiCR::GlobalMemorySlot > promoteLocalMemorySlot(const std::shared_ptr< HiCR::LocalMemorySlot > &memorySlot, HiCR::GlobalMemorySlot::tag_t tag) override
Definition communicationManager.hpp:76
__INLINE__ std::shared_ptr< HiCR::GlobalMemorySlot > getGlobalMemorySlotImpl(const HiCR::backend::hwloc::GlobalMemorySlot::tag_t tag, const HiCR::backend::hwloc::GlobalMemorySlot::globalKey_t globalKey) override
Definition communicationManager.hpp:62
__INLINE__ void destroyPromotedGlobalMemorySlot(const std::shared_ptr< HiCR::GlobalMemorySlot > &memorySlot) override
Definition communicationManager.hpp:90
CommunicationManager(Core &core)
Definition communicationManager.hpp:52
Definition core.hpp:42
__INLINE__ std::shared_ptr< GlobalMemorySlot > getGlobalSlot(const GlobalMemorySlot::tag_t tag, const GlobalMemorySlot::globalKey_t key) const
Definition core.hpp:103
__INLINE__ void fence()
Definition core.hpp:184
__INLINE__ void insertGlobalSlot(const GlobalMemorySlot::tag_t tag, const GlobalMemorySlot::globalKey_t key, const std::shared_ptr< GlobalMemorySlot > &slot)
Definition core.hpp:81
__INLINE__ void removeGlobalSlot(const GlobalMemorySlot::tag_t tag, const GlobalMemorySlot::globalKey_t key)
Definition core.hpp:131
Provides a definition for the base backend's communication manager class.
Provides a definition for a HiCR Local Memory Slot class.
This file implements the core mechanism to exchange slots and detect instances for the pthreads backe...
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67