summaryrefslogtreecommitdiff
path: root/free-gpu
blob: 3df48e96bf41281fb963c3cb0e11bd572049cc82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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