我的机器上有3个GTX Titan GPU。我使用cifar10_train.py运行Cifar10中提供的示例,并获得以下输出:
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)上初始化自身。
我的问题是为什么它只能在两个设备上进行操作,并且有什么方法可以防止这种情况发生?(我只希望它像有一个GPU一样运行)
请参阅:使用GPU
手动放置设备
如果您希望某个特定的操作在您选择的设备上运行,而不是在为您自动选择的设备上运行,则可以将其tf.device
用于创建设备上下文,以使该上下文中的所有操作都具有相同的设备分配。
# Creates a graph.
with tf.device('/cpu:0'):
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))
您将看到现在将a和b分配给cpu:0
。由于未为MatMul
操作明确指定设备,因此TensorFlow运行时将根据操作和可用设备(在此示例中为gpu:0)选择一个设备,并在需要时自动在设备之间复制张量。
Device mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Tesla K40c, pci bus
id: 0000:05:00.0
b: /job:localhost/replica:0/task:0/cpu:0
a: /job:localhost/replica:0/task:0/cpu:0
MatMul: /job:localhost/replica:0/task:0/gpu:0
[[ 22. 28.]
[ 49. 64.]]
较早答案2。
请参阅:使用GPU
在多GPU系统上使用单个GPU
如果系统中有多个GPU,则默认情况下将选择ID最低的GPU。如果要在其他GPU上运行,则需要明确指定首选项:
# 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)
较早答案1。
您的CUDA应用程序是否需要针对特定的GPU?如果要编写启用GPU的代码,通常会使用设备查询来选择所需的GPU。但是,一种快速简便的测试解决方案是使用环境变量CUDA_VISIBLE_DEVICES来限制CUDA应用程序可以看到的设备。如果您尝试共享节点上的资源,或者希望启用GPU的可执行文件以特定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将枚举从零开始的可见设备。在最后一种情况下,设备0、2、3将显示为设备0、1、2。如果将字符串的顺序更改为“ 2,3,0”,则设备2,3,0将被枚举为0, 1,2 如果将CUDA_VISIBLE_DEVICES设置为不存在的设备,则将屏蔽所有设备。您可以指定有效和无效设备编号的混合。将枚举无效值之前的所有设备,而将屏蔽无效值之后的所有设备。
要确定系统中可用硬件的设备ID,可以运行CUDA SDK中包含的NVIDIA deviceQuery可执行文件。编程愉快!
克里斯·梅森
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句