首先检测是否可用 GPU

importtensorflow as tfprint('GPU', tf.test.is_gpu_available()) #GPU True

tf.device

Tensorflow 通过 tf.device 指定每个操作运行的设备,可以指定本地的 CPU、GPU,还可以指定远程服务器;

Tensorflow 会给每个本地设备一个名称,如 /cpu:0,即使电脑有多块 CPU ,tf 不会做区分,统一叫 /cpu:0,而 如果有多块 GPU,第 n 块 GPU 叫 /gpu:n,n 从 0 开始;

with tf.device('/cpu:0'):

d1= tf.Variable(1.)

d2= tf.Variable(2., name='d2')

with tf.device('/gpu:0'):

d3=tf.add(d1, d2)

with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:

sess.run(tf.global_variables_initializer())print(sess.run(d3))

log_device_placement

tf 提供了 log_device_placement 来查看 计算在 哪个设备上运行;

d1 = tf.constant(1.)

d2= tf.constant(2., name='d2')

d3=tf.add(d1, d2)### log_device_placement 记录了计算在哪个设备执行

with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:print(sess.run(d3))#Device mapping:#/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Quadro P600, pci bus id: 0000:02:00.0, compute capability: 6.1#Add: (Add): /job:localhost/replica:0/task:0/device:GPU:0#Const: (Const): /job:localhost/replica:0/task:0/device:GPU:0#d2: (Const): /job:localhost/replica:0/task:0/device:GPU:0#3.0

注意

1. 在 配置好 GPU 的 tf 中,计算优先被分配到 GPU 上;

2. 如果有多块 GPU,tf 也会优先叫计算放到 /gpu:0 上,而其他 GPU 不会被安排任务,如果需要放到其他 GPU,可通过 tf.device 指定

allow_soft_placement

不是所有操作都能在 GPU 上运行;

如在 GPU 上用 tf.Variable 创建变量时,只支持实数型(float16、float32、double),不支持整型;

#在CPU上运行tf.Variable

a_cpu = tf.Variable(0, name="a_cpu")

with tf.device('/gpu:0'):#将tf.Variable强制放在GPU上。

#a_gpu = tf.Variable(0, name="a_gpu")

##### 上句报错如下

#tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot assign a device for operation a_gpu:

#Could not satisfy explicit device specification '/device:GPU:0' because no supported kernel for GPU devices is available.

a_gpu = tf.Variable(0., name="a_gpu") ### 这样写就不报错

sess= tf.Session(config=tf.ConfigProto(log_device_placement=True))

sess.run(tf.initialize_all_variables())

为了避免某些操作不能放在 GPU 上而报错,allow_soft_placement 可以将报错的操作自动放到 CPU 上;

a_cpu = tf.Variable(0, name="a_cpu")

with tf.device('/gpu:0'):

a_gpu= tf.Variable(0, name="a_gpu")#通过allow_soft_placement参数自动将无法放在GPU上的操作放回CPU上

sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True))

sess.run(tf.initialize_all_variables())#Device mapping:#/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Quadro P600, pci bus id: 0000:02:00.0, compute capability: 6.1#a_cpu: (VariableV2): /job:localhost/replica:0/task:0/device:CPU:0#a_cpu/Assign: (Assign): /job:localhost/replica:0/task:0/device:CPU:0#a_cpu/read: (Identity): /job:localhost/replica:0/task:0/device:CPU:0# #a_gpu: (VariableV2): /job:localhost/replica:0/task:0/device:CPU:0#a_gpu/Assign: (Assign): /job:localhost/replica:0/task:0/device:CPU:0#a_gpu/read: (Identity): /job:localhost/replica:0/task:0/device:CPU:0 ##### 把 GPU 上的操作放到 CPU 上了# #init/NoOp: (NoOp): /job:localhost/replica:0/task:0/device:GPU:0#init/NoOp_1: (NoOp): /job:localhost/replica:0/task:0/device:GPU:0#init: (NoOp): /job:localhost/replica:0/task:0/device:GPU:0#a_cpu/initial_value: (Const): /job:localhost/replica:0/task:0/device:CPU:0#a_gpu/initial_value: (Const): /job:localhost/replica:0/task:0/device:GPU:0

GPU 资源分配

虽然说 GPU 可以加速,但通常不会把所有操作都放在 GPU 上,大致有如下原则:

1. 把计算密集型的操作放到 GPU 上

GPU 是相对独立的资源,将计算转入和转出 GPU 都需要额外的时间,而且 GPU 需要将计算所需的数据 从内存复制到 GPU 设备上,也需要额外的时间,

tensorflow 可自动完成这些操作,但为了提高运算效率,尽量将相关运算放到同一设备上;

2.通过环境变量分配 GPU 和显存;

################################### 通过环境变量分配 GPU 资源 ######################################## tensorflow 默认会占用所有 GPU (多块GPU) 和 所有显存,当然我们可以指定 GPU 或者 动态分配显存

################# 在运行时设置环境变量 ###################### 只使用第二块GPU(GPU编号从0开始)#在demo_code.py中,机器上的第二块GPU的名称变成/gpu:0,在运行时所有/gpu:0的运算将被放在第二块GPU上

CUDA_VISIBLE_DEVICES=1python demo_code.py##### 只使用第一块和第二块GPU

CUDA_VISIBLE_DEVICES=0,1python demo_code.py################# 在程序中设置环境变量 #################

importos

