Last active
March 4, 2022 16:02
-
-
Save barak/74234bf8cf9e064fab3c392e9dcd4a3e to your computer and use it in GitHub Desktop.
use nvidia-smi to find available GPU(s)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash -f | |
set -e | |
k=1 | |
s=1 | |
function usage() { | |
echo "find-gpu [K [SECS]]" | |
echo " Prints the indices of the K (default: ${k}) least-used" | |
echo " GPUs, as a comma-separated list. Symmetries broken" | |
echo " randomly. Sleeps randomly between 0 and SECS (default: ${s})" | |
echo " seconds before running, to further break symmetry and" | |
echo " avoid races." | |
} | |
if [ $# = 1 -a x$1 = x-h ]; then | |
usage | |
exit 0 | |
fi | |
case $# in | |
0) ;; | |
1) k=$1 ;; | |
2) k=$1; s=$2 ;; | |
*) usage; exit 1 ;; | |
esac | |
sleep $(echo "scale=3; ${s}*${RANDOM}/32767" | bc) | |
nvidia-smi --format=csv --query-gpu=index,utilization.memory,utilization.gpu \ | |
| tail +2 \ | |
| tr -d '%' \ | |
| awk 'BEGIN {FS=","; srand()} {print $1, $2+$3+0.01*rand()}' \ | |
| sort -n --key=2 \ | |
| cut --fields=1 --delimiter=' ' \ | |
| head -${k} \ | |
| fmt \ | |
| tr ' ' ',' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment