🐜 fix(scripts): Use class codes to check for lspci GPU devices (#168)

Fixes gpu_helpers returning "Non-VGA" devices as GPUs.

I was curious about DeepSeek's so I tried it's hand with this as my head
isn't quite up-to-speed yet 😅
But yeah it seems less error-prone than previous naive string-approach.

---------

Co-authored-by: DatCaptainHorse <DatCaptainHorse@users.noreply.github.com>
Co-authored-by: Wanjohi <elviswanjohi47@gmail.com>
Co-authored-by: Wanjohi <71614375+wanjohiryan@users.noreply.github.com>
This commit is contained in:
Kristian Ollikainen
2025-01-28 06:52:05 +02:00
committed by GitHub
parent 4c33d7fe00
commit 431733a0e8
4 changed files with 43 additions and 49 deletions

View File

@@ -1,52 +1,47 @@
#!/bin/bash
set -euo pipefail
declare -a vendor_full_map=()
declare -a vendor_id_map=()
declare -A vendor_index_map=()
declare -A vendor_devices=()
function get_gpu_info {
# Initialize arrays/maps to avoid unbound variable errors
vendor_full_map=()
vendor_id_map=()
vendor_index_map=()
vendor_devices=()
# Use lspci to detect GPU info
gpu_info=$(lspci | grep -i 'vga\|3d\|display')
local gpu_info=$(lspci -nn | grep -E '\<(0300|0302|0380)\>')
# Parse each line of GPU info
while IFS= read -r line; do
# Extract vendor name and ID from lspci output
vendor=$(echo "$line" | awk -F: '{print $3}' | sed -E 's/^[[:space:]]+//g' | tr '[:upper:]' '[:lower:]')
id=$(echo "$line" | awk '{print $1}')
# Extract vendor_id from [vendor_id:device_id]
local vendor_id=$(echo "$line" | sed -nE 's/.*\[([[:xdigit:]]{4}):[[:xdigit:]]{4}\].*/\1/p' | tr '[:upper:]' '[:lower:]')
local id=$(echo "$line" | awk '{print $1}')
# Normalize vendor name
if [[ $vendor =~ .*nvidia.* ]]; then
vendor="nvidia"
elif [[ $vendor =~ .*intel.* ]]; then
vendor="intel"
elif [[ $vendor =~ .*advanced[[:space:]]micro[[:space:]]devices.* ]]; then
vendor="amd"
elif [[ $vendor =~ .*ati.* ]]; then
vendor="amd"
else
vendor="unknown"
fi
# Map vendor_id to known vendors
local vendor="unknown"
case "$vendor_id" in
10de) vendor="nvidia" ;;
8086) vendor="intel" ;;
1002 | 1022) vendor="amd" ;;
# Add other vendor IDs as needed
esac
# Add to arrays/maps if unique
if ! [[ "${vendor_index_map[$vendor]:-}" ]]; then
vendor_index_map[$vendor]="${#vendor_full_map[@]}"
vendor_full_map+=("$vendor")
if [[ "$vendor" != "unknown" ]]; then
vendor_devices["$vendor"]+="$id "
fi
vendor_id_map+=("$id")
done <<< "$gpu_info"
done <<<"$gpu_info"
}
function debug_gpu_info {
echo "Vendor Full Map: ${vendor_full_map[*]}"
echo "Vendor ID Map: ${vendor_id_map[*]}"
echo "Vendor Index Map:"
for key in "${!vendor_index_map[@]}"; do
echo " $key: ${vendor_index_map[$key]}"
echo "Detected GPUs:"
for vendor in "${!vendor_devices[@]}"; do
echo " $vendor: ${vendor_devices[$vendor]}"
done
}
# # Usage example:
# get_gpu_info
# debug_gpu_info
# # Access NVIDIA GPUs specifically
# if [[ -n "${vendor_devices[nvidia]:-}" ]]; then
# echo "NVIDIA GPUs detected: ${vendor_devices[nvidia]}"
# else
# echo "No NVIDIA GPUs found"
# fi