Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.
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
11 changes: 11 additions & 0 deletions engine/common/hardware_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ struct NvidiaAddInfo {
};
struct AmdAddInfo {};
using GPUAddInfo = std::variant<NvidiaAddInfo, AmdAddInfo>;

enum class GpuType {
kGpuTypeOther = 0,
kGpuTypeIntegrated = 1,
kGpuTypeDiscrete = 2,
kGpuTypeVirtual = 3,
kGpuTypeCpu = 4,
kGpuTypeMaxEnum = 0x7FFFFFFF
};

struct GPU {
std::string id;
uint32_t device_id;
Expand All @@ -80,6 +90,7 @@ struct GPU {
std::string uuid;
bool is_activated = true;
std::string vendor;
GpuType gpu_type;
};

inline Json::Value ToJson(const std::vector<GPU>& gpus) {
Expand Down
36 changes: 26 additions & 10 deletions engine/services/hardware_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -322,23 +322,39 @@ void HardwareService::UpdateHardwareInfos() {
}
}
CTL_INF("Activated GPUs before: " << debug_b);
auto has_nvidia = [&gpus] {
for (auto const& g : gpus) {
if (g.vendor == cortex::hw::kNvidiaStr) {
return true;
}
}
return false;
}();

for (auto const& gpu : gpus) {
// ignore error
// Note: only support NVIDIA for now, so hardware_id = software_id
if (db_service_->HasHardwareEntry(gpu.uuid)) {
auto res = db_service_->UpdateHardwareEntry(gpu.uuid, std::stoi(gpu.id),
std::stoi(gpu.id));
std::stoi(gpu.id));
if (res.has_error()) {
CTL_WRN(res.error());
}
} else {
auto res =
db_service_->AddHardwareEntry(HwEntry{.uuid = gpu.uuid,
.type = "gpu",
.hardware_id = std::stoi(gpu.id),
.software_id = std::stoi(gpu.id),
.activated = true,
.priority = INT_MAX});
// iGPU should be deactivated by default
// Only activate Nvidia GPUs if both AMD and Nvidia GPUs exists
auto activated = [&gpu, &gpus, has_nvidia] {
if (gpu.gpu_type != cortex::hw::GpuType::kGpuTypeDiscrete)
return false;
if (has_nvidia && gpu.vendor != cortex::hw::kNvidiaStr)
return false;
return true;
};
auto res = db_service_->AddHardwareEntry(
HwEntry{.uuid = gpu.uuid,
.type = "gpu",
.hardware_id = std::stoi(gpu.id),
.software_id = std::stoi(gpu.id),
.activated = activated(),
.priority = INT_MAX});
if (res.has_error()) {
CTL_WRN(res.error());
}
Expand Down
36 changes: 21 additions & 15 deletions engine/utils/hardware/gpu/vulkan/vulkan_gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,26 @@
#endif

namespace cortex::hw {
constexpr const uint32_t NVIDIA_VENDOR = 0x10DE;
constexpr const uint32_t AMD_VENDOR = 0x1002;
constexpr const uint32_t INTEL_VENDOR = 0x8086;
constexpr const uint32_t ARM_VENDOR = 0x13B5;
constexpr const uint32_t kNvidiaVendor = 0x10DE;
constexpr const uint32_t kAmdVendor = 0x1002;
constexpr const uint32_t kIntelVendor = 0x8086;
constexpr const uint32_t kArmVendor = 0x13B5;

constexpr const auto kAmdStr = "AMD";
constexpr const auto kNvidiaStr = "NVIDIA";
constexpr const auto kIntelStr = "INTEL";
constexpr const auto kArmStr = "ARM";

inline std::string GetVendorStr(uint32_t vendor_id) {
switch (vendor_id) {
case AMD_VENDOR:
return "AMD";
case NVIDIA_VENDOR:
return "NVIDIA";
case INTEL_VENDOR:
return "INTEL";
case ARM_VENDOR:
return "ARM";
case kAmdVendor:
return kAmdStr;
case kNvidiaVendor:
return kNvidiaStr;
case kIntelVendor:
return kIntelStr;
case kArmVendor:
return kArmStr;
default:
return std::to_string(vendor_id);
}
Expand Down Expand Up @@ -446,8 +451,8 @@ class VulkanGpu {
#endif
int free_vram_MiB =
total_vram_MiB > used_vram_MiB ? total_vram_MiB - used_vram_MiB : 0;
if (device_properties.vendorID == NVIDIA_VENDOR ||
device_properties.vendorID == AMD_VENDOR) {
if (device_properties.vendorID == kNvidiaVendor ||
device_properties.vendorID == kAmdVendor) {
gpus.emplace_back(cortex::hw::GPU{
.id = std::to_string(id),
.device_id = device_properties.deviceID,
Expand All @@ -457,7 +462,8 @@ class VulkanGpu {
.free_vram = free_vram_MiB,
.total_vram = total_vram_MiB,
.uuid = uuid_to_string(device_id_properties.deviceUUID),
.vendor = GetVendorStr(device_properties.vendorID)});
.vendor = GetVendorStr(device_properties.vendorID),
.gpu_type = static_cast<GpuType>(device_properties.deviceType)});
}
id++;
}
Expand Down
5 changes: 3 additions & 2 deletions engine/utils/hardware/gpu_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ inline std::vector<GPU> GetGPUInfo() {
.compute_cap = nvidia_gpus[i].compute_cap.value_or("unknown")};
vulkan_gpus[j].free_vram = std::stoll(nvidia_gpus[i].vram_free);
vulkan_gpus[j].total_vram = std::stoll(nvidia_gpus[i].vram_total);
vulkan_gpus[j].vendor = nvidia_gpus[i].vendor;
vulkan_gpus[j].vendor = nvidia_gpus[i].vendor;
}
}
}
Expand Down Expand Up @@ -55,7 +55,8 @@ inline std::vector<GPU> GetGPUInfo() {
.free_vram = std::stoi(n.vram_free),
.total_vram = std::stoi(n.vram_total),
.uuid = n.uuid,
.vendor = n.vendor});
.vendor = n.vendor,
.gpu_type = GpuType::kGpuTypeDiscrete});
}
return res;
}
Expand Down
Loading