Skip to content

Instantly share code, notes, and snippets.

@cynthia
Created August 3, 2017 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cynthia/ff2536500e17bd7c097d7a1e8b092687 to your computer and use it in GitHub Desktop.
Save cynthia/ff2536500e17bd7c097d7a1e8b092687 to your computer and use it in GitHub Desktop.
Simple script for checking free memory across all Nvidia GPUs. Linux+Nvidia only. Requires NVML.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from pynvml import *
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
ROWS, COLUMNS = [int(e) for e in os.popen('stty size', 'r').read().split()]
BAR_SIZE = COLUMNS - 9
def print_bar(device_id, usage):
bar_size = int(BAR_SIZE * usage)
print('GPU %d> [%s%s%s%s%s%s]' % (device_id, OKGREEN, '|' * (bar_size - 2), FAIL, '||', ' ' * (BAR_SIZE - bar_size), ENDC))
nvmlInit()
device_count = nvmlDeviceGetCount()
for d in range(device_count):
h = nvmlDeviceGetHandleByIndex(d)
m = nvmlDeviceGetMemoryInfo(h)
print_bar(d, float(m.used) / float(m.total))
print('Total: %fMBs' % (m.total / 1048576))
print('Used: %fMBs' % (m.used / 1048576))
print('Free: %fMBs\n' % (m.free / 1048576))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment