Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. import subprocess
  2. import csv
  3.  
  4.  
  5. def available_gpu() -> str:
  6. """
  7. return indices of not occupied (memory usage less than 200 MiB) GPUs
  8. """
  9. with open('gpu_usage.txt', 'w+') as gpu_usage:
  10. subprocess.run(['nvidia-smi',
  11. '--query-gpu=index,memory.used',
  12. '--format=csv,noheader,nounits'],
  13. stdout=gpu_usage)
  14.  
  15. with open('gpu_usage.txt', 'r') as gpu_usage:
  16. gpus = csv.reader(gpu_usage, delimiter=',')
  17. for gpu in gpus:
  18. if int(gpu[1]) < 200:
  19. subprocess.run(['rm', 'gpu_usage.txt'])
  20. return gpu[0]
  21. raise ValueError('There is no available GPU')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement