版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38784098/article/details/86536078
一、查看信息
在一套标准的系统上通常有多个计算设备. TensorFlow 支持 CPU 和 GPU 这两种设备. 我们用指定字符串 strings
来标识这些设备. 比如:
"/cpu:0"
: 机器中的 CPU"/gpu:0"
: 机器中的 GPU, 如果你有一个的话."/gpu:1"
: 机器中的第二个 GPU, 以此类推...
如果一个 TensorFlow 的 operation 中兼有 CPU 和 GPU 的实现, 当这个算子被指派设备时, GPU 有优先权. 比如matmul
中 CPU 和 GPU kernel 函数都存在. 那么在 cpu:0
和 gpu:0
中, matmul
operation 会被指派给 gpu:0
.
查看显卡型号
lspci | grep -i vga
1.nvidia
nvidia-smi
2.intel
1)安装intel-gpu-tools工具
yum install intel-gpu-tools
2)安装完毕后, 系统中会多种三个gpu工具: intel_gpu_abrt intel_gpu_time intel_gpu_top
其中,常用的是intel_gpu_top和intel_gpu_time。
二、指定GPU、CPU运行
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
注:上述代码一定要放在import tensorflow或keras等之前,否则不起作用。
2.在多GPU系统里使用单一GPU
为了获取你的 operations 和 Tensor 被指派到哪个设备上运行, 用 log_device_placement
新建一个 session
, 并设置为 True
.
# 新建一个 graph.
with tf.device('/gpu: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)
# 新建 session with log_device_placement 并设置为 True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# 运行这个 op.
print sess.run(c)
3.kreas
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0' if on_server is False else '0,1'