Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions source/adapters/hip/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,39 @@
#include <hip/hip_runtime.h>
#include <ur/ur.hpp>

// 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
Expand Down
8 changes: 4 additions & 4 deletions source/adapters/hip/enqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -1141,13 +1141,13 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemImageCopy(
std::get<SurfaceMem>(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<SurfaceMem>(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);
Expand Down
2 changes: 1 addition & 1 deletion source/adapters/hip/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ urKernelSetArgMemObj(ur_kernel_handle_t hKernel, uint32_t argIndex,
auto array = std::get<SurfaceMem>(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) {
Expand Down
10 changes: 9 additions & 1 deletion source/adapters/hip/usm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down