Skip to content

Commit 60ad384

Browse files
author
Jaime Arteaga
committed
[UR][L0] Add several fixes to L0 adapter for test-usm
Signed-off-by: Jaime Arteaga <[email protected]>
1 parent 109ed46 commit 60ad384

File tree

4 files changed

+85
-49
lines changed

4 files changed

+85
-49
lines changed

source/adapters/level_zero/context.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ struct ur_context_handle_t_ : _ur_object {
115115
SharedReadOnlyMemProxyPools;
116116
umf::pool_unique_handle_t HostMemProxyPool;
117117

118+
// Map associating pools created with urUsmPoolCreate and internal pools
119+
std::list<ur_usm_pool_handle_t> UsmPoolHandles{};
120+
118121
// We need to store all memory allocations in the context because there could
119122
// be kernels with indirect access. Kernels with indirect access start to
120123
// reference all existing memory allocations at the time when they are

source/adapters/level_zero/usm.cpp

Lines changed: 80 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,15 @@ static ur_result_t USMDeviceAllocImpl(void **ResultPtr,
185185
ZeDesc.pNext = &RelaxedDesc;
186186
}
187187

188-
ZE2UR_CALL(zeMemAllocDevice, (Context->ZeContext, &ZeDesc, Size, Alignment,
189-
Device->ZeDevice, ResultPtr));
188+
ze_result_t ZeResult =
189+
zeMemAllocDevice(Context->ZeContext, &ZeDesc, Size, Alignment,
190+
Device->ZeDevice, ResultPtr);
191+
if (ZeResult != ZE_RESULT_SUCCESS) {
192+
if (ZeResult == ZE_RESULT_ERROR_UNSUPPORTED_SIZE) {
193+
return UR_RESULT_ERROR_INVALID_USM_SIZE;
194+
}
195+
return ze2urResult(ZeResult);
196+
}
190197

191198
UR_ASSERT(Alignment == 0 ||
192199
reinterpret_cast<std::uintptr_t>(*ResultPtr) % Alignment == 0,
@@ -224,8 +231,15 @@ static ur_result_t USMSharedAllocImpl(void **ResultPtr,
224231
ZeDevDesc.pNext = &RelaxedDesc;
225232
}
226233

227-
ZE2UR_CALL(zeMemAllocShared, (Context->ZeContext, &ZeDevDesc, &ZeHostDesc,
228-
Size, Alignment, Device->ZeDevice, ResultPtr));
234+
ze_result_t ZeResult =
235+
zeMemAllocShared(Context->ZeContext, &ZeDevDesc, &ZeHostDesc, Size,
236+
Alignment, Device->ZeDevice, ResultPtr);
237+
if (ZeResult != ZE_RESULT_SUCCESS) {
238+
if (ZeResult == ZE_RESULT_ERROR_UNSUPPORTED_SIZE) {
239+
return UR_RESULT_ERROR_INVALID_USM_SIZE;
240+
}
241+
return ze2urResult(ZeResult);
242+
}
229243

230244
UR_ASSERT(Alignment == 0 ||
231245
reinterpret_cast<std::uintptr_t>(*ResultPtr) % Alignment == 0,
@@ -252,8 +266,14 @@ static ur_result_t USMHostAllocImpl(void **ResultPtr,
252266
// TODO: translate PI properties to Level Zero flags
253267
ZeStruct<ze_host_mem_alloc_desc_t> ZeHostDesc;
254268
ZeHostDesc.flags = 0;
255-
ZE2UR_CALL(zeMemAllocHost,
256-
(Context->ZeContext, &ZeHostDesc, Size, Alignment, ResultPtr));
269+
ze_result_t ZeResult = zeMemAllocHost(Context->ZeContext, &ZeHostDesc, Size,
270+
Alignment, ResultPtr);
271+
if (ZeResult != ZE_RESULT_SUCCESS) {
272+
if (ZeResult == ZE_RESULT_ERROR_UNSUPPORTED_SIZE) {
273+
return UR_RESULT_ERROR_INVALID_USM_SIZE;
274+
}
275+
return ze2urResult(ZeResult);
276+
}
257277

258278
UR_ASSERT(Alignment == 0 ||
259279
reinterpret_cast<std::uintptr_t>(*ResultPtr) % Alignment == 0,
@@ -597,6 +617,40 @@ UR_APIEXPORT ur_result_t UR_APICALL urUSMGetMemAllocInfo(
597617
ZE2UR_CALL(zeMemGetAddressRange, (Context->ZeContext, Ptr, nullptr, &Size));
598618
return ReturnValue(Size);
599619
}
620+
case UR_USM_ALLOC_INFO_POOL: {
621+
auto UMFPool = umfPoolByPtr(Ptr);
622+
if (!UMFPool) {
623+
return UR_RESULT_ERROR_INVALID_VALUE;
624+
}
625+
626+
std::shared_lock<ur_shared_mutex> ContextLock(Context->Mutex);
627+
628+
auto SearchMatchingPool =
629+
[](std::unordered_map<ur_device_handle_t, umf::pool_unique_handle_t>
630+
&PoolMap,
631+
umf_memory_pool_handle_t UMFPool) {
632+
for (auto &PoolPair : PoolMap) {
633+
if (PoolPair.second.get() == UMFPool) {
634+
return true;
635+
}
636+
}
637+
return false;
638+
};
639+
640+
for (auto &Pool : Context->UsmPoolHandles) {
641+
if (SearchMatchingPool(Pool->DeviceMemPools, UMFPool)) {
642+
return ReturnValue(Pool);
643+
}
644+
if (SearchMatchingPool(Pool->SharedMemPools, UMFPool)) {
645+
return ReturnValue(Pool);
646+
}
647+
if (Pool->HostMemPool.get() == UMFPool) {
648+
return ReturnValue(Pool);
649+
}
650+
}
651+
652+
return UR_RESULT_ERROR_INVALID_VALUE;
653+
}
600654
default:
601655
urPrint("urUSMGetMemAllocInfo: unsupported ParamName\n");
602656
return UR_RESULT_ERROR_INVALID_VALUE;
@@ -746,6 +800,7 @@ ur_result_t L0HostMemoryProvider::allocateImpl(void **ResultPtr, size_t Size,
746800
ur_usm_pool_handle_t_::ur_usm_pool_handle_t_(ur_context_handle_t Context,
747801
ur_usm_pool_desc_t *PoolDesc) {
748802

803+
this->Context = Context;
749804
zeroInit = static_cast<uint32_t>(PoolDesc->flags &
750805
UR_USM_POOL_FLAG_ZERO_INITIALIZE_BLOCK);
751806

@@ -829,6 +884,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urUSMPoolCreate(
829884
try {
830885
*Pool = reinterpret_cast<ur_usm_pool_handle_t>(
831886
new ur_usm_pool_handle_t_(Context, PoolDesc));
887+
888+
std::shared_lock<ur_shared_mutex> ContextLock(Context->Mutex);
889+
Context->UsmPoolHandles.insert(Context->UsmPoolHandles.cend(), *Pool);
890+
832891
} catch (const UsmAllocationException &Ex) {
833892
return Ex.getError();
834893
}
@@ -846,6 +905,8 @@ ur_result_t
846905
urUSMPoolRelease(ur_usm_pool_handle_t Pool ///< [in] pointer to USM memory pool
847906
) {
848907
if (Pool->RefCount.decrementAndTest()) {
908+
std::shared_lock<ur_shared_mutex> ContextLock(Pool->Context->Mutex);
909+
Pool->Context->UsmPoolHandles.remove(Pool);
849910
delete Pool;
850911
}
851912
return UR_RESULT_SUCCESS;
@@ -859,13 +920,19 @@ ur_result_t urUSMPoolGetInfo(
859920
///< property
860921
size_t *PropSizeRet ///< [out] size in bytes returned in pool property value
861922
) {
862-
std::ignore = Pool;
863-
std::ignore = PropName;
864-
std::ignore = PropSize;
865-
std::ignore = PropValue;
866-
std::ignore = PropSizeRet;
867-
urPrint("[UR][L0] %s function not implemented!\n", __FUNCTION__);
868-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
923+
UrReturnHelper ReturnValue(PropSize, PropValue, PropSizeRet);
924+
925+
switch (PropName) {
926+
case UR_USM_POOL_INFO_REFERENCE_COUNT: {
927+
return ReturnValue(Pool->RefCount.load());
928+
}
929+
case UR_USM_POOL_INFO_CONTEXT: {
930+
return ReturnValue(Pool->Context);
931+
}
932+
default: {
933+
return UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION;
934+
}
935+
}
869936
}
870937

871938
// If indirect access tracking is not enabled then this functions just performs

source/adapters/level_zero/usm.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ struct ur_usm_pool_handle_t_ : _ur_object {
2929
SharedReadOnlyMemPools;
3030
umf::pool_unique_handle_t HostMemPool;
3131

32+
ur_context_handle_t Context{};
33+
3234
ur_usm_pool_handle_t_(ur_context_handle_t Context,
3335
ur_usm_pool_desc_t *PoolDesc);
3436
};
Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +0,0 @@
1-
urUSMDeviceAllocTest.Success/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UsePoolEnabled
2-
urUSMDeviceAllocTest.Success/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UsePoolDisabled
3-
urUSMDeviceAllocTest.SuccessWithDescriptors/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UsePoolEnabled
4-
urUSMDeviceAllocTest.SuccessWithDescriptors/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UsePoolDisabled
5-
urUSMDeviceAllocTest.InvalidUSMSize/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UsePoolEnabled
6-
urUSMDeviceAllocTest.InvalidUSMSize/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UsePoolDisabled
7-
urUSMFreeTest.SuccessDeviceAlloc/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}_
8-
urUSMFreeTest.SuccessHostAlloc/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}_
9-
urUSMFreeTest.SuccessSharedAlloc/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}_
10-
urUSMAllocInfoTest.Success/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UR_USM_ALLOC_INFO_TYPE
11-
urUSMAllocInfoTest.Success/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UR_USM_ALLOC_INFO_BASE_PTR
12-
urUSMAllocInfoTest.Success/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UR_USM_ALLOC_INFO_SIZE
13-
urUSMAllocInfoTest.Success/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UR_USM_ALLOC_INFO_DEVICE
14-
urUSMAllocInfoTest.Success/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UR_USM_ALLOC_INFO_POOL
15-
urUSMGetMemAllocInfoTest.InvalidNullHandleContext/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}_
16-
urUSMGetMemAllocInfoTest.InvalidNullPointerMem/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}_
17-
urUSMGetMemAllocInfoTest.InvalidEnumeration/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}_
18-
urUSMGetMemAllocInfoTest.InvalidValuePropSize/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}_
19-
urUSMHostAllocTest.Success/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UsePoolEnabled
20-
urUSMHostAllocTest.Success/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UsePoolDisabled
21-
urUSMHostAllocTest.SuccessWithDescriptors/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UsePoolEnabled
22-
urUSMHostAllocTest.SuccessWithDescriptors/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UsePoolDisabled
23-
urUSMHostAllocTest.InvalidUSMSize/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UsePoolEnabled
24-
urUSMHostAllocTest.InvalidUSMSize/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UsePoolDisabled
25-
urUSMPoolGetInfoTestWithInfoParam.Success/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UR_USM_POOL_INFO_CONTEXT
26-
urUSMPoolGetInfoTestWithInfoParam.Success/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UR_USM_POOL_INFO_REFERENCE_COUNT
27-
urUSMPoolGetInfoTest.InvalidSizeTooSmall/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}_
28-
urUSMPoolRetainTest.Success/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}_
29-
urUSMSharedAllocTest.Success/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UsePoolEnabled
30-
urUSMSharedAllocTest.Success/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UsePoolDisabled
31-
urUSMSharedAllocTest.SuccessWithDescriptors/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UsePoolEnabled
32-
urUSMSharedAllocTest.SuccessWithDescriptors/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UsePoolDisabled
33-
urUSMSharedAllocTest.SuccessWithMultipleAdvices/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UsePoolEnabled
34-
urUSMSharedAllocTest.SuccessWithMultipleAdvices/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UsePoolDisabled
35-
urUSMSharedAllocTest.InvalidUSMSize/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UsePoolEnabled
36-
urUSMSharedAllocTest.InvalidUSMSize/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}___UsePoolDisabled

0 commit comments

Comments
 (0)