【问题标题】:How to prevent specific GPU to be used in Python code如何防止在 Python 代码中使用特定的 GPU
【发布时间】:2020-04-19 02:16:03
【问题描述】:

我的这个问题和1: How to set specific gpu in tensorflow? 一样,但它并没有解决我的问题。

我的 PC 中有 4 个 GPU,我想在 GPU 0 上运行代码,但每当我运行我的 tensorflow 代码时,我的代码总是只在 GPU 2 上运行。阅读这些(2、3、@ 987654324@) 解决方案和信息 我试图通过添加来解决我的问题:

  • os.environ['CUDA_VISIBLE_DEVICES']= '0' 在 python 代码中
  • 或CUDA_VISIBLE_DEVICES 作为 PyCharm 项目配置设置中的环境变量。
  • 此外,我还在代码或环境变量中添加CUDA_LAUNCH_BLOCKING=2以阻止 GPU 2。阻止任何 GPU 的方法是否正确?

以上解决方案对我不起作用。代码始终在 GPU 2 上运行。我通过 watch nvidia-smi 检查了它。 我的系统环境是

  • Ubuntu 16.04
  • RTX2080Ti(全部 4 个 GPU)
  • 驱动程序版本 418.74
  • CUDA 9.0 和 CuDNN 7.5
  • Tensorflow-GPU 1.9.0

对这个问题有什么建议吗?在 PyCharm 或 python 代码中的项目设置中添加环境变量是连线的……仍然只有 GPU 2 是可见的。当我删除CUDA_VISIBLE_DEVICESthen tensorflow 检测到所有 4 个 GPU,但代码仅在 GPU 2 上运行。

【问题讨论】:

  • 你可以检查一下,NVIDIA_VISIBLE_DEVICES=$gpu_id CUDA_VISIBLE_DEVICES=0 将 gpu id 设置为你想要的 gpu,比如 0,1,2 或 3

标签: python tensorflow pycharm


【解决方案1】:

我在 tensorflow 2.0.0 中试过这个

physical_devices = tf.config.experimental.list_physical_devices('GPU')

tf.config.experimental.set_visible_devices(physical_devices[0], 'GPU')

logical_devices = tf.config.experimental.list_logical_devices('GPU')

这应该让你的代码在 GPU 索引 0 中运行

【讨论】:

  • 您的建议在 tf 2.0 中工作正常,但在 tf 1.9 中不起作用,我认为在 2.0 中更改了一些功能。我试图通过检查这些链接在 tf 1.9 中获得相同的可能性。 Same issue 和 official tf guide。要使用某些 GPU,有最简单的方法 MirroredStrategy()defined in this link 但它也不起作用。
  • 你可以试试 MultiWorkerMirroredStrategy,我在 tf2.0 也试过这个
猜你喜欢
  • 2019-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多