forked from espnet/espnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfree-gpu.sh
executable file
·51 lines (44 loc) · 1.13 KB
/
free-gpu.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env bash
# Author: Gaurav Kumar
# Usage: e.g.
# % free-gpu.sh -n 2
# 1,2
# Allow requests for multiple GPUs
# (Optional) defaults to 1
req_gpus=1
while getopts ':n:' opt; do
case ${opt} in
n)
req_gpus=${OPTARG}
;;
:)
echo "Option -${OPTARG} requires an argument." >&2
exit 1
;;
*)
echo "Option -${OPTARG} is not supported" >&2
exit 1
;;
esac
done
# Number of free GPUs on a machine
n_gpus=$(lspci | grep -i "nvidia" | grep -c -v "Audio")
# Return -1 if there are no GPUs on the machine
# or if the requested number of GPUs exceed
# the number of GPUs installed.
if [ ${n_gpus} -eq 0 ] || [ ${req_gpus} -gt ${n_gpus} ]; then
echo "-1"
exit 1
fi
# shellcheck disable=SC2026
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 ${req_gpus})
# return -1 if not enough free GPUs were found
if [[ $(echo ${f_gpu} | grep -v '^$' | wc -w) -ne ${req_gpus} ]]; then
echo "-1"
exit 1
else
echo ${f_gpu} | sed 's: :,:g'
fi