Coder Social home page Coder Social logo

Get gpu usage by pid about pynvml HOT 4 OPEN

gpuopenanalytics avatar gpuopenanalytics commented on June 26, 2024
Get gpu usage by pid

from pynvml.

Comments (4)

rjzamora avatar rjzamora commented on June 26, 2024 3

Using nvmlDeviceGetAccountingStats might be solution, but I have not personally tested or experimented with this. I cannot immediately test this myself, because root/admin access is need to set the appropriate accounting mode for the target device (e.g. using nvmlDeviceSetAccountingMode).

If you are interested in getting a pid breakdown of gpu-memory consomption for a given device, yout can use nvmlDeviceGetComputeRunningProcesses:

import pynvml

pynvml.nvmlInit()
for dev_id in range(pynvml.nvmlDeviceGetCount()):
    handle = pynvml.nvmlDeviceGetHandleByIndex(dev_id)
    for proc in pynvml.nvmlDeviceGetComputeRunningProcesses(handle):
        print(
            "pid %d using %d bytes of memory on device %d."
            % (proc.pid, proc.usedGpuMemory, dev_id)
        )

I will try to take a harder look into this later and will update here if I find anything.

from pynvml.

cod3licious avatar cod3licious commented on June 26, 2024 2

This is the code I'm using now. It only works for type "C" processes, not type "G" in the list when calling nvidia-smi directly in the terminal (e.g. my neural network in python was a type "C" process, Xorg type "G") but I think it should suffice:

import os
from pynvml.smi import nvidia_smi
from bs4 import BeautifulSoup as bs


def get_gpu_usage(pid=None):
    if pid is None:
        # get current process id
        pid = os.getpid()
    pid = str(pid)
    # find associated gpu usage
    soup = bs(nvidia_smi.getInstance().XmlDeviceQuery(), "lxml")
    for p in soup.find("processes").find_all("process_info"):
        if p.find("pid").get_text() == pid:
            return p.find("used_memory").get_text()
    # process not found
    return "0 MiB"

from pynvml.

cod3licious avatar cod3licious commented on June 26, 2024

I'd love that feature as well! Calling .XmlDeviceQuery() gives an XML where the different processes are listed so it should be possible if something like beautiful soup could be an additional dependency.

from pynvml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.