Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if GPU memory is available using PyOpenCL

I would like to know if there is a way to check how much GPU memory is available before a function uses it. I have code that often uses 1.5 GB of GPU memory or more, and if something else is using the GPU when my program wants to use it, I get a MemoryError exception or something similar.

I would like to implement some sort of code so that I can check to see if the GPU has enough memory available, and if it does, go ahead and run, but if not, wait until it IS available.

(Preferably, I would like to check before trying to use the GPU rather than using a try-except loop and just retrying if it fails)

I checked the PyOpenCL documentation to see if there was something relevant under device_info, but I couldn't find any actual descriptions.

like image 710
johnny_be Avatar asked Apr 22 '15 23:04

johnny_be


People also ask

How do I check my GPU allocation memory?

To monitor the overall GPU resource usage, click the Performance tab, scroll down the left pane, and find the “GPU” option. Here you can watch real-time usage. It displays different graphs for what is happening with your system — like encoding videos or gameplay.

How do I check my GPU memory in Tensorflow?

import tensorflow as tf import numpy as np from kmeanstf import KMeansTF print("GPU Available: ", tf. test. is_gpu_available()) nn=1000 dd=250000 print("{:,d} bytes". format(nn*dd*4)) dic = {} for x in "ABCD": dic[x]=tf.

How do I check my GPU RAM in Python?

You will need to install nvidia-ml-py3 library in python (pip install nvidia-ml-py3) which provides the bindings to NVIDIA Management library. Here is the code snippet: Thats it!

What is PyOpenCL?

PyOpenCL lets you access GPUs and other massively parallel compute devices from Python. It tries to offer computing goodness in the spirit of its sister project PyCUDA: Object cleanup tied to lifetime of objects. This idiom, often called RAII in C++, makes it much easier to write correct, leak- and crash-free code.


1 Answers

This is not possible, and is actually a limitation of OpenCL, not just PyOpenCL. See here.

On NVIDIA devices, you can use nvidia-ml-py. Then you can do something like this:

from pynvml import *
nvmlInit()
for i in range(nvmlDeviceGetCount()):
    handle = nvmlDeviceGetHandleByIndex(i)
    meminfo = nvmlDeviceGetMemoryInfo(handle)
    print("%s: %0.1f MB free, %0.1f MB used, %0.1f MB total" % (
        nvmlDeviceGetName(handle),
        meminfo.free/1024.**2, meminfo.used/1024.**2, meminfo.total/1024.**2))
nvmlShutdown()
like image 119
hunse Avatar answered Oct 22 '22 06:10

hunse