Last active
August 9, 2016 20:10
-
-
Save noisychannel/cdf57e2f177e98ae653230323a093d1e to your computer and use it in GitHub Desktop.
Get the first free GPU on a machine
This file contains 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
#!/usr/bin/env bash | |
# Number of free GPUs on a machine | |
export n_gpus=`lspci | grep -i "nvidia" | wc -l` | |
# Return -1 if there are no GPUs on the machine | |
if [ $n_gpus -eq 0 ]; then | |
echo "-1" | |
exit -1 | |
fi | |
f_gpu=`nvidia-smi | sed -e '1,/Processes/d' \ | |
| tail -n+3 | head -n-1 | awk '{print $2}'\ | |
| awk -v ng=$n_gpus 'BEGIN{for (n=0;n<ng;++n){g[n] = 1}} {delete g[$1];} END{for (i in g) print i}' \ | |
| tail -n 1` | |
# return -1 if no free GPU was found | |
if [ `echo $f_gpu | grep -v '^$' | wc -l` -eq 0 ]; then | |
echo "-1" | |
exit -1 | |
else | |
echo $f_gpu | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment