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

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/opencl/memoryManager.hpp Source File
HiCR
memoryManager.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>
32
33namespace HiCR::backend::opencl
34{
43{
44 public:
45
51 MemoryManager(const std::unordered_map<opencl::Device::deviceIdentifier_t, std::shared_ptr<cl::CommandQueue>> &deviceQueueMap)
52 : HiCR::MemoryManager(),
53 _deviceQueueMap(deviceQueueMap)
54 {}
55
59 ~MemoryManager() = default;
60
61 private:
62
63 __INLINE__ std::shared_ptr<HiCR::LocalMemorySlot> allocateLocalMemorySlotImpl(std::shared_ptr<HiCR::MemorySpace> memorySpace, const size_t size) override
64 {
65 auto memSpaceType = memSpaceType_t::none;
66
67 // Getting up-casted pointer for the opencl instance, first try with the device memory space
68 auto openclMemorySpace = dynamic_pointer_cast<const opencl::MemorySpace>(memorySpace);
69 auto hwlocMemorySpace = dynamic_pointer_cast<hwloc::MemorySpace>(memorySpace);
70
71 // Checking whether the memory space passed belongs to the device or to the host
72 if (hwlocMemorySpace != nullptr) { memSpaceType = memSpaceType_t::host; }
73 if (openclMemorySpace != nullptr)
74 {
75 if (openclMemorySpace->getType() == "OpenCL Host RAM") { memSpaceType = memSpaceType_t::host; }
76 if (openclMemorySpace->getType() == "OpenCL GPU RAM") { memSpaceType = memSpaceType_t::device; }
77 }
78
79 if (memSpaceType == memSpaceType_t::device) { return allocateLocalDeviceMemorySlot(memorySpace, size); }
80 if (memSpaceType == memSpaceType_t::host) { return allocateLocalHostMemorySlot(memorySpace, size); }
81 HICR_THROW_LOGIC("The passed memory space is not supported by this memory manager. Supported opencl and hwloc\n");
82 }
83
84 __INLINE__ std::shared_ptr<HiCR::LocalMemorySlot> allocateLocalDeviceMemorySlot(const std::shared_ptr<HiCR::MemorySpace> memorySpace, const size_t size)
85 {
86 cl_int err;
87 auto queue = getQueue(memorySpace);
88 auto context = queue->getInfo<CL_QUEUE_CONTEXT>();
89
90 // Create OpenCL buffer on the device ready for read and write operations
91 auto deviceBuffer = std::make_shared<cl::Buffer>(context, CL_MEM_READ_WRITE, size, nullptr, &err);
92 if (err != CL_SUCCESS) [[unlikely]] { HICR_THROW_RUNTIME("Can not allocate local memory slot on the device: %d", err); }
93
94 // Map it to the host in order to get a pointer to the data. Data are mapped in memory for writing
95 auto hostPtr = queue->enqueueMapBuffer(*deviceBuffer, CL_TRUE, CL_MAP_READ | CL_MAP_READ | CL_MAP_WRITE, 0, size, nullptr, nullptr, &err);
96 if (err != CL_SUCCESS || hostPtr == nullptr) [[unlikely]] { HICR_THROW_RUNTIME("Can not retrieve pointer: %d", err); }
97
98 auto memSlot = std::make_shared<opencl::LocalMemorySlot>(hostPtr, size, deviceBuffer, memorySpace);
99
100 // create the new memory slot
101 return memSlot;
102 }
103
104 __INLINE__ std::shared_ptr<HiCR::LocalMemorySlot> allocateLocalHostMemorySlot(const std::shared_ptr<HiCR::MemorySpace> memorySpace, const size_t size)
105 {
106 cl_int err;
107 auto queue = getQueue(memorySpace);
108 auto context = queue->getInfo<CL_QUEUE_CONTEXT>();
109
110 // Create OpenCL buffer
111 auto hostBuffer = std::make_shared<cl::Buffer>(context, CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR, size, nullptr, &err);
112 if (err != CL_SUCCESS) [[unlikely]] { HICR_THROW_RUNTIME("Can not allocate local memory slot on the host: %d", err); }
113
114 // Map it to the host in order to get a pointer to the data. Data are mapped in memory for writing
115 auto hostPtr = queue->enqueueMapBuffer(*hostBuffer, CL_TRUE, CL_MAP_READ | CL_MAP_WRITE, 0, size, nullptr, nullptr, &err);
116 if (err != CL_SUCCESS || hostPtr == nullptr) [[unlikely]] { HICR_THROW_RUNTIME("Can not retrieve pointer: %d", err); }
117
118 auto memSlot = std::make_shared<opencl::LocalMemorySlot>(hostPtr, size, hostBuffer, memorySpace);
119
120 // create the new memory slot
121 return memSlot;
122 }
123
127 __INLINE__ std::shared_ptr<HiCR::LocalMemorySlot> registerLocalMemorySlotImpl(std::shared_ptr<HiCR::MemorySpace> memorySpace, void *const ptr, const size_t size) override
128 {
129 auto memSpaceType = memSpaceType_t::none;
130
131 // Getting up-casted pointer for the opencl instance, first try with the device memory space
132 auto openclMemorySpace = dynamic_pointer_cast<const opencl::MemorySpace>(memorySpace);
133 auto hwlocMemorySpace = dynamic_pointer_cast<hwloc::MemorySpace>(memorySpace);
134
135 // Checking whether the memory space passed belongs to the device or to the host
136 if (hwlocMemorySpace != nullptr) { memSpaceType = memSpaceType_t::host; }
137 if (openclMemorySpace != nullptr)
138 {
139 if (openclMemorySpace->getType() == "OpenCL Host RAM") { memSpaceType = memSpaceType_t::host; }
140 if (openclMemorySpace->getType() == "OpenCL GPU RAM") { memSpaceType = memSpaceType_t::device; }
141 }
142
143 // Checking whether the memory space passed belongs to the host
144 if (memSpaceType == memSpaceType_t::device) [[unlikely]]
145 {
146 HICR_THROW_RUNTIME("Can not register local memory slot on the provided memory space: %s", memorySpace->getType().c_str());
147 }
148
149 cl_int err;
150 auto queue = getQueue(memorySpace);
151 auto context = queue->getInfo<CL_QUEUE_CONTEXT>();
152
153 // Create OpenCL buffer
154 auto buffer = std::make_shared<cl::Buffer>(context, CL_MEM_USE_HOST_PTR, size, ptr, &err);
155 if (err != CL_SUCCESS) [[unlikely]] { HICR_THROW_RUNTIME("Can not register local memory slot on the host: %d", err); }
156
157 // Map it to the host in order to get a pointer to the data. Data are mapped in memory for writing
158 auto hostPtr = queue->enqueueMapBuffer(*buffer, CL_TRUE, CL_MAP_READ | CL_MAP_WRITE, 0, size, nullptr, nullptr, &err);
159 if (err != CL_SUCCESS || hostPtr == nullptr) [[unlikely]] { HICR_THROW_RUNTIME("Can not retrieve pointer: %d", err); }
160
161 // create the new memory slot
162 return std::make_shared<opencl::LocalMemorySlot>(hostPtr, size, buffer, memorySpace);
163 }
164
165 __INLINE__ void memsetImpl(const std::shared_ptr<HiCR::LocalMemorySlot> memorySlot, int value, size_t size) override
166 {
167 auto m = dynamic_pointer_cast<opencl::LocalMemorySlot>(memorySlot);
168 if (m == nullptr) [[unlikely]] { HICR_THROW_RUNTIME("Unsupported local memory slot: %s", memorySlot->getMemorySpace()->getType().c_str()); }
169
170 cl_int clValue = value;
171
172 auto completionEvent = cl::Event();
173 auto queue = getQueue(m->getMemorySpace());
174 auto err = queue->enqueueFillBuffer(*(m->getBuffer()), clValue, 0, m->getSize(), nullptr, &completionEvent);
175 if (err != CL_SUCCESS) [[unlikely]] { HICR_THROW_RUNTIME("Can not perform memset: %d", err); }
176 completionEvent.wait();
177 }
178
179 __INLINE__ void freeLocalMemorySlotImpl(std::shared_ptr<HiCR::LocalMemorySlot> memorySlot) override
180 {
181 auto m = dynamic_pointer_cast<opencl::LocalMemorySlot>(memorySlot);
182 if (m == nullptr) [[unlikely]] { HICR_THROW_RUNTIME("Unsupported local memory slot: %s", memorySlot->getMemorySpace()->getType().c_str()); }
183
184 auto queue = getQueue(m->getMemorySpace());
185 auto buffer = m->getBuffer();
186
187 auto err = queue->enqueueUnmapMemObject(*buffer, m->getPointer());
188 if (err != CL_SUCCESS) [[unlikely]] { HICR_THROW_RUNTIME("Can not unmap host pointer: %d", err); }
189
190 m->getBuffer().reset();
191 }
192
198 __INLINE__ void deregisterLocalMemorySlotImpl(std::shared_ptr<HiCR::LocalMemorySlot> memorySlot) override {}
199
200 private:
201
205 enum memSpaceType_t
206 {
210 none,
211
215 host,
216
220 device
221 };
222
226 const std::unordered_map<opencl::Device::deviceIdentifier_t, std::shared_ptr<cl::CommandQueue>> _deviceQueueMap;
227
235 std::shared_ptr<cl::CommandQueue> getQueue(std::shared_ptr<HiCR::MemorySpace> memorySpace)
236 {
237 auto openclMemorySpace = dynamic_pointer_cast<opencl::MemorySpace>(memorySpace);
238 auto hwlocMemorySpace = dynamic_pointer_cast<hwloc::MemorySpace>(memorySpace);
239 if (hwlocMemorySpace != nullptr) { return _deviceQueueMap.begin()->second; }
240 if (openclMemorySpace != nullptr)
241 {
242 auto device = openclMemorySpace->getDevice().lock();
243 auto deviceId = device->getId();
244 return _deviceQueueMap.at(deviceId);
245 }
246 HICR_THROW_LOGIC("The passed memory space is not supported by this memory manager. Supported opencl and hwloc\n");
247 }
248};
249
250} // namespace HiCR::backend::opencl
This file implements the memory space class for the HWLoc-based backend.
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 space class for the OpenCL backend.
Definition memoryManager.hpp:51
uint64_t deviceIdentifier_t
Definition device.hpp:43
Definition memoryManager.hpp:43
MemoryManager(const std::unordered_map< opencl::Device::deviceIdentifier_t, std::shared_ptr< cl::CommandQueue > > &deviceQueueMap)
Definition memoryManager.hpp:51
Provides a definition for the base backend's memory manager class.
#define HICR_THROW_RUNTIME(...)
Definition exceptions.hpp:74
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67