@@ -1618,25 +1618,55 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMMemcpy2D(
1618
1618
hipPointerAttribute_t srcAttribs{};
1619
1619
hipPointerAttribute_t dstAttribs{};
1620
1620
1621
+ // Determine if pSrc and/or pDst are system allocated pageable host memory.
1621
1622
bool srcIsSystemAlloc{false };
1622
1623
bool dstIsSystemAlloc{false };
1623
-
1624
- hipError_t hipRes{};
1625
- // hipErrorInvalidValue returned from hipPointerGetAttributes for a non-null
1626
- // pointer refers to an OS-allocation, hence pageable host memory. However,
1627
- // this means we cannot rely on the attributes result, hence we mark system
1628
- // pageable memory allocation manually as host memory. The HIP runtime can
1629
- // handle the registering/unregistering of the memory as long as the right
1630
- // copy-kind (direction) is provided to hipMemcpy2DAsync for this case.
1631
- hipRes = hipPointerGetAttributes (&srcAttribs, (const void *)pSrc);
1632
- if (hipRes == hipErrorInvalidValue && pSrc)
1624
+ // Error code hipErrorInvalidValue returned from hipPointerGetAttributes
1625
+ // for a non-null pointer refers to an OS-allocation, hence we can work
1626
+ // with the assumption that this is a pointer to a pageable host memory.
1627
+ // Since ROCm version 6.0.0, the enum hipMemoryType can also be marked as
1628
+ // hipMemoryTypeUnregistered explicitly to relay that information better.
1629
+ // This means we cannot rely on any attribute result, hence we just mark
1630
+ // the pointer handle as system allocated pageable host memory.
1631
+ // The HIP runtime can handle the registering/unregistering of the memory
1632
+ // as long as the right copy-kind (direction) is provided to hipMemcpy2D*.
1633
+ hipError_t hipRet = hipPointerGetAttributes (&srcAttribs, pSrc);
1634
+ if (pSrc && hipRet == hipErrorInvalidValue)
1633
1635
srcIsSystemAlloc = true ;
1634
- hipRes = hipPointerGetAttributes (&dstAttribs, (const void *)pDst);
1635
- if (hipRes == hipErrorInvalidValue && pDst )
1636
+ hipRet = hipPointerGetAttributes (&dstAttribs, (const void *)pDst);
1637
+ if (pDst && hipRet == hipErrorInvalidValue)
1636
1638
dstIsSystemAlloc = true ;
1639
+ #if HIP_VERSION_MAJOR >= 6
1640
+ srcIsSystemAlloc |= srcAttribs.type == hipMemoryTypeUnregistered;
1641
+ dstIsSystemAlloc |= dstAttribs.type == hipMemoryTypeUnregistered;
1642
+ #endif
1637
1643
1638
- const unsigned int srcMemType{srcAttribs.type };
1639
- const unsigned int dstMemType{dstAttribs.type };
1644
+ unsigned int srcMemType{srcAttribs.type };
1645
+ unsigned int dstMemType{dstAttribs.type };
1646
+
1647
+ // ROCm 5.7.1 finally started updating the type attribute member to
1648
+ // hipMemoryTypeManaged for shared memory allocations(hipMallocManaged).
1649
+ // Hence, we use a separate query that verifies the pointer use via flags.
1650
+ #if HIP_VERSION >= 50700001
1651
+ // Determine the source/destination memory type for shared allocations.
1652
+ //
1653
+ // NOTE: The hipPointerGetAttribute API is marked as [BETA] and fails with
1654
+ // exit code -11 when passing a system allocated pointer to it.
1655
+ if (!srcIsSystemAlloc && srcAttribs.isManaged ) {
1656
+ UR_ASSERT (srcAttribs.hostPointer && srcAttribs.hostPointer ,
1657
+ UR_RESULT_ERROR_INVALID_VALUE);
1658
+ UR_CHECK_ERROR (hipPointerGetAttribute (
1659
+ &srcMemType, HIP_POINTER_ATTRIBUTE_MEMORY_TYPE,
1660
+ reinterpret_cast <hipDeviceptr_t>(const_cast <void *>(pSrc))));
1661
+ }
1662
+ if (!dstIsSystemAlloc && dstAttribs.isManaged ) {
1663
+ UR_ASSERT (dstAttribs.hostPointer && dstAttribs.devicePointer ,
1664
+ UR_RESULT_ERROR_INVALID_VALUE);
1665
+ UR_CHECK_ERROR (
1666
+ hipPointerGetAttribute (&dstMemType, HIP_POINTER_ATTRIBUTE_MEMORY_TYPE,
1667
+ reinterpret_cast <hipDeviceptr_t>(pDst)));
1668
+ }
1669
+ #endif
1640
1670
1641
1671
const bool srcIsHost{(srcMemType == hipMemoryTypeHost) || srcIsSystemAlloc};
1642
1672
const bool srcIsDevice{srcMemType == hipMemoryTypeDevice};
0 commit comments