SHARE
TWEET

Untitled

a guest Aug 23rd, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top