Skip to content

Commit b20a877

Browse files
Georgi Mirazchiyskikbenzie
authored andcommitted
Update urEnqueueUSMAdvise entry point to always return a valid event
1 parent c1ae337 commit b20a877

File tree

1 file changed

+45
-36
lines changed

1 file changed

+45
-36
lines changed

source/adapters/hip/enqueue.cpp

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,37 +1532,6 @@ urEnqueueUSMAdvise(ur_queue_handle_t hQueue, const void *pMem, size_t size,
15321532
void *HIPDevicePtr = const_cast<void *>(pMem);
15331533
ur_device_handle_t Device = hQueue->getContext()->getDevice();
15341534

1535-
// If the device does not support managed memory access, we can't set
1536-
// mem_advise.
1537-
if (!getAttribute(Device, hipDeviceAttributeManagedMemory)) {
1538-
setErrorMessage("mem_advise ignored as device does not support "
1539-
" managed memory access",
1540-
UR_RESULT_SUCCESS);
1541-
return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
1542-
}
1543-
1544-
// Passing MEM_ADVICE_SET/MEM_ADVICE_CLEAR_PREFERRED_LOCATION to hipMemAdvise
1545-
// on a GPU device requires the GPU device to report a non-zero value for
1546-
// hipDeviceAttributeConcurrentManagedAccess. Therefore, ignore the mem advice
1547-
// if concurrent managed memory access is not available.
1548-
if (advice & (UR_USM_ADVICE_FLAG_SET_PREFERRED_LOCATION |
1549-
UR_USM_ADVICE_FLAG_CLEAR_PREFERRED_LOCATION |
1550-
UR_USM_ADVICE_FLAG_SET_ACCESSED_BY_DEVICE |
1551-
UR_USM_ADVICE_FLAG_CLEAR_ACCESSED_BY_DEVICE |
1552-
UR_USM_ADVICE_FLAG_DEFAULT)) {
1553-
if (!getAttribute(Device, hipDeviceAttributeConcurrentManagedAccess)) {
1554-
setErrorMessage("mem_advise ignored as device does not support "
1555-
"concurrent managed access",
1556-
UR_RESULT_SUCCESS);
1557-
return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
1558-
}
1559-
1560-
// TODO: If pMem points to valid system-allocated pageable memory, we should
1561-
// check that the device also has the hipDeviceAttributePageableMemoryAccess
1562-
// property, so that a valid read-only copy can be created on the device.
1563-
// This also applies for UR_USM_MEM_ADVICE_SET/MEM_ADVICE_CLEAR_READ_MOSTLY.
1564-
}
1565-
15661535
#if HIP_VERSION_MAJOR >= 5
15671536
// NOTE: The hipPointerGetAttribute API is marked as beta, meaning, while this
15681537
// is feature complete, it is still open to changes and outstanding issues.
@@ -1574,10 +1543,10 @@ urEnqueueUSMAdvise(ur_queue_handle_t hQueue, const void *pMem, size_t size,
15741543
#endif
15751544

15761545
ur_result_t Result = UR_RESULT_SUCCESS;
1577-
std::unique_ptr<ur_event_handle_t_> EventPtr{nullptr};
15781546

15791547
try {
15801548
ScopedContext Active(Device);
1549+
std::unique_ptr<ur_event_handle_t_> EventPtr{nullptr};
15811550

15821551
if (phEvent) {
15831552
EventPtr =
@@ -1586,6 +1555,48 @@ urEnqueueUSMAdvise(ur_queue_handle_t hQueue, const void *pMem, size_t size,
15861555
EventPtr->start();
15871556
}
15881557

1558+
// Helper to ensure returning a valid event on early exit.
1559+
auto releaseEvent = [&EventPtr, &phEvent]() -> void {
1560+
if (phEvent) {
1561+
UR_CHECK_ERROR(EventPtr->record());
1562+
*phEvent = EventPtr.release();
1563+
}
1564+
};
1565+
1566+
// If the device does not support managed memory access, we can't set
1567+
// mem_advise.
1568+
if (!getAttribute(Device, hipDeviceAttributeManagedMemory)) {
1569+
releaseEvent();
1570+
setErrorMessage("mem_advise ignored as device does not support "
1571+
"managed memory access",
1572+
UR_RESULT_SUCCESS);
1573+
return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
1574+
}
1575+
1576+
// Passing MEM_ADVICE_SET/MEM_ADVICE_CLEAR_PREFERRED_LOCATION to
1577+
// hipMemAdvise on a GPU device requires the GPU device to report a non-zero
1578+
// value for hipDeviceAttributeConcurrentManagedAccess. Therefore, ignore
1579+
// the mem advice if concurrent managed memory access is not available.
1580+
if (advice & (UR_USM_ADVICE_FLAG_SET_PREFERRED_LOCATION |
1581+
UR_USM_ADVICE_FLAG_CLEAR_PREFERRED_LOCATION |
1582+
UR_USM_ADVICE_FLAG_SET_ACCESSED_BY_DEVICE |
1583+
UR_USM_ADVICE_FLAG_CLEAR_ACCESSED_BY_DEVICE |
1584+
UR_USM_ADVICE_FLAG_DEFAULT)) {
1585+
if (!getAttribute(Device, hipDeviceAttributeConcurrentManagedAccess)) {
1586+
releaseEvent();
1587+
setErrorMessage("mem_advise ignored as device does not support "
1588+
"concurrent managed access",
1589+
UR_RESULT_SUCCESS);
1590+
return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
1591+
}
1592+
1593+
// TODO: If pMem points to valid system-allocated pageable memory, we
1594+
// should check that the device also has the
1595+
// hipDeviceAttributePageableMemoryAccess property, so that a valid
1596+
// read-only copy can be created on the device. This also applies for
1597+
// UR_USM_MEM_ADVICE_SET/MEM_ADVICE_CLEAR_READ_MOSTLY.
1598+
}
1599+
15891600
const auto DeviceID = Device->get();
15901601
if (advice & UR_USM_ADVICE_FLAG_DEFAULT) {
15911602
UR_CHECK_ERROR(
@@ -1600,17 +1611,15 @@ urEnqueueUSMAdvise(ur_queue_handle_t hQueue, const void *pMem, size_t size,
16001611
// currently unmapped advice arguments as not supported by this platform.
16011612
// Therefore, warn the user instead of throwing and aborting the runtime.
16021613
if (Result == UR_RESULT_ERROR_INVALID_ENUMERATION) {
1614+
releaseEvent();
16031615
setErrorMessage("mem_advise is ignored as the advice argument is not "
16041616
"supported by this device",
16051617
UR_RESULT_SUCCESS);
16061618
return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
16071619
}
16081620
}
16091621

1610-
if (phEvent) {
1611-
Result = EventPtr->record();
1612-
*phEvent = EventPtr.release();
1613-
}
1622+
releaseEvent();
16141623
} catch (ur_result_t err) {
16151624
Result = err;
16161625
} catch (...) {

0 commit comments

Comments
 (0)