Skip to content

Commit 5f0e4f8

Browse files
Jaime ArteagaCompute-Runtime-Automation
authored andcommitted
Revert "Move memory managers to L0::Context (1/N)"
This reverts commit 9ce887b8b53a787a7e0a0d808c96e295655ae57b. Signed-off-by: Jaime Arteaga <jaime.a.arteaga.molina@intel.com>
1 parent 4752914 commit 5f0e4f8

36 files changed

+472
-565
lines changed

level_zero/core/source/context/context.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,6 @@ 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;
135131

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

level_zero/core/source/context/context_imp.cpp

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

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

2823
return ZE_RESULT_SUCCESS;
@@ -83,8 +78,8 @@ ze_result_t ContextImp::allocHostMem(const ze_host_mem_alloc_desc_t *hostDesc,
8378
this->rootDeviceIndices,
8479
this->deviceBitfields);
8580

86-
auto usmPtr = this->driverHandle->getSvmAllocsManager()->createHostUnifiedMemoryAllocation(size,
87-
unifiedMemoryProperties);
81+
auto usmPtr = this->driverHandle->svmAllocsManager->createHostUnifiedMemoryAllocation(size,
82+
unifiedMemoryProperties);
8883
if (usmPtr == nullptr) {
8984
return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY;
9085
}
@@ -162,7 +157,7 @@ ze_result_t ContextImp::allocDeviceMem(ze_device_handle_t hDevice,
162157
}
163158

164159
void *usmPtr =
165-
this->driverHandle->getSvmAllocsManager()->createUnifiedMemoryAllocation(size, unifiedMemoryProperties);
160+
this->driverHandle->svmAllocsManager->createUnifiedMemoryAllocation(size, unifiedMemoryProperties);
166161
if (usmPtr == nullptr) {
167162
return ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY;
168163
}
@@ -221,9 +216,9 @@ ze_result_t ContextImp::allocSharedMem(ze_device_handle_t hDevice,
221216
}
222217

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

