Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use CUDA_VISIBLE_DEVICES to control GPU in Tensorflow

Shulou Source: shulou.com Published: 2022-06-02 06:11:52 09月27日 Update

This article is to share with you about how to use CUDA_VISIBLE_DEVICES to control GPU in Tensorflow. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

Os.environ ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # arrange GPU devices from 0 in PCI_BUS_ID order

Os.environ ["CUDA_VISIBLE_DEVICES"] = "0" # sets the current GPU device to be device 0 only. The device name is'/ gpu:0'.

Os.environ ["CUDA_VISIBLE_DEVICES"] = "1" # sets the current GPU device to be device 1 only. The device name is'/ gpu:1'

Os.environ ["CUDA_VISIBLE_DEVICES"] = "0Magne1" # set the current GPU device to 0Magne1. The names are'/ gpu:0','/ gpu:1', respectively.

Os.environ ["CUDA_VISIBLE_DEVICES"] = "1Magol 0" # sets the GPU device currently in use to two devices, named'/ gpu:1','/ gpu:0', respectively. Indicates that the No. 1 device is preferred, and then the No. 0 device is used.

If the server has multiple GPU,tensorflow, all of them will be used by default. If you only want to use part of the GPU, you can set the visibility of the GPU through the parameter CUDA_VISIBLE_DEVICES.

Example:

Environment Variable Syntax ResultsCUDA_VISIBLE_DEVICES=1 Only device 1 will be seenCUDA_VISIBLE_DEVICES=0,1 Devices 0 and 1 will be visibleCUDA_VISIBLE_DEVICES= "0jue 1" > set on the terminal

Before the terminal calls the Python script, you can set the CUDA_VISIBLE_DEVICES variable as follows:

$CUDA_VISIBLE_DEVICES=1 python my_script.py

So the my_script.py script can only use GPU 1.

Set up within the Python script

If you want to set the GPU to be used in the script of Python, you can use os.environ as follows:

Import osos.environ ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue # 152os.environ ["CUDA_VISIBLE_DEVICES"] = "1"

Check the visibility of TensorFlow to GPU:

From tensorflow.python.client import device_libprint (device_lib.list_local_devices ()) above is how to use CUDA_VISIBLE_DEVICES to control GPU in Tensorflow. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Device name script control two more knowledge article terminal part utility parameter variable can be passed multiple that is work meeting article server look. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Tech Info Xiaomi NVidia Linux