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

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/opencl/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 <CL/opencl.hpp>
33
34namespace HiCR::backend::opencl
35{
36
41{
42 public:
43
49 CommunicationManager(const std::unordered_map<opencl::Device::deviceIdentifier_t, std::shared_ptr<cl::CommandQueue>> &deviceQueueMap)
50 : HiCR::CommunicationManager(),
51 _deviceQueueMap(deviceQueueMap)
52 {}
53
55
68 __INLINE__ void memcpyAsync(const std::shared_ptr<HiCR::LocalMemorySlot> &destination,
69 const size_t dst_offset,
70 const std::shared_ptr<HiCR::LocalMemorySlot> &source,
71 const size_t src_offset,
72 const size_t size,
73 const cl::CommandQueue *queue)
74 {
75 memcpyInternal(destination, dst_offset, source, src_offset, size, queue);
76 }
77
78 private:
79
80 __INLINE__ void exchangeGlobalMemorySlotsImpl(const HiCR::GlobalMemorySlot::tag_t tag, const std::vector<globalKeyMemorySlotPair_t> &memorySlots) override
81 {
82 HICR_THROW_RUNTIME("Not yet implemented for this backend");
83 }
84
85 __INLINE__ void queryMemorySlotUpdatesImpl(std::shared_ptr<HiCR::LocalMemorySlot> memorySlot) override { HICR_THROW_RUNTIME("Not yet implemented for this backend"); }
86
87 __INLINE__ std::shared_ptr<HiCR::GlobalMemorySlot> getGlobalMemorySlotImpl(const HiCR::GlobalMemorySlot::tag_t tag, const HiCR::GlobalMemorySlot::globalKey_t globalKey) override
88 {
89 return nullptr;
90 }
91
98 __INLINE__ void destroyGlobalMemorySlotImpl(const std::shared_ptr<HiCR::GlobalMemorySlot> memorySlot) override { HICR_THROW_RUNTIME("Not yet implemented for this backend"); }
99
110 __INLINE__ void memcpyImpl(const std::shared_ptr<HiCR::LocalMemorySlot> &destination,
111 const size_t dst_offset,
112 const std::shared_ptr<HiCR::LocalMemorySlot> &source,
113 const size_t src_offset,
114 const size_t size) override
115 {
116 memcpyInternal(destination, dst_offset, source, src_offset, size, nullptr);
117 }
118
119 __INLINE__ void memcpyInternal(const std::shared_ptr<HiCR::LocalMemorySlot> &destination,
120 const size_t dst_offset,
121 const std::shared_ptr<HiCR::LocalMemorySlot> &source,
122 const size_t src_offset,
123 const size_t size,
124 const cl::CommandQueue *queue)
125 {
126 cl_int err = CL_SUCCESS;
127 // Using up-casting to determine device types
128 auto src = dynamic_pointer_cast<opencl::LocalMemorySlot>(source);
129 auto dst = dynamic_pointer_cast<opencl::LocalMemorySlot>(destination);
130
131 // Checking whether the memory slot unit passed is compatible with this backend
132 if (src == nullptr) [[unlikely]] { HICR_THROW_LOGIC("The passed source memory slot is not supported by this backend\n"); }
133 if (dst == nullptr) [[unlikely]] { HICR_THROW_LOGIC("The passed destination memory slot is not supported by this backend\n"); }
134
135 // Async version
136 if (queue != nullptr)
137 {
138 // Unmap src buffer
139 unmap(queue, src);
140
141 // Unmap dst buffer
142 unmap(queue, dst);
143
144 err = queue->enqueueCopyBuffer(*(src->getBuffer()), *(dst->getBuffer()), src_offset, dst_offset, size, nullptr, nullptr);
145 if (err != CL_SUCCESS) [[unlikely]] { HICR_THROW_RUNTIME("Can not perform memcpy. Err: %d", err); }
146 }
147
148 // Sync version
149 if (queue == nullptr)
150 {
151 queue = getQueue(dst->getMemorySpace()).get();
152
153 // Unmap src buffer
154 unmap(queue, src);
155
156 // Unmap dst buffer
157 unmap(queue, dst);
158
159 cl::Event completionEvent;
160 err = queue->enqueueCopyBuffer(*(src->getBuffer()), *(dst->getBuffer()), src_offset, dst_offset, size, nullptr, &completionEvent);
161 if (err != CL_SUCCESS) [[unlikely]] { HICR_THROW_RUNTIME("Can not perform memcpy. Err: %d", err); }
162 completionEvent.wait();
163 }
164
165 // Map the src buffer
166 map(queue, src);
167
168 // Map the dst buffer
169 map(queue, dst);
170
171 // Increasing message received/sent counters for both memory slots
172 increaseMessageRecvCounter(*destination);
174 }
175
176 __INLINE__ void fenceImpl(const HiCR::GlobalMemorySlot::tag_t tag) override {}
177
178 __INLINE__ bool acquireGlobalLockImpl(std::shared_ptr<HiCR::GlobalMemorySlot> memorySlot) override { HICR_THROW_RUNTIME("Not yet implemented for this backend"); }
179
180 __INLINE__ void releaseGlobalLockImpl(std::shared_ptr<HiCR::GlobalMemorySlot> memorySlot) override { HICR_THROW_RUNTIME("Not yet implemented for this backend"); }
181
182 private:
183
187 const std::unordered_map<opencl::Device::deviceIdentifier_t, std::shared_ptr<cl::CommandQueue>> _deviceQueueMap;
188
196 std::shared_ptr<cl::CommandQueue> getQueue(std::shared_ptr<HiCR::MemorySpace> memorySpace)
197 {
198 auto openclMemorySpace = dynamic_pointer_cast<opencl::MemorySpace>(memorySpace);
199 auto hwlocMemorySpace = dynamic_pointer_cast<hwloc::MemorySpace>(memorySpace);
200 if (hwlocMemorySpace != nullptr) { return _deviceQueueMap.begin()->second; }
201 if (openclMemorySpace != nullptr)
202 {
203 auto device = openclMemorySpace->getDevice().lock();
204 auto deviceId = device->getId();
205 return _deviceQueueMap.at(deviceId);
206 }
207 HICR_THROW_LOGIC("The passed memory space is not supported by this memory manager. Supported opencl and hwloc\n");
208 }
209
216 __INLINE__ void map(const cl::CommandQueue *queue, std::shared_ptr<HiCR::backend::opencl::LocalMemorySlot> &memSlot)
217 {
218 cl_int err;
219 memSlot->getPointer() = queue->enqueueMapBuffer(*(memSlot->getBuffer()), CL_TRUE, CL_MAP_READ | CL_MAP_WRITE, 0, memSlot->getSize(), nullptr, nullptr, &err);
220 if (err != CL_SUCCESS) [[unlikely]] { HICR_THROW_RUNTIME("Can not map the buffer. Error: %d", err); }
221 }
222
229 __INLINE__ void unmap(const cl::CommandQueue *queue, std::shared_ptr<HiCR::backend::opencl::LocalMemorySlot> &memSlot)
230 {
231 auto err = queue->enqueueUnmapMemObject(*(memSlot->getBuffer()), memSlot->getPointer(), nullptr, nullptr);
232 if (err != CL_SUCCESS) [[unlikely]] { HICR_THROW_RUNTIME("Can not unmap the buffer. Error: %d", err); }
233 }
234};
235
236} // namespace HiCR::backend::opencl
This file implements the Device class for the OpenCL backend.
Provides a definition for the local memory slot class for the OpenCL backend.
This file implements the memory manager class for the OpenCL backend.
This file implements the memory space class for the OpenCL 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:41
__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 cl::CommandQueue *queue)
Definition communicationManager.hpp:68
CommunicationManager(const std::unordered_map< opencl::Device::deviceIdentifier_t, std::shared_ptr< cl::CommandQueue > > &deviceQueueMap)
Definition communicationManager.hpp:49
uint64_t deviceIdentifier_t
Definition device.hpp:43
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