Skip to content

Commit a6bb897

Browse files
Adjust dev memory available based on impl scaling
Signed-off-by: John Falkowski <[email protected]>
1 parent 6d51024 commit a6bb897

File tree

4 files changed

+131
-8
lines changed

4 files changed

+131
-8
lines changed

level_zero/core/source/context/context_imp.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "level_zero/core/source/context/context_imp.h"
99

10+
#include "shared/source/command_container/implicit_scaling.h"
1011
#include "shared/source/memory_manager/memory_operations_handler.h"
1112
#include "shared/source/memory_manager/unified_memory_manager.h"
1213

@@ -135,18 +136,27 @@ ze_result_t ContextImp::allocDeviceMem(ze_device_handle_t hDevice,
135136
return ZE_RESULT_SUCCESS;
136137
}
137138

139+
neoDevice = this->driverHandle->devices[0]->getNEODevice();
140+
138141
if (lookupTable.relaxedSizeAllowed == false &&
139-
(size > this->driverHandle->devices[0]->getNEODevice()->getDeviceInfo().maxMemAllocSize)) {
142+
(size > neoDevice->getDeviceInfo().maxMemAllocSize)) {
140143
*ptr = nullptr;
141144
return ZE_RESULT_ERROR_UNSUPPORTED_SIZE;
142145
}
143146

144-
if (lookupTable.relaxedSizeAllowed &&
145-
(size > this->driverHandle->devices[0]->getNEODevice()->getDeviceInfo().globalMemSize)) {
147+
uint64_t globalMemSize = neoDevice->getDeviceInfo().globalMemSize;
148+
149+
uint32_t numSubDevices = neoDevice->getNumSubDevices();
150+
if ((!(NEO::ImplicitScalingHelper::isImplicitScalingEnabled(neoDevice->getDeviceBitfield(), true))) && (numSubDevices > 1)) {
151+
globalMemSize = globalMemSize / numSubDevices;
152+
}
153+
if (lookupTable.relaxedSizeAllowed && (size > globalMemSize)) {
146154
*ptr = nullptr;
147155
return ZE_RESULT_ERROR_UNSUPPORTED_SIZE;
148156
}
149157

158+
neoDevice = Device::fromHandle(hDevice)->getNEODevice();
159+
150160
deviceBitfields[rootDeviceIndex] = neoDevice->getDeviceBitfield();
151161

152162
NEO::SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::DEVICE_UNIFIED_MEMORY, this->driverHandle->rootDeviceIndices, deviceBitfields);
@@ -186,20 +196,27 @@ ze_result_t ContextImp::allocSharedMem(ze_device_handle_t hDevice,
186196
}
187197
}
188198

199+
auto neoDevice = this->devices.begin()->second->getNEODevice();
189200
if (relaxedSizeAllowed == false &&
190-
(size > this->devices.begin()->second->getNEODevice()->getDeviceInfo().maxMemAllocSize)) {
201+
(size > neoDevice->getDeviceInfo().maxMemAllocSize)) {
191202
*ptr = nullptr;
192203
return ZE_RESULT_ERROR_UNSUPPORTED_SIZE;
193204
}
194205

206+
neoDevice = this->driverHandle->devices[0]->getNEODevice();
207+
uint64_t globalMemSize = neoDevice->getDeviceInfo().globalMemSize;
208+
209+
uint32_t numSubDevices = neoDevice->getNumSubDevices();
210+
if ((!(NEO::ImplicitScalingHelper::isImplicitScalingEnabled(neoDevice->getDeviceBitfield(), true))) && (numSubDevices > 1)) {
211+
globalMemSize = globalMemSize / numSubDevices;
212+
}
195213
if (relaxedSizeAllowed &&
196-
(size > this->driverHandle->devices[0]->getNEODevice()->getDeviceInfo().globalMemSize)) {
214+
(size > globalMemSize)) {
197215
*ptr = nullptr;
198216
return ZE_RESULT_ERROR_UNSUPPORTED_SIZE;
199217
}
200218

