From 5935c778f163e48eca63210d4c7deb795f40f032 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Mestre?= Date: Fri, 29 Dec 2023 15:56:50 +0000 Subject: [PATCH 1/5] [CUDA] Implement urMemImageGetInfo --- source/adapters/cuda/common.hpp | 10 ++ source/adapters/cuda/memory.cpp | 108 +++++++++++++++++- .../memory/memory_adapter_cuda.match | 18 +-- 3 files changed, 115 insertions(+), 21 deletions(-) diff --git a/source/adapters/cuda/common.hpp b/source/adapters/cuda/common.hpp index 67223c45bc..49985732e4 100644 --- a/source/adapters/cuda/common.hpp +++ b/source/adapters/cuda/common.hpp @@ -12,6 +12,16 @@ #include #include +/** + * Call a UR API and, if the result is not UR_RESULT_SUCCESS, automatically + * return from the current function. + */ +#define UR_RETURN_ON_FAILURE(urCall) \ + if (const ur_result_t ur_result_macro = urCall; \ + ur_result_macro != UR_RESULT_SUCCESS) { \ + return ur_result_macro; \ + } + ur_result_t mapErrorUR(CUresult Result); /// Converts CUDA error into UR error codes, and outputs error information diff --git a/source/adapters/cuda/memory.cpp b/source/adapters/cuda/memory.cpp index f479522fb3..3fa6854522 100644 --- a/source/adapters/cuda/memory.cpp +++ b/source/adapters/cuda/memory.cpp @@ -398,11 +398,109 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageCreate( return Result; } -/// \TODO Not implemented -UR_APIEXPORT ur_result_t UR_APICALL urMemImageGetInfo(ur_mem_handle_t, - ur_image_info_t, size_t, - void *, size_t *) { - return UR_RESULT_ERROR_UNSUPPORTED_FEATURE; +UR_APIEXPORT ur_result_t UR_APICALL urMemImageGetInfo(ur_mem_handle_t hMemory, + ur_image_info_t propName, + size_t propSize, + void *pPropValue, + size_t *pPropSizeRet) { + UR_ASSERT(hMemory->isImage(), UR_RESULT_ERROR_INVALID_MEM_OBJECT); + + auto Context = hMemory->getContext(); + + ScopedContext Active(Context); + UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet); + + try { + CUDA_ARRAY3D_DESCRIPTOR ArrayInfo; + + UR_CHECK_ERROR(cuArray3DGetDescriptor( + &ArrayInfo, std::get(hMemory->Mem).getArray())); + + const auto cuda2urFormat = [](CUarray_format CUFormat, + ur_image_channel_type_t *ChannelType) { + switch (CUFormat) { + case CU_AD_FORMAT_UNSIGNED_INT8: + *ChannelType = UR_IMAGE_CHANNEL_TYPE_UNSIGNED_INT8; + break; + case CU_AD_FORMAT_UNSIGNED_INT16: + *ChannelType = UR_IMAGE_CHANNEL_TYPE_UNSIGNED_INT16; + break; + case CU_AD_FORMAT_UNSIGNED_INT32: + *ChannelType = UR_IMAGE_CHANNEL_TYPE_UNSIGNED_INT32; + break; + case CU_AD_FORMAT_SIGNED_INT8: + *ChannelType = UR_IMAGE_CHANNEL_TYPE_SIGNED_INT8; + break; + case CU_AD_FORMAT_SIGNED_INT16: + *ChannelType = UR_IMAGE_CHANNEL_TYPE_SIGNED_INT16; + break; + case CU_AD_FORMAT_SIGNED_INT32: + *ChannelType = UR_IMAGE_CHANNEL_TYPE_SIGNED_INT32; + break; + case CU_AD_FORMAT_HALF: + *ChannelType = UR_IMAGE_CHANNEL_TYPE_HALF_FLOAT; + break; + case CU_AD_FORMAT_FLOAT: + *ChannelType = UR_IMAGE_CHANNEL_TYPE_FLOAT; + break; + default: + return UR_RESULT_ERROR_UNSUPPORTED_IMAGE_FORMAT; + } + return UR_RESULT_SUCCESS; + }; + + const auto cudaFormatToElementSize = [](CUarray_format CUFormat, + size_t *Size) { + switch (CUFormat) { + case CU_AD_FORMAT_UNSIGNED_INT8: + case CU_AD_FORMAT_SIGNED_INT8: + *Size = 1; + break; + case CU_AD_FORMAT_UNSIGNED_INT16: + case CU_AD_FORMAT_SIGNED_INT16: + case CU_AD_FORMAT_HALF: + *Size = 2; + break; + case CU_AD_FORMAT_UNSIGNED_INT32: + case CU_AD_FORMAT_SIGNED_INT32: + case CU_AD_FORMAT_FLOAT: + *Size = 4; + break; + default: + return UR_RESULT_ERROR_UNSUPPORTED_IMAGE_FORMAT; + } + return UR_RESULT_SUCCESS; + }; + + switch (propName) { + case UR_IMAGE_INFO_FORMAT: + ur_image_channel_type_t ChannelType; + UR_RETURN_ON_FAILURE(cuda2urFormat(ArrayInfo.Format, &ChannelType)); + return ReturnValue( + ur_image_format_t{UR_IMAGE_CHANNEL_ORDER_RGBA, ChannelType}); + case UR_IMAGE_INFO_WIDTH: + return ReturnValue(ArrayInfo.Width); + case UR_IMAGE_INFO_HEIGHT: + return ReturnValue(ArrayInfo.Height); + case UR_IMAGE_INFO_DEPTH: + return ReturnValue(ArrayInfo.Depth); + case UR_IMAGE_INFO_ELEMENT_SIZE: + size_t Size; + UR_RETURN_ON_FAILURE(cudaFormatToElementSize(ArrayInfo.Format, &Size)); + return ReturnValue(Size); + case UR_IMAGE_INFO_ROW_PITCH: + case UR_IMAGE_INFO_SLICE_PITCH: + return UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION; + + default: + return UR_RESULT_ERROR_INVALID_ENUMERATION; + } + + } catch (ur_result_t Err) { + return Err; + } catch (...) { + return UR_RESULT_ERROR_UNKNOWN; + } } /// Implements a buffer partition in the CUDA backend. diff --git a/test/conformance/memory/memory_adapter_cuda.match b/test/conformance/memory/memory_adapter_cuda.match index 5fe265ae8e..394f8d86a7 100644 --- a/test/conformance/memory/memory_adapter_cuda.match +++ b/test/conformance/memory/memory_adapter_cuda.match @@ -1,17 +1,3 @@ urMemBufferCreateWithNativeHandleTest.Success/NVIDIA_CUDA_BACKEND___{{.*}}_ -{{OPT}}urMemGetInfoImageTest.Success/NVIDIA_CUDA_BACKEND___{{.*}}___UR_MEM_INFO_SIZE -{{OPT}}urMemGetInfoImageTest.Success/NVIDIA_CUDA_BACKEND___{{.*}}___UR_MEM_INFO_CONTEXT -{{OPT}}urMemImageGetInfoTest.Success/NVIDIA_CUDA_BACKEND___{{.*}}___UR_IMAGE_INFO_FORMAT -{{OPT}}urMemImageGetInfoTest.Success/NVIDIA_CUDA_BACKEND___{{.*}}___UR_IMAGE_INFO_ELEMENT_SIZE -{{OPT}}urMemImageGetInfoTest.Success/NVIDIA_CUDA_BACKEND___{{.*}}___UR_IMAGE_INFO_ROW_PITCH -{{OPT}}urMemImageGetInfoTest.Success/NVIDIA_CUDA_BACKEND___{{.*}}___UR_IMAGE_INFO_SLICE_PITCH -{{OPT}}urMemImageGetInfoTest.Success/NVIDIA_CUDA_BACKEND___{{.*}}___UR_IMAGE_INFO_WIDTH -{{OPT}}urMemImageGetInfoTest.Success/NVIDIA_CUDA_BACKEND___{{.*}}___UR_IMAGE_INFO_HEIGHT -{{OPT}}urMemImageGetInfoTest.Success/NVIDIA_CUDA_BACKEND___{{.*}}___UR_IMAGE_INFO_DEPTH -{{OPT}}urMemImageGetInfoTest.InvalidSizeSmall/NVIDIA_CUDA_BACKEND___{{.*}}___UR_IMAGE_INFO_FORMAT -{{OPT}}urMemImageGetInfoTest.InvalidSizeSmall/NVIDIA_CUDA_BACKEND___{{.*}}___UR_IMAGE_INFO_ELEMENT_SIZE -{{OPT}}urMemImageGetInfoTest.InvalidSizeSmall/NVIDIA_CUDA_BACKEND___{{.*}}___UR_IMAGE_INFO_ROW_PITCH -{{OPT}}urMemImageGetInfoTest.InvalidSizeSmall/NVIDIA_CUDA_BACKEND___{{.*}}___UR_IMAGE_INFO_SLICE_PITCH -{{OPT}}urMemImageGetInfoTest.InvalidSizeSmall/NVIDIA_CUDA_BACKEND___{{.*}}___UR_IMAGE_INFO_WIDTH -{{OPT}}urMemImageGetInfoTest.InvalidSizeSmall/NVIDIA_CUDA_BACKEND___{{.*}}___UR_IMAGE_INFO_HEIGHT -{{OPT}}urMemImageGetInfoTest.InvalidSizeSmall/NVIDIA_CUDA_BACKEND___{{.*}}___UR_IMAGE_INFO_DEPTH +urMemImageGetInfoTest.Success/NVIDIA_CUDA_BACKEND___{{.*}}___UR_IMAGE_INFO_ROW_PITCH +urMemImageGetInfoTest.Success/NVIDIA_CUDA_BACKEND___{{.*}}___UR_IMAGE_INFO_SLICE_PITCH From 8b27a7d21e4c06a0b48a4c079f1a67ffed29a226 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Mestre?= Date: Fri, 29 Dec 2023 16:54:01 +0000 Subject: [PATCH 2/5] Fix match file --- test/conformance/memory/memory_adapter_cuda.match | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/conformance/memory/memory_adapter_cuda.match b/test/conformance/memory/memory_adapter_cuda.match index 394f8d86a7..60c2a3a89a 100644 --- a/test/conformance/memory/memory_adapter_cuda.match +++ b/test/conformance/memory/memory_adapter_cuda.match @@ -1,3 +1,5 @@ urMemBufferCreateWithNativeHandleTest.Success/NVIDIA_CUDA_BACKEND___{{.*}}_ -urMemImageGetInfoTest.Success/NVIDIA_CUDA_BACKEND___{{.*}}___UR_IMAGE_INFO_ROW_PITCH -urMemImageGetInfoTest.Success/NVIDIA_CUDA_BACKEND___{{.*}}___UR_IMAGE_INFO_SLICE_PITCH +{{OPT}}urMemGetInfoImageTest.Success/NVIDIA_CUDA_BACKEND___{{.*}}___UR_MEM_INFO_SIZE +{{OPT}}urMemGetInfoImageTest.Success/NVIDIA_CUDA_BACKEND___{{.*}}___UR_MEM_INFO_CONTEXT +urMemImageGetInfoTest.InvalidSizeSmall/NVIDIA_CUDA_BACKEND___{{.*}}___UR_IMAGE_INFO_ROW_PITCH +urMemImageGetInfoTest.InvalidSizeSmall/NVIDIA_CUDA_BACKEND___{{.*}}___UR_IMAGE_INFO_SLICE_PITCH From 14021f1d10728a90d9fe414c5bef14e7ea48415a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Mestre?= Date: Mon, 1 Jan 2024 15:37:40 +0000 Subject: [PATCH 3/5] Fix match file --- test/conformance/memory/memory_adapter_cuda.match | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/conformance/memory/memory_adapter_cuda.match b/test/conformance/memory/memory_adapter_cuda.match index 60c2a3a89a..b8c027fe4b 100644 --- a/test/conformance/memory/memory_adapter_cuda.match +++ b/test/conformance/memory/memory_adapter_cuda.match @@ -1,5 +1,5 @@ urMemBufferCreateWithNativeHandleTest.Success/NVIDIA_CUDA_BACKEND___{{.*}}_ {{OPT}}urMemGetInfoImageTest.Success/NVIDIA_CUDA_BACKEND___{{.*}}___UR_MEM_INFO_SIZE {{OPT}}urMemGetInfoImageTest.Success/NVIDIA_CUDA_BACKEND___{{.*}}___UR_MEM_INFO_CONTEXT -urMemImageGetInfoTest.InvalidSizeSmall/NVIDIA_CUDA_BACKEND___{{.*}}___UR_IMAGE_INFO_ROW_PITCH -urMemImageGetInfoTest.InvalidSizeSmall/NVIDIA_CUDA_BACKEND___{{.*}}___UR_IMAGE_INFO_SLICE_PITCH +urMemImageGetInfoTest.Success/NVIDIA_CUDA_BACKEND___{{.*}}___UR_IMAGE_INFO_ROW_PITCH +urMemImageGetInfoTest.Success/NVIDIA_CUDA_BACKEND___{{.*}}___UR_IMAGE_INFO_SLICE_PITCH From f0fc018ddc944889caa8a6202c8bf2b61162aa18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Mestre?= Date: Thu, 25 Jan 2024 14:36:02 +0000 Subject: [PATCH 4/5] Address review feedback --- source/adapters/cuda/common.hpp | 10 ---------- source/adapters/cuda/memory.cpp | 4 ++-- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/source/adapters/cuda/common.hpp b/source/adapters/cuda/common.hpp index 49985732e4..67223c45bc 100644 --- a/source/adapters/cuda/common.hpp +++ b/source/adapters/cuda/common.hpp @@ -12,16 +12,6 @@ #include #include -/** - * Call a UR API and, if the result is not UR_RESULT_SUCCESS, automatically - * return from the current function. - */ -#define UR_RETURN_ON_FAILURE(urCall) \ - if (const ur_result_t ur_result_macro = urCall; \ - ur_result_macro != UR_RESULT_SUCCESS) { \ - return ur_result_macro; \ - } - ur_result_t mapErrorUR(CUresult Result); /// Converts CUDA error into UR error codes, and outputs error information diff --git a/source/adapters/cuda/memory.cpp b/source/adapters/cuda/memory.cpp index 3fa6854522..8f544fdf9f 100644 --- a/source/adapters/cuda/memory.cpp +++ b/source/adapters/cuda/memory.cpp @@ -475,7 +475,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageGetInfo(ur_mem_handle_t hMemory, switch (propName) { case UR_IMAGE_INFO_FORMAT: ur_image_channel_type_t ChannelType; - UR_RETURN_ON_FAILURE(cuda2urFormat(ArrayInfo.Format, &ChannelType)); + UR_CHECK_ERROR(cuda2urFormat(ArrayInfo.Format, &ChannelType)); return ReturnValue( ur_image_format_t{UR_IMAGE_CHANNEL_ORDER_RGBA, ChannelType}); case UR_IMAGE_INFO_WIDTH: @@ -486,7 +486,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageGetInfo(ur_mem_handle_t hMemory, return ReturnValue(ArrayInfo.Depth); case UR_IMAGE_INFO_ELEMENT_SIZE: size_t Size; - UR_RETURN_ON_FAILURE(cudaFormatToElementSize(ArrayInfo.Format, &Size)); + UR_CHECK_ERROR(cudaFormatToElementSize(ArrayInfo.Format, &Size)); return ReturnValue(Size); case UR_IMAGE_INFO_ROW_PITCH: case UR_IMAGE_INFO_SLICE_PITCH: From 46604beae4786a3ad6d5152dbfe74b4b7aebe669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Mestre?= Date: Mon, 29 Jan 2024 18:30:25 +0000 Subject: [PATCH 5/5] Fix compiler error --- source/adapters/cuda/memory.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/source/adapters/cuda/memory.cpp b/source/adapters/cuda/memory.cpp index 8f544fdf9f..f097d2474e 100644 --- a/source/adapters/cuda/memory.cpp +++ b/source/adapters/cuda/memory.cpp @@ -473,21 +473,23 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageGetInfo(ur_mem_handle_t hMemory, }; switch (propName) { - case UR_IMAGE_INFO_FORMAT: - ur_image_channel_type_t ChannelType; + case UR_IMAGE_INFO_FORMAT: { + ur_image_channel_type_t ChannelType{}; UR_CHECK_ERROR(cuda2urFormat(ArrayInfo.Format, &ChannelType)); return ReturnValue( ur_image_format_t{UR_IMAGE_CHANNEL_ORDER_RGBA, ChannelType}); + } case UR_IMAGE_INFO_WIDTH: return ReturnValue(ArrayInfo.Width); case UR_IMAGE_INFO_HEIGHT: return ReturnValue(ArrayInfo.Height); case UR_IMAGE_INFO_DEPTH: return ReturnValue(ArrayInfo.Depth); - case UR_IMAGE_INFO_ELEMENT_SIZE: - size_t Size; + case UR_IMAGE_INFO_ELEMENT_SIZE: { + size_t Size = 0; UR_CHECK_ERROR(cudaFormatToElementSize(ArrayInfo.Format, &Size)); return ReturnValue(Size); + } case UR_IMAGE_INFO_ROW_PITCH: case UR_IMAGE_INFO_SLICE_PITCH: return UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION;