summaryrefslogtreecommitdiff
path: root/free-gpu
diff options
context:
space:
mode:
Diffstat (limited to 'free-gpu')
-rwxr-xr-xfree-gpu23
1 files changed, 23 insertions, 0 deletions
diff --git a/free-gpu b/free-gpu
new file mode 100755
index 0000000..3df48e9
--- /dev/null
+++ b/free-gpu
@@ -0,0 +1,23 @@
+#!/usr/bin/env bash
+
+export n_gpus=`lspci | grep -i "controller: nvidia" | wc -l`
+
+if [ $n_gpus -eq 0 ]; then
+ echo "-1"
+ exit -1
+fi
+
+if [[ -x $(which nvidia-smi) ]]; then
+ 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}' \
+ | shuf -n 1 `
+fi
+
+if [ `echo $f_gpu | grep -v '^$' | wc -l` -eq 0 ]; then
+ echo "-1"
+ exit -1
+else
+ echo $f_gpu
+fi
+