Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[GPU] Enumerate MCDM adapters for device ID discovery on Windows (#38636)

Aleksandr Voron0414f7e6883b
diff --git a/src/plugins/intel_gpu/src/runtime/driver_dev_id.cpp b/src/plugins/intel_gpu/src/runtime/driver_dev_id.cpp
new file mode 100644
index 00000000000..24eea3b02be
--- /dev/null
+++ b/src/plugins/intel_gpu/src/runtime/driver_dev_id.cpp
@@ -0,0 +1,186 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "driver_dev_id.hpp"
+
+#include <algorithm>
+#include <cstdio>
+#include <cstring>
+#include <fstream>
+#include <string>
+#include <vector>
+
+#include "openvino/util/log.hpp"
+
+#ifdef _WIN32
+# ifndef WIN32_LEAN_AND_MEAN
+# define WIN32_LEAN_AND_MEAN
+# endif
+# ifndef NOMINMAX
+# define NOMINMAX
+# endif
+// The include order is significant: devguid.h needs DEFINE_GUID from windows.h, and initguid.h makes
+// devguid.h define the GUID_DEVCLASS_* constants in this translation unit.
+// clang-format off
+# include <windows.h>
+# include <initguid.h>
+# include <devguid.h>
+# include <setupapi.h>
+// clang-format on
+#endif
+
+namespace cldnn {
+
+namespace {
+
+constexpr int intel_vendor_id = 0x8086;
+
+bool all_equal(const std::vector<int>& ids) {
+ return std::all_of(ids.begin(), ids.end(), [&ids](int id) {
+ return id == ids.front();
+ });
+}
+
+void remove_unused_ids(std::vector<int>& ids) {
+ const std::vector<int> unused_ids = {0x4905, 0x4906, 0x4907, 0x4908};
+ ids.erase(std::remove_if(ids.begin(),
+ ids.end(),
+ [&unused_ids](int id) {
+ return std::find(unused_ids.begin(), unused_ids.end(), id) != unused_ids.end();
+ }),
+ ids.end());
+}
+
+#ifdef _WIN32
+// Collects Intel device IDs of display adapters into `all` and of any adapters located at `pci` into `matched`.
+// Compute accelerators are used for location matching only, as the class also covers non-GPU devices like NPUs.
+void collect_ids(const std::optional<pci_bus_info>& pci, std::vector<int>& all, std::vector<int>& matched) {
+ // SetupAPI exposes bus, device and function only, so a non-zero PCI domain cannot be matched
+ const bool match_location = pci.has_value() && pci->pci_domain == 0;
+ const GUID* device_classes[] = {&GUID_DEVCLASS_COMPUTEACCELERATOR, &GUID_DEVCLASS_DISPLAY};
+
+ for (const GUID* device_class : device_classes) {
+ HDEVINFO device_info_set = SetupDiGetClassDevsA(device_class, NULL, NULL, DIGCF_PRESENT);
+ if (device_info_set == INVALID_HANDLE_VALUE) {
+ continue;
+ }
+
+ SP_DEVINFO_DATA devinfo_data;
+ std::memset(&devinfo_data, 0, sizeof(devinfo_data));
+ devinfo_data.cbSize = sizeof(devinfo_data);
+
+ for (DWORD dev_idx = 0; SetupDiEnumDeviceInfo(device_info_set, dev_idx, &devinfo_data); dev_idx++) {
+ const size_t kBufSize = 512;
+ char buf[kBufSize];
+ if (!SetupDiGetDeviceInstanceIdA(device_info_set, &devinfo_data, buf, kBufSize, NULL)) {
+ continue;
+ }
+
+ char* vendor_pos = std::strstr(buf, "VEN_");
+ if (vendor_pos == NULL || std::stoi(vendor_pos + 4, NULL, 16) != intel_vendor_id) {
+ continue;
+ }
+ char* device_pos = std::strstr(vendor_pos, "DEV_");
+ if (device_pos == NULL) {
+ continue;
+ }
+ const int device_id = std::stoi(device_pos + 4, NULL, 16);
+ if (device_class == &GUID_DEVCLASS_DISPLAY) {
+ all.push_back(device_id);
+ }
+
+ if (match_location) {
+ DWORD bus = 0;
+ DWORD address = 0;
+ DWORD property_type = 0;
+ if (SetupDiGetDeviceRegistryPropertyA(device_info_set,
+ &devinfo_data,
+ SPDRP_BUSNUMBER,
+ &property_type,
+ reinterpret_cast<BYTE*>(&bus),
+ sizeof(bus),
+ nullptr) &&
+ property_type == REG_DWORD &&
+ SetupDiGetDeviceRegistryPropertyA(device_info_set,
+ &devinfo_data,
+ SPDRP_ADDRESS,
+ &property_type,
+ reinterpret_cast<BYTE*>(&address),
+ sizeof(address),
+ nullptr) &&
+ property_type == REG_DWORD && bus == pci->pci_bus && HIWORD(address) == pci->pci_device &&
+ LOWORD(address) == pci->pci_function) {
+ matched.push_back(device_id);
+ }
+ }
+ }
+
+ SetupDiDestroyDeviceInfoList(device_info_set);
+ }
+}
+#elif defined(__linux__)
+bool read_intel_device_id(const std::string& dev_base, int& device_id) {
+ std::ifstream vendor_ifs(dev_base + "vendor");
+ int ven_id = 0;
+ if (!(vendor_ifs >> std::hex >> ven_id) || ven_id != intel_vendor_id) {
+ return false;
+ }
+ std::ifstream device_ifs(dev_base + "device");
+ return static_cast<bool>(device_ifs >> std::hex >> device_id);
+}
+
+// Collects the ID of the device located at `pci` into `matched` and the ID of the integrated GPU at the
+// legacy fixed location into `all`.
+void collect_ids(const std::optional<pci_bus_info>& pci, std::vector<int>& all, std::vector<int>& matched) {
+ int device_id = 0;
+ if (pci.has_value()) {
+ char location[64];
+ std::snprintf(location,
+ sizeof(location),
+ "/sys/bus/pci/devices/%04x:%02x:%02x.%x/",
+ pci->pci_domain,
+ pci->pci_bus,
+ pci->pci_device,
+ pci->pci_function);
+ if (read_intel_device_id(location, device_id)) {
+ matched.push_back(device_id);
+ }
+ }
+ if (read_intel_device_id("/sys/devices/pci0000:00/0000:00:02.0/", device_id)) {
+ all.push_back(device_id);
+ }
+}
+#endif
+
+} // namespace
+
+int driver_dev_id(const std::optional<pci_bus_info>& pci) {
+ std::vector<int> all;
+ std::vector<int> matched;
+#if defined(_WIN32) || defined(__linux__)
+ collect_ids(pci, all, matched);
+#endif
+ remove_unused_ids(all);
+ remove_unused_ids(matched);
+
+ if (!matched.empty() && all_equal(matched)) {
+ return matched.front();
+ }
+ if (all.empty()) {
+ return 0;
+ }
+ if (all_equal(all)) {
+ return all.front();
+ }
+
+ char last_id[16];
+ std::snprintf(last_id, sizeof(last_id), "0x%X", all.back());
+ OPENVINO_WARN("[GPU] Unable to match the device to a single Intel PCI device ID among ",
+ all.size(),
+ " adapters; using the last enumerated one: ",
+ last_id);
+ return all.back();
+}
+
+} // namespace cldnn
diff --git a/src/plugins/intel_gpu/src/runtime/driver_dev_id.hpp b/src/plugins/intel_gpu/src/runtime/driver_dev_id.hpp
new file mode 100644
index 00000000000..fc0eae0184e
--- /dev/null
+++ b/src/plugins/intel_gpu/src/runtime/driver_dev_id.hpp
@@ -0,0 +1,20 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <optional>
+
+#include "intel_gpu/runtime/device_info.hpp"
+
+namespace cldnn {
+
+/// @brief Fallback lookup of the Intel PCI device ID through OS facilities (SetupAPI on Windows, sysfs on Linux).
+/// @param pci PCI location of the device the ID is requested for, if the runtime can report it.
+/// @return The device ID or 0 if it cannot be determined.
+/// @details If the OS entry for @p pci is found, its ID is returned. Otherwise, if all Intel display adapters share
+/// one ID, that ID is returned. If they differ, a warning is logged and the last enumerated ID is returned.
+int driver_dev_id(const std::optional<pci_bus_info>& pci);
+
+} // namespace cldnn
diff --git a/src/plugins/intel_gpu/src/runtime/ocl/ocl_device.cpp b/src/plugins/intel_gpu/src/runtime/ocl/ocl_device.cpp
index 7122b5dff52..64ee1671c41 100644
--- a/src/plugins/intel_gpu/src/runtime/ocl/ocl_device.cpp
+++ b/src/plugins/intel_gpu/src/runtime/ocl/ocl_device.cpp
@@ -19,11 +19,13 @@
#include <iostream>
#include <limits>
#include <map>
+#include <optional>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
+#include "driver_dev_id.hpp"
#include "intel_gpu/runtime/debug_configuration.hpp"
#include "ocl_common.hpp"
@@ -34,8 +36,6 @@
# ifndef NOMINMAX
# define NOMINMAX
# endif
-# include <devguid.h>
-# include <setupapi.h>
# include <windows.h>
# include <cstring>
@@ -84,73 +84,12 @@ gpu_arch convert_ngen_arch(ngen::HW gpu_arch) {
}
#endif
-int driver_dev_id() {
- const std::vector<int> unused_ids = {0x4905, 0x4906, 0x4907, 0x4908};
- std::vector<int> result;
-
-#ifdef _WIN32
- {
- HDEVINFO device_info_set = SetupDiGetClassDevsA(&GUID_DEVCLASS_DISPLAY, NULL, NULL, DIGCF_PRESENT);
- if (device_info_set == INVALID_HANDLE_VALUE)
- return 0;
-
- SP_DEVINFO_DATA devinfo_data;
- std::memset(&devinfo_data, 0, sizeof(devinfo_data));
- devinfo_data.cbSize = sizeof(devinfo_data);
-
- for (DWORD dev_idx = 0; SetupDiEnumDeviceInfo(device_info_set, dev_idx, &devinfo_data); dev_idx++) {
- const size_t kBufSize = 512;
- char buf[kBufSize];
- if (!SetupDiGetDeviceInstanceIdA(device_info_set, &devinfo_data, buf, kBufSize, NULL)) {
- continue;
- }
-
- char* vendor_pos = std::strstr(buf, "VEN_");
- if (vendor_pos != NULL && std::stoi(vendor_pos + 4, NULL, 16) == 0x8086) {
- char* device_pos = strstr(vendor_pos, "DEV_");
- if (device_pos != NULL) {
- result.push_back(std::stoi(device_pos + 4, NULL, 16));
- }
- }
- }
-
- if (device_info_set) {
- SetupDiDestroyDeviceInfoList(device_info_set);
- }
- }
-#elif defined(__linux__)
- {
- std::string dev_base{"/sys/devices/pci0000:00/0000:00:02.0/"};
- std::ifstream ifs(dev_base + "vendor");
- if (ifs.good()) {
- int ven_id;
- ifs >> std::hex >> ven_id;
- ifs.close();
- if (ven_id == 0x8086) {
- ifs.open(dev_base + "device");
- if (ifs.good()) {
- int res = 0;
- ifs >> std::hex >> res;
- result.push_back(res);
- }
- }
- }
- }
-#endif
-
- auto id_itr = result.begin();
- while (id_itr != result.end()) {
- if (std::find(unused_ids.begin(), unused_ids.end(), *id_itr) != unused_ids.end()) {
- id_itr = result.erase(id_itr);
- } else {
- id_itr++;
- }
- }
-
- if (result.empty()) {
- return 0;
+std::optional<pci_bus_info> get_pci_bus_info(const cl::Device& device) {
+ cl_device_pci_bus_info_khr pci_info{};
+ if (clGetDeviceInfo(device.get(), CL_DEVICE_PCI_BUS_INFO_KHR, sizeof(pci_info), &pci_info, nullptr) != CL_SUCCESS) {
+ return std::nullopt;
}
- return result.back();
+ return pci_bus_info{pci_info.pci_domain, pci_info.pci_bus, pci_info.pci_device, pci_info.pci_function};
}
device_type get_device_type(const cl::Device& device) {
@@ -192,7 +131,7 @@ bool get_imad_support(const cl::Device& device) {
if (get_device_type(device) == device_type::integrated_gpu) {
const std::vector<int> imad_ids = {0x9A40, 0x9A49, 0x9A59, 0x9AD9, 0x9A60, 0x9A68, 0x9A70, 0x9A78, 0x9A7F, 0x9AF8, 0x9AC0, 0x9AC9};
- int dev_id = driver_dev_id();
+ int dev_id = driver_dev_id(get_pci_bus_info(device));
if (dev_id == 0) {
return false;
}
@@ -323,7 +262,7 @@ device_info init_device_info(const cl::Device& device, const cl::Context& contex
0};
} else {
info.gfx_ver = {0, 0, 0};
- info.device_id = driver_dev_id();
+ info.device_id = driver_dev_id(get_pci_bus_info(device));
info.num_slices = 0;
info.num_sub_slices_per_slice = 0;
info.num_eus_per_sub_slice = 0;
diff --git a/src/plugins/intel_gpu/src/runtime/sources.cmake b/src/plugins/intel_gpu/src/runtime/sources.cmake
index 79b672f771f..14d541528eb 100644
--- a/src/plugins/intel_gpu/src/runtime/sources.cmake
+++ b/src/plugins/intel_gpu/src/runtime/sources.cmake
@@ -7,6 +7,8 @@ set(GPU_RUNTIME_MAIN_SOURCES
${CMAKE_CURRENT_LIST_DIR}/device.cpp
${CMAKE_CURRENT_LIST_DIR}/device_query.cpp
${CMAKE_CURRENT_LIST_DIR}/dispatch_probe.cpp
+ ${CMAKE_CURRENT_LIST_DIR}/driver_dev_id.cpp
+ ${CMAKE_CURRENT_LIST_DIR}/driver_dev_id.hpp
${CMAKE_CURRENT_LIST_DIR}/engine.cpp
${CMAKE_CURRENT_LIST_DIR}/engine_configuration.cpp
${CMAKE_CURRENT_LIST_DIR}/event.cpp
diff --git a/src/plugins/intel_gpu/src/runtime/sycl/sycl_device.cpp b/src/plugins/intel_gpu/src/runtime/sycl/sycl_device.cpp
index a23a0b80b2d..2e852e85187 100644
--- a/src/plugins/intel_gpu/src/runtime/sycl/sycl_device.cpp
+++ b/src/plugins/intel_gpu/src/runtime/sycl/sycl_device.cpp
@@ -13,6 +13,7 @@
#include "sycl_device.hpp"
#include "sycl_common.hpp"
+#include "driver_dev_id.hpp"
#include "intel_gpu/runtime/debug_configuration.hpp"
#include <map>
@@ -27,6 +28,8 @@
#include <chrono>
#include <fstream>
#include <iostream>
+#include <optional>
+#include <sstream>
#include <utility>
#ifdef _WIN32
@@ -37,8 +40,6 @@
#define NOMINMAX
#endif
#include <windows.h>
-#include <setupapi.h>
-#include <devguid.h>
#include <cstring>
#else
#include <unistd.h>
@@ -75,74 +76,26 @@ gpu_arch convert_ngen_arch(ngen::HW gpu_arch) {
}
#endif
-int driver_dev_id() {
- const std::vector<int> unused_ids = {
- 0x4905, 0x4906, 0x4907, 0x4908
- };
- std::vector<int> result;
-
-#ifdef _WIN32
- {
- HDEVINFO device_info_set = SetupDiGetClassDevsA(&GUID_DEVCLASS_DISPLAY, NULL, NULL, DIGCF_PRESENT);
- if (device_info_set == INVALID_HANDLE_VALUE)
- return 0;
-
- SP_DEVINFO_DATA devinfo_data;
- std::memset(&devinfo_data, 0, sizeof(devinfo_data));
- devinfo_data.cbSize = sizeof(devinfo_data);
-
- for (DWORD dev_idx = 0; SetupDiEnumDeviceInfo(device_info_set, dev_idx, &devinfo_data); dev_idx++) {
- const size_t kBufSize = 512;
- char buf[kBufSize];
- if (!SetupDiGetDeviceInstanceIdA(device_info_set, &devinfo_data, buf, kBufSize, NULL)) {
- continue;
- }
-
- char* vendor_pos = std::strstr(buf, "VEN_");
- if (vendor_pos != NULL && std::stoi(vendor_pos + 4, NULL, 16) == 0x8086) {
- char* device_pos = strstr(vendor_pos, "DEV_");
- if (device_pos != NULL) {
- result.push_back(std::stoi(device_pos + 4, NULL, 16));
- }
- }
- }
-
- if (device_info_set) {
- SetupDiDestroyDeviceInfoList(device_info_set);
- }
+std::optional<pci_bus_info> get_pci_bus_info(const ::sycl::device& device) {
+ if (!device.has(::sycl::aspect::ext_intel_pci_address)) {
+ return std::nullopt;
}
-#elif defined(__linux__)
- {
- std::string dev_base{ "/sys/devices/pci0000:00/0000:00:02.0/" };
- std::ifstream ifs(dev_base + "vendor");
- if (ifs.good()) {
- int ven_id;
- ifs >> std::hex >> ven_id;
- ifs.close();
- if (ven_id == 0x8086) {
- ifs.open(dev_base + "device");
- if (ifs.good()) {
- int res = 0;
- ifs >> std::hex >> res;
- result.push_back(res);
- }
- }
- }
- }
-#endif
- auto id_itr = result.begin();
- while (id_itr != result.end()) {
- if (std::find(unused_ids.begin(), unused_ids.end(), *id_itr) != unused_ids.end())
- id_itr = result.erase(id_itr);
- else
- id_itr++;
+ // The address has the BDF form, e.g. "0000:03:00.0"
+ std::istringstream pci_address(device.get_info<::sycl::ext::intel::info::device::pci_address>());
+ unsigned int domain = 0;
+ unsigned int bus = 0;
+ unsigned int dev = 0;
+ unsigned int function = 0;
+ char domain_separator = 0;
+ char bus_separator = 0;
+ char function_separator = 0;
+ pci_address >> std::hex >> domain >> domain_separator >> bus >> bus_separator >> dev >> function_separator >> function;
+ if (pci_address.fail() || pci_address.peek() != std::char_traits<char>::eof() || domain_separator != ':' ||
+ bus_separator != ':' || function_separator != '.' || bus > 0xff || dev > 0x1f || function > 7) {
+ return std::nullopt;
}
-
- if (result.empty())
- return 0;
- else
- return result.back();
+ return pci_bus_info{domain, bus, dev, function};
}
device_type get_device_type(const ::sycl::device& device) {
@@ -166,7 +119,7 @@ bool get_imad_support(const ::sycl::device& device) {
0x9A60, 0x9A68, 0x9A70, 0x9A78,
0x9A7F, 0x9AF8, 0x9AC0, 0x9AC9
};
- int dev_id = driver_dev_id();
+ int dev_id = driver_dev_id(get_pci_bus_info(device));
if (dev_id == 0)
return false;
@@ -284,7 +237,7 @@ device_info init_device_info(const ::sycl::device& device, const ::sycl::context
info.gfx_ver = {0, 0, 0}; // no corresponding info in SYCL
} else {
info.gfx_ver = {0, 0, 0};
- info.device_id = driver_dev_id();
+ info.device_id = driver_dev_id(get_pci_bus_info(device));
info.num_slices = 0;
info.num_sub_slices_per_slice = 0;
info.num_eus_per_sub_slice = 0;