@@ -1618,25 +1618,47 @@ 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
+ // hipErrorInvalidValue or hipErrorMemoryAllocation returned from
1625
+ // hipPointerGetAttributes, for a non-null pointer, refers to an
1626
+ // OS-allocation, hence pageable host memory. However, this means we cannot
1627
+ // rely on any attribute result, hence we mark it as system allocated
1628
+ // pageable host memory. The HIP runtime can handle the
1629
+ // registering/unregistering of the memory as long as the right copy-kind
1630
+ // (direction) is provided to hipMemcpy2DAsync for this case.
1631
+ if (pSrc &&
1632
+ hipPointerGetAttributes (&srcAttribs, (const void *)pSrc) != hipSuccess)
1633
1633
srcIsSystemAlloc = true ;
1634
- hipRes = hipPointerGetAttributes (&dstAttribs, ( const void *) pDst);
1635
- if (hipRes == hipErrorInvalidValue && pDst)
1634
+ if ( pDst &&
1635
+ hipPointerGetAttributes (&dstAttribs, ( const void *) pDst) != hipSuccess )
1636
1636
dstIsSystemAlloc = true ;
1637
1637
1638
- const unsigned int srcMemType{srcAttribs.type };
1639
- const unsigned int dstMemType{dstAttribs.type };
1638
+ unsigned int srcMemType{srcAttribs.type };
1639
+ unsigned int dstMemType{dstAttribs.type };
1640
+
1641
+ // ROCm 5.7.1 finally started updating the type attribute member to
1642
+ // hipMemoryTypeManaged for shared memory allocations(hipMallocManaged).
1643
+ // Hence, we use a separate query that verifies the pointer use via flags.
1644
+ #if HIP_VERSION >= 50700001
1645
+ // Determine the source/destionation memory type for shared allocations.
1646
+ //
1647
+ // NOTE: The hipPointerGetAttribute API is marked as [BETA] and fails with
1648
+ // exit code -11 when passing a system allocated pointer to it.
1649
+ if (!srcIsSystemAlloc && srcAttribs.isManaged ) {
1650
+ assert (srcAttribs.hostPointer && srcAttribs.hostPointer );
1651
+ UR_CHECK_ERROR (hipPointerGetAttribute (
1652
+ &srcMemType, HIP_POINTER_ATTRIBUTE_MEMORY_TYPE,
1653
+ reinterpret_cast <hipDeviceptr_t>(const_cast <void *>(pSrc))));
1654
+ }
1655
+ if (!dstIsSystemAlloc && dstAttribs.isManaged ) {
1656
+ assert (dstAttribs.hostPointer && dstAttribs.devicePointer );
1657
+ UR_CHECK_ERROR (
1658
+ hipPointerGetAttribute (&dstMemType, HIP_POINTER_ATTRIBUTE_MEMORY_TYPE,
1659
+ reinterpret_cast <hipDeviceptr_t>(pDst)));
1660
+ }
1661
+ #endif
1640
1662
1641
1663
const bool srcIsHost{(srcMemType == hipMemoryTypeHost) || srcIsSystemAlloc};
1642
1664
const bool srcIsDevice{srcMemType == hipMemoryTypeDevice};
0 commit comments