/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 // Getting up-casted pointer for the opencl instance, first try with the device memory space
66 auto openclMemorySpace = dynamic_pointer_cast<opencl::MemorySpace>(memorySpace);
67 auto hwlocMemorySpace = dynamic_pointer_cast<hwloc::MemorySpace>(memorySpace);
68
69 // Checking whether the memory space passed belongs to the device or to the host
70 if (hwlocMemorySpace != nullptr) { return allocateLocalDeviceMemorySlot(memorySpace, size); }
71 if (openclMemorySpace != nullptr) { return allocateLocalHostMemorySlot(memorySpace, size); }
72
73 HICR_THROW_LOGIC("The passed memory space is not supported by this memory manager. Supported opencl and hwloc\n");
74 }
75
76 __INLINE__ std::shared_ptr<HiCR::LocalMemorySlot> allocateLocalDeviceMemorySlot(const std::shared_ptr<HiCR::MemorySpace> memorySpace, const size_t size)
77 {
78 cl_int err;
79 auto queue = getQueue(memorySpace);
80 auto context = queue->getInfo<CL_QUEUE_CONTEXT>();
81
82 // Create OpenCL buffer on the device ready for read and write operations
83 auto deviceBuffer = std::make_shared<cl::Buffer>(context, CL_MEM_READ_WRITE, size, nullptr, &err);
84 if (err != CL_SUCCESS) [[unlikely]] { HICR_THROW_RUNTIME("Can not allocate local memory slot on the device: %d", err); }
85
86 // Map it to the host in order to get a pointer to the data. Data are mapped in memory for writing
87 auto hostPtr = queue->enqueueMapBuffer(*deviceBuffer, CL_TRUE, CL_MAP_READ | CL_MAP_READ | CL_MAP_WRITE, 0, size, nullptr, nullptr, &err);
88 if (err != CL_SUCCESS || hostPtr == nullptr) [[unlikely]] { HICR_THROW_RUNTIME("Can not retrieve pointer: %d", err); }
89
90 auto memSlot = std::make_shared<opencl::LocalMemorySlot>(hostPtr, size, deviceBuffer, memorySpace);
91
92 // create the new memory slot
93 return memSlot;
94 }
95
96 __INLINE__ std::shared_ptr<HiCR::LocalMemorySlot> allocateLocalHostMemorySlot(const std::shared_ptr<HiCR::MemorySpace> memorySpace, const size_t size)
97 {
98 cl_int err;
99 auto queue = getQueue(memorySpace);
100 auto context = queue->getInfo<CL_QUEUE_CONTEXT>();
101
102 // Create OpenCL buffer
103 auto hostBuffer = std::make_shared<cl::Buffer>(context, CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR, size, nullptr, &err);
104 if (err != CL_SUCCESS) [[unlikely]] { HICR_THROW_RUNTIME("Can not allocate local memory slot on the host: %d", err); }
105
106 // Map it to the host in order to get a pointer to the data. Data are mapped in memory for writing
107 auto hostPtr = queue->enqueueMapBuffer(*hostBuffer, CL_TRUE, CL_MAP_READ | CL_MAP_WRITE, 0, size, nullptr, nullptr, &err);
108 if (err != CL_SUCCESS || hostPtr == nullptr) [[unlikely]] { HICR_THROW_RUNTIME("Can not retrieve pointer: %d", err); }
109
110 auto memSlot = std::make_shared<opencl::LocalMemorySlot>(hostPtr, size, hostBuffer, memorySpace);
111
112 // create the new memory slot
113 return memSlot;
114 }
115
119 __INLINE__ std::shared_ptr<HiCR::LocalMemorySlot> registerLocalMemorySlotImpl(std::shared_ptr<HiCR::MemorySpace> memorySpace, void *const ptr, const size_t size) override
120 {
121 // Getting up-casted pointer for the opencl instance, first try with the device memory space
122 auto openclMemorySpace = dynamic_pointer_cast<const opencl::MemorySpace>(memorySpace);
123
124 // Checking whether the memory space passed belongs to the device or to the host
125 if (openclMemorySpace != nullptr) [[unlikely]] { HICR_THROW_RUNTIME("Can not register local memory slot on the provided memory space: %s", memorySpace->getType().c_str()); }
126
127 cl_int err;
128 auto queue = getQueue(memorySpace);
129 auto context = queue->getInfo<CL_QUEUE_CONTEXT>();
130
131 // Create OpenCL buffer
132 auto buffer = std::make_shared<cl::Buffer>(context, CL_MEM_USE_HOST_PTR, size, ptr, &err);
133 if (err != CL_SUCCESS) [[unlikely]] { HICR_THROW_RUNTIME("Can not register local memory slot on the host: %d", err); }
134
135 // Map it to the host in order to get a pointer to the data. Data are mapped in memory for writing
136 auto hostPtr = queue->enqueueMapBuffer(*buffer, CL_TRUE, CL_MAP_READ | CL_MAP_WRITE, 0, size, nullptr, nullptr, &err);
137 if (err != CL_SUCCESS || hostPtr == nullptr) [[unlikely]] { HICR_THROW_RUNTIME("Can not retrieve pointer: %d", err); }
138
139 // create the new memory slot
140 return std::make_shared<opencl::LocalMemorySlot>(hostPtr, size, buffer, memorySpace);
141 }
142
143 __INLINE__ void memsetImpl(const std::shared_ptr<HiCR::LocalMemorySlot> memorySlot, int value, size_t size) override
144 {
145 auto m = dynamic_pointer_cast<opencl::LocalMemorySlot>(memorySlot);
146 if (m == nullptr) [[unlikely]] { HICR_THROW_RUNTIME("Unsupported local memory slot: %s", memorySlot->getMemorySpace()->getType().c_str()); }
147
148 cl_int clValue = value;
149
150 auto completionEvent = cl::Event();
151 auto queue = getQueue(m->getMemorySpace());
152 auto err = queue->enqueueFillBuffer(*(m->getBuffer()), clValue, 0, m->getSize(), nullptr, &completionEvent);
153 if (err != CL_SUCCESS) [[unlikely]] { HICR_THROW_RUNTIME("Can not perform memset: %d", err); }
154 completionEvent.wait();
155 }
156
157 __INLINE__ void freeLocalMemorySlotImpl(std::shared_ptr<HiCR::LocalMemorySlot> memorySlot) override
158 {
159 auto m = dynamic_pointer_cast<opencl::LocalMemorySlot>(memorySlot);
160 if (m == nullptr) [[unlikely]] { HICR_THROW_RUNTIME("Unsupported local memory slot: %s", memorySlot->getMemorySpace()->getType().c_str()); }
161
162 auto queue = getQueue(m->getMemorySpace());
163 auto buffer = m->getBuffer();
164
165 auto err = queue->enqueueUnmapMemObject(*buffer, m->getPointer());
166 if (err != CL_SUCCESS) [[unlikely]] { HICR_THROW_RUNTIME("Can not unmap host pointer: %d", err); }
167
168 m->getBuffer().reset();
169 }
170
176 __INLINE__ void deregisterLocalMemorySlotImpl(std::shared_ptr<HiCR::LocalMemorySlot> memorySlot) override {}
177
178 private:
179
183 enum memSpaceType_t
184 {
188 none,
189
193 host,
194
198 device
199 };
200
204 const std::unordered_map<opencl::Device::deviceIdentifier_t, std::shared_ptr<cl::CommandQueue>> _deviceQueueMap;
205
213 std::shared_ptr<cl::CommandQueue> getQueue(std::shared_ptr<HiCR::MemorySpace> memorySpace)
214 {
215 auto openclMemorySpace = dynamic_pointer_cast<opencl::MemorySpace>(memorySpace);
216 auto hwlocMemorySpace = dynamic_pointer_cast<hwloc::MemorySpace>(memorySpace);
217 if (hwlocMemorySpace != nullptr) { return _deviceQueueMap.begin()->second; }
218 if (openclMemorySpace != nullptr)
219 {
220 auto device = openclMemorySpace->getDevice().lock();
221 auto deviceId = device->getId();
222 return _deviceQueueMap.at(deviceId);
223 }
224 HICR_THROW_LOGIC("The passed memory space is not supported by this memory manager. Supported opencl and hwloc\n");
225 }
226};
227
228} // 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