How to know the number of GPUs from python ~ Notes on using multiprocessing with pytorch ~

Introduction

If you use multiprocessing with pytorch, you may get angry at the initialization of CUDA.

RuntimeError: cuda runtime error (3) : initialization error at /pytorch/aten/src/THC/THCGeneral.cpp:50
THCudaCheck FAIL file=/pytorch/aten/src/THC/THCGeneral.cpp line=50 error=3 : initialization error

Why i got angry

After various investigations, I found various documents about spawn, but it seems that it was caused by using torch.cuda.device_count () in my case.

So, I want to know the number of GPUs without torch.cuda.device_count ().

Get the number of GPUs from python

Rely on nvidia-smi. For linux:

import subprocess
msg = subprocess.check_output("nvidia-smi --query-gpu=index --format=csv", shell=True)
n_devices = max(0, len(msg.decode().split("\n")) - 2)

Please be aware of pytorch's CUDA initialization problem.

Recommended Posts

How to know the number of GPUs from python ~ Notes on using multiprocessing with pytorch ~
How to know the port number of the xinetd service
How to get the number of digits in Python
How to correctly upgrade the software when building Linux (CentOS) with Vagrant ~ Using the example of upgrading from Python 2.7 to Python 3.6 ~
How to identify the element with the smallest number of characters in a Python list?
An easy way to pad the number with zeros depending on the number of digits [Python]
How to count the number of occurrences of each element in the list in Python with weight
[Hyperledger Iroha] Notes on how to use the Python SDK
Notes on using MeCab from Python
Notes on using rstrip with python.
How to scrape stock prices of individual stocks from the Nikkei newspaper website with Python
How to know the internal structure of an object in Python
How to get followers and followers from python using the Mastodon API
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
How to update the python version of Cloud Shell on GCP
How to output the number of VIEWs, likes, and stocks of articles posted on Qiita to CSV (created with "Python + Qiita API v2")
Learning notes from the beginning of Python 1
Learning notes from the beginning of Python 2
How to find out the number of CPUs without using the sar command
How to deal with OAuth2 error when using Google APIs from Python
[Completed version] Try to find out the number of residents in the town from the address list with Python
Strategy on how to monetize with Python Java
Calculate the total number of combinations with python
How to specify attributes with Mock of python
Introduction to Python with Atom (on the way)
[Python] Notes on accelerating genetic algorithms using multiprocessing
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
How to get the key on Amazon S3 with Boto 3, implementation example, notes
How to build an environment for using multiple versions of Python on Mac
When using PyQtGraph with Python Pyside, pay attention to the order of import
[Python] How to save images on the Web at once with Beautiful Soup
Visualize the timeline of the number of issues on GitHub assigned to you in Python
Checklist on how to avoid turning the elements of numpy's array with for
Note: How to get the last day of the month with python (added the first day of the month)
[Python Tips] How to retrieve multiple keys with the maximum value from the dictionary
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
Try to measure the position of the object on the desk (real coordinate system) from the camera image with Python + OpenCV
python beginners tried to predict the number of criminals
[Python] How to remove duplicate values from the list
Think about how to program Python on the iPad
Learn Nim with Python (from the beginning of the year).
How to scrape image data from flickr with python
[Introduction to Python] How to iterate with the range function?
How to know the current directory in Python in Blender
[Python] How to specify the download location with youtube-dl
Try to estimate the number of likes on Twitter
Autoencoder with Chainer (Notes on how to use + trainer)
[Python] Summary of how to specify the color of the figure
[Introduction to Python] How to stop the loop using break?
[Python] How to rewrite the table style with python-pptx [python-pptx]
Study from the beginning of Python Hour8: Using packages
How to enjoy Python on Android !! Programming on the go !!
How much do you know the basics of Python?
From the initial state of CentOS8 to running php python perl ruby with nginx
Extract images and tables from pdf with python to reduce the burden of reporting
A memo on how to overcome the difficult problem of capturing FX with AI
The 15th offline real-time I tried to solve the problem of how to write with python
How to deal with the phenomenon that Python (Jupyter notebook) executed on WSL becomes Aborted
I tried to find the entropy of the image with python
I tried to simulate how the infection spreads with Python