201-
auto neoDevice = this->devices.begin()->second->getNEODevice();
202-
219+
neoDevice = this->devices.begin()->second->getNEODevice();
203220
auto deviceBitfields = this->deviceBitfields;
204221
NEO::Device *unifiedMemoryPropertiesDevice = nullptr;
205222
if (hDevice) {

level_zero/core/source/device/device_imp.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "level_zero/core/source/device/device_imp.h"
99

1010
#include "shared/source/built_ins/sip.h"
11+
#include "shared/source/command_container/implicit_scaling.h"
1112
#include "shared/source/debug_settings/debug_settings_manager.h"
1213
#include "shared/source/device/device_info.h"
1314
#include "shared/source/device/sub_device.h"
@@ -275,7 +276,12 @@ ze_result_t DeviceImp::getMemoryProperties(uint32_t *pCount, ze_device_memory_pr
275276
auto &hwInfoConfig = *NEO::HwInfoConfig::get(hwInfo.platform.eProductFamily);
276277
pMemProperties->maxClockRate = hwInfoConfig.getDeviceMemoryMaxClkRate(&hwInfo);
277278
pMemProperties->maxBusWidth = deviceInfo.addressBits;
278-
pMemProperties->totalSize = deviceInfo.globalMemSize;
279+
if (NEO::ImplicitScalingHelper::isImplicitScalingEnabled(this->getNEODevice()->getDeviceBitfield(), true) ||
280+
this->numSubDevices == 0) {
281+
pMemProperties->totalSize = deviceInfo.globalMemSize;
282+
} else {
283+
pMemProperties->totalSize = deviceInfo.globalMemSize / this->numSubDevices;
284+
}
279285

280286
return ZE_RESULT_SUCCESS;
281287
}

level_zero/core/test/unit_tests/sources/device/test_device.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,24 @@ struct MultipleDevicesTest : public ::testing::Test {
12011201
const uint32_t numSubDevices = 2u;
12021202
};
12031203

1204+
TEST_F(MultipleDevicesTest, whenCallingGetMemoryPropertiesWithSubDevicesThenCorrectSizeReturned) {
1205+
L0::Device *device0 = driverHandle->devices[0];
1206+
uint32_t count = 1;
1207+
1208+
DebugManager.flags.EnableWalkerPartition.set(0);
1209+
ze_device_memory_properties_t memProperties = {};
1210+
ze_result_t res = device0->getMemoryProperties(&count, &memProperties);
1211+
EXPECT_EQ(res, ZE_RESULT_SUCCESS);
1212+
EXPECT_EQ(1u, count);
1213+
EXPECT_EQ(memProperties.totalSize, device0->getNEODevice()->getDeviceInfo().globalMemSize / numSubDevices);
1214+
1215+
DebugManager.flags.EnableWalkerPartition.set(1);
1216+
res = device0->getMemoryProperties(&count, &memProperties);
1217+
EXPECT_EQ(res, ZE_RESULT_SUCCESS);
1218+
EXPECT_EQ(1u, count);
1219+
EXPECT_EQ(memProperties.totalSize, device0->getNEODevice()->getDeviceInfo().globalMemSize);
1220+
}
1221+
12041222
TEST_F(MultipleDevicesTest, whenRetrievingNumberOfSubdevicesThenCorrectNumberIsReturned) {
12051223
L0::Device *device0 = driverHandle->devices[0];
12061224

level_zero/core/test/unit_tests/sources/memory/test_memory.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2794,5 +2794,87 @@ TEST_F(SharedAllocMultiDeviceTests, whenAllocatinSharedMemoryWithNonNullDeviceIn
27942794
EXPECT_EQ(res, ZE_RESULT_SUCCESS);
27952795
}
27962796

2797+
struct MemAllocMultiSubDeviceTests : public ::testing::Test {
2798+
void SetUp() override {
2799+
NEO::MockCompilerEnableGuard mock(true);
2800+
DebugManager.flags.CreateMultipleSubDevices.set(numSubDevices);
2801+
auto executionEnvironment = new NEO::ExecutionEnvironment;
2802+
auto devices = NEO::DeviceFactory::createDevices(*executionEnvironment);
2803+
driverHandle = std::make_unique<DriverHandleImp>();
2804+
ze_result_t res = driverHandle->initialize(std::move(devices));
2805+
EXPECT_EQ(ZE_RESULT_SUCCESS, res);
2806+
prevSvmAllocsManager = driverHandle->svmAllocsManager;
2807+
currSvmAllocsManager = new SVMAllocsManagerSharedAllocMultiDeviceMock(driverHandle->memoryManager);
2808+
driverHandle->svmAllocsManager = currSvmAllocsManager;
2809+
2810+
context = std::make_unique<ContextMultiDeviceMock>(driverHandle.get());
2811+
EXPECT_NE(context, nullptr);
2812+
2813+
for (uint32_t i = 0; i < numRootDevices; i++) {
2814+
auto device = driverHandle->devices[i];
2815+
context->getDevices().insert(std::make_pair(device->toHandle(), device));
2816+
auto neoDevice = device->getNEODevice();
2817+
context->rootDeviceIndices.insert(neoDevice->getRootDeviceIndex());
2818+
context->deviceBitfields.insert({neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()});
2819+
}
2820+
}
2821+
2822+
void TearDown() override {
2823+
driverHandle->svmAllocsManager = prevSvmAllocsManager;
2824+
delete currSvmAllocsManager;
2825+
}
2826+
2827+
DebugManagerStateRestore restorer;
2828+
NEO::SVMAllocsManager *prevSvmAllocsManager;
2829+
SVMAllocsManagerSharedAllocMultiDeviceMock *currSvmAllocsManager;
2830+
std::unique_ptr<DriverHandleImp> driverHandle;
2831+
std::unique_ptr<ContextMultiDeviceMock> context;
2832+
const uint32_t numSubDevices = 2u;
2833+
const uint32_t numRootDevices = 1u;
2834+
};
2835+
2836+
TEST_F(MemAllocMultiSubDeviceTests, whenAllocatingDeviceMemorySubDeviceMemorySizeUsedWhenImplicitScalingDisabled) {
2837+
ze_device_mem_alloc_desc_t deviceDesc = {};
2838+
void *ptr = nullptr;
2839+
size_t size = driverHandle->devices[0]->getNEODevice()->getDeviceInfo().globalMemSize;
2840+
deviceDesc.stype = ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC;
2841+
ze_relaxed_allocation_limits_exp_desc_t relaxedSizeDesc = {};
2842+
relaxedSizeDesc.stype = ZE_STRUCTURE_TYPE_RELAXED_ALLOCATION_LIMITS_EXP_DESC;
2843+
relaxedSizeDesc.flags = ZE_RELAXED_ALLOCATION_LIMITS_EXP_FLAG_MAX_SIZE;
2844+
deviceDesc.pNext = &relaxedSizeDesc;
2845+
2846+
DebugManager.flags.EnableWalkerPartition.set(0);
2847+
2848+
ze_result_t res = context->allocDeviceMem(driverHandle->devices[0]->toHandle(), &deviceDesc, size, 0u, &ptr);
2849+
EXPECT_EQ(res, ZE_RESULT_ERROR_UNSUPPORTED_SIZE);
2850+
2851+
DebugManager.flags.EnableWalkerPartition.set(1);
2852+
2853+
res = context->allocDeviceMem(driverHandle->devices[0]->toHandle(), &deviceDesc, size, 0u, &ptr);
2854+
EXPECT_EQ(res, ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY);
2855+
}
2856+
2857+
TEST_F(MemAllocMultiSubDeviceTests, whenAllocatingSharedMemorySubDeviceMemorySizeUsedWhenImplicitScalingDisabled) {
2858+
ze_device_mem_alloc_desc_t deviceDesc = {};
2859+
ze_host_mem_alloc_desc_t hostDesc = {};
2860+
void *ptr = nullptr;
2861+
size_t size = driverHandle->devices[0]->getNEODevice()->getDeviceInfo().globalMemSize;
2862+
deviceDesc.stype = ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC;
2863+
ze_relaxed_allocation_limits_exp_desc_t relaxedSizeDesc = {};
2864+
relaxedSizeDesc.stype = ZE_STRUCTURE_TYPE_RELAXED_ALLOCATION_LIMITS_EXP_DESC;
2865+
relaxedSizeDesc.flags = ZE_RELAXED_ALLOCATION_LIMITS_EXP_FLAG_MAX_SIZE;
2866+
deviceDesc.pNext = &relaxedSizeDesc;
2867+
2868+
DebugManager.flags.EnableWalkerPartition.set(0);
2869+
2870+
ze_result_t res = context->allocSharedMem(driverHandle->devices[0]->toHandle(), &deviceDesc, &hostDesc, size, 0u, &ptr);
2871+
EXPECT_EQ(res, ZE_RESULT_ERROR_UNSUPPORTED_SIZE);
2872+
2873+
DebugManager.flags.EnableWalkerPartition.set(1);
2874+
2875+
res = context->allocSharedMem(driverHandle->devices[0]->toHandle(), &deviceDesc, &hostDesc, size, 0u, &ptr);
2876+
EXPECT_EQ(res, ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY);
2877+
}
2878+
27972879
} // namespace ult
27982880
} // namespace L0

0 commit comments

Comments
 (0)