• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Featured
    • C# Tutorials
      • LinQ Tutorials
      • Facebook C# API Tutorials
    • PHP Tutorials
      • CodeIgniter Tutorials
    • Amazon AWS Tutorials
  • Categories
    • Programming
    • Development
    • Database
    • Web Server
    • Source Control
    • Management
    • Project
  • About
  • Write
  • Contact

CodeSamplez.com

Programming, Web development, Cloud Technologies

You are here: Home / Programming / Getting Started With NVML Python API

Getting Started With NVML Python API

January 28, 2015 by Rana Ahsan 1 Comment

nvml python

When I participated in Google Summer Of Code last summer, I worked for Ganglia, which is a very popular open source distributed monitoring system. I have worked on its nvidia gpu module, which is written in python language. This is my first official contribution to a widely used open source software.

While working here, I came to realize that, there aren’t that much resources exist online. Also, the original NVML API PDF reference I got, was for C programming language, not for python. However, I managed to convert the C API call to python call, mainly because of the super easy nature of python language. Finally, I managed to become flexible with it and implemented new features, enhanced existing features on this plugin module.

So, in this nvml python tutorial, which is intended for complete beginners, I will be sharing some common ways to interact with NVIDIA GPU from python, so that beginners who are new to this platform, can get a better start.

Installing Dependency:

First of all, you will have to make sure you have a Nvidia GPU on the machine you will be working on(just to mention who might mixed up, GPU and graphics card are two different thing). To install the python dependency, you can use the latest package from here:
Link: https://pypi.python.org/pypi/nvidia-ml-py/

Initialize Connection:

When you are just starting with your first ever application on NVML, first thing you need to do is to set up a connection to the GPU(s) object. This is as easy as one liner method call. However, you should handle exception on this statement. Because, if it doesn’t find compatible GPUs in the system it is running on, it will throw an error. Here is a code sample:

    try:
        nvmlInit()
    except NVMLError, err:
        print "Failed to initialize NVML: ", err
        print "Exiting..."
        os._exit(1)

Terminate A Connection

Same rule applies for terminating a connection. You will need to handle exception on this too:

    try:
        nvmlShutdown()
    except NVMLError, err:
        print "Error shutting down NVML:"
        return 1

Get Number Of GPUs:

Now, there can be several number of GPUs connected to the machine and NVML can interact with all of them as per your need. To know how many GPUs are connected, we can just call like below:

numOfGPUs = int(nvmlDeviceGetCount())

Get Reference To A GPU Object By Index:

Provided that you have the number of GPUs, most easy way to reference to them is by 0 based index system. So, you will just need to pass the index from 0 to N-1(where N is the number of GPUs we did get from above code) to ‘nvmlDeviceGetHandleByIndex’ method like below:

gpuObj = nvmlDeviceGetHandleByIndex(gpu_id);

Few Other useful operations:

As you now have reference to a specific GPU, you can start interacting with it and gather necessary information from it as well.
Some common operations are given here, just as a starting point. Use the reference manual to get other necessary calls as per your need.

#get GPU temperature
temperature = nvmlDeviceGetTemperature(gpu_device, NVML_TEMPERATURE_GPU)

#get GPU memory total
totalMemory = nvmlDeviceGetMemoryInfo(gpu_device).total

#get GPU memory used
usedMemory = nvmlDeviceGetMemoryInfo(gpu_device).used

Final Words:

Hope this basic NVML python tutorial will help you start interfacing to Nvidia GPU from python. To know or debug/verify your call, you should also use a CLI tool in parallel to your python script, about which you can know more on the official documentation . However, if you are having any issues regarding the area covered in this tutorial, please let me know via comments. Happy coding 🙂

Share If Liked

  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pocket (Opens in new window)

You may also like

Filed Under: Programming Tagged With: nvml, python

About Rana Ahsan

Rana is a passionate software engineer/Technology Enthusiast.
Github: ranacseruet

Reader Interactions

Comments

  1. Matthias says

    November 8, 2015 at 5:33 pm

    Dear autor,

    this is a great tutorial and I have been able to get the information out of my GPU except the frame rate. I searched the pynvml.py for a “getFPS”-function, but unfortunately none is available. Do you have any idea on how to get the frames per second? For example my MSI Afterburner is monitoring this value.

    What I want to do basically is to gather some CPU/GPU Information and send it via USB to my Arduino (maybe at 0.5 Hz), which should display the information on a separate small display.

    Thanks in advance for your suggestion on how to get the FPS in python! 🙂

    Regards,
    Matthias

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Follow Us

  • Twitter
  • Facebook

Top Posts & Pages

  • How To Work With JSON In Node.js / JavaScript
    How To Work With JSON In Node.js / JavaScript
  • PHP HTML5 Video Streaming Tutorial
    PHP HTML5 Video Streaming Tutorial
  • How To Work With C# Serial Port Communication
    How To Work With C# Serial Port Communication
  • Facebook C# API Tutorials
    Facebook C# API Tutorials
  • LinQ Query With Like Operator
    LinQ Query With Like Operator
  • Using Supervisord Web Interface And Plugin
    Using Supervisord Web Interface And Plugin
  • Get Facebook C# Api Access Token
    Get Facebook C# Api Access Token
  • Getting Started With UDP Programming in Java
    Getting Started With UDP Programming in Java
  • Utilizing Config File In C#.NET Application
    Utilizing Config File In C#.NET Application
  • Generate HTTP Requests using c#
    Generate HTTP Requests using c#

Recent Posts

  • Building Auth With JWT – Part 2
  • Building Auth With JWT – Part 1
  • Document Your REST API Like A Pro
  • Understanding Golang Error Handling
  • Web Application Case Studies You Must Read

Tags

.net angularjs apache api audio auth authenticatin aws c# cloud server codeigniter deployment docker doctrine facebook git github golang htaccess html5 http javascript jwt linq mysql nodejs oop performance php phpmyadmin plugin process python regular expression scalability server smarty socket.io tfs tips unit-test utility web application wordpress wpf

Footer

Archives

Follow Us

  • Twitter
  • Facebook

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Copyright © 2023