Skip to content

Commit 1f1fbb1

Browse files
Jaime ArteagaCompute-Runtime-Automation
authored andcommitted
Move memory managers to L0::Context (1/N)
Signed-off-by: Jaime Arteaga <[email protected]>
1 parent 5a911c9 commit 1f1fbb1

36 files changed

+566
-473
lines changed

level_zero/core/source/context/context.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ struct Context : _ze_context_handle_t {
128128
virtual ze_result_t createImage(ze_device_handle_t hDevice,
129129
const ze_image_desc_t *desc,
130130
ze_image_handle_t *phImage) = 0;
131+
virtual NEO::MemoryManager *getMemoryManager() = 0;
132+
virtual void setMemoryManager(NEO::MemoryManager *memoryManager) = 0;
133+
virtual NEO::SVMAllocsManager *getSvmAllocsManager() = 0;
134+
virtual void setSvmAllocsManager(NEO::SVMAllocsManager *) = 0;
131135

132136
static Context *fromHandle(ze_context_handle_t handle) { return static_cast<Context *>(handle); }
133137
inline ze_context_handle_t toHandle() { return this; }

level_zero/core/source/context/context_imp.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
namespace L0 {
1919

2020
ze_result_t ContextImp::destroy() {
21+
if (this->svmAllocsManager) {
22+
delete this->svmAllocsManager;
23+
this->svmAllocsManager = nullptr;
24+
}
25+
2126
delete this;
2227

2328
return ZE_RESULT_SUCCESS;
@@ -78,8 +83,8 @@ ze_result_t ContextImp::allocHostMem(const ze_host_mem_alloc_desc_t *hostDesc,
7883
this->rootDeviceIndices,
7984
this->deviceBitfields);
8085

81-
auto usmPtr = this->driverHandle->svmAllocsManager->createHostUnifiedMemoryAllocation(size,
82-
unifiedMemoryProperties);
86+
auto usmPtr = this->driverHandle->getSvmAllocsManager()->createHostUnifiedMemoryAllocation(size,
87+
unifiedMemoryProperties);
8388
if (usmPtr == nullptr) {
8489
return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY;
8590
}
@@ -157,7 +162,7 @@ ze_result_t ContextImp::allocDeviceMem(ze_device_handle_t hDevice,
157162
}
158163

159164
void *usmPtr =
160-
this->driverHandle->svmAllocsManager->createUnifiedMemoryAllocation(size, unifiedMemoryProperties);
165+
this->driverHandle->getSvmAllocsManager()->createUnifiedMemoryAllocation(size, unifiedMemoryProperties);
161166
if (usmPtr == nullptr) {
162167
return ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY;
163168
}
@@ -216,9 +221,9 @@ ze_result_t ContextImp::allocSharedMem(ze_device_handle_t hDevice,
216221
}
217222

218223
auto usmPtr =
219-
this->driverHandle->svmAllocsManager->createSharedUnifiedMemoryAllocation(size,
220-
unifiedMemoryProperties,
221-
static_cast<void *>(neoDevice->getSpecializedDevice<L0::Device>()));
224+
this->driverHandle->getSvmAllocsManager()->createSharedUnifiedMemoryAllocation(size,
225+
unifiedMemoryProperties,
226+
static_cast<void *>(neoDevice->getSpecializedDevice<L0::Device>()));
222227

223228
if (usmPtr == nullptr) {
224229
return ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY;
@@ -229,7 +234,7 @@ ze_result_t ContextImp::allocSharedMem(ze_device_handle_t hDevice,
229234
}
230235

231236
ze_result_t ContextImp::freeMem(const void *ptr) {
232-
auto allocation = this->driverHandle->svmAllocsManager->getSVMAlloc(ptr);
237+
auto allocation = this->driverHandle->getSvmAllocsManager()->getSVMAlloc(ptr);
233238
if (allocation == nullptr) {
234239
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
235240
}
@@ -244,13 +249,13 @@ ze_result_t ContextImp::freeMem(const void *ptr) {
244249
auto peerAllocData = &iter->second;
245250
auto peerAlloc = peerAllocData->gpuAllocations.getDefaultGraphicsAllocation();
246251
auto peerPtr = reinterpret_cast<void *>(peerAlloc->getGpuAddress());
247-
this->driverHandle->svmAllocsManager->freeSVMAlloc(peerPtr);
252+
this->driverHandle->getSvmAllocsManager()->freeSVMAlloc(peerPtr);
248253
}
249254
}
250255

251-
this->driverHandle->svmAllocsManager->freeSVMAlloc(const_cast<void *>(ptr));
252-
if (this->driverHandle->svmAllocsManager->getSvmMapOperation(ptr)) {
253-
this->driverHandle->svmAllocsManager->removeSvmMapOperation(ptr);
256+
this->driverHandle->getSvmAllocsManager()->freeSVMAlloc(const_cast<void *>(ptr));
257+
if (this->driverHandle->getSvmAllocsManager()->getSvmMapOperation(ptr)) {
258+
this->driverHandle->getSvmAllocsManager()->removeSvmMapOperation(ptr);
254259
}
255260
return ZE_RESULT_SUCCESS;
256261
}
@@ -326,7 +331,7 @@ ze_result_t ContextImp::evictImage(ze_device_handle_t hDevice, ze_image_handle_t
326331
ze_result_t ContextImp::getMemAddressRange(const void *ptr,
327332
void **pBase,
328333
size_t *pSize) {
329-
NEO::SvmAllocationData *allocData = this->driverHandle->svmAllocsManager->getSVMAlloc(ptr);
334+
NEO::SvmAllocationData *allocData = this->driverHandle->getSvmAllocsManager()->getSVMAlloc(ptr);
330335
if (allocData) {
331336
NEO::GraphicsAllocation *alloc;
332337
alloc = allocData->gpuAllocations.getDefaultGraphicsAllocation();
@@ -351,7 +356,7 @@ ze_result_t ContextImp::closeIpcMemHandle(const void *ptr) {
351356

352357
ze_result_t ContextImp::getIpcMemHandle(const void *ptr,
353358
ze_ipc_mem_handle_t *pIpcHandle) {
354-
NEO::SvmAllocationData *allocData = this->driverHandle->svmAllocsManager->getSVMAlloc(ptr);
359+
NEO::SvmAllocationData *allocData = this->driverHandle->getSvmAllocsManager()->getSVMAlloc(ptr);
355360
if (allocData) {
356361
uint64_t handle = allocData->gpuAllocations.getDefaultGraphicsAllocation()->peekInternalHandle(this->driverHandle->getMemoryManager());
357362
memcpy_s(reinterpret_cast<void *>(pIpcHandle->data),
@@ -385,7 +390,7 @@ ze_result_t ContextImp::openIpcMemHandle(ze_device_handle_t hDevice,
385390
ze_result_t ContextImp::getMemAllocProperties(const void *ptr,
386391
ze_memory_allocation_properties_t *pMemAllocProperties,
387392
ze_device_handle_t *phDevice) {
388-
auto alloc = driverHandle->svmAllocsManager->getSVMAlloc(ptr);
393+
auto alloc = driverHandle->getSvmAllocsManager()->getSVMAlloc(ptr);
389394
if (nullptr == alloc) {
390395
pMemAllocProperties->type = ZE_MEMORY_TYPE_UNKNOWN;
391396
return ZE_RESULT_SUCCESS;

level_zero/core/source/context/context_imp.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,28 @@ struct ContextImp : Context {
115115
return devices;
116116
}
117117

118+
NEO::MemoryManager *getMemoryManager() override {
119+
return this->memoryManager;
120+
}
121+
void setMemoryManager(NEO::MemoryManager *memoryManager) override {
122+
this->memoryManager = memoryManager;
123+
}
124+
NEO::SVMAllocsManager *getSvmAllocsManager() override {
125+
return this->svmAllocsManager;
126+
}
127+
128+
void setSvmAllocsManager(NEO::SVMAllocsManager *svmManager) override {
129+
this->svmAllocsManager = svmManager;
130+
}
131+
118132
std::set<uint32_t> rootDeviceIndices = {};
119133
std::map<uint32_t, NEO::DeviceBitfield> deviceBitfields;
120134

121135
bool isDeviceDefinedForThisContext(Device *inDevice);
122136

137+
NEO::MemoryManager *memoryManager = nullptr;
138+
NEO::SVMAllocsManager *svmAllocsManager = nullptr;
139+
123140
protected:
124141
std::map<ze_device_handle_t, Device *> devices;
125142
DriverHandleImp *driverHandle = nullptr;

level_zero/core/source/driver/driver_handle.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ struct DriverHandle : _ze_driver_handle_t {
4747
bool *allocationRangeCovered) = 0;
4848

4949
virtual NEO::SVMAllocsManager *getSvmAllocsManager() = 0;
50+
virtual void setSvmAllocsManager(NEO::SVMAllocsManager *) = 0;
5051
virtual ze_result_t sysmanEventsListen(uint32_t timeout, uint32_t count, zes_device_handle_t *phDevices,
5152
uint32_t *pNumDeviceEvents, zes_event_type_flags_t *pEvents) = 0;
5253
virtual ze_result_t sysmanEventsListenEx(uint64_t timeout, uint32_t count, zes_device_handle_t *phDevices,

level_zero/core/source/driver/driver_handle_imp.cpp

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,48 @@ ze_result_t DriverHandleImp::createContext(const ze_context_desc_t *desc,
5353
}
5454
}
5555

56+
bool multiOsContextDriver = false;
5657
for (auto devicePair : context->getDevices()) {
5758
auto neoDevice = devicePair.second->getNEODevice();
59+
multiOsContextDriver |= devicePair.second->isMultiDeviceCapable();
5860
context->rootDeviceIndices.insert(neoDevice->getRootDeviceIndex());
5961
context->deviceBitfields.insert({neoDevice->getRootDeviceIndex(),
6062
neoDevice->getDeviceBitfield()});
6163
}
6264

65+
if (this->mainContext == nullptr) {
66+
this->mainContext = context;
67+
68+
if (this->getMemoryManager() == nullptr) {
69+
this->setMemoryManager(context->getDevices().begin()->second->getNEODevice()->getMemoryManager());
70+
}
71+
72+
this->setSvmAllocsManager(new NEO::SVMAllocsManager(this->getMemoryManager(), multiOsContextDriver));
73+
74+
this->getMemoryManager()->setForceNonSvmForExternalHostPtr(true);
75+
76+
if (NEO::DebugManager.flags.EnableHostPointerImport.get() == 1) {
77+
createHostPointerManager();
78+
}
79+
}
80+
6381
return ZE_RESULT_SUCCESS;
6482
}
6583

6684
NEO::MemoryManager *DriverHandleImp::getMemoryManager() {
67-
return this->memoryManager;
85+
return this->mainContext->getMemoryManager();
6886
}
6987

7088
void DriverHandleImp::setMemoryManager(NEO::MemoryManager *memoryManager) {
71-
this->memoryManager = memoryManager;
89+
this->mainContext->setMemoryManager(memoryManager);
7290
}
7391

7492
NEO::SVMAllocsManager *DriverHandleImp::getSvmAllocsManager() {
75-
return this->svmAllocsManager;
93+
return this->mainContext->getSvmAllocsManager();
94+
}
95+
96+
void DriverHandleImp::setSvmAllocsManager(NEO::SVMAllocsManager *svmManager) {
97+
this->mainContext->setSvmAllocsManager(svmManager);
7698
}
7799

78100
ze_result_t DriverHandleImp::getApiVersion(ze_api_version_t *version) {
@@ -133,10 +155,6 @@ DriverHandleImp::~DriverHandleImp() {
133155
for (auto &device : this->devices) {
134156
delete device;
135157
}
136-
if (this->svmAllocsManager) {
137-
delete this->svmAllocsManager;
138-
this->svmAllocsManager = nullptr;
139-
}
140158
}
141159

142160
ze_result_t DriverHandleImp::initialize(std::vector<std::unique_ptr<NEO::Device>> neoDevices) {
@@ -151,13 +169,6 @@ ze_result_t DriverHandleImp::initialize(std::vector<std::unique_ptr<NEO::Device>
151169
continue;
152170
}
153171

154-
if (this->memoryManager == nullptr) {
155-
this->memoryManager = neoDevice->getMemoryManager();
156-
if (this->memoryManager == nullptr) {
157-
return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY;
158-
}
159-
}
160-
161172
const auto rootDeviceIndex = neoDevice->getRootDeviceIndex();
162173
auto rootDeviceEnvironment = neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[rootDeviceIndex].get();
163174

@@ -189,21 +200,12 @@ ze_result_t DriverHandleImp::initialize(std::vector<std::unique_ptr<NEO::Device>
189200
return ZE_RESULT_ERROR_UNINITIALIZED;
190201
}
191202

192-
this->svmAllocsManager = new NEO::SVMAllocsManager(memoryManager, multiOsContextDriver);
193-
if (this->svmAllocsManager == nullptr) {
194-
return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY;
195-
}
196-
197203
this->numDevices = static_cast<uint32_t>(this->devices.size());
198204

199205
extensionFunctionsLookupMap = getExtensionFunctionsLookupMap();
200206

201207
uuidTimestamp = static_cast<uint64_t>(std::chrono::system_clock::now().time_since_epoch().count());
202208

203-
if (NEO::DebugManager.flags.EnableHostPointerImport.get() == 1) {
204-
createHostPointerManager();
205-
}
206-
207209
return ZE_RESULT_SUCCESS;
208210
}
209211

@@ -223,8 +225,6 @@ DriverHandle *DriverHandle::create(std::vector<std::unique_ptr<NEO::Device>> dev
223225

224226
GlobalDriver = driverHandle;
225227

226-
driverHandle->getMemoryManager()->setForceNonSvmForExternalHostPtr(true);
227-
228228
return driverHandle;
229229
}
230230

@@ -250,8 +250,8 @@ bool DriverHandleImp::findAllocationDataForRange(const void *buffer,
250250
NEO::SvmAllocationData **allocData) {
251251
// Make sure the host buffer does not overlap any existing allocation
252252
const char *baseAddress = reinterpret_cast<const char *>(buffer);
253-
NEO::SvmAllocationData *beginAllocData = svmAllocsManager->getSVMAlloc(baseAddress);
254-
NEO::SvmAllocationData *endAllocData = svmAllocsManager->getSVMAlloc(baseAddress + size - 1);
253+
NEO::SvmAllocationData *beginAllocData = getSvmAllocsManager()->getSVMAlloc(baseAddress);
254+
NEO::SvmAllocationData *endAllocData = getSvmAllocsManager()->getSVMAlloc(baseAddress + size - 1);
255255

256256
if (allocData) {
257257
if (beginAllocData) {
@@ -275,8 +275,8 @@ std::vector<NEO::SvmAllocationData *> DriverHandleImp::findAllocationsWithinRang
275275
std::vector<NEO::SvmAllocationData *> allocDataArray;
276276
const char *baseAddress = reinterpret_cast<const char *>(buffer);
277277
// Check if the host buffer overlaps any existing allocation
278-
NEO::SvmAllocationData *beginAllocData = svmAllocsManager->getSVMAlloc(baseAddress);
279-
NEO::SvmAllocationData *endAllocData = svmAllocsManager->getSVMAlloc(baseAddress + size - 1);
278+
NEO::SvmAllocationData *beginAllocData = this->getSvmAllocsManager()->getSVMAlloc(baseAddress);
279+
NEO::SvmAllocationData *endAllocData = this->getSvmAllocsManager()->getSVMAlloc(baseAddress + size - 1);
280280

281281
// Add the allocation that matches the beginning address
282282
if (beginAllocData) {

level_zero/core/source/driver/driver_handle_imp.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
#include "shared/source/os_interface/os_library.h"
1111

1212
#include "level_zero/api/extensions/public/ze_exp_ext.h"
13+
#include "level_zero/core/source/context/context.h"
1314
#include "level_zero/core/source/driver/driver_handle.h"
1415
#include "level_zero/core/source/get_extension_function_lookup_map.h"
1516

1617
namespace L0 {
1718
class HostPointerManager;
19+
struct Context;
1820

1921
struct DriverHandleImp : public DriverHandle {
2022
~DriverHandleImp() override;
@@ -38,6 +40,7 @@ struct DriverHandleImp : public DriverHandle {
3840
ze_result_t openEventPoolIpcHandle(ze_ipc_event_pool_handle_t hIpc, ze_event_pool_handle_t *phEventPool) override;
3941
ze_result_t checkMemoryAccessFromDevice(Device *device, const void *ptr) override;
4042
NEO::SVMAllocsManager *getSvmAllocsManager() override;
43+
void setSvmAllocsManager(NEO::SVMAllocsManager *) override;
4144
ze_result_t initialize(std::vector<std::unique_ptr<NEO::Device>> neoDevices);
4245
bool findAllocationDataForRange(const void *buffer,
4346
size_t size,
@@ -90,11 +93,10 @@ struct DriverHandleImp : public DriverHandle {
9093

9194
uint64_t uuidTimestamp = 0u;
9295

93-
NEO::MemoryManager *memoryManager = nullptr;
94-
NEO::SVMAllocsManager *svmAllocsManager = nullptr;
95-
9696
uint32_t numDevices = 0;
9797

98+
Context *mainContext = nullptr;
99+
98100
std::set<uint32_t> rootDeviceIndices = {};
99101
std::map<uint32_t, NEO::DeviceBitfield> deviceBitfields;
100102

level_zero/core/source/event/event.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ ze_result_t EventPoolImp::initialize(DriverHandle *driver, Context *context, uin
9696
alignedSize,
9797
allocationType,
9898
false,
99-
(deviceBitfield.count() > 1) && driverHandleImp->svmAllocsManager->getMultiOsContextSupport(),
99+
(deviceBitfield.count() > 1) && driverHandleImp->getSvmAllocsManager()->getMultiOsContextSupport(),
100100
deviceBitfield};
101101
unifiedMemoryProperties.flags.isUSMHostAllocation = true;
102102
unifiedMemoryProperties.flags.isUSMDeviceAllocation = false;

level_zero/core/source/memory/memory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void *DriverHandleImp::importFdHandle(ze_device_handle_t hDevice, ze_ipc_memory_
5151
}
5252

5353
ze_result_t DriverHandleImp::checkMemoryAccessFromDevice(Device *device, const void *ptr) {
54-
auto allocation = svmAllocsManager->getSVMAlloc(ptr);
54+
auto allocation = this->getSvmAllocsManager()->getSVMAlloc(ptr);
5555
if (allocation == nullptr) {
5656
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
5757
}

level_zero/core/test/unit_tests/fixtures/cmdlist_fixture.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,23 @@ class CommandListFixture : public DeviceFixture {
3131
eventDesc.wait = 0;
3232
eventDesc.signal = 0;
3333

34-
eventPool = std::unique_ptr<EventPool>(EventPool::create(driverHandle.get(), context, 0, nullptr, &eventPoolDesc));
35-
event = std::unique_ptr<Event>(Event::create(eventPool.get(), &eventDesc, device));
34+
eventPool = EventPool::create(driverHandle.get(), context, 0, nullptr, &eventPoolDesc);
35+
event = Event::create(eventPool, &eventDesc, device);
3636
}
3737

3838
void TearDown() override {
39+
if (event) {
40+
event->destroy();
41+
}
42+
if (eventPool) {
43+
eventPool->destroy();
44+
}
3945
DeviceFixture::TearDown();
4046
}
4147

4248
std::unique_ptr<L0::ult::CommandList> commandList;
43-
std::unique_ptr<EventPool> eventPool;
44-
std::unique_ptr<Event> event;
49+
EventPool *eventPool = nullptr;
50+
Event *event = nullptr;
4551
};
4652

4753
} // namespace ult

level_zero/core/test/unit_tests/fixtures/device_fixture.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
#include "opencl/test/unit_test/mocks/mock_compilers.h"
1616

1717
#include "level_zero/core/source/context/context_imp.h"
18-
#include "level_zero/core/test/unit_tests/mocks/mock_driver_handle.h"
18+
#include "level_zero/core/test/unit_tests/mock.h"
19+
#include "level_zero/core/test/unit_tests/mocks/mock_device.h"
20+
#include "level_zero/core/test/unit_tests/mocks/mock_memory_manager.h"
21+
#include "level_zero/core/test/unit_tests/white_box.h"
1922

2023
namespace L0 {
2124
struct Context;

0 commit comments

Comments
 (0)