Skip to content

Commit 0711559

Browse files
committed
[HIP] Fix memory type detection in USM copy2D
1 parent 4aeb55e commit 0711559

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

source/adapters/hip/enqueue.cpp

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,25 +1618,47 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMMemcpy2D(
16181618
hipPointerAttribute_t srcAttribs{};
16191619
hipPointerAttribute_t dstAttribs{};
16201620

1621+
// Determine if pSrc and/or pDst are system allocated pageable host memory.
16211622
bool srcIsSystemAlloc{false};
16221623
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)
16331633
srcIsSystemAlloc = true;
1634-
hipRes = hipPointerGetAttributes(&dstAttribs, (const void *)pDst);
1635-
if (hipRes == hipErrorInvalidValue && pDst)
1634+
if (pDst &&
1635+
hipPointerGetAttributes(&dstAttribs, (const void *)pDst) != hipSuccess)
16361636
dstIsSystemAlloc = true;
16371637

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
16401662

16411663
const bool srcIsHost{(srcMemType == hipMemoryTypeHost) || srcIsSystemAlloc};
16421664
const bool srcIsDevice{srcMemType == hipMemoryTypeDevice};

0 commit comments

Comments
 (0)