docs: def best_gpu(max_usage=USAGE_THRESHOLD, verbose=False):
      '''
      Return the name of a device to use, either 'cpu' or 'gpu0', 'gpu1',...
      The least-used GPU with usage under the constant threshold will be chosen;
      ties are broken randomly.
      '''
      try:
          proc = subprocess.Popen("nvidia-smi", stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE)
          output, error = proc.communicate()
          if error:
              raise Exception(error)
      except Exception, e:
          sys.stderr.write("Couldn't run nvidia-smi to find best GPU, using CPU: %s\n" % str(e))
          sys.stderr.write("(This is normal if you have no GPU or haven't configured CUDA.)\n")
          return "cpu"
  
      usages = parse_output(output)
  
      pct_usage = [max(u.mem, cpu_backoff(u)) for u in usages]
      max_usage = min(max_usage, min(pct_usage))
  
      open_gpus = [index for index, usage in enumerate(usages)
                   if max(usage.mem, cpu_backoff(usage)) <= max_usage]
      if verbose:
          print('Best GPUs:')
          for index in open_gpus:
              print('%d: %s fan, %s mem, %s cpu' %
                    (index, format_percent(usages[index].fan),
                     format_percent(usages[index].mem),
                     format_percent(usages[index].cpu)))
  
      if open_gpus:
          result = "gpu" + str(random.choice(open_gpus))
      else:
          result = "cpu"
  
      if verbose:
          print('Chosen: ' + result)
      return result