Skip to content

Instantly share code, notes, and snippets.

@bzamecnik
Created April 4, 2018 21:26
Show Gist options
  • Save bzamecnik/191fc1d65c9792e91127be500100d467 to your computer and use it in GitHub Desktop.
Save bzamecnik/191fc1d65c9792e91127be500100d467 to your computer and use it in GitHub Desktop.
CUDA next/all available GPUs
# Returns the next or all available GPU id(s), or -1 if all are busy.
# Usage:
#
# One GPU:
#
# $ CUDA_VISIBLE_DEVICES=$(available_gpu) train.sh
#
# All GPUs (comma-separated):
#
# $ CUDA_VISIBLE_DEVICES=$(available_gpu) train.sh
function available_gpu() {
for i in $(nvidia-smi -L|sed -E 's/^GPU ([0-9]+).*/\1/')
do
no_process=$(nvidia-smi -i $i |grep 'No running processes found'|wc -l)
if [ "$no_process" = "1" ]
then
echo $i
return
fi
done
echo "-1"
}
available_gpu
function available_gpus() {
for i in $(nvidia-smi -L|sed -E 's/^GPU ([0-9]+).*/\1/')
do
no_process=$(nvidia-smi -i $i |grep 'No running processes found'|wc -l)
if [ "$no_process" = "1" ]
then
echo -n "$i,"
fi
done
echo
}
available_gpus
@bzamecnik
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment