@@ -1603,24 +1603,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMPrefetch(
1603
1603
UR_ASSERT (size <= PointerRangeSize, UR_RESULT_ERROR_INVALID_SIZE);
1604
1604
ur_device_handle_t Device = hQueue->getDevice ();
1605
1605
1606
- // Certain cuda devices and Windows do not have support for some Unified
1607
- // Memory features. cuMemPrefetchAsync requires concurrent memory access
1608
- // for managed memory. Therefore, ignore prefetch hint if concurrent managed
1609
- // memory access is not available.
1610
- if (!getAttribute (Device, CU_DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS)) {
1611
- UR_LOG (WARN, " Prefetch hint ignored as device does not support "
1612
- " concurrent managed access." );
1613
- return UR_RESULT_SUCCESS;
1614
- }
1615
-
1616
- unsigned int IsManaged;
1617
- UR_CHECK_ERROR (cuPointerGetAttribute (
1618
- &IsManaged, CU_POINTER_ATTRIBUTE_IS_MANAGED, (CUdeviceptr)pMem));
1619
- if (!IsManaged) {
1620
- UR_LOG (WARN, " Prefetch hint ignored as prefetch only works with USM." );
1621
- return UR_RESULT_SUCCESS;
1622
- }
1623
-
1624
1606
ur_result_t Result = UR_RESULT_SUCCESS;
1625
1607
std::unique_ptr<ur_event_handle_t_> EventPtr{nullptr };
1626
1608
@@ -1635,12 +1617,35 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMPrefetch(
1635
1617
UR_COMMAND_MEM_BUFFER_COPY, hQueue, CuStream));
1636
1618
UR_CHECK_ERROR (EventPtr->start ());
1637
1619
}
1620
+
1621
+ // Ensure we release the event even on early exit
1622
+ OnScopeExit ReleaseEvent ([&]() {
1623
+ if (phEvent) {
1624
+ UR_CHECK_ERROR (EventPtr->record ());
1625
+ *phEvent = EventPtr.release ();
1626
+ }
1627
+ });
1628
+
1629
+ // Certain cuda devices and Windows do not have support for some Unified
1630
+ // Memory features. cuMemPrefetchAsync requires concurrent memory access
1631
+ // for managed memory. Therefore, ignore prefetch hint if concurrent managed
1632
+ // memory access is not available.
1633
+ if (!getAttribute (Device, CU_DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS)) {
1634
+ UR_LOG (WARN, " Prefetch hint ignored as device does not support "
1635
+ " concurrent managed access." );
1636
+ return UR_RESULT_SUCCESS;
1637
+ }
1638
+
1639
+ unsigned int IsManaged;
1640
+ UR_CHECK_ERROR (cuPointerGetAttribute (
1641
+ &IsManaged, CU_POINTER_ATTRIBUTE_IS_MANAGED, (CUdeviceptr)pMem));
1642
+ if (!IsManaged) {
1643
+ UR_LOG (WARN, " Prefetch hint ignored as prefetch only works with USM." );
1644
+ return UR_RESULT_SUCCESS;
1645
+ }
1646
+
1638
1647
UR_CHECK_ERROR (
1639
1648
cuMemPrefetchAsync ((CUdeviceptr)pMem, size, Device->get (), CuStream));
1640
- if (phEvent) {
1641
- UR_CHECK_ERROR (EventPtr->record ());
1642
- *phEvent = EventPtr.release ();
1643
- }
1644
1649
} catch (ur_result_t Err) {
1645
1650
Result = Err;
1646
1651
}
@@ -1656,37 +1661,6 @@ urEnqueueUSMAdvise(ur_queue_handle_t hQueue, const void *pMem, size_t size,
1656
1661
&PointerRangeSize, CU_POINTER_ATTRIBUTE_RANGE_SIZE, (CUdeviceptr)pMem));
1657
1662
UR_ASSERT (size <= PointerRangeSize, UR_RESULT_ERROR_INVALID_SIZE);
1658
1663
1659
- // Certain cuda devices and Windows do not have support for some Unified
1660
- // Memory features. Passing CU_MEM_ADVISE_SET/CLEAR_PREFERRED_LOCATION and
1661
- // to cuMemAdvise on a GPU device requires the GPU device to report a non-zero
1662
- // value for CU_DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS. Therfore, ignore
1663
- // memory advise if concurrent managed memory access is not available.
1664
- if ((advice & UR_USM_ADVICE_FLAG_SET_PREFERRED_LOCATION) ||
1665
- (advice & UR_USM_ADVICE_FLAG_CLEAR_PREFERRED_LOCATION) ||
1666
- (advice & UR_USM_ADVICE_FLAG_SET_ACCESSED_BY_DEVICE) ||
1667
- (advice & UR_USM_ADVICE_FLAG_CLEAR_ACCESSED_BY_DEVICE) ||
1668
- (advice & UR_USM_ADVICE_FLAG_DEFAULT)) {
1669
- ur_device_handle_t Device = hQueue->getDevice ();
1670
- if (!getAttribute (Device, CU_DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS)) {
1671
- UR_LOG (WARN, " Mem advise ignored as device does not support "
1672
- " concurrent managed access." );
1673
- return UR_RESULT_SUCCESS;
1674
- }
1675
-
1676
- // TODO: If ptr points to valid system-allocated pageable memory we should
1677
- // check that the device also has the
1678
- // CU_DEVICE_ATTRIBUTE_PAGEABLE_MEMORY_ACCESS property.
1679
- }
1680
-
1681
- unsigned int IsManaged;
1682
- UR_CHECK_ERROR (cuPointerGetAttribute (
1683
- &IsManaged, CU_POINTER_ATTRIBUTE_IS_MANAGED, (CUdeviceptr)pMem));
1684
- if (!IsManaged) {
1685
- UR_LOG (WARN,
1686
- " Memory advice ignored as memory advices only works with USM." );
1687
- return UR_RESULT_SUCCESS;
1688
- }
1689
-
1690
1664
ur_result_t Result = UR_RESULT_SUCCESS;
1691
1665
std::unique_ptr<ur_event_handle_t_> EventPtr{nullptr };
1692
1666
@@ -1700,6 +1674,47 @@ urEnqueueUSMAdvise(ur_queue_handle_t hQueue, const void *pMem, size_t size,
1700
1674
UR_CHECK_ERROR (EventPtr->start ());
1701
1675
}
1702
1676
1677
+ // Ensure we release the event even on early exit
1678
+ OnScopeExit ReleaseEvent ([&]() {
1679
+ if (phEvent) {
1680
+ UR_CHECK_ERROR (EventPtr->record ());
1681
+ *phEvent = EventPtr.release ();
1682
+ }
1683
+ });
1684
+
1685
+ // Certain cuda devices and Windows do not have support for some Unified
1686
+ // Memory features. Passing CU_MEM_ADVISE_SET/CLEAR_PREFERRED_LOCATION and
1687
+ // to cuMemAdvise on a GPU device requires the GPU device to report a
1688
+ // non-zero value for CU_DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS.
1689
+ // Therfore, ignore memory advise if concurrent managed memory access is not
1690
+ // available.
1691
+ if ((advice & UR_USM_ADVICE_FLAG_SET_PREFERRED_LOCATION) ||
1692
+ (advice & UR_USM_ADVICE_FLAG_CLEAR_PREFERRED_LOCATION) ||
1693
+ (advice & UR_USM_ADVICE_FLAG_SET_ACCESSED_BY_DEVICE) ||
1694
+ (advice & UR_USM_ADVICE_FLAG_CLEAR_ACCESSED_BY_DEVICE) ||
1695
+ (advice & UR_USM_ADVICE_FLAG_DEFAULT)) {
1696
+ ur_device_handle_t Device = hQueue->getDevice ();
1697
+ if (!getAttribute (Device,
1698
+ CU_DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS)) {
1699
+ UR_LOG (WARN, " Mem advise ignored as device does not support "
1700
+ " concurrent managed access." );
1701
+ return UR_RESULT_SUCCESS;
1702
+ }
1703
+
1704
+ // TODO: If ptr points to valid system-allocated pageable memory we should
1705
+ // check that the device also has the
1706
+ // CU_DEVICE_ATTRIBUTE_PAGEABLE_MEMORY_ACCESS property.
1707
+ }
1708
+
1709
+ unsigned int IsManaged;
1710
+ UR_CHECK_ERROR (cuPointerGetAttribute (
1711
+ &IsManaged, CU_POINTER_ATTRIBUTE_IS_MANAGED, (CUdeviceptr)pMem));
1712
+ if (!IsManaged) {
1713
+ UR_LOG (WARN,
1714
+ " Memory advice ignored as memory advices only works with USM." );
1715
+ return UR_RESULT_SUCCESS;
1716
+ }
1717
+
1703
1718
if (advice & UR_USM_ADVICE_FLAG_DEFAULT) {
1704
1719
UR_CHECK_ERROR (cuMemAdvise ((CUdeviceptr)pMem, size,
1705
1720
CU_MEM_ADVISE_UNSET_READ_MOSTLY,
@@ -1714,11 +1729,6 @@ urEnqueueUSMAdvise(ur_queue_handle_t hQueue, const void *pMem, size_t size,
1714
1729
Result = setCuMemAdvise ((CUdeviceptr)pMem, size, advice,
1715
1730
hQueue->getDevice ()->get ());
1716
1731
}
1717
-
1718
- if (phEvent) {
1719
- UR_CHECK_ERROR (EventPtr->record ());
1720
- *phEvent = EventPtr.release ();
1721
- }
1722
1732
} catch (ur_result_t err) {
1723
1733
Result = err;
1724
1734
} catch (...) {
0 commit comments