Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess
- import csv
- def available_gpu() -> str:
- """
- return indices of not occupied (memory usage less than 200 MiB) GPUs
- """
- with open('gpu_usage.txt', 'w+') as gpu_usage:
- subprocess.run(['nvidia-smi',
- '--query-gpu=index,memory.used',
- '--format=csv,noheader,nounits'],
- stdout=gpu_usage)
- with open('gpu_usage.txt', 'r') as gpu_usage:
- gpus = csv.reader(gpu_usage, delimiter=',')
- for gpu in gpus:
- if int(gpu[1]) < 200:
- subprocess.run(['rm', 'gpu_usage.txt'])
- return gpu[0]
- raise ValueError('There is no available GPU')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement