Skip to content

Commit 1008730

Browse files
authored
[UR] Fixes for using ONEAPI_DEVICE_SELECTOR with linear ids (#18029)
Linear IDs (the `0` in `opencl:0`) did not function as intended when there was multiple platforms; each platform started counting at zero. This patch changes it so that they start counting at an appropriate number that should match `urinfo`. `urinfo` itself has also been updated to not display linear IDs when the device list is being filtered, as they will not be correct. This matches the behavior of sycl-ls and is different to the `--no-linear-ids` which changes the format of the output.
1 parent 38bed82 commit 1008730

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

unified-runtime/source/loader/ur_lib.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ ur_result_t urDeviceGetSelected(ur_platform_handle_t hPlatform,
454454
std::vector<DeviceSpec> acceptDeviceList;
455455
std::vector<DeviceSpec> discardDeviceList;
456456

457+
bool deviceIdsInUse = false;
457458
for (auto &termPair : mapODS) {
458459
std::string backend = termPair.first;
459460
// TODO: Figure out how to process all ODS errors rather than returning
@@ -578,6 +579,10 @@ ur_result_t urDeviceGetSelected(ur_platform_handle_t hPlatform,
578579
deviceList.push_back(DeviceSpec{DevicePartLevel::ROOT, hardwareType,
579580
firstDeviceId, 0, 0, nullptr});
580581
}
582+
583+
if (deviceList.back().rootId != DeviceIdTypeALL) {
584+
deviceIdsInUse = true;
585+
}
581586
}
582587
}
583588

@@ -621,7 +626,41 @@ ur_result_t urDeviceGetSelected(ur_platform_handle_t hPlatform,
621626
return UR_RESULT_ERROR_DEVICE_NOT_FOUND;
622627
}
623628

624-
DeviceIdType deviceCount = 0;
629+
uint32_t startIdCount = 0;
630+
if (deviceIdsInUse) {
631+
ur_adapter_handle_t adapter;
632+
if (UR_RESULT_SUCCESS !=
633+
urPlatformGetInfo(hPlatform, UR_PLATFORM_INFO_ADAPTER,
634+
sizeof(adapter), &adapter, nullptr)) {
635+
return UR_RESULT_ERROR_INVALID_PLATFORM;
636+
}
637+
638+
uint32_t numPlatforms;
639+
if (UR_RESULT_SUCCESS !=
640+
urPlatformGet(adapter, 0, nullptr, &numPlatforms)) {
641+
return UR_RESULT_ERROR_INVALID_PLATFORM;
642+
}
643+
std::vector<ur_platform_handle_t> platforms;
644+
platforms.resize(numPlatforms);
645+
if (UR_RESULT_SUCCESS !=
646+
urPlatformGet(adapter, numPlatforms, platforms.data(), nullptr)) {
647+
return UR_RESULT_ERROR_INVALID_PLATFORM;
648+
}
649+
650+
for (auto p : platforms) {
651+
if (p == hPlatform) {
652+
break;
653+
}
654+
uint32_t deviceCount;
655+
if (UR_RESULT_SUCCESS !=
656+
urDeviceGet(p, UR_DEVICE_TYPE_ALL, 0, nullptr, &deviceCount)) {
657+
return UR_RESULT_ERROR_INVALID_PLATFORM;
658+
}
659+
startIdCount += deviceCount;
660+
}
661+
}
662+
663+
DeviceIdType deviceCount = startIdCount;
625664
std::transform(rootDeviceHandles.cbegin(), rootDeviceHandles.cend(),
626665
std::back_inserter(rootDevices),
627666
[&](ur_device_handle_t urDeviceHandle) {

unified-runtime/tools/urinfo/urinfo.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct app {
1717
bool verbose = false;
1818
bool linear_ids = true;
1919
bool ignore_device_selector = false;
20+
bool using_environment_selector = false;
2021
ur_loader_config_handle_t loaderConfig = nullptr;
2122
std::vector<ur_adapter_handle_t> adapters;
2223
std::unordered_map<ur_adapter_handle_t, std::vector<ur_platform_handle_t>>
@@ -34,6 +35,7 @@ struct app {
3435
"To see all devices, use the "
3536
"--ignore-device-selector CLI option.\n\n",
3637
device_selector);
38+
using_environment_selector = true;
3739
}
3840
}
3941
UR_CHECK(urLoaderConfigCreate(&loaderConfig));
@@ -139,7 +141,12 @@ devices which are currently visible in the local execution environment.
139141
auto device = devices[deviceIndex];
140142
auto device_type = urinfo::getDeviceType(device);
141143

142-
if (linear_ids) {
144+
// If we are using ONEAPI_DEVICE_SELECTOR, then the device id numbers
145+
// won't match, so don't print them. This matches the behavior of
146+
// sycl-ls.
147+
if (using_environment_selector) {
148+
std::cout << "[" << adapter_backend << ":" << device_type << "]";
149+
} else if (linear_ids) {
143150
std::cout << "[" << adapter_backend << ":" << device_type << "]";
144151
std::cout << "[" << adapter_backend << ":" << adapter_device_id
145152
<< "]";

0 commit comments

Comments
 (0)