Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GPU: Add VendorID and DeviceID information under Windows #83

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ int main(int argc, char** argv) {
std::cout << gpu.frequency_MHz() << std::endl;
std::cout << std::left << std::setw(20) << " cores:";
std::cout << gpu.num_cores() << std::endl;
std::cout << std::left << std::setw(20) << " vendor_id:";
std::cout << gpu.vendor_id() << std::endl;
std::cout << std::left << std::setw(20) << " device_id:";
std::cout << gpu.device_id() << std::endl;
}

hwinfo::Memory memory;
Expand Down
2 changes: 2 additions & 0 deletions include/hwinfo/gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class GPU {
HWI_NODISCARD int64_t frequency_MHz() const;
HWI_NODISCARD int num_cores() const;
HWI_NODISCARD int id() const;
HWI_NODISCARD const std::string& vendor_id() const;
HWI_NODISCARD const std::string& device_id() const;

private:
GPU() = default;
Expand Down
29 changes: 29 additions & 0 deletions include/hwinfo/utils/stringutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,41 @@
#include <cstdint>
#include <cstring>
#include <locale>
#include <regex>
#include <string>
#include <vector>

namespace hwinfo {
namespace utils {

/**
* Replaces an occurence once in the entire string. Stops at first match
*
* @param input The input string
* @param from The token to match
* @param to The token to use as a replace with matched ones
*/
inline bool replaceOnce(std::string& input, const std::string& from, const std::string& to) {
size_t start_pos = input.find(from);

if (start_pos == std::string::npos) return false;

input.replace(start_pos, from.length(), to);

return true;
}

/**
* Replaces all occurences in the entire string.
*
* @param input The input string
* @param from The token to match
* @param to The token to use as a replace with matched ones
*/
inline void replaceAll(std::string& input, const char from, const char to) {
std::replace(input.begin(), input.end(), from, to);
}

/**
* remove all white spaces (' ', '\t', '\n') from start and end of input
* inplace!
Expand Down
6 changes: 6 additions & 0 deletions src/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ int64_t GPU::frequency_MHz() const { return _frequency_MHz; }
// _____________________________________________________________________________________________________________________
int GPU::num_cores() const { return _num_cores; }

// _____________________________________________________________________________________________________________________
const std::string& GPU::vendor_id() const { return _vendor_id; }

// _____________________________________________________________________________________________________________________
const std::string& GPU::device_id() const { return _device_id; }

} // namespace hwinfo
17 changes: 16 additions & 1 deletion src/windows/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace hwinfo {
std::vector<GPU> getAllGPUs() {
utils::WMI::_WMI wmi;
const std::wstring query_string(
L"SELECT Name, AdapterCompatibility, DriverVersion, AdapterRam "
L"SELECT Name, AdapterCompatibility, DriverVersion, AdapterRam, PNPDeviceID "
L"FROM WIN32_VideoController");
bool success = wmi.execute_query(query_string);
if (!success) {
Expand Down Expand Up @@ -60,6 +60,21 @@ std::vector<GPU> getAllGPUs() {
if (SUCCEEDED(hr)) {
gpu._memory_Bytes = vt_prop.uintVal;
}
hr = obj->Get(L"PNPDeviceID", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
std::string ret = utils::wstring_to_std_string(vt_prop.bstrVal);
if (utils::starts_with(ret, "PCI\\")) {
utils::replaceOnce(ret, "PCI\\", "");
std::vector<std::string> ids = utils::split(ret, "&");
gpu._vendor_id = ids[0];
utils::replaceOnce(gpu._vendor_id, "VEN_", "");
gpu._device_id = ids[1];
utils::replaceOnce(gpu._device_id, "DEV_", "");
} else {
gpu._vendor_id = "0";
gpu._device_id = "0";
}
}
VariantClear(&vt_prop);
obj->Release();
gpus.push_back(std::move(gpu));
Expand Down
Loading