TensorFlow默认情况下使用机器中的所有可用的GPU? [英] Does TensorFlow by default use all available GPUs in the machine?

查看:5537
本文介绍了TensorFlow默认情况下使用机器中的所有可用的GPU?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的机器中有3个GTX Titan GPU。我使用cifar10_train.py运行Cifar10中提供的示例,并获得以下输出:

I have 3 GTX Titan GPUs in my machine. I run the example provided in Cifar10 with cifar10_train.py and got the following output:

I tensorflow/core/common_runtime/gpu/gpu_init.cc:60] cannot enable peer access from device ordinal 0 to device ordinal 1
I tensorflow/core/common_runtime/gpu/gpu_init.cc:60] cannot enable peer access from device ordinal 1 to device ordinal 0
I tensorflow/core/common_runtime/gpu/gpu_init.cc:127] DMA: 0 1 
I tensorflow/core/common_runtime/gpu/gpu_init.cc:137] 0:   Y N 
I tensorflow/core/common_runtime/gpu/gpu_init.cc:137] 1:   N Y 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:694] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX TITAN, pci bus id: 0000:03:00.0)
I tensorflow/core/common_runtime/gpu/gpu_device.cc:694] Creating TensorFlow device (/gpu:1) -> (device: 1, name: GeForce GTX TITAN, pci bus id: 0000:84:00.0)

它看起来我TensorFlow试图在两个设备(gpu0和gpu1)上初始化自己。

It looks to me that TensorFlow is trying to initialize itself on two devices (gpu0 and gpu1).

我的问题是为什么它只在两个设备上,并有什么办法来防止这种情况? (我只想让它像一个GPU一样运行)

My question is why it only does that on two devices and is there any way to prevent that? (I only want it to run as if there is a single GPU)

推荐答案

请参阅:使用GPU

使用单个GPU多GPU系统

如果系统中有多个GPU,默认情况下将选择具有最低ID的GPU。如果您想在不同的GPU上运行,您需要明确指定首选项:

If you have more than one GPU in your system, the GPU with the lowest ID will be selected by default. If you would like to run on a different GPU, you will need to specify the preference explicitly:

# Creates a graph.
with tf.device('/gpu:2'):
  a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
  b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
  c = tf.matmul(a, b)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print sess.run(c)

早期答案。

CUDA_VISIBLE_DEVICES - 屏蔽GPU


您的CUDA应用程序需要定位到特定的GPU吗?如果你是
编写GPU启用的代码,你通常使用设备查询
选择所需的GPU。然而,
测试的一个快速和容易的解决方案是使用环境变量CUDA_VISIBLE_DEVICES到
限制您的CUDA应用程序看到的设备。如果你试图在一个节点上共享资源,或者你想要
的GPU的可执行程序来指定一个特定的GPU,这可以是

Does your CUDA application need to target a specific GPU? If you are writing GPU enabled code, you would typically use a device query to select the desired GPUs. However, a quick and easy solution for testing is to use the environment variable CUDA_VISIBLE_DEVICES to restrict the devices that your CUDA application sees. This can be useful if you are attempting to share resources on a node or you want your GPU enabled executable to target a specific GPU.

环境变量语法

结果

CUDA_VISIBLE_DEVICES = 1只看到设备1
CUDA_VISIBLE_DEVICES = 0 ,1设备0和1将可见
CUDA_VISIBLE_DEVICES =0,1同上,引号是可选的
CUDA_VISIBLE_DEVICES = 0,2,3设备0,2,3将可见;设备1
被屏蔽

CUDA_VISIBLE_DEVICES=1 Only device 1 will be seen CUDA_VISIBLE_DEVICES=0,1 Devices 0 and 1 will be visible CUDA_VISIBLE_DEVICES="0,1" Same as above, quotation marks are optional CUDA_VISIBLE_DEVICES=0,2,3 Devices 0, 2, 3 will be visible; device 1 is masked

CUDA将枚举从零开始的可见设备。在最后
的情况下,设备0,2,3将显示为设备0,1,2。如果将
的字符串顺序改为2,3,0,则设备2,3 ,0将被分别枚举
为0,1,2。如果CUDA_VISIBLE_DEVICES设置为
不存在的设备,则所有设备都将被屏蔽。您可以指定
有效和无效设备号的混合。在无效值
之前的所有设备都将被枚举,而无效值之后的所有设备将被
屏蔽。

CUDA will enumerate the visible devices starting at zero. In the last case, devices 0, 2, 3 will appear as devices 0, 1, 2. If you change the order of the string to "2,3,0", devices 2,3,0 will be enumerated as 0,1,2 respectively. If CUDA_VISIBLE_DEVICES is set to a device that does not exist, all devices will be masked. You can specify a mix of valid and invalid device numbers. All devices before the invalid value will be enumerated, while all devices after the invalid value will be masked.

要确定设备ID系统中可用的硬件
,您可以运行CUDA SDK中包含的NVIDIA的deviceQuery可执行文件。
快乐的程式设计!

To determine the device ID for the available hardware in your system, you can run NVIDIA’s deviceQuery executable included in the CUDA SDK. Happy programming!

Chris Mason

Chris Mason

这篇关于TensorFlow默认情况下使用机器中的所有可用的GPU?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