os.environ['CUDA_VISIBLE_DEVICES'] = '2' ### 设定只使用 第3块 GPU

################# 动态分配显存 ###################### TF 默认占用 GPU 的所有显存,我们可以手动分配显存,使得一块 GPU 可以同时运行多个任务

config =tf.ConfigProto()#让TensorFlow按需分配显存

config.gpu_options.allow_growth =True#或者直接按固定的比例分配

config.gpu_options.per_process_gpu_memory_fraction = 0.4 ### 占用所有可使用GPU的40%显存

session = tf.Session(config=config, ...)

多 GPU 训练

见 参考资料1

分布式 Tensorflow

见 参考资料1

参考资料:

float gpu 加速_tensorflow - GPU 加速相关推荐

  1. OpenCV之gpu 模块. 使用GPU加速的计算机视觉:GPU上的相似度检测(PNSR 和 SSIM)

    GPU上的相似度检测(PNSR 和 SSIM) 学习目标 在 OpenCV的视频输入和相似度测量 教程中我们已经学习了检测两幅图像相似度的两种方法:PSNR和SSIM.正如我们所看到的,执行这些算法需 ...

  2. FFmpeg在Intel GPU上的硬件加速与优化

    英特尔提供了一套基于VA-API/Media SDK的硬件加速方案,通过在FFmpeg中集成Intel GPU的媒体硬件加速能力,为用户提供更多的收益.本文来自英特尔资深软件开发工程师赵军在LiveV ...

  3. 国产高端GPU,国产替代加速(附国产厂家汇总)

    前言 2022年8月9日,壁仞科技在上海发布首款通用GPU芯片BR100,标志着中国企业第一次打破了此前一直由国际巨头保持的通用GPU全球算力纪录: 8月31日,美国政府命令芯片厂商英伟达(NVIDI ...

  4. FFmpeg 使用 Nvidia GPU 进行转码加速

    参考: https://blog.csdn.net/JineD/article/details/128420330?csdn_share_tail=%7B%22type%22%3A%22blog%22 ...

  5. How to Avoid Branching on the GPU 如何在GPU避免分支

    在GPU避免分支的方法 Authored by Brandon Fogerty(XR Graphics Engineer at Unity Technologies.) with Additional ...

  6. 安卓禁用硬件加速_Android硬件加速

    最近项目中遇到了因为硬件加速引起的一些问题,故这里深入学习了解一下关于硬件加速的一些东西 背景 什么是硬件加速? 硬件加速是Android系统在绘制图形时采取的一种方式. 图形的绘制,本质上就是界面的 ...

  7. 华为服务器gpu卡型号,GPU运算服务器推荐

    GPU运算服务器推荐 内容精选 换一换 只有运行中的云服务器才允许用户登录.Windows操作系统用户名"Administrator".忘记密码,请先通过"重置密码&qu ...

  8. 华为服务器gpu卡型号,gpu服务器 华为云

    gpu服务器 华为云 内容精选 换一换 云手机作为一种新型服务,依靠华为云的服务器底座.创新技术及周边服务的天然优势,让移动应用不但可以在物理手机运行,还可以在云端智能运行.相比常见的手机模拟方案,云 ...

  9. TensorFlow指定使用GPU 多块gpu

    持续监控GPU使用情况命令: $ watch -n 10 nvidia-smi 1 一.指定使用某个显卡 如果机器中有多块GPU,tensorflow会默认吃掉所有能用的显存, 如果实验室多人公用一台 ...

最新文章

  1. AAAI 2020论文解读:商汤科技提出新弱监督目标检测框架
  2. 《c语言从入门到精通》看书笔记——第1章 C语言的概述
  3. vim cheat-sheet
  4. 对微软的敌视何时休? 从一篇语言评论文章对C#的评价说起
  5. 天猫浏览型应用的CDN静态化架构演变(转)
  6. POS Tagging 标签类型查询表(Penn Treebank Project)
  7. mysql 深入视图和索引
  8. NUC1041 数字三角形【DP】
  9. 深度linux磁盘编辑,磁盘管理(分区,格式化,维护,写入,配额)- 磁盘管理 - Deepin深度系统用户手册...
  10. png 微软ppt 透明度_工作必备,如何用PPT把logo玩出千种花样?
  11. Smarty自学笔记
  12. 萤石开放平台-摄像头画面如何在小程序上播放?
  13. 解决java:找不到符号办法
  14. 阿里云服务器ECS测评
  15. 32位系统支持多大内存 Windows32位/64位系统最大支持内存详解
  16. 网络三定律:摩尔定律、吉尔德定律和迈特卡夫定律
  17. java中String转byte 编码格式
  18. vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5
  19. 判断电路是正反馈还是负反馈,电流反馈还是电压反馈
  20. 面试程序员被问“你的缺点是什么”,我该怎么回答?

热门文章

  1. 浅谈Horizon DaaS平台 - 崛起的桌面云平台
  2. 77.redis介绍 安装 持久化 数据类型
  3. CenTos6.8安装Heartbeat
  4. 报告显示:至2020年,paas在云端的普及率将达32%
  5. Linux环境下oracle client安装和配置
  6. Elasticsearch学习笔记-04修改数据
  7. linux下杀死进程的10种方法
  8. Gartner 认定 Microsoft 为具有远见卓识的云基础结构即服务提供商
  9. Android 使用摄像头拍照
  10. 使用Quartus建立第一个FPGA工程