Skip to content

Commit 75e313c

Browse files
feature: prepare for l0 usm device pooling
Related-To: NEO-6893 Signed-off-by: Dominik Dabek <[email protected]>
1 parent c2266fc commit 75e313c

File tree

23 files changed

+459
-35
lines changed

23 files changed

+459
-35
lines changed

level_zero/core/source/context/context_imp.cpp

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,17 @@ ze_result_t ContextImp::allocDeviceMem(ze_device_handle_t hDevice,
274274
unifiedMemoryProperties.allocationFlags.flags.resource48Bit = productHelper.is48bResourceNeededForRayTracing();
275275
}
276276

277-
if (false == lookupTable.exportMemory &&
278-
neoDevice->getUsmMemAllocPoolsManager()) {
279-
neoDevice->getUsmMemAllocPoolsManager()->ensureInitialized(this->driverHandle->svmAllocsManager);
280-
if (auto usmPtrFromPool = neoDevice->getUsmMemAllocPoolsManager()->createUnifiedMemoryAllocation(size, unifiedMemoryProperties)) {
281-
*ptr = usmPtrFromPool;
282-
return ZE_RESULT_SUCCESS;
277+
if (false == lookupTable.exportMemory) {
278+
if (neoDevice->getUsmMemAllocPoolsManager()) {
279+
if (auto usmPtrFromPool = neoDevice->getUsmMemAllocPoolsManager()->createUnifiedMemoryAllocation(size, unifiedMemoryProperties)) {
280+
*ptr = usmPtrFromPool;
281+
return ZE_RESULT_SUCCESS;
282+
}
283+
} else if (neoDevice->getUsmMemAllocPool()) {
284+
if (auto usmPtrFromPool = neoDevice->getUsmMemAllocPool()->createUnifiedMemoryAllocation(size, unifiedMemoryProperties)) {
285+
*ptr = usmPtrFromPool;
286+
return ZE_RESULT_SUCCESS;
287+
}
283288
}
284289
}
285290

@@ -464,6 +469,10 @@ ze_result_t ContextImp::freeMem(const void *ptr, bool blocking) {
464469
blocking)) {
465470
return ZE_RESULT_SUCCESS;
466471
}
472+
} else if (auto deviceUsmPool = allocation->device->getUsmMemAllocPool()) {
473+
if (deviceUsmPool->freeSVMAlloc(ptr, blocking)) {
474+
return ZE_RESULT_SUCCESS;
475+
}
467476
}
468477
}
469478
this->driverHandle->svmAllocsManager->freeSVMAlloc(const_cast<void *>(ptr), blocking);
@@ -487,6 +496,18 @@ ze_result_t ContextImp::freeMemExt(const ze_memory_free_ext_desc_t *pMemFreeDesc
487496
this->freePeerAllocations(ptr, false, Device::fromHandle(pairDevice.second));
488497
}
489498

499+
if (InternalMemoryType::hostUnifiedMemory == allocation->memoryType) {
500+
if (this->driverHandle->usmHostMemAllocPool.freeSVMAlloc(ptr, false)) {
501+
return ZE_RESULT_SUCCESS;
502+
}
503+
} else if (InternalMemoryType::deviceUnifiedMemory == allocation->memoryType) {
504+
if (auto deviceUsmPool = allocation->device->getUsmMemAllocPool()) {
505+
if (deviceUsmPool->freeSVMAlloc(ptr, false)) {
506+
return ZE_RESULT_SUCCESS;
507+
}
508+
}
509+
}
510+
490511
this->driverHandle->svmAllocsManager->freeSVMAllocDefer(const_cast<void *>(ptr));
491512
return ZE_RESULT_SUCCESS;
492513
}
@@ -641,6 +662,11 @@ void ContextImp::setIPCHandleData(NEO::GraphicsAllocation *graphicsAllocation, u
641662
ipcData.poolOffset = poolOffset;
642663
break;
643664
}
665+
} else if (auto deviceUsmMemAllocPool = neoDevice->getUsmMemAllocPool()) {
666+
if (auto poolOffset = deviceUsmMemAllocPool->getOffsetInPool(addrToPtr(ptrAddress))) {
667+
ipcData.poolOffset = poolOffset;
668+
break;
669+
}
644670
}
645671
}
646672
}

