Skip to content

Commit 3a91bcf

Browse files
Jaime ArteagaCompute-Runtime-Automation
authored andcommitted
Limit allocations to available global mem size
Signed-off-by: Jaime Arteaga <jaime.a.arteaga.molina@intel.com>
1 parent 52bc464 commit 3a91bcf

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

level_zero/core/source/context/context_imp.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ ze_result_t ContextImp::allocDeviceMem(ze_device_handle_t hDevice,
142142
return ZE_RESULT_ERROR_UNSUPPORTED_SIZE;
143143
}
144144

145+
if (relaxedSizeAllowed &&
146+
(size > this->driverHandle->devices[0]->getNEODevice()->getDeviceInfo().globalMemSize)) {
147+
*ptr = nullptr;
148+
return ZE_RESULT_ERROR_UNSUPPORTED_SIZE;
149+
}
150+
145151
auto neoDevice = Device::fromHandle(hDevice)->getNEODevice();
146152
auto rootDeviceIndex = neoDevice->getRootDeviceIndex();
147153
auto deviceBitfields = this->driverHandle->deviceBitfields;
@@ -191,6 +197,12 @@ ze_result_t ContextImp::allocSharedMem(ze_device_handle_t hDevice,
191197
return ZE_RESULT_ERROR_UNSUPPORTED_SIZE;
192198
}
193199

200+
if (relaxedSizeAllowed &&
201+
(size > this->driverHandle->devices[0]->getNEODevice()->getDeviceInfo().globalMemSize)) {
202+
*ptr = nullptr;
203+
return ZE_RESULT_ERROR_UNSUPPORTED_SIZE;
204+
}
205+
194206
auto neoDevice = this->devices.begin()->second->getNEODevice();
195207

196208
auto deviceBitfields = this->deviceBitfields;

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,25 @@ TEST_F(MemoryRelaxedSizeTests,
362362
EXPECT_EQ(ZE_RESULT_SUCCESS, result);
363363
}
364364

365+
TEST_F(MemoryRelaxedSizeTests,
366+
givenCallToDeviceAllocWithLargerThanGlobalMemSizeAndRelaxedFlagThenAllocationIsNotMade) {
367+
size_t size = device->getNEODevice()->getDeviceInfo().globalMemSize + 1;
368+
size_t alignment = 1u;
369+
void *ptr = nullptr;
370+
371+
ze_device_mem_alloc_desc_t deviceDesc = {};
372+
deviceDesc.stype = ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC;
373+
ze_relaxed_allocation_limits_exp_desc_t relaxedSizeDesc = {};
374+
relaxedSizeDesc.stype = ZE_STRUCTURE_TYPE_RELAXED_ALLOCATION_LIMITS_EXP_DESC;
375+
relaxedSizeDesc.flags = ZE_RELAXED_ALLOCATION_LIMITS_EXP_FLAG_MAX_SIZE;
376+
deviceDesc.pNext = &relaxedSizeDesc;
377+
ze_result_t result = context->allocDeviceMem(device->toHandle(),
378+
&deviceDesc,
379+
size, alignment, &ptr);
380+
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_SIZE, result);
381+
EXPECT_EQ(nullptr, ptr);
382+
}
383+
365384
TEST_F(MemoryRelaxedSizeTests,
366385
givenCallToDeviceAllocWithLargerThanAllowedSizeAndRelaxedFlagWithIncorrectFlagThenAllocationIsNotMade) {
367386
size_t size = device->getNEODevice()->getHardwareCapabilities().maxMemAllocSize + 1;
@@ -459,6 +478,27 @@ TEST_F(MemoryRelaxedSizeTests,
459478
EXPECT_EQ(ZE_RESULT_SUCCESS, result);
460479
}
461480

481+
TEST_F(MemoryRelaxedSizeTests,
482+
givenCallToSharedAllocWithLargerThanGlobalMemSizeAndRelaxedFlagThenAllocationIsNotMade) {
483+
size_t size = device->getNEODevice()->getDeviceInfo().globalMemSize + 1;
484+
size_t alignment = 1u;
485+
void *ptr = nullptr;
486+
487+
ze_device_mem_alloc_desc_t deviceDesc = {};
488+
deviceDesc.stype = ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC;
489+
ze_relaxed_allocation_limits_exp_desc_t relaxedSizeDesc = {};
490+
relaxedSizeDesc.stype = ZE_STRUCTURE_TYPE_RELAXED_ALLOCATION_LIMITS_EXP_DESC;
491+
relaxedSizeDesc.flags = ZE_RELAXED_ALLOCATION_LIMITS_EXP_FLAG_MAX_SIZE;
492+
deviceDesc.pNext = &relaxedSizeDesc;
493+
ze_host_mem_alloc_desc_t hostDesc = {};
494+
ze_result_t result = context->allocSharedMem(device->toHandle(),
495+
&deviceDesc,
496+
&hostDesc,
497+
size, alignment, &ptr);
498+
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_SIZE, result);
499+
EXPECT_EQ(nullptr, ptr);
500+
}
501+
462502
TEST_F(MemoryRelaxedSizeTests,
463503
givenCallToSharedAllocWithLargerThanAllowedSizeAndRelaxedFlagWithIncorrectFlagThenAllocationIsNotMade) {
464504
size_t size = device->getNEODevice()->getHardwareCapabilities().maxMemAllocSize + 1;

0 commit comments

Comments
 (0)