Skip to content

Commit cace491

Browse files
committed
[UR][Offload] Use new olMemInfo systems
We no longer track allocations, and `olMemInfo` is implemented fully.
1 parent 61de220 commit cace491

File tree

4 files changed

+81
-39
lines changed

4 files changed

+81
-39
lines changed

unified-runtime/source/adapters/offload/context.hpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,15 @@ struct ur_context_handle_t_ : RefCounted {
2929
~ur_context_handle_t_() { urDeviceRelease(Device); }
3030

3131
ur_device_handle_t Device;
32-
std::unordered_map<void *, alloc_info_t> AllocTypeMap;
3332

34-
std::optional<alloc_info_t> getAllocType(const void *UsmPtr) {
35-
for (auto &pair : AllocTypeMap) {
36-
if (UsmPtr >= pair.first &&
37-
reinterpret_cast<uintptr_t>(UsmPtr) <
38-
reinterpret_cast<uintptr_t>(pair.first) + pair.second.Size) {
39-
return pair.second;
40-
}
33+
ol_result_t getAllocType(const void *UsmPtr, ol_alloc_type_t &Type) {
34+
auto Err = olGetMemInfo(Device->Platform->OffloadPlatform, UsmPtr,
35+
OL_MEM_INFO_TYPE, sizeof(Type), &Type);
36+
if (Err && Err->Code == OL_ERRC_NOT_FOUND) {
37+
// Treat unknown allocations as host
38+
Type = OL_ALLOC_TYPE_HOST;
39+
return OL_SUCCESS;
4140
}
42-
return std::nullopt;
41+
return Err;
4342
}
4443
};

unified-runtime/source/adapters/offload/enqueue.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -440,17 +440,18 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMMemcpy(
440440
ur_queue_handle_t hQueue, bool blocking, void *pDst, const void *pSrc,
441441
size_t size, uint32_t numEventsInWaitList,
442442
const ur_event_handle_t *phEventWaitList, ur_event_handle_t *phEvent) {
443-
auto GetDevice = [&](const void *Ptr) {
444-
auto Res = hQueue->UrContext->getAllocType(Ptr);
445-
if (!Res)
446-
return Adapter->HostDevice;
447-
return Res->Type == OL_ALLOC_TYPE_HOST ? Adapter->HostDevice
448-
: hQueue->OffloadDevice;
449-
};
450-
451-
return doMemcpy(UR_COMMAND_USM_MEMCPY, hQueue, pDst, GetDevice(pDst), pSrc,
452-
GetDevice(pSrc), size, blocking, numEventsInWaitList,
453-
phEventWaitList, phEvent);
443+
ol_alloc_type_t DstTy;
444+
OL_RETURN_ON_ERR(hQueue->UrContext->getAllocType(pDst, DstTy));
445+
ol_device_handle_t Dst =
446+
DstTy == OL_ALLOC_TYPE_HOST ? Adapter->HostDevice : hQueue->OffloadDevice;
447+
448+
ol_alloc_type_t SrcTy;
449+
OL_RETURN_ON_ERR(hQueue->UrContext->getAllocType(pDst, SrcTy));
450+
ol_device_handle_t Src =
451+
SrcTy == OL_ALLOC_TYPE_HOST ? Adapter->HostDevice : hQueue->OffloadDevice;
452+
453+
return doMemcpy(UR_COMMAND_USM_MEMCPY, hQueue, pDst, Dst, pSrc, Src, size,
454+
blocking, numEventsInWaitList, phEventWaitList, phEvent);
454455

455456
return UR_RESULT_SUCCESS;
456457
}

unified-runtime/source/adapters/offload/memory.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemRelease(ur_mem_handle_t hMem) {
8080
// Subbuffers should not free their parents
8181
if (!BufferImpl->Parent) {
8282
// TODO: Handle registered host memory
83-
OL_RETURN_ON_ERR(olMemFree(BufferImpl->Ptr));
83+
OL_RETURN_ON_ERR(olMemFree(
84+
hMem->Context->Device->Platform->OffloadPlatform, BufferImpl->Ptr));
8485
} else {
8586
return urMemRelease(BufferImpl->Parent);
8687
}

unified-runtime/source/adapters/offload/usm.cpp

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urUSMHostAlloc(ur_context_handle_t hContext,
2222
size_t size, void **ppMem) {
2323
OL_RETURN_ON_ERR(olMemAlloc(hContext->Device->OffloadDevice,
2424
OL_ALLOC_TYPE_HOST, size, ppMem));
25-
26-
hContext->AllocTypeMap.insert_or_assign(
27-
*ppMem, alloc_info_t{OL_ALLOC_TYPE_HOST, size});
2825
return UR_RESULT_SUCCESS;
2926
}
3027

@@ -33,9 +30,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urUSMDeviceAlloc(
3330
ur_usm_pool_handle_t, size_t size, void **ppMem) {
3431
OL_RETURN_ON_ERR(olMemAlloc(hContext->Device->OffloadDevice,
3532
OL_ALLOC_TYPE_DEVICE, size, ppMem));
36-
37-
hContext->AllocTypeMap.insert_or_assign(
38-
*ppMem, alloc_info_t{OL_ALLOC_TYPE_DEVICE, size});
3933
return UR_RESULT_SUCCESS;
4034
}
4135

@@ -44,23 +38,70 @@ UR_APIEXPORT ur_result_t UR_APICALL urUSMSharedAlloc(
4438
ur_usm_pool_handle_t, size_t size, void **ppMem) {
4539
OL_RETURN_ON_ERR(olMemAlloc(hContext->Device->OffloadDevice,
4640
OL_ALLOC_TYPE_MANAGED, size, ppMem));
47-
48-
hContext->AllocTypeMap.insert_or_assign(
49-
*ppMem, alloc_info_t{OL_ALLOC_TYPE_MANAGED, size});
5041
return UR_RESULT_SUCCESS;
5142
}
5243

5344
UR_APIEXPORT ur_result_t UR_APICALL urUSMFree(ur_context_handle_t hContext,
5445
void *pMem) {
55-
hContext->AllocTypeMap.erase(pMem);
56-
return offloadResultToUR(olMemFree(pMem));
46+
return offloadResultToUR(
47+
olMemFree(hContext->Device->Platform->OffloadPlatform, pMem));
5748
}
5849

59-
UR_APIEXPORT ur_result_t UR_APICALL urUSMGetMemAllocInfo(
60-
[[maybe_unused]] ur_context_handle_t hContext,
61-
[[maybe_unused]] const void *pMem,
62-
[[maybe_unused]] ur_usm_alloc_info_t propName,
63-
[[maybe_unused]] size_t propSize, [[maybe_unused]] void *pPropValue,
64-
[[maybe_unused]] size_t *pPropSizeRet) {
65-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
50+
UR_APIEXPORT ur_result_t UR_APICALL
51+
urUSMGetMemAllocInfo(ur_context_handle_t hContext, const void *pMem,
52+
ur_usm_alloc_info_t propName, size_t propSize,
53+
void *pPropValue, size_t *pPropSizeRet) {
54+
UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet);
55+
56+
auto Platform = hContext->Device->Platform->OffloadPlatform;
57+
ol_mem_info_t olInfo;
58+
59+
switch (propName) {
60+
case UR_USM_ALLOC_INFO_TYPE:
61+
olInfo = OL_MEM_INFO_TYPE;
62+
break;
63+
case UR_USM_ALLOC_INFO_BASE_PTR:
64+
olInfo = OL_MEM_INFO_BASE;
65+
break;
66+
case UR_USM_ALLOC_INFO_SIZE:
67+
olInfo = OL_MEM_INFO_SIZE;
68+
break;
69+
case UR_USM_ALLOC_INFO_DEVICE:
70+
// Contexts can only contain one device
71+
return ReturnValue(hContext->Device);
72+
case UR_USM_ALLOC_INFO_POOL:
73+
default:
74+
return UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION;
75+
break;
76+
}
77+
78+
if (pPropSizeRet) {
79+
OL_RETURN_ON_ERR(olGetMemInfoSize(Platform, pMem, olInfo, pPropSizeRet));
80+
}
81+
82+
if (pPropValue) {
83+
OL_RETURN_ON_ERR(
84+
olGetMemInfo(Platform, pMem, olInfo, propSize, pPropValue));
85+
86+
if (propName == UR_USM_ALLOC_INFO_TYPE) {
87+
auto *OlType = reinterpret_cast<ol_alloc_type_t *>(pPropValue);
88+
auto *UrType = reinterpret_cast<ur_usm_type_t *>(pPropValue);
89+
switch (*OlType) {
90+
case OL_ALLOC_TYPE_HOST:
91+
*UrType = UR_USM_TYPE_HOST;
92+
break;
93+
case OL_ALLOC_TYPE_DEVICE:
94+
*UrType = UR_USM_TYPE_DEVICE;
95+
break;
96+
case OL_ALLOC_TYPE_MANAGED:
97+
*UrType = UR_USM_TYPE_SHARED;
98+
break;
99+
default:
100+
*UrType = UR_USM_TYPE_UNKNOWN;
101+
break;
102+
}
103+
}
104+
}
105+
106+
return UR_RESULT_SUCCESS;
66107
}

0 commit comments

Comments
 (0)