Skip to content

Instantly share code, notes, and snippets.

@okomarov
Created July 18, 2018 15:15
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save okomarov/c0d9fd0718f6f9b40c701e61523dfed1 to your computer and use it in GitHub Desktop.
Save okomarov/c0d9fd0718f6f9b40c701e61523dfed1 to your computer and use it in GitHub Desktop.
Check allocated GPU by Colab
# memory footprint support libraries/code
!ln -sf /opt/bin/nvidia-smi /usr/bin/nvidia-smi
!pip install gputil
!pip install psutil
!pip install humanize
import psutil
import humanize
import os
import GPUtil as GPU
GPUs = GPU.getGPUs()
# XXX: only one GPU on Colab and isn’t guaranteed
gpu = GPUs[0]
def printm():
process = psutil.Process(os.getpid())
print("Gen RAM Free: " + humanize.naturalsize( psutil.virtual_memory().available ), " | Proc size: " + humanize.naturalsize( process.memory_info().rss))
print("GPU RAM Free: {0:.0f}MB | Used: {1:.0f}MB | Util {2:3.0f}% | Total {3:.0f}MB".format(gpu.memoryFree, gpu.memoryUsed, gpu.memoryUtil*100, gpu.memoryTotal))
printm()
@soumendrabagh
Copy link

Any specific reasons I am getting an error:

Requirement already satisfied: gputil in /usr/local/lib/python3.6/dist-packages (1.4.0)
Requirement already satisfied: psutil in /usr/local/lib/python3.6/dist-packages (5.4.8)
Requirement already satisfied: humanize in /usr/local/lib/python3.6/dist-packages (0.5.1)

IndexError Traceback (most recent call last)
in ()
9 GPUs = GPU.getGPUs()
10 # XXX: only one GPU on Colab and isn’t guaranteed
---> 11 gpu = GPUs[0]
12 def printm():
13 process = psutil.Process(os.getpid())

IndexError: list index out of range

@vv111y
Copy link

vv111y commented May 23, 2020

@soumendrabagh you need to change the runtime type to gpu, it is cpu by default

@soumendrabagh
Copy link

@soumendrabagh you need to change the runtime type to gpu, it is cpu by default

Thank you very much. I did the same. I appreciate your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment