Created
April 4, 2018 21:26
-
-
Save bzamecnik/191fc1d65c9792e91127be500100d467 to your computer and use it in GitHub Desktop.
CUDA next/all available GPUs
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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Python version: https://pypi.org/project/nvgpu/