Skip to content

Commit 0b04127

Browse files
authored
Merge pull request #2365 from aarongreig/aaron/addAdapterPlatformQuery
Add query to retrieve adapter handle from platform.
2 parents 9249b69 + 51df6cf commit 0b04127

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
@@ -1093,6 +1093,8 @@ typedef enum ur_platform_info_t {
10931093
///< info needs to be dynamically queried.
10941094
UR_PLATFORM_INFO_BACKEND = 6, ///< [::ur_platform_backend_t] The backend of the platform. Identifies the
10951095
///< native backend adapter implementing this platform.
1096+
UR_PLATFORM_INFO_ADAPTER = 7, ///< [::ur_adapter_handle_t] The adapter handle associated with the
1097+
///< platform.
10961098
/// @cond
10971099
UR_PLATFORM_INFO_FORCE_UINT32 = 0x7fffffff
10981100
/// @endcond
@@ -1118,7 +1120,7 @@ typedef enum ur_platform_info_t {
11181120
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
11191121
/// + `NULL == hPlatform`
11201122
/// - ::UR_RESULT_ERROR_INVALID_ENUMERATION
1121-
/// + `::UR_PLATFORM_INFO_BACKEND < propName`
1123+
/// + `::UR_PLATFORM_INFO_ADAPTER < propName`
11221124
/// - ::UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION
11231125
/// + If `propName` is not supported by the adapter.
11241126
/// - ::UR_RESULT_ERROR_INVALID_SIZE

include/ur_print.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2074,6 +2074,9 @@ inline std::ostream &operator<<(std::ostream &os, enum ur_platform_info_t value)
20742074
case UR_PLATFORM_INFO_BACKEND:
20752075
os << "UR_PLATFORM_INFO_BACKEND";
20762076
break;
2077+
case UR_PLATFORM_INFO_ADAPTER:
2078+
os << "UR_PLATFORM_INFO_ADAPTER";
2079+
break;
20772080
default:
20782081
os << "unknown enumerator";
20792082
break;
@@ -2127,6 +2130,19 @@ inline ur_result_t printTagged(std::ostream &os, const void *ptr, ur_platform_in
21272130

21282131
os << ")";
21292132
} break;
2133+
case UR_PLATFORM_INFO_ADAPTER: {
2134+
const ur_adapter_handle_t *tptr = (const ur_adapter_handle_t *)ptr;
2135+
if (sizeof(ur_adapter_handle_t) > size) {
2136+
os << "invalid size (is: " << size << ", expected: >=" << sizeof(ur_adapter_handle_t) << ")";
2137+
return UR_RESULT_ERROR_INVALID_SIZE;
2138+
}
2139+
os << (const void *)(tptr) << " (";
2140+
2141+
ur::details::printPtr(os,
2142+
*tptr);
2143+
2144+
os << ")";
2145+
} break;
21302146
default:
21312147
os << "unknown enumerator";
21322148
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;
@@ -39,8 +40,29 @@ TEST_P(urPlatformGetInfoTest, Success) {
3940
std::vector<char> name(size);
4041
ASSERT_SUCCESS(
4142
urPlatformGetInfo(platform, info_type, size, name.data(), nullptr));
42-
if (info_type != UR_PLATFORM_INFO_BACKEND) {
43+
switch (info_type) {
44+
case UR_PLATFORM_INFO_NAME:
45+
case UR_PLATFORM_INFO_VENDOR_NAME:
46+
case UR_PLATFORM_INFO_VERSION:
47+
case UR_PLATFORM_INFO_EXTENSIONS:
48+
case UR_PLATFORM_INFO_PROFILE: {
4349
ASSERT_EQ(size, std::strlen(name.data()) + 1);
50+
break;
51+
}
52+
case UR_PLATFORM_INFO_BACKEND: {
53+
ASSERT_EQ(size, sizeof(ur_platform_backend_t));
54+
break;
55+
}
56+
case UR_PLATFORM_INFO_ADAPTER: {
57+
auto queried_adapter =
58+
*reinterpret_cast<ur_adapter_handle_t *>(name.data());
59+
auto adapter_found =
60+
std::find(adapters.begin(), adapters.end(), queried_adapter);
61+
ASSERT_NE(adapter_found, adapters.end());
62+
break;
63+
}
64+
default:
65+
break;
4466
}
4567
}
4668

tools/urinfo/urinfo.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ inline void printPlatformInfos(ur_platform_handle_t hPlatform,
4747
std::cout << prefix;
4848
printPlatformInfo<ur_platform_backend_t>(hPlatform,
4949
UR_PLATFORM_INFO_BACKEND);
50+
std::cout << prefix;
51+
printPlatformInfo<ur_adapter_handle_t>(hPlatform, UR_PLATFORM_INFO_ADAPTER);
5052
}
5153

5254
inline void printDeviceInfos(ur_device_handle_t hDevice,

0 commit comments

Comments
 (0)