228223
if (usmPtr == nullptr) {
229224
return ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY;
@@ -234,7 +229,7 @@ ze_result_t ContextImp::allocSharedMem(ze_device_handle_t hDevice,
234229
}
235230

236231
ze_result_t ContextImp::freeMem(const void *ptr) {
237-
auto allocation = this->driverHandle->getSvmAllocsManager()->getSVMAlloc(ptr);
232+
auto allocation = this->driverHandle->svmAllocsManager->getSVMAlloc(ptr);
238233
if (allocation == nullptr) {
239234
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
240235
}
@@ -249,13 +244,13 @@ ze_result_t ContextImp::freeMem(const void *ptr) {
249244
auto peerAllocData = &iter->second;
250245
auto peerAlloc = peerAllocData->gpuAllocations.getDefaultGraphicsAllocation();
251246
auto peerPtr = reinterpret_cast<void *>(peerAlloc->getGpuAddress());
252-
this->driverHandle->getSvmAllocsManager()->freeSVMAlloc(peerPtr);
247+
this->driverHandle->svmAllocsManager->freeSVMAlloc(peerPtr);
253248
}
254249
}
255250

256-
this->driverHandle->getSvmAllocsManager()->freeSVMAlloc(const_cast<void *>(ptr));
257-
if (this->driverHandle->getSvmAllocsManager()->getSvmMapOperation(ptr)) {
258-
this->driverHandle->getSvmAllocsManager()->removeSvmMapOperation(ptr);
251+
this->driverHandle->svmAllocsManager->freeSVMAlloc(const_cast<void *>(ptr));
252+
if (this->driverHandle->svmAllocsManager->getSvmMapOperation(ptr)) {
253+
this->driverHandle->svmAllocsManager->removeSvmMapOperation(ptr);
259254
}
260255
return ZE_RESULT_SUCCESS;
261256
}
@@ -331,7 +326,7 @@ ze_result_t ContextImp::evictImage(ze_device_handle_t hDevice, ze_image_handle_t
331326
ze_result_t ContextImp::getMemAddressRange(const void *ptr,
332327
void **pBase,
333328
size_t *pSize) {
334-
NEO::SvmAllocationData *allocData = this->driverHandle->getSvmAllocsManager()->getSVMAlloc(ptr);
329+
NEO::SvmAllocationData *allocData = this->driverHandle->svmAllocsManager->getSVMAlloc(ptr);
335330
if (allocData) {
336331
NEO::GraphicsAllocation *alloc;
337332
alloc = allocData->gpuAllocations.getDefaultGraphicsAllocation();
@@ -356,7 +351,7 @@ ze_result_t ContextImp::closeIpcMemHandle(const void *ptr) {
356351

357352
ze_result_t ContextImp::getIpcMemHandle(const void *ptr,
358353
ze_ipc_mem_handle_t *pIpcHandle) {
359-
NEO::SvmAllocationData *allocData = this->driverHandle->getSvmAllocsManager()->getSVMAlloc(ptr);
354+
NEO::SvmAllocationData *allocData = this->driverHandle->svmAllocsManager->getSVMAlloc(ptr);
360355
if (allocData) {
361356
uint64_t handle = allocData->gpuAllocations.getDefaultGraphicsAllocation()->peekInternalHandle(this->driverHandle->getMemoryManager());
362357
memcpy_s(reinterpret_cast<void *>(pIpcHandle->data),
@@ -390,7 +385,7 @@ ze_result_t ContextImp::openIpcMemHandle(ze_device_handle_t hDevice,
390385
ze_result_t ContextImp::getMemAllocProperties(const void *ptr,
391386
ze_memory_allocation_properties_t *pMemAllocProperties,
392387
ze_device_handle_t *phDevice) {
393-
auto alloc = driverHandle->getSvmAllocsManager()->getSVMAlloc(ptr);
388+
auto alloc = driverHandle->svmAllocsManager->getSVMAlloc(ptr);
394389
if (nullptr == alloc) {
395390
pMemAllocProperties->type = ZE_MEMORY_TYPE_UNKNOWN;
396391
return ZE_RESULT_SUCCESS;

level_zero/core/source/context/context_imp.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -115,28 +115,11 @@ 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-
132118
std::set<uint32_t> rootDeviceIndices = {};
133119
std::map<uint32_t, NEO::DeviceBitfield> deviceBitfields;
134120

135121
bool isDeviceDefinedForThisContext(Device *inDevice);
136122

137-
NEO::MemoryManager *memoryManager = nullptr;
138-
NEO::SVMAllocsManager *svmAllocsManager = nullptr;
139-
140123
protected:
141124
std::map<ze_device_handle_t, Device *> devices;
142125
DriverHandleImp *driverHandle = nullptr;

level_zero/core/source/driver/driver_handle.h

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

4949
virtual NEO::SVMAllocsManager *getSvmAllocsManager() = 0;
50-
virtual void setSvmAllocsManager(NEO::SVMAllocsManager *) = 0;
5150
virtual ze_result_t sysmanEventsListen(uint32_t timeout, uint32_t count, zes_device_handle_t *phDevices,
5251
uint32_t *pNumDeviceEvents, zes_event_type_flags_t *pEvents) = 0;
5352
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,48 +53,26 @@ ze_result_t DriverHandleImp::createContext(const ze_context_desc_t *desc,
5353
}
5454
}
5555

56-
bool multiOsContextDriver = false;
5756
for (auto devicePair : context->getDevices()) {
5857
auto neoDevice = devicePair.second->getNEODevice();
59-
multiOsContextDriver |= devicePair.second->isMultiDeviceCapable();
6058
context->rootDeviceIndices.insert(neoDevice->getRootDeviceIndex());
6159
context->deviceBitfields.insert({neoDevice->getRootDeviceIndex(),
6260
neoDevice->getDeviceBitfield()});
6361
}
6462

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-
8163
return ZE_RESULT_SUCCESS;
8264
}
8365

8466
NEO::MemoryManager *DriverHandleImp::getMemoryManager() {
85-
return this->mainContext->getMemoryManager();
67+
return this->memoryManager;
8668
}
8769

8870
void DriverHandleImp::setMemoryManager(NEO::MemoryManager *memoryManager) {
89-
this->mainContext->setMemoryManager(memoryManager);
71+
this->memoryManager = memoryManager;
9072
}
9173

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

10078
ze_result_t DriverHandleImp::getApiVersion(ze_api_version_t *version) {
@@ -155,6 +133,10 @@ DriverHandleImp::~DriverHandleImp() {
155133
for (auto &device : this->devices) {
156134
delete device;
157135
}
136+
if (this->svmAllocsManager) {
137+
delete this->svmAllocsManager;
138+
this->svmAllocsManager = nullptr;
139+
}
158140
}
159141

160142
ze_result_t DriverHandleImp::initialize(std::vector<std::unique_ptr<NEO::Device>> neoDevices) {
@@ -169,6 +151,13 @@ ze_result_t DriverHandleImp::initialize(std::vector<std::unique_ptr<NEO::Device>
169151
continue;
170152
}
171153

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+
172161
const auto rootDeviceIndex = neoDevice->getRootDeviceIndex();
173162
auto rootDeviceEnvironment = neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[rootDeviceIndex].get();
174163

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

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

205199
extensionFunctionsLookupMap = getExtensionFunctionsLookupMap();
206200

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

203+
if (NEO::DebugManager.flags.EnableHostPointerImport.get() == 1) {
204+
createHostPointerManager();
205+
}
206+
209207
return ZE_RESULT_SUCCESS;
210208
}
211209

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

226224
GlobalDriver = driverHandle;
227225

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 = getSvmAllocsManager()->getSVMAlloc(baseAddress);
254-
NEO::SvmAllocationData *endAllocData = getSvmAllocsManager()->getSVMAlloc(baseAddress + size - 1);
253+
NEO::SvmAllocationData *beginAllocData = svmAllocsManager->getSVMAlloc(baseAddress);
254+
NEO::SvmAllocationData *endAllocData = svmAllocsManager->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 = this->getSvmAllocsManager()->getSVMAlloc(baseAddress);
279-
NEO::SvmAllocationData *endAllocData = this->getSvmAllocsManager()->getSVMAlloc(baseAddress + size - 1);
278+
NEO::SvmAllocationData *beginAllocData = svmAllocsManager->getSVMAlloc(baseAddress);
279+
NEO::SvmAllocationData *endAllocData = svmAllocsManager->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: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
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"
1413
#include "level_zero/core/source/driver/driver_handle.h"
1514
#include "level_zero/core/source/get_extension_function_lookup_map.h"
1615

1716
namespace L0 {
1817
class HostPointerManager;
19-
struct Context;
2018

2119
struct DriverHandleImp : public DriverHandle {
2220
~DriverHandleImp() override;
@@ -40,7 +38,6 @@ struct DriverHandleImp : public DriverHandle {
4038
ze_result_t openEventPoolIpcHandle(ze_ipc_event_pool_handle_t hIpc, ze_event_pool_handle_t *phEventPool) override;
4139
ze_result_t checkMemoryAccessFromDevice(Device *device, const void *ptr) override;
4240
NEO::SVMAllocsManager *getSvmAllocsManager() override;
43-
void setSvmAllocsManager(NEO::SVMAllocsManager *) override;
4441
ze_result_t initialize(std::vector<std::unique_ptr<NEO::Device>> neoDevices);
4542
bool findAllocationDataForRange(const void *buffer,
4643
size_t size,
@@ -93,9 +90,10 @@ struct DriverHandleImp : public DriverHandle {
9390

9491
uint64_t uuidTimestamp = 0u;
9592

96-
uint32_t numDevices = 0;
93+
NEO::MemoryManager *memoryManager = nullptr;
94+
NEO::SVMAllocsManager *svmAllocsManager = nullptr;
9795

98-
Context *mainContext = nullptr;
96+
uint32_t numDevices = 0;
9997

10098
std::set<uint32_t> rootDeviceIndices = {};
10199
std::map<uint32_t, NEO::DeviceBitfield> deviceBitfields;

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->getSvmAllocsManager()->getMultiOsContextSupport(),
99+
(deviceBitfield.count() > 1) && driverHandleImp->svmAllocsManager->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 = this->getSvmAllocsManager()->getSVMAlloc(ptr);
54+
auto allocation = svmAllocsManager->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: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,17 @@ class CommandListFixture : public DeviceFixture {
3131
eventDesc.wait = 0;
3232
eventDesc.signal = 0;
3333

34-
eventPool = EventPool::create(driverHandle.get(), context, 0, nullptr, &eventPoolDesc);
35-
event = Event::create(eventPool, &eventDesc, device);
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));
3636
}
3737

3838
void TearDown() override {
39-
if (event) {
40-
event->destroy();
41-
}
42-
if (eventPool) {
43-
eventPool->destroy();
44-
}
4539
DeviceFixture::TearDown();
4640
}
4741

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

5347
} // namespace ult

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
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/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"
18+
#include "level_zero/core/test/unit_tests/mocks/mock_driver_handle.h"
2219

2320
namespace L0 {
2421
struct Context;

0 commit comments

Comments
 (0)