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

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/ascend/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
17/*
18 * Copyright Huawei Technologies Switzerland AG
19 * All rights reserved.
20 */
21
29#pragma once
30
31#include <acl/acl.h>
37
38namespace HiCR::backend::ascend
39{
40
49{
50 public:
51
56 {
61
66
70 device
71 };
72
79
92 __INLINE__ void memcpyAsync(const std::shared_ptr<HiCR::LocalMemorySlot> &destination,
93 const size_t dst_offset,
94 const std::shared_ptr<HiCR::LocalMemorySlot> &source,
95 const size_t src_offset,
96 const size_t size,
97 const aclrtStream stream)
98 {
99 memcpyInternal(destination, dst_offset, source, src_offset, size, stream);
100 }
101
102 private:
103
104 __INLINE__ void exchangeGlobalMemorySlotsImpl(const HiCR::GlobalMemorySlot::tag_t tag, const std::vector<globalKeyMemorySlotPair_t> &memorySlots) override
105 {
106 HICR_THROW_RUNTIME("Not yet implemented for this backend");
107 }
108
109 __INLINE__ void queryMemorySlotUpdatesImpl(std::shared_ptr<HiCR::LocalMemorySlot> memorySlot) override { HICR_THROW_RUNTIME("Not yet implemented for this backend"); }
110
111 __INLINE__ std::shared_ptr<HiCR::GlobalMemorySlot> getGlobalMemorySlotImpl(const HiCR::GlobalMemorySlot::tag_t tag, const HiCR::GlobalMemorySlot::globalKey_t globalKey) override
112 {
113 return nullptr;
114 }
115
122 __INLINE__ void destroyGlobalMemorySlotImpl(const std::shared_ptr<HiCR::GlobalMemorySlot> memorySlot) override { HICR_THROW_RUNTIME("Not yet implemented for this backend"); }
123
133 __INLINE__ void memcpyImpl(const std::shared_ptr<HiCR::LocalMemorySlot> &destination,
134 const size_t dst_offset,
135 const std::shared_ptr<HiCR::LocalMemorySlot> &source,
136 const size_t src_offset,
137 const size_t size) override
138 {
139 memcpyInternal(destination, dst_offset, source, src_offset, size, NULL);
140 }
141
156 __INLINE__ void memcpyInternal(const std::shared_ptr<HiCR::LocalMemorySlot> &destination,
157 const size_t dst_offset,
158 const std::shared_ptr<HiCR::LocalMemorySlot> &source,
159 const size_t src_offset,
160 const size_t size,
161 const aclrtStream stream)
162 {
163 // Storage for device type
166
167 // Using up-casting to determine device types
168 auto sd = dynamic_pointer_cast<ascend::LocalMemorySlot>(source);
169 auto dd = dynamic_pointer_cast<ascend::LocalMemorySlot>(destination);
170 auto sh = dynamic_pointer_cast<HiCR::LocalMemorySlot>(source);
171 auto dh = dynamic_pointer_cast<HiCR::LocalMemorySlot>(destination);
172
173 if (sh != NULL) srcType = deviceType_t::host;
174 if (dh != NULL) dstType = deviceType_t::host;
175 if (sd != NULL) srcType = deviceType_t::device;
176 if (dd != NULL) dstType = deviceType_t::device;
177
178 // Checking whether the memory slot unit passed is compatible with this backend
179 if (srcType == deviceType_t::none) HICR_THROW_LOGIC("The passed source memory slot is not supported by this backend\n");
180 if (dstType == deviceType_t::none) HICR_THROW_LOGIC("The passed destination memory slot is not supported by this backend\n");
181
182 // Storage for pointers and memcpy kind
183 aclrtMemcpyKind memcpyKind;
184 auto srcPtr = source->getPointer();
185 auto dstPtr = destination->getPointer();
186
187 // Determining which device context to use for copying
188 std::shared_ptr<ascend::LocalMemorySlot> deviceMemSlot = NULL;
189 if (srcType == deviceType_t::host && dstType == deviceType_t::host)
190 {
191 deviceMemSlot = NULL;
192 memcpyKind = ACL_MEMCPY_HOST_TO_HOST;
193 }
194 if (srcType == deviceType_t::host && dstType == deviceType_t::device)
195 {
196 deviceMemSlot = dd;
197 memcpyKind = ACL_MEMCPY_HOST_TO_DEVICE;
198 }
199 if (srcType == deviceType_t::device && dstType == deviceType_t::host)
200 {
201 deviceMemSlot = sd;
202 memcpyKind = ACL_MEMCPY_DEVICE_TO_HOST;
203 }
204 if (srcType == deviceType_t::device && dstType == deviceType_t::device)
205 {
206 deviceMemSlot = dd;
207 memcpyKind = ACL_MEMCPY_DEVICE_TO_DEVICE;
208 }
209
210 // Calculating actual offsets
211 const auto actualSrcPtr = (uint8_t *)srcPtr + src_offset;
212 const auto actualDstPtr = (uint8_t *)dstPtr + dst_offset;
213
214 // If a device is involved in this operation, select it and use its stream to perform the operation
215 if (deviceMemSlot != NULL) dynamic_pointer_cast<ascend::MemorySpace>(deviceMemSlot->getMemorySpace())->getDevice().lock()->select();
216
217 // Now executing memcpy depending on whether a stream was specified
218 aclError err;
219 if (stream != NULL) err = aclrtMemcpyAsync(actualDstPtr, size, actualSrcPtr, size, memcpyKind, stream);
220 if (stream == NULL) err = aclrtMemcpy(actualDstPtr, size, actualSrcPtr, size, memcpyKind);
221
222 // Check for errors
223 if (err != ACL_SUCCESS) HICR_THROW_RUNTIME("Could not perform memcpy of type %d. Error %d", memcpyKind, err);
224
225 // Increasing message received/sent counters for both memory slots
226 increaseMessageRecvCounter(*destination);
228 }
229
230 __INLINE__ void fenceImpl(const HiCR::GlobalMemorySlot::tag_t tag) override
231 {
232 // Not yet implemented
233 }
234
235 __INLINE__ bool acquireGlobalLockImpl(std::shared_ptr<HiCR::GlobalMemorySlot> memorySlot) override { HICR_THROW_RUNTIME("Not yet implemented for this backend"); }
236
237 __INLINE__ void releaseGlobalLockImpl(std::shared_ptr<HiCR::GlobalMemorySlot> memorySlot) override { HICR_THROW_RUNTIME("Not yet implemented for this backend"); }
238};
239
240} // namespace HiCR::backend::ascend
This file implements the Device class for the Ascend backend.
Provides a definition for the local memory slot class for the Ascend backend.
This file implements the memory space class for the Ascend backend.
Definition communicationManager.hpp:54
__INLINE__ void increaseMessageRecvCounter(HiCR::LocalMemorySlot &memorySlot) noexcept
Definition communicationManager.hpp:652
__INLINE__ void increaseMessageSentCounter(HiCR::LocalMemorySlot &memorySlot) noexcept
Definition communicationManager.hpp:659
uint64_t tag_t
Definition globalMemorySlot.hpp:49
uint64_t globalKey_t
Definition globalMemorySlot.hpp:44
Definition communicationManager.hpp:49
CommunicationManager()
Definition communicationManager.hpp:76
deviceType_t
Definition communicationManager.hpp:56
@ host
Definition communicationManager.hpp:65
@ device
Definition communicationManager.hpp:70
@ none
Definition communicationManager.hpp:60
__INLINE__ void memcpyAsync(const std::shared_ptr< HiCR::LocalMemorySlot > &destination, const size_t dst_offset, const std::shared_ptr< HiCR::LocalMemorySlot > &source, const size_t src_offset, const size_t size, const aclrtStream stream)
Definition communicationManager.hpp:92
Provides a definition for the base backend's communication manager class.
Provides a definition for a HiCR Global Memory Slot class.
#define HICR_THROW_RUNTIME(...)
Definition exceptions.hpp:74
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67