Skip to content

Commit fe813f3

Browse files
authored
[SYCL][HIP] Add managed memory USM checks for HIP prefetch (#10761)
Adds `hipDeviceAttributeManagedMemory` attribute check to verify the HIP device supports managed memory in order to use the prefetch API as well as checking the pointer to migrate is actually a managed allocation (USM/SVM). If the check fails we return early and set a warning message (with `UR_RESULT_SUCCESS` as return code). As a follow-up. we may need to detect system-allocated memory and either continue with just a warning message or throw a more targeted error message and exit.
1 parent dc24a47 commit fe813f3

File tree

1 file changed

+25
-0
lines changed
  • sycl/plugins/unified_runtime/ur/adapters/hip

1 file changed

+25
-0
lines changed

sycl/plugins/unified_runtime/ur/adapters/hip/enqueue.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,31 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMPrefetch(
13171317
const ur_event_handle_t *phEventWaitList, ur_event_handle_t *phEvent) {
13181318
#if HIP_VERSION_MAJOR >= 5
13191319
void *HIPDevicePtr = const_cast<void *>(pMem);
1320+
ur_device_handle_t Device = hQueue->getContext()->getDevice();
1321+
1322+
// If the device does not support managed memory access, we can't set
1323+
// mem_advise.
1324+
if (!getAttribute(Device, hipDeviceAttributeManagedMemory)) {
1325+
setErrorMessage("mem_advise ignored as device does not support "
1326+
" managed memory access",
1327+
UR_RESULT_SUCCESS);
1328+
return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
1329+
}
1330+
1331+
hipPointerAttribute_t attribs;
1332+
// TODO: hipPointerGetAttributes will fail if pMem is non-HIP allocated
1333+
// memory, as it is neither registered as host memory, nor into the address
1334+
// space for the current device, meaning the pMem ptr points to a
1335+
// system-allocated memory. This means we may need to check system-alloacted
1336+
// memory and handle the failure more gracefully.
1337+
UR_CHECK_ERROR(hipPointerGetAttributes(&attribs, pMem));
1338+
// async prefetch requires USM pointer (or hip SVM) to work.
1339+
if (!attribs.isManaged) {
1340+
setErrorMessage("Prefetch hint ignored as prefetch only works with USM",
1341+
UR_RESULT_SUCCESS);
1342+
return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
1343+
}
1344+
13201345
unsigned int PointerRangeSize = 0;
13211346
UR_CHECK_ERROR(hipPointerGetAttribute(&PointerRangeSize,
13221347
HIP_POINTER_ATTRIBUTE_RANGE_SIZE,

0 commit comments

Comments
 (0)