Skip to content

Instantly share code, notes, and snippets.

@ShawonAshraf
Last active March 27, 2023 17:09
Show Gist options
  • Save ShawonAshraf/ca52d0f6ad31454370c2af4fdfb70803 to your computer and use it in GitHub Desktop.
Save ShawonAshraf/ca52d0f6ad31454370c2af4fdfb70803 to your computer and use it in GitHub Desktop.
import pynvml
pynvml.nvmlInit()
device_count = pynvml.nvmlDeviceGetCount()
for i in range(device_count):
try:
handle = pynvml.nvmlDeviceGetHandleByIndex(i)
util = pynvml.nvmlDeviceGetUtilizationRates(handle)
print("========================================")
print(f"GPU Index: {i}")
print(f"Name: {pynvml.nvmlDeviceGetName(handle)} || Utilization: {util.gpu}%")
info = pynvml.nvmlDeviceGetMemoryInfo(handle)
print(f"Memory Usage (free/total): {info.free / 1024 / 1024}/{info.total / 1024 / 1024}")
print("========================================\n")
except Exception:
print("GPU ", i, " can't be accessed")
pynvml.nvmlShutdown()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment