Skip to content

Commit 7b70c16

Browse files
authored
[UR][Offload] Native handle wrappers for device and platform (#19563)
And also a small comment explaining why context isn't supported.
1 parent 9f71f9f commit 7b70c16

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

unified-runtime/source/adapters/offload/context.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ urContextRelease(ur_context_handle_t hContext) {
5959
return UR_RESULT_SUCCESS;
6060
}
6161

62+
// Offload currently doesn't have an equivalent to context handles
6263
UR_APIEXPORT ur_result_t UR_APICALL
6364
urContextGetNativeHandle(ur_context_handle_t, ur_native_handle_t *) {
6465
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;

unified-runtime/source/adapters/offload/device.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <ur/ur.hpp>
1313
#include <ur_api.h>
1414

15+
#include "adapters/offload/adapter.hpp"
1516
#include "device.hpp"
1617
#include "platform.hpp"
1718
#include "ur2offload.hpp"
@@ -211,14 +212,32 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceSelectBinary(
211212
return UR_RESULT_ERROR_INVALID_BINARY;
212213
}
213214

214-
UR_APIEXPORT ur_result_t UR_APICALL
215-
urDeviceGetNativeHandle(ur_device_handle_t, ur_native_handle_t *) {
216-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
215+
UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetNativeHandle(
216+
ur_device_handle_t UrDevice, ur_native_handle_t *Handle) {
217+
*Handle = reinterpret_cast<ur_native_handle_t>(UrDevice->OffloadDevice);
218+
return UR_RESULT_SUCCESS;
217219
}
218220

219221
UR_APIEXPORT ur_result_t UR_APICALL urDeviceCreateWithNativeHandle(
220-
ur_native_handle_t, ur_adapter_handle_t,
221-
const ur_device_native_properties_t *, ur_device_handle_t *) {
222+
ur_native_handle_t hNativeDevice, ur_adapter_handle_t hAdapter,
223+
const ur_device_native_properties_t *, ur_device_handle_t *phDevice) {
224+
ol_device_handle_t OlDevice =
225+
reinterpret_cast<ol_device_handle_t>(hNativeDevice);
226+
227+
// Currently, all devices are found at initialization, there is no way to
228+
// create sub devices yet
229+
for (auto &P : hAdapter->Platforms) {
230+
auto Found =
231+
std::find_if(P->Devices.begin(), P->Devices.end(),
232+
[&](std::unique_ptr<ur_device_handle_t_> &PDevice) {
233+
return PDevice->OffloadDevice == OlDevice;
234+
});
235+
if (Found != P->Devices.end()) {
236+
*phDevice = Found->get();
237+
return UR_RESULT_SUCCESS;
238+
}
239+
}
240+
222241
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
223242
}
224243

unified-runtime/source/adapters/offload/platform.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,27 @@ urPlatformGetBackendOption(ur_platform_handle_t, const char *pFrontendOption,
9595
return UR_RESULT_ERROR_INVALID_VALUE;
9696
}
9797

98-
UR_APIEXPORT ur_result_t UR_APICALL
99-
urPlatformGetNativeHandle(ur_platform_handle_t, ur_native_handle_t *) {
100-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
98+
UR_APIEXPORT ur_result_t UR_APICALL urPlatformGetNativeHandle(
99+
ur_platform_handle_t hAdapter, ur_native_handle_t *phNativePlatform) {
100+
*phNativePlatform =
101+
reinterpret_cast<ur_native_handle_t>(hAdapter->OffloadPlatform);
102+
return UR_RESULT_SUCCESS;
101103
}
102104

103105
UR_APIEXPORT ur_result_t UR_APICALL urPlatformCreateWithNativeHandle(
104-
ur_native_handle_t, ur_adapter_handle_t,
105-
const ur_platform_native_properties_t *, ur_platform_handle_t *) {
106+
ur_native_handle_t hNativePlatform, ur_adapter_handle_t hAdapter,
107+
const ur_platform_native_properties_t *, ur_platform_handle_t *phPlatform) {
108+
109+
auto Found = std::find_if(
110+
hAdapter->Platforms.begin(), hAdapter->Platforms.end(), [&](auto &P) {
111+
return P->OffloadPlatform ==
112+
reinterpret_cast<ol_platform_handle_t>(hNativePlatform);
113+
});
114+
if (Found != hAdapter->Platforms.end()) {
115+
*phPlatform = Found->get();
116+
return UR_RESULT_SUCCESS;
117+
}
118+
106119
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
107120
}
108121

0 commit comments

Comments
 (0)