From 0942e74cf3a78d2d624023d271234b388d1df9d8 Mon Sep 17 00:00:00 2001 From: Andrey Alekseenko Date: Sat, 16 Dec 2023 21:01:40 +0100 Subject: [PATCH 1/3] Fix getArrayDesc on ROCm 6 and make it return error codes --- source/adapters/hip/common.hpp | 35 ++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/source/adapters/hip/common.hpp b/source/adapters/hip/common.hpp index 2649657f47..d7eea780a5 100644 --- a/source/adapters/hip/common.hpp +++ b/source/adapters/hip/common.hpp @@ -15,24 +15,39 @@ #include #include -// Hipify doesn't support cuArrayGetDescriptor, on AMD the hipArray can just be -// indexed, but on NVidia it is an opaque type and needs to go through -// cuArrayGetDescriptor so implement a utility function to get the array -// properties -inline void getArrayDesc(hipArray *Array, hipArray_Format &Format, - size_t &Channels) { +// Before ROCm 6, hipify doesn't support cuArrayGetDescriptor, on AMD the +// hipArray can just be indexed, but on NVidia it is an opaque type and needs to +// go through cuArrayGetDescriptor so implement a utility function to get the +// array properties +inline static hipError_t getArrayDesc(hipArray *Array, hipArray_Format &Format, + size_t &Channels) { +#if HIP_VERSION_MAJOR >= 6 + HIP_ARRAY_DESCRIPTOR ArrayDesc; + hipError_t err = hipArrayGetDescriptor(&ArrayDesc, Array); + if (err == hipSuccess) { + Format = ArrayDesc.Format; + Channels = ArrayDesc.NumChannels; + } + return err; +#else #if defined(__HIP_PLATFORM_AMD__) Format = Array->Format; Channels = Array->NumChannels; + return hipSuccess; #elif defined(__HIP_PLATFORM_NVIDIA__) CUDA_ARRAY_DESCRIPTOR ArrayDesc; - cuArrayGetDescriptor(&ArrayDesc, (CUarray)Array); - - Format = ArrayDesc.Format; - Channels = ArrayDesc.NumChannels; + CUresult err = cuArrayGetDescriptor(&ArrayDesc, (CUarray)Array); + if (err == CUDA_SUCCESS) { + Format = ArrayDesc.Format; + Channels = ArrayDesc.NumChannels; + return hipSuccess; + } else { + return hipErrorUnknown; // No easy way to map CUerror to hipError + } #else #error("Must define exactly one of __HIP_PLATFORM_AMD__ or __HIP_PLATFORM_NVIDIA__"); #endif +#endif } // HIP on NVIDIA headers guard hipArray3DCreate behind __CUDACC__, this does not From 205953dad7d0be3963888c84541128a8ce9dd8da Mon Sep 17 00:00:00 2001 From: Andrey Alekseenko Date: Sat, 16 Dec 2023 21:02:07 +0100 Subject: [PATCH 2/3] Check error codes from getArrayDesc --- source/adapters/hip/enqueue.cpp | 8 ++++---- source/adapters/hip/kernel.cpp | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/source/adapters/hip/enqueue.cpp b/source/adapters/hip/enqueue.cpp index 5f7fffba35..7875650b85 100644 --- a/source/adapters/hip/enqueue.cpp +++ b/source/adapters/hip/enqueue.cpp @@ -1017,7 +1017,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemImageRead( hipArray_Format Format; size_t NumChannels; - getArrayDesc(Array, Format, NumChannels); + UR_CHECK_ERROR(getArrayDesc(Array, Format, NumChannels)); int ElementByteSize = imageElementByteSize(Format); @@ -1078,7 +1078,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemImageWrite( hipArray_Format Format; size_t NumChannels; - getArrayDesc(Array, Format, NumChannels); + UR_CHECK_ERROR(getArrayDesc(Array, Format, NumChannels)); int ElementByteSize = imageElementByteSize(Format); @@ -1141,13 +1141,13 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemImageCopy( std::get(hImageSrc->Mem).getArray(hQueue->getDevice()); hipArray_Format SrcFormat; size_t SrcNumChannels; - getArrayDesc(SrcArray, SrcFormat, SrcNumChannels); + UR_CHECK_ERROR(getArrayDesc(SrcArray, SrcFormat, SrcNumChannels)); hipArray *DstArray = std::get(hImageDst->Mem).getArray(hQueue->getDevice()); hipArray_Format DstFormat; size_t DstNumChannels; - getArrayDesc(DstArray, DstFormat, DstNumChannels); + UR_CHECK_ERROR(getArrayDesc(DstArray, DstFormat, DstNumChannels)); UR_ASSERT(SrcFormat == DstFormat, UR_RESULT_ERROR_INVALID_IMAGE_FORMAT_DESCRIPTOR); diff --git a/source/adapters/hip/kernel.cpp b/source/adapters/hip/kernel.cpp index ec58bafcc6..e3eb37dc88 100644 --- a/source/adapters/hip/kernel.cpp +++ b/source/adapters/hip/kernel.cpp @@ -282,7 +282,7 @@ urKernelSetArgMemObj(ur_kernel_handle_t hKernel, uint32_t argIndex, auto array = std::get(hArgValue->Mem).getArray(Device); hipArray_Format Format; size_t NumChannels; - getArrayDesc(array, Format, NumChannels); + UR_CHECK_ERROR(getArrayDesc(array, Format, NumChannels)); if (Format != HIP_AD_FORMAT_UNSIGNED_INT32 && Format != HIP_AD_FORMAT_SIGNED_INT32 && Format != HIP_AD_FORMAT_HALF && Format != HIP_AD_FORMAT_FLOAT) { From 34831f4bf1db420c2f993a998a434782b42961b3 Mon Sep 17 00:00:00 2001 From: Andrey Alekseenko Date: Sat, 16 Dec 2023 21:02:22 +0100 Subject: [PATCH 3/3] ROCm 6: use hipPointerAttributeType.type when supported --- source/adapters/hip/usm.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/source/adapters/hip/usm.cpp b/source/adapters/hip/usm.cpp index e63379d13b..abd8c2e97f 100644 --- a/source/adapters/hip/usm.cpp +++ b/source/adapters/hip/usm.cpp @@ -72,7 +72,11 @@ USMFreeImpl([[maybe_unused]] ur_context_handle_t hContext, void *pMem) { try { hipPointerAttribute_t hipPointerAttributeType; UR_CHECK_ERROR(hipPointerGetAttributes(&hipPointerAttributeType, pMem)); - unsigned int Type = hipPointerAttributeType.memoryType; +#if HIP_VERSION >= 50600000 + const auto Type = hipPointerAttributeType.type; +#else + const auto Type = hipPointerAttributeType.memoryType; +#endif UR_ASSERT(Type == hipMemoryTypeDevice || Type == hipMemoryTypeHost, UR_RESULT_ERROR_INVALID_MEM_OBJECT); if (Type == hipMemoryTypeDevice) { @@ -170,7 +174,11 @@ urUSMGetMemAllocInfo(ur_context_handle_t hContext, const void *pMem, return ReturnValue(UR_USM_TYPE_SHARED); } UR_CHECK_ERROR(hipPointerGetAttributes(&hipPointerAttributeType, pMem)); +#if HIP_VERSION >= 50600000 + Value = hipPointerAttributeType.type; +#else Value = hipPointerAttributeType.memoryType; +#endif UR_ASSERT(Value == hipMemoryTypeDevice || Value == hipMemoryTypeHost, UR_RESULT_ERROR_INVALID_MEM_OBJECT); if (Value == hipMemoryTypeDevice) {