Skip to content

Instantly share code, notes, and snippets.

@szampardi
Last active October 21, 2021 20:21
Show Gist options
  • Save szampardi/59988ecd186861ad8434f3b29e1212a4 to your computer and use it in GitHub Desktop.
Save szampardi/59988ecd186861ad8434f3b29e1212a4 to your computer and use it in GitHub Desktop.
nvidia-settings headless gpu oc
#!/bin/bash -e
# gpus.json
# to find out uuid, first match PCI port with GPU UUID
# #> lspci -vnn | grep VGA -A 2 && cat /proc/driver/nvidia/gpus/*/information
# then confirm
# #> nvidia-smi -L
if [[ -z ${1} ]] || [[ ! -s ${1} ]]; then
printf 'must provide json config file with first argument\n'
exit 127
fi
if ! command -v jq 1>/dev/null; then
printf 'need jq to work\n'
exit 2
fi
export DISPLAY=:0
export XAUTHORITY=/var/run/lightdm/root/:0
nvidia-smi -pm 1
jq -r 'keys[]' "${1}" | while read -r _name; do
printf '\n\n'
printf '=%.0s' {1..72}
_j="$(jq --arg name "${_name}" '.[$name]' "${1}")"
_uuid=$(jq -r .uuid <<<"${_j}")
_n=$(nvidia-smi -L | awk "/${_uuid}/ {print \$2}" | cut -d':' -f1)
printf "\nGPU #%s\t%s\n\n" "${_n}" "${_name}"
printf '=%.0s' {1..72}
printf '\n'
_pl=$(jq -r .pl <<<"${_j}")
_fan_speed=$(jq -r .fan_speed <<<"${_j}")
_logo_brightness=$(jq -r .logo_brightness <<<"${_j}")
printf 'set power limit to %dW.. ' "${_pl}"
nvidia-smi -i "${_n}" -pl "${_pl}"
nvidia-settings -a "[gpu:${_n}]/GpuPowerMizerMode=1"
printf 'setting fan speed to %d%s..\n' "${_fan_speed}" "%"
case "${_fan_speed}" in
"" | "null" | "auto" | 0) nvidia-settings -a [gpu:${_n}]/GPUFanControlState=0 ;;
*)
nvidia-settings -a "[gpu:${_n}]/GPUFanControlState=1"
nvidia-settings -a "[fan:${_n}]/GPUTargetFanSpeed=${_fan_speed}"
;;
esac
printf 'setting logo brightness..\n'
nvidia-settings -a "[gpu:${_n}]/GPULogoBrightness=${_logo_brightness}"
printf 'configuring core speed\n'
_s=0
for _state_offset in $(jq -r '.core_offset[]' <<<"${_j}"); do
if [[ ${_state_offset} != 0 ]]; then
printf '\nsetting P%d offset %d.. ' "${_s}" "${_state_offset}"
nvidia-settings -a "[gpu:${_n}]/GPUGraphicsClockOffset[${_s}]=${_state_offset}"
fi
_s=$((_s + 1))
done
printf 'configuring mem speed\n'
_s=0
for _state_offset in $(jq -r '.mem_offset[]' <<<"${_j}"); do
if [[ ${_state_offset} != 0 ]]; then
printf '\nsetting P%d mem offset %d.. ' "${_s}" "${_state_offset}"
nvidia-settings -a "[gpu:${_n}]/GPUMemoryTransferRateOffset[${_s}]=${_state_offset}"
fi
_s=$((_s + 1))
done
printf '=%.0s' {1..72}
printf '\n\n'
done
{
"NVIDIA GeForce GTX 1070 Ti (Micro-Star International Co., Ltd. [MSI])": {
"uuid": "GPU-ad494100-42c0-8d15-c875-bdf30068a8b6",
"pl": 120,
"fan_speed": 60,
"logo_brightness": 0,
"core_offset": [
0,
0,
144,
144
],
"mem_offset": [
0,
0,
1024,
1024
]
},
"NVIDIA GeForce GTX 1070 Ti (ZOTAC International (MCO) Ltd.)": {
"uuid": "GPU-debd1c1d-d07f-c9c3-f965-319c75bb441c",
"pl": 120,
"fan_speed": 60,
"logo_brightness": 0,
"core_offset": [
0,
0,
144,
144
],
"mem_offset": [
0,
0,
1024,
1024
]
}
}
# nvidia-xconfig: X configuration file generated by nvidia-xconfig
# nvidia-xconfig: version 470.74
Section "ServerLayout"
Identifier "Layout0"
Screen 0 "Screen0"
Screen 1 "Screen1" LeftOf "Screen0"
Screen 2 "Screen2" RightOf "Screen1"
InputDevice "Keyboard0" "CoreKeyboard"
InputDevice "Mouse0" "CorePointer"
EndSection
Section "Files"
EndSection
Section "InputDevice"
# generated from default
Identifier "Mouse0"
Driver "mouse"
Option "Protocol" "auto"
Option "Device" "/dev/mouse"
Option "Emulate3Buttons" "no"
Option "ZAxisMapping" "4 5"
EndSection
Section "InputDevice"
# generated from default
Identifier "Keyboard0"
Driver "kbd"
EndSection
Section "Monitor"
Identifier "Monitor0"
VendorName "Unknown"
ModelName "Unknown"
Option "DPMS"
EndSection
Section "Monitor"
Identifier "Monitor1"
VendorName "Unknown"
ModelName "Unknown"
Option "DPMS"
EndSection
Section "Monitor"
Identifier "Monitor2"
VendorName "Unknown"
ModelName "Unknown"
Option "DPMS"
EndSection
Section "Device"
Identifier "Device0"
Driver "nvidia"
VendorName "NVIDIA Corporation"
BoardName "NVIDIA GeForce RTX 3060 Ti"
BusID "PCI:1:0:0"
EndSection
Section "Device"
Identifier "Device1"
Driver "nvidia"
VendorName "NVIDIA Corporation"
BoardName "NVIDIA GeForce GTX 1070 Ti"
BusID "PCI:2:0:0"
EndSection
Section "Device"
Identifier "Device2"
Driver "nvidia"
VendorName "NVIDIA Corporation"
BoardName "NVIDIA GeForce GTX 1070 Ti"
BusID "PCI:4:0:0"
EndSection
Section "Screen"
Identifier "Screen0"
Device "Device0"
Monitor "Monitor0"
DefaultDepth 24
SubSection "Display"
Depth 24
EndSubSection
Option "AllowEmptyInitialConfiguration" "True"
Option "ConnectedMonitor" "DFP-0"
Option "Interactive" "False"
Option "Coolbits" "24"
EndSection
Section "Screen"
Identifier "Screen1"
Device "Device1"
Monitor "Monitor1"
DefaultDepth 24
SubSection "Display"
Depth 24
EndSubSection
Option "AllowEmptyInitialConfiguration" "True"
Option "ConnectedMonitor" "DFP-1"
Option "Interactive" "False"
Option "Coolbits" "24"
EndSection
Section "Screen"
Identifier "Screen2"
Device "Device2"
Monitor "Monitor2"
DefaultDepth 24
SubSection "Display"
Depth 24
EndSubSection
Option "AllowEmptyInitialConfiguration" "True"
Option "ConnectedMonitor" "DFP-2"
Option "Interactive" "False"
Option "Coolbits" "24"
EndSection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment