简体   繁体   English

在特定的 gpu 上运行 python 代码以获得较低的 python 版本

[英]Run python code on specific gpu for lower python versions

I am trying to run a python code on a specific GPU on our server.我正在尝试在我们服务器上的特定 GPU 上运行 python 代码。 The server has four GPUs.服务器有四个 GPU。 When I run the code using a virtual environment installed with python 3.8 and tensorflow 2.2, it works correctly on the specific GPU just by adding the below few lines at the first of the script.当我使用安装了 python 3.8 和 tensorflow 2.2 的虚拟环境运行代码时,只需在脚本的开头添加以下几行,它就可以在特定的 GPU 上正常工作。

import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "2"  # run the code on a specified GPU

Many threads recommend use the above code to run python scripts on a specific GPU such as here and here .许多线程建议使用上述代码在特定的 GPU 上运行 python 脚本,例如此处此处

However, When I tried to use the same way to run another python code on another virtual environment (with lower specifications) that was installed with python version 3.6.9 and tensorflow 1.12, it does not run on the GPU but on the CPU. However, When I tried to use the same way to run another python code on another virtual environment (with lower specifications) that was installed with python version 3.6.9 and tensorflow 1.12, it does not run on the GPU but on the CPU.

How can I run python code on a specific GPU in the case of the second virtual environment?在第二个虚拟环境的情况下,如何在特定的 GPU 上运行 python 代码?

You can use export CUDA_VISIBLE_DEVICES to define which GPUs are visible to the application.您可以使用export CUDA_VISIBLE_DEVICES来定义哪些 GPU 对应用程序可见。 For example, if you want GPUs 0 and 2 visible, use export CUDA_VISIBLE_DEVICES=0,2 .例如,如果您希望 GPU 0 和 2 可见,请使用export CUDA_VISIBLE_DEVICES=0,2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM