Skip to content

Commit

Permalink
[hip] Implement urDeviceGetNativeHandle
Browse files Browse the repository at this point in the history
This is mostly just a copy of the CUDA version of this implementation.
  • Loading branch information
frasercrmck committed May 21, 2024
1 parent 42d72b6 commit a56cfe6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
54 changes: 52 additions & 2 deletions source/adapters/hip/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//
//===----------------------------------------------------------------------===//

#include "adapter.hpp"
#include "device.hpp"
#include "context.hpp"
#include "event.hpp"
Expand Down Expand Up @@ -950,8 +951,57 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetNativeHandle(
}

UR_APIEXPORT ur_result_t UR_APICALL urDeviceCreateWithNativeHandle(
ur_native_handle_t, ur_platform_handle_t,
const ur_device_native_properties_t *, ur_device_handle_t *) {
ur_native_handle_t hNativeDevice, ur_platform_handle_t hPlatform,
const ur_device_native_properties_t *pProperties,
ur_device_handle_t *phDevice) {
std::ignore = pProperties;

// We can't cast between ur_native_handle_t and hipDevice_t, so memcpy the
// bits instead
hipDevice_t HIPDevice = 0;
memcpy(&HIPDevice, &hNativeDevice, sizeof(hipDevice_t));

auto IsDevice = [=](std::unique_ptr<ur_device_handle_t_> &Dev) {
return Dev->get() == HIPDevice;
};

// If a platform is provided just check if the device is in it
if (hPlatform) {
auto SearchRes = std::find_if(begin(hPlatform->Devices),
end(hPlatform->Devices), IsDevice);
if (SearchRes != end(hPlatform->Devices)) {
*phDevice = SearchRes->get();
return UR_RESULT_SUCCESS;
}
}

// Get list of platforms
uint32_t NumPlatforms = 0;
ur_adapter_handle_t AdapterHandle = &adapter;
ur_result_t Result =
urPlatformGet(&AdapterHandle, 1, 0, nullptr, &NumPlatforms);
if (Result != UR_RESULT_SUCCESS)
return Result;

std::vector<ur_platform_handle_t> Platforms(NumPlatforms);

Result =
urPlatformGet(&AdapterHandle, 1, NumPlatforms, Platforms.data(), nullptr);
if (Result != UR_RESULT_SUCCESS)
return Result;

// Iterate through platforms to find device that matches nativeHandle
for (const auto Platform : Platforms) {
auto SearchRes = std::find_if(std::begin(Platform->Devices),
std::end(Platform->Devices), IsDevice);
if (SearchRes != end(Platform->Devices)) {
*phDevice = static_cast<ur_device_handle_t>((*SearchRes).get());
return UR_RESULT_SUCCESS;
}
}

// If the provided nativeHandle cannot be matched to an
// existing device return error
return UR_RESULT_ERROR_INVALID_OPERATION;
}

Expand Down
1 change: 0 additions & 1 deletion test/conformance/device/device_adapter_hip.match
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
{{OPT}}urDeviceCreateWithNativeHandleTest.Success
{{OPT}}urDeviceGetGlobalTimestampTest.SuccessSynchronizedTime

0 comments on commit a56cfe6

Please sign in to comment.