Skip to content

Commit f359a59

Browse files
committed
[HIP] Fix memory type detection in USM copy2D
1 parent 29ee45c commit f359a59

File tree

1 file changed

+39
-14
lines changed

1 file changed

+39
-14
lines changed

source/adapters/hip/enqueue.cpp

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,25 +1610,50 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMMemcpy2D(
16101610
hipPointerAttribute_t srcAttribs{};
16111611
hipPointerAttribute_t dstAttribs{};
16121612

1613+
// Determine if pSrc and/or pDst are system allocated pageable host memory.
16131614
bool srcIsSystemAlloc{false};
16141615
bool dstIsSystemAlloc{false};
1615-
1616-
hipError_t hipRes{};
1617-
// hipErrorInvalidValue returned from hipPointerGetAttributes for a non-null
1618-
// pointer refers to an OS-allocation, hence pageable host memory. However,
1619-
// this means we cannot rely on the attributes result, hence we mark system
1620-
// pageable memory allocation manually as host memory. The HIP runtime can
1621-
// handle the registering/unregistering of the memory as long as the right
1622-
// copy-kind (direction) is provided to hipMemcpy2DAsync for this case.
1623-
hipRes = hipPointerGetAttributes(&srcAttribs, (const void *)pSrc);
1624-
if (hipRes == hipErrorInvalidValue && pSrc)
1616+
// hipErrorInvalidValue or hipErrorMemoryAllocation returned from
1617+
// hipPointerGetAttributes, for a non-null pointer, refers to an
1618+
// OS-allocation, hence pageable host memory. However, this means we cannot
1619+
// rely on any attribute result, hence we mark it as system allocated
1620+
// pageable host memory. The HIP runtime can handle the
1621+
// registering/unregistering of the memory as long as the right copy-kind
1622+
// (direction) is provided to hipMemcpy2DAsync for this case.
1623+
if (pSrc && hipPointerGetAttributes(&srcAttribs, (const void *)pSrc) !=
1624+
hipSuccess)
16251625
srcIsSystemAlloc = true;
1626-
hipRes = hipPointerGetAttributes(&dstAttribs, (const void *)pDst);
1627-
if (hipRes == hipErrorInvalidValue && pDst)
1626+
if (pDst && hipPointerGetAttributes(&dstAttribs, (const void *)pDst) !=
1627+
hipSuccess)
16281628
dstIsSystemAlloc = true;
16291629

1630-
const unsigned int srcMemType{srcAttribs.type};
1631-
const unsigned int dstMemType{dstAttribs.type};
1630+
unsigned int srcMemType{srcAttribs.type};
1631+
unsigned int dstMemType{dstAttribs.type};
1632+
1633+
// ROCm 5.7.1 finally started updating the type attribute member to
1634+
// hipMemoryTypeManaged for shared memory allocations(hipMallocManaged).
1635+
// Hence, we use a separate query that verifies the pointer use via flags.
1636+
#if HIP_VERSION >= 50700001
1637+
// Determine the source/destionation memory type for shared allocations.
1638+
//
1639+
// NOTE: The hipPointerGetAttribute API is marked as [BETA] and fails with
1640+
// exit code -11 when passing a system allocated pointer to it.
1641+
if (!srcIsSystemAlloc && srcAttribs.isManaged) {
1642+
assert(srcAttribs.hostPointer && srcAttribs.hostPointer);
1643+
UR_CHECK_ERROR(hipPointerGetAttribute(
1644+
&srcMemType, HIP_POINTER_ATTRIBUTE_MEMORY_TYPE,
1645+
reinterpret_cast<hipDeviceptr_t>(const_cast<void *>(pSrc))));
1646+
}
1647+
if (!dstIsSystemAlloc && dstAttribs.isManaged) {
1648+
assert(dstAttribs.hostPointer && dstAttribs.devicePointer);
1649+
UR_CHECK_ERROR(
1650+
hipPointerGetAttribute(&dstMemType, HIP_POINTER_ATTRIBUTE_MEMORY_TYPE,
1651+
reinterpret_cast<hipDeviceptr_t>(pDst)));
1652+
}
1653+
#endif
1654+
1655+
//printf("srcMemType = %i\n", srcMemType);
1656+
//printf("dstMemType = %i\n", dstMemType);
16321657

16331658
const bool srcIsHost{(srcMemType == hipMemoryTypeHost) || srcIsSystemAlloc};
16341659
const bool srcIsDevice{srcMemType == hipMemoryTypeDevice};

0 commit comments

Comments
 (0)