level_zero/core/source/device/device_imp.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,6 +1625,8 @@ void DeviceImp::releaseResources() {
16251625

16261626
getNEODevice()->getMemoryManager()->freeGraphicsMemory(syncDispatchTokenAllocation);
16271627

1628+
getNEODevice()->cleanupUsmAllocationPool();
1629+
16281630
this->bcsSplit.releaseResources();
16291631

16301632
if (neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[neoDevice->getRootDeviceIndex()]->debugger.get()) {

level_zero/core/source/driver/driver_handle_imp.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,15 @@ ze_result_t DriverHandleImp::initialize(std::vector<std::unique_ptr<NEO::Device>
300300
}
301301
this->svmAllocsManager->initUsmAllocationsCaches(*this->devices[0]->getNEODevice());
302302
this->initHostUsmAllocPool();
303-
303+
for (auto &device : this->devices) {
304+
this->initDeviceUsmAllocPool(*device->getNEODevice());
305+
if (auto deviceUsmAllocPool = device->getNEODevice()->getUsmMemAllocPool()) {
306+
deviceUsmAllocPool->ensureInitialized(this->svmAllocsManager);
307+
}
308+
if (auto deviceUsmAllocPoolsManager = device->getNEODevice()->getUsmMemAllocPoolsManager()) {
309+
deviceUsmAllocPoolsManager->ensureInitialized(this->svmAllocsManager);
310+
}
311+
}
304312
this->numDevices = static_cast<uint32_t>(this->devices.size());
305313

306314
uuidTimestamp = static_cast<uint64_t>(std::chrono::system_clock::now().time_since_epoch().count());
@@ -352,6 +360,23 @@ void DriverHandleImp::initHostUsmAllocPool() {
352360
}
353361
}
354362

363+
void DriverHandleImp::initDeviceUsmAllocPool(NEO::Device &device) {
364+
const uint64_t minServicedSize = 0u;
365+
const uint64_t maxServicedSize = 1 * MemoryConstants::megaByte;
366+
bool enabled = NEO::ApiSpecificConfig::isDeviceUsmPoolingEnabled() && device.getProductHelper().isDeviceUsmPoolAllocatorSupported();
367+
uint64_t poolSize = 2 * MemoryConstants::megaByte;
368+
369+
if (NEO::debugManager.flags.EnableDeviceUsmAllocationPool.get() != -1) {
370+
enabled = NEO::debugManager.flags.EnableDeviceUsmAllocationPool.get() > 0;
371+
poolSize = NEO::debugManager.flags.EnableDeviceUsmAllocationPool.get() * MemoryConstants::megaByte;
372+
}
373+
374+
if (enabled) {
375+
device.resetUsmAllocationPool(new NEO::UsmMemAllocPool(rootDeviceIndices, deviceBitfields, &device, InternalMemoryType::deviceUnifiedMemory,
376+
poolSize, minServicedSize, maxServicedSize));
377+
}
378+
}
379+
355380
ze_result_t DriverHandleImp::getDevice(uint32_t *pCount, ze_device_handle_t *phDevices) {
356381

357382
// If the user has requested FLAT or COMBINED device hierarchy model, then report all the sub devices as devices.

level_zero/core/source/driver/driver_handle_imp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ struct DriverHandleImp : public DriverHandle {
124124
std::map<uint64_t, IpcHandleTracking *> &getIPCHandleMap() { return this->ipcHandles; };
125125
[[nodiscard]] std::unique_lock<std::mutex> lockIPCHandleMap() { return std::unique_lock<std::mutex>(this->ipcHandleMapMutex); };
126126
void initHostUsmAllocPool();
127+
void initDeviceUsmAllocPool(NEO::Device &device);
127128

128129
std::unique_ptr<HostPointerManager> hostPointerManager;
129130

level_zero/core/source/helpers/api_specific_config_l0.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ bool ApiSpecificConfig::isHostAllocationCacheEnabled() {
6060
}
6161

6262
bool ApiSpecificConfig::isDeviceUsmPoolingEnabled() {
63-
return true;
63+
return false;
6464
}
6565

6666
bool ApiSpecificConfig::isHostUsmPoolingEnabled() {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ struct DeviceFixture {
5959
template <typename T>
6060
struct DeviceFixtureWithCustomMemoryManager : public DeviceFixture {
6161
void setUp() {
62+
debugManager.flags.EnableDeviceUsmAllocationPool.set(0);
6263
auto executionEnvironment = NEO::MockDevice::prepareExecutionEnvironment(defaultHwInfo.get(), 0u);
6364
memoryManager = new T(*executionEnvironment);
6465
executionEnvironment->memoryManager.reset(memoryManager);
@@ -68,7 +69,7 @@ struct DeviceFixtureWithCustomMemoryManager : public DeviceFixture {
6869
void tearDown() {
6970
DeviceFixture::tearDown();
7071
}
71-
72+
DebugManagerStateRestore restorer;
7273
T *memoryManager = nullptr;
7374
};
7475

level_zero/core/test/unit_tests/fixtures/memory_ipc_fixture.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,11 @@ void MemoryExportImportImplicitScalingTest::SetUp() {
536536
}
537537

538538
void MemoryExportImportImplicitScalingTest::TearDown() {
539+
// cleanup pool before restoring svm manager
540+
for (auto device : driverHandle->devices) {
541+
device->getNEODevice()->cleanupUsmAllocationPool();
542+
device->getNEODevice()->resetUsmAllocationPool(nullptr);
543+
}
539544
driverHandle->svmAllocsManager = prevSvmAllocsManager;
540545
delete currSvmAllocsManager;
541546
driverHandle->setMemoryManager(prevMemoryManager);

level_zero/core/test/unit_tests/sources/cmdlist/test_in_order_cmdlist_1.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5488,8 +5488,9 @@ HWTEST_F(InOrderCmdListTests, givenExternalSyncStorageWhenCreatingCounterBasedEv
54885488
EXPECT_EQ(counterValue, inOrderExecInfo->getCounterValue());
54895489
EXPECT_EQ(castToUint64(externalStorageAllocProperties.deviceAddress), inOrderExecInfo->getBaseDeviceAddress());
54905490
EXPECT_NE(nullptr, inOrderExecInfo->getDeviceCounterAllocation());
5491-
5492-
auto lockedPtr = reinterpret_cast<uint64_t *>(ptrOffset(inOrderExecInfo->getDeviceCounterAllocation()->getLockedPtr(), sizeof(uint64_t)));
5491+
SvmAllocationData *deviceAlloc = context->getDriverHandle()->getSvmAllocsManager()->getSVMAlloc(reinterpret_cast<void *>(devAddress));
5492+
auto offset = ptrDiff(devAddress, deviceAlloc->gpuAllocations.getDefaultGraphicsAllocation()->getGpuAddress());
5493+
auto lockedPtr = reinterpret_cast<uint64_t *>(ptrOffset(inOrderExecInfo->getDeviceCounterAllocation()->getLockedPtr(), sizeof(uint64_t) + offset));
54935494

54945495
EXPECT_EQ(inOrderExecInfo->getBaseHostAddress(), lockedPtr);
54955496
EXPECT_EQ(inOrderExecInfo->getExternalHostAllocation(), inOrderExecInfo->getDeviceCounterAllocation());

level_zero/core/test/unit_tests/sources/debugger/windows/test_l0_debugger_windows.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020-2024 Intel Corporation
2+
* Copyright (C) 2020-2025 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -34,6 +34,7 @@ namespace ult {
3434
struct L0DebuggerWindowsFixture {
3535
void setUp() {
3636
debugManager.flags.ForcePreferredAllocationMethod.set(static_cast<int32_t>(GfxMemoryAllocationMethod::useUmdSystemPtr));
37+
debugManager.flags.EnableDeviceUsmAllocationPool.set(0);
3738
executionEnvironment = new NEO::ExecutionEnvironment;
3839
executionEnvironment->setDebuggingMode(NEO::DebuggingMode::online);
3940
executionEnvironment->prepareRootDeviceEnvironments(1);

level_zero/core/test/unit_tests/sources/driver/test_driver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ TEST_F(DriverVersionTest, givenExternalAllocatorWhenCallingGetExtensionPropertie
198198
DebugManagerStateRestore restorer;
199199
NEO::debugManager.flags.UseBindlessMode.set(1);
200200
NEO::debugManager.flags.UseExternalAllocatorForSshAndDsh.set(1);
201+
NEO::debugManager.flags.EnableDeviceUsmAllocationPool.set(0);
201202

202203
auto hwInfo = *NEO::defaultHwInfo;
203204
NEO::MockDevice *neoDevice = NEO::MockDevice::createWithNewExecutionEnvironment<NEO::MockDevice>(&hwInfo);

0 commit comments

Comments
 (0)