Skip to content

Commit 493b83f

Browse files
committed
Add query to retrieve adapter handle from platform.
1 parent 2dac0da commit 493b83f

File tree

17 files changed

+115
-9
lines changed

17 files changed

+115
-9
lines changed

include/ur_api.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,8 @@ typedef enum ur_platform_info_t {
10871087
///< info needs to be dynamically queried.
10881088
UR_PLATFORM_INFO_BACKEND = 6, ///< [::ur_platform_backend_t] The backend of the platform. Identifies the
10891089
///< native backend adapter implementing this platform.
1090+
UR_PLATFORM_INFO_ADAPTER = 7, ///< [::ur_adapter_handle_t] The adapter handle associated with the
1091+
///< platform.
10901092
/// @cond
10911093
UR_PLATFORM_INFO_FORCE_UINT32 = 0x7fffffff
10921094
/// @endcond
@@ -1112,7 +1114,7 @@ typedef enum ur_platform_info_t {
11121114
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
11131115
/// + `NULL == hPlatform`
11141116
/// - ::UR_RESULT_ERROR_INVALID_ENUMERATION
1115-
/// + `::UR_PLATFORM_INFO_BACKEND < propName`
1117+
/// + `::UR_PLATFORM_INFO_ADAPTER < propName`
11161118
/// - ::UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION
11171119
/// + If `propName` is not supported by the adapter.
11181120
/// - ::UR_RESULT_ERROR_INVALID_SIZE

include/ur_print.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2024,6 +2024,9 @@ inline std::ostream &operator<<(std::ostream &os, enum ur_platform_info_t value)
20242024
case UR_PLATFORM_INFO_BACKEND:
20252025
os << "UR_PLATFORM_INFO_BACKEND";
20262026
break;
2027+
case UR_PLATFORM_INFO_ADAPTER:
2028+
os << "UR_PLATFORM_INFO_ADAPTER";
2029+
break;
20272030
default:
20282031
os << "unknown enumerator";
20292032
break;
@@ -2077,6 +2080,19 @@ inline ur_result_t printTagged(std::ostream &os, const void *ptr, ur_platform_in
20772080

20782081
os << ")";
20792082
} break;
2083+
case UR_PLATFORM_INFO_ADAPTER: {
2084+
const ur_adapter_handle_t *tptr = (const ur_adapter_handle_t *)ptr;
2085+
if (sizeof(ur_adapter_handle_t) > size) {
2086+
os << "invalid size (is: " << size << ", expected: >=" << sizeof(ur_adapter_handle_t) << ")";
2087+
return UR_RESULT_ERROR_INVALID_SIZE;
2088+
}
2089+
os << (const void *)(tptr) << " (";
2090+
2091+
ur::details::printPtr(os,
2092+
*tptr);
2093+
2094+
os << ")";
2095+
} break;
20802096
default:
20812097
os << "unknown enumerator";
20822098
return UR_RESULT_ERROR_INVALID_ENUMERATION;

scripts/core/platform.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ etors:
7777
- name: BACKEND
7878
value: "6"
7979
desc: "[$x_platform_backend_t] The backend of the platform. Identifies the native backend adapter implementing this platform."
80-
80+
- name: ADAPTER
81+
value: "7"
82+
desc: "[$x_adapter_handle_t] The adapter handle associated with the platform."
8183
--- #--------------------------------------------------------------------------
8284
type: function
8385
desc: "Retrieves various information about platform"

source/adapters/cuda/platform.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//===----------------------------------------------------------------------===//
1010

1111
#include "platform.hpp"
12+
#include "adapter.hpp"
1213
#include "common.hpp"
1314
#include "context.hpp"
1415
#include "device.hpp"
@@ -41,6 +42,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urPlatformGetInfo(
4142
case UR_PLATFORM_INFO_BACKEND: {
4243
return ReturnValue(UR_PLATFORM_BACKEND_CUDA);
4344
}
45+
case UR_PLATFORM_INFO_ADAPTER: {
46+
return ReturnValue(&adapter);
47+
}
4448
default:
4549
return UR_RESULT_ERROR_INVALID_ENUMERATION;
4650
}

source/adapters/hip/platform.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//===----------------------------------------------------------------------===//
1010

1111
#include "platform.hpp"
12+
#include "adapter.hpp"
1213
#include "context.hpp"
1314

1415
UR_APIEXPORT ur_result_t UR_APICALL
@@ -34,6 +35,9 @@ urPlatformGetInfo(ur_platform_handle_t, ur_platform_info_t propName,
3435
case UR_PLATFORM_INFO_EXTENSIONS: {
3536
return ReturnValue("");
3637
}
38+
case UR_PLATFORM_INFO_ADAPTER: {
39+
return ReturnValue(&adapter);
40+
}
3741
default:
3842
return UR_RESULT_ERROR_INVALID_ENUMERATION;
3943
}

source/adapters/level_zero/platform.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ ur_result_t urPlatformGetInfo(
9595
return ReturnValue(Platform->ZeDriverApiVersion.c_str());
9696
case UR_PLATFORM_INFO_BACKEND:
9797
return ReturnValue(UR_PLATFORM_BACKEND_LEVEL_ZERO);
98+
case UR_PLATFORM_INFO_ADAPTER:
99+
return ReturnValue(GlobalAdapter);
98100
default:
99101
logger::debug("urPlatformGetInfo: unrecognized ParamName");
100102
return UR_RESULT_ERROR_INVALID_VALUE;

source/adapters/native_cpu/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ set(TARGET_NAME ur_adapter_native_cpu)
99

1010
add_ur_adapter(${TARGET_NAME}
1111
SHARED
12+
${CMAKE_CURRENT_SOURCE_DIR}/adapter.hpp
1213
${CMAKE_CURRENT_SOURCE_DIR}/adapter.cpp
1314
${CMAKE_CURRENT_SOURCE_DIR}/command_buffer.cpp
1415
${CMAKE_CURRENT_SOURCE_DIR}/common.cpp

source/adapters/native_cpu/adapter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//
99
//===----------------------------------------------------------------------===//
1010

11+
#include "adapter.hpp"
1112
#include "common.hpp"
1213
#include "ur_api.h"
1314

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===---------------- adapter.hpp - Native CPU Adapter --------------------===//
2+
//
3+
// Copyright (C) 2024 Intel Corporation
4+
//
5+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
6+
// Exceptions. See LICENSE.TXT
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
11+
struct ur_adapter_handle_t_;
12+
13+
extern ur_adapter_handle_t_ Adapter;

source/adapters/native_cpu/platform.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//===----------------------------------------------------------------------===//
1010

1111
#include "platform.hpp"
12+
#include "adapter.hpp"
1213
#include "common.hpp"
1314

1415
#include "ur/ur.hpp"
@@ -75,9 +76,9 @@ urPlatformGetInfo(ur_platform_handle_t hPlatform, ur_platform_info_t propName,
7576
return ReturnValue("");
7677

7778
case UR_PLATFORM_INFO_BACKEND:
78-
// TODO(alcpz): PR with this enum value at
79-
// https://github.com/oneapi-src/unified-runtime
8079
return ReturnValue(UR_PLATFORM_BACKEND_NATIVE_CPU);
80+
case UR_PLATFORM_INFO_ADAPTER:
81+
return ReturnValue(&Adapter);
8182
default:
8283
DIE_NO_IMPLEMENTATION;
8384
}

source/adapters/opencl/platform.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//===----------------------------------------------------------------------===//
1010

1111
#include "platform.hpp"
12+
#include "adapter.hpp"
1213

1314
ur_result_t cl_adapter::getPlatformVersion(cl_platform_id Plat,
1415
oclv::OpenCLVersion &Version) {
@@ -57,6 +58,8 @@ urPlatformGetInfo(ur_platform_handle_t hPlatform, ur_platform_info_t propName,
5758
switch (static_cast<uint32_t>(propName)) {
5859
case UR_PLATFORM_INFO_BACKEND:
5960
return ReturnValue(UR_PLATFORM_BACKEND_OPENCL);
61+
case UR_PLATFORM_INFO_ADAPTER:
62+
return ReturnValue(ur::cl::getAdapter());
6063
case UR_PLATFORM_INFO_NAME:
6164
case UR_PLATFORM_INFO_VENDOR_NAME:
6265
case UR_PLATFORM_INFO_VERSION:

source/loader/layers/validation/ur_valddi.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ __urdlllocal ur_result_t UR_APICALL urPlatformGetInfo(
280280
return UR_RESULT_ERROR_INVALID_NULL_POINTER;
281281
}
282282

283-
if (UR_PLATFORM_INFO_BACKEND < propName) {
283+
if (UR_PLATFORM_INFO_ADAPTER < propName) {
284284
return UR_RESULT_ERROR_INVALID_ENUMERATION;
285285
}
286286

source/loader/ur_ldrddi.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,43 @@ __urdlllocal ur_result_t UR_APICALL urPlatformGetInfo(
289289
// convert loader handle to platform handle
290290
hPlatform = reinterpret_cast<ur_platform_object_t *>(hPlatform)->handle;
291291

292+
// this value is needed for converting adapter handles to loader handles
293+
size_t sizeret = 0;
294+
if (pPropSizeRet == NULL) {
295+
pPropSizeRet = &sizeret;
296+
}
297+
292298
// forward to device-platform
293299
result =
294300
pfnGetInfo(hPlatform, propName, propSize, pPropValue, pPropSizeRet);
295301

302+
if (UR_RESULT_SUCCESS != result) {
303+
return result;
304+
}
305+
306+
try {
307+
if (pPropValue != nullptr) {
308+
switch (propName) {
309+
case UR_PLATFORM_INFO_ADAPTER: {
310+
ur_adapter_handle_t *handles =
311+
reinterpret_cast<ur_adapter_handle_t *>(pPropValue);
312+
size_t nelements = *pPropSizeRet / sizeof(ur_adapter_handle_t);
313+
for (size_t i = 0; i < nelements; ++i) {
314+
if (handles[i] != nullptr) {
315+
handles[i] = reinterpret_cast<ur_adapter_handle_t>(
316+
context->factories.ur_adapter_factory.getInstance(
317+
handles[i], dditable));
318+
}
319+
}
320+
} break;
321+
default: {
322+
} break;
323+
}
324+
}
325+
} catch (std::bad_alloc &) {
326+
result = UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
327+
}
328+
296329
return result;
297330
}
298331

source/loader/ur_libapi.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ ur_result_t UR_APICALL urPlatformGet(
557557
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
558558
/// + `NULL == hPlatform`
559559
/// - ::UR_RESULT_ERROR_INVALID_ENUMERATION
560-
/// + `::UR_PLATFORM_INFO_BACKEND < propName`
560+
/// + `::UR_PLATFORM_INFO_ADAPTER < propName`
561561
/// - ::UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION
562562
/// + If `propName` is not supported by the adapter.
563563
/// - ::UR_RESULT_ERROR_INVALID_SIZE

source/ur_api.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ ur_result_t UR_APICALL urPlatformGet(
501501
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
502502
/// + `NULL == hPlatform`
503503
/// - ::UR_RESULT_ERROR_INVALID_ENUMERATION
504-
/// + `::UR_PLATFORM_INFO_BACKEND < propName`
504+
/// + `::UR_PLATFORM_INFO_ADAPTER < propName`
505505
/// - ::UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION
506506
/// + If `propName` is not supported by the adapter.
507507
/// - ::UR_RESULT_ERROR_INVALID_SIZE

test/conformance/platform/urPlatformGetInfo.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ INSTANTIATE_TEST_SUITE_P(
1919
urPlatformGetInfo, urPlatformGetInfoTest,
2020
::testing::Values(UR_PLATFORM_INFO_NAME, UR_PLATFORM_INFO_VENDOR_NAME,
2121
UR_PLATFORM_INFO_VERSION, UR_PLATFORM_INFO_EXTENSIONS,
22-
UR_PLATFORM_INFO_PROFILE, UR_PLATFORM_INFO_BACKEND),
22+
UR_PLATFORM_INFO_PROFILE, UR_PLATFORM_INFO_BACKEND,
23+
UR_PLATFORM_INFO_ADAPTER),
2324
[](const ::testing::TestParamInfo<ur_platform_info_t> &info) {
2425
std::stringstream ss;
2526
ss << info.param;
@@ -38,8 +39,29 @@ TEST_P(urPlatformGetInfoTest, Success) {
3839
std::vector<char> name(size);
3940
ASSERT_SUCCESS(
4041
urPlatformGetInfo(platform, info_type, size, name.data(), nullptr));
41-
if (info_type != UR_PLATFORM_INFO_BACKEND) {
42+
switch (info_type) {
43+
case UR_PLATFORM_INFO_NAME:
44+
case UR_PLATFORM_INFO_VENDOR_NAME:
45+
case UR_PLATFORM_INFO_VERSION:
46+
case UR_PLATFORM_INFO_EXTENSIONS:
47+
case UR_PLATFORM_INFO_PROFILE: {
4248
ASSERT_EQ(size, std::strlen(name.data()) + 1);
49+
break;
50+
}
51+
case UR_PLATFORM_INFO_BACKEND: {
52+
ASSERT_EQ(size, sizeof(ur_platform_backend_t));
53+
break;
54+
}
55+
case UR_PLATFORM_INFO_ADAPTER: {
56+
auto queried_adapter =
57+
*reinterpret_cast<ur_adapter_handle_t *>(name.data());
58+
auto adapter_found =
59+
std::find(adapters.begin(), adapters.end(), queried_adapter);
60+
ASSERT_NE(adapter_found, adapters.end());
61+
break;
62+
}
63+
default:
64+
break;
4365
}
4466
}
4567

tools/urinfo/urinfo.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ inline void printPlatformInfos(ur_platform_handle_t hPlatform,
4545
std::cout << prefix;
4646
printPlatformInfo<ur_platform_backend_t>(hPlatform,
4747
UR_PLATFORM_INFO_BACKEND);
48+
std::cout << prefix;
49+
printPlatformInfo<ur_adapter_handle_t>(hPlatform, UR_PLATFORM_INFO_ADAPTER);
4850
}
4951

5052
inline void printDeviceInfos(ur_device_handle_t hDevice,

0 commit comments

Comments
 (0)