Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use CUDA_VISIBLE_DEVICES to control GPU in Tensorflow

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report