Skip to content

Commit

Permalink
Merge pull request #1632 from frasercrmck/fraser/hip-device-get-nativ…
Browse files Browse the repository at this point in the history
…e-handle

[HIP] Implement urDeviceGetNativeHandle
  • Loading branch information
kbenzie authored Jun 4, 2024
2 parents 5255031 + 7294170 commit 267d8ed
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
54 changes: 52 additions & 2 deletions source/adapters/hip/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//===----------------------------------------------------------------------===//

#include "device.hpp"
#include "adapter.hpp"
#include "context.hpp"
#include "event.hpp"

Expand Down Expand Up @@ -954,8 +955,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,
[[maybe_unused]] const ur_device_native_properties_t *pProperties,
ur_device_handle_t *phDevice) {
// 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;

// We can only have a maximum of one platform.
if (NumPlatforms != 1)
return UR_RESULT_ERROR_INVALID_OPERATION;

ur_platform_handle_t Platform = nullptr;

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

// Iterate through the platform's devices to find the device that matches
// nativeHandle
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
2 changes: 0 additions & 2 deletions test/conformance/device/device_adapter_hip.match
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
urDeviceCreateWithNativeHandleTest.Success
urDeviceCreateWithNativeHandleTest.SuccessWithOwnedNativeHandle
urDeviceCreateWithNativeHandleTest.SuccessWithUnOwnedNativeHandle
{{OPT}}urDeviceGetGlobalTimestampTest.SuccessSynchronizedTime

0 comments on commit 267d8ed

Please sign in to comment.