Skip to content

Commit 8fa17b4

Browse files
authored
[SYCL][CUDA][HIP] Fix PI_MEM_ALLOC_DEVICE USM pointer query (#5325)
This fixes the `USM/pointer_query.cpp` test from `llvm-test-suite` for HIP. For HIP the code was using the device pointer to index the device array for some reason, which is obviously incorrect, now it should be using the correct property to get the device index on the hardware. In addition for both HIP and CUDA the query was using the device index to index into the array of devices on the platform, this is incorrect since #4571 which moved every device in its own platform. Therefore to get the correct device we now need to use the index on the list of platforms, and then `0` on the list of devices.
1 parent 4d031a4 commit 8fa17b4

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

sycl/plugins/cuda/pi_cuda.cpp

+12-5
Original file line numberDiff line numberDiff line change
@@ -4825,12 +4825,19 @@ pi_result cuda_piextUSMGetMemAllocInfo(pi_context context, const void *ptr,
48254825
#endif
48264826
}
48274827
case PI_MEM_ALLOC_DEVICE: {
4828-
unsigned int value;
4828+
// get device index associated with this pointer
4829+
unsigned int device_idx;
48294830
result = PI_CHECK_ERROR(cuPointerGetAttribute(
4830-
&value, CU_POINTER_ATTRIBUTE_DEVICE_ORDINAL, (CUdeviceptr)ptr));
4831-
pi_platform platform;
4832-
result = cuda_piPlatformsGet(1, &platform, nullptr);
4833-
pi_device device = platform->devices_[value].get();
4831+
&device_idx, CU_POINTER_ATTRIBUTE_DEVICE_ORDINAL, (CUdeviceptr)ptr));
4832+
4833+
// currently each device is in its own platform, so find the platform at
4834+
// the same index
4835+
std::vector<pi_platform> platforms;
4836+
platforms.resize(device_idx + 1);
4837+
result = cuda_piPlatformsGet(device_idx + 1, platforms.data(), nullptr);
4838+
4839+
// get the device from the platform
4840+
pi_device device = platforms[device_idx]->devices_[0].get();
48344841
return getInfo(param_value_size, param_value, param_value_size_ret,
48354842
device);
48364843
}

sycl/plugins/hip/pi_hip.cpp

+11-7
Original file line numberDiff line numberDiff line change
@@ -4792,15 +4792,19 @@ pi_result hip_piextUSMGetMemAllocInfo(pi_context context, const void *ptr,
47924792
}
47934793

47944794
case PI_MEM_ALLOC_DEVICE: {
4795-
unsigned int value;
4795+
// get device index associated with this pointer
47964796
result = PI_CHECK_ERROR(
47974797
hipPointerGetAttributes(&hipPointerAttributeType, ptr));
4798-
auto devicePointer =
4799-
static_cast<int *>(hipPointerAttributeType.devicePointer);
4800-
value = *devicePointer;
4801-
pi_platform platform;
4802-
result = hip_piPlatformsGet(1, &platform, nullptr);
4803-
pi_device device = platform->devices_[value].get();
4798+
int device_idx = hipPointerAttributeType.device;
4799+
4800+
// currently each device is in its own platform, so find the platform at
4801+
// the same index
4802+
std::vector<pi_platform> platforms;
4803+
platforms.resize(device_idx + 1);
4804+
result = hip_piPlatformsGet(device_idx + 1, platforms.data(), nullptr);
4805+
4806+
// get the device from the platform
4807+
pi_device device = platforms[device_idx]->devices_[0].get();
48044808
return getInfo(param_value_size, param_value, param_value_size_ret,
48054809
device);
48064810
}

0 commit comments

Comments
 (0)