Skip to content

Instantly share code, notes, and snippets.

@sborquez
Last active May 10, 2022 16:27
Show Gist options
  • Save sborquez/c60d506d8e823059f8a3866993d35e2e to your computer and use it in GitHub Desktop.
Save sborquez/c60d506d8e823059f8a3866993d35e2e to your computer and use it in GitHub Desktop.
Python bash wrappers
#!/bin/bash
# Add a timestamp to the python's output.
# Example:
# dpython -c "[print(i) for i in range(5)]"
# [2019-01-20 14:19:39] 0
# [2019-01-20 14:19:39] 1
# [2019-01-20 14:19:39] 2
# [2019-01-20 14:19:39] 3
# [2019-01-20 14:19:39] 4
# Setup:
# mkdir ~/bin && cp ./dpython.sh ~/bin/dpython
# chmod u+x ~/bin/dpython
# shellrc="$HOME/.$(ps | grep `echo $$` | awk '{ print $4 }')rc"
# echo 'export PATH="$HOME/bin:$PATH"' >> $shellrc
# echo 'alias dpy=dpython' >> $shellrc
python "$@" | while read n; do
echo "$(date '+[%Y-%m-%d %H:%M:%S] ') $n";
done
#!/bin/bash
# Run a python interpreter with an available GPU.
# Example:
# gpython -c "import torch; torch.as_tensor([1,2,3], device='cuda')"
# Setup:
# mkdir ~/bin && cp ./gpython.sh ~/bin/gpython
# chmod u+x ~/bin/gpython
# shellrc="$HOME/.$(ps | grep `echo $$` | awk '{ print $4 }')rc"
# echo 'export PATH="$HOME/bin:$PATH"' >> $shellrc
# echo 'alias gpy=gpython' >> $shellrc
# Get list of gpus
gpus=$(nvidia-smi -L | grep -o "GPU .")
readarray -t gpus <<<"$gpus"
# Find an available GPU
for gpu in "${gpus[@]}"
do
gpu="$(echo $gpu | cut -d' ' -f2)"
gpu_no_processes=$(nvidia-smi -q --display=PIDS -i $gpu | grep "Processes.*: None")
if [[ -n $gpu_no_processes ]]
then
assigned_gpu=$gpu
break
fi
done
# Run the interpreter if a GPU is available
if [[ -n $assigned_gpu ]]
then
echo "Assigned GPU $assigned_gpu"
export CUDA_VISIBLE_DEVICES=$assigned_gpu; python $@
else
echo "No GPUs availables"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment