Tensorflow支持的设备包括CPU、GPU,它们都可以用strings表示。
/cpu:0
机器的CPU/device:GPU:0
机器的第1个GPU
+/device:GPU:1
机器的第2个GPU
Tensorflow operation有CPU与GPU实现,当operation被赋予device时,GPU拥有较高的优先级。
例如,matmul
同时有CPU与GPU的kernel,如果系统同时存在cpu:0
与gpu:0
,则gpu:0
会被选择执行.
Device配置记录
为方便记录opeartion与tensor被赋予何种device,在创建session时,配置log_device_placement
选项
# Creates a graph.
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))
则会看到如下输出
Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Tesla K40c, pci bus
id: 0000:05:00.0
b: /job:localhost/replica:0/task:0/device:GPU:0
a: /job:localhost/replica:0/task:0/device:GPU:0
MatMul: /job:localhost/replica:0/task:0/device:GPU:0
[[ 22. 28.]
[ 49. 64.]]
【参考】
1。 Tensorflow GPU:https://www.tensorflow.org/programmers\_guide/using\_gpu