目录:

介绍

记录设备状态

手动分配状态

允许GPU内存增长

在多GPU系统是使用单个GPU

使用多个 GPU

一、介绍

在一个典型的系统中,有多个计算设备。在 TensorFlow 中支持的设备类型包括 CPU 和 GPU。他们用字符串来表达,例如:

  • “/cpu:0”: 机器的 CPU
  • “/device:GPU:0”: 机器的 GPU 如果你只有一个
  • “/device:GPU:1”: 机器的第二个 GPU

如果 TensorFlow 操作同时有 CPU 和 GPU 的实现,操作将会优先分配给 GPU 设备。例如,matmul 同时有 CPU 和 GPU 核心,在一个系统中同时有设备 cpu:0 和 gpu:0,gpu:0 将会被选择来执行 matmul。

二、记录设备状态

为了确定你的操作和张量分配给了哪一个设备,创建一个把 log_device_placement 的配置选项设置为 True 的会话即可。

创建一个计算图

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,它的 log_device_placement 被设置为 True.

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

运行这个操作

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.]]

三、手动分配设备

如果你希望一个特定的操作运行在一个你选择的设备上,而不是自动选择的设备,你可以使用 tf.device 来创建一个设备环境,这样所有在这个环境的操作会有相同的设备分配选项。

创建一个会话

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)

创建一个 session,它的 log_device_placement 被设置为 True

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

运行这个操作

print(sess.run(c))

你将会看到 a 和 b 被分配给了 cpu:0。因为没有指定特定的设备来执行 matmul 操作,TensorFlow 将会根据操作和已有的设备来选择(在这个例子中是 gpu:0),并且如果有需要会自动在设备之间复制张量。

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/cpu:0

a: /job:localhost/replica:0/task:0/cpu:0

MatMul: /job:localhost/replica:0/task:0/device:GPU:0

[[ 22. 28.]

[ 49. 64.]]

四、允许 GPU 内存增长

默认情况下,TensorFlow 将几乎所有的 GPU的显存(受 CUDA_VISIBLE_DEVICES 影响)映射到进程。 通过减少内存碎片,可以更有效地使用设备上宝贵的GPU内存资源。

在某些情况下,只需要分配可用内存的一个子集给进程,或者仅根据进程需要增加内存使用量。 TensorFlow 在 Session 上提供了两个 Config 选项来控制这个选项。

第一个是 allow_growth 选项,它根据运行时的需要分配 GPU 内存:它开始分配很少的内存,并且随着 Sessions 运行并需要更多的 GPU 内存,我们根据 TensorFlow 进程需要继续扩展了GPU所需的内存区域。请注意,我们不释放内存,因为这会导致内存碎片变得更糟。要打开此选项,请通过以下方式在 ConfigProto 中设置选项:

config = tf.ConfigProto()

config.gpu_options.allow_growth = True

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

第二种方法是 per_process_gpu_memory_fraction 选项,它决定了每个可见GPU应该分配的总内存量的一部分。例如,可以通过以下方式告诉 TensorFlow 仅分配每个GPU的总内存的40%:

config = tf.ConfigProto()

config.gpu_options.per_process_gpu_memory_fraction = 0.4

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

如果要真正限制 TensorFlow 进程可用的GPU内存量,这非常有用。

五、在多GPU系统上使用单个GPU

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

创建一个计算图

with tf.device(‘/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)

创建一个 log_device_placement 设置为True 的会话

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

运行这个操作

print(sess.run(c))

你会看到现在 a 和 b 被分配给 cpu:0。 由于未明确指定设备用于 MatMul 操作,因此 TensorFlow 运行时将根据操作和可用设备(本例中为 gpu:0)选择一个设备,并根据需要自动复制设备之间的张量。

如果指定的设备不存在,将得到 InvalidArgumentError:

InvalidArgumentError: Invalid argument: Cannot assign a device to node ‘b’:

Could not satisfy explicit device specification ‘/device:GPU:2’

[[Node: b = Const[dtype=DT_FLOAT, value=Tensor<type: float shape: [3,2]

values: 1 2 3…>, _device=”/device:GPU:2”]()]]

如果希望 TensorFlow 在指定的设备不存在的情况下自动选择现有的受支持设备来运行操作,则可以在创建会话时在配置选项中将 allow_soft_placement 设置为 True。

创建计算图

with tf.device(‘/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)

创建一个 allow_soft_placement 和 log_device_placement 设置为 True 的会话

sess = tf.Session(config=tf.ConfigProto(

allow_soft_placement=True, log_device_placement=True))

运行这个操作

print(sess.run(c))

六、使用多个 GPU

如果您想要在多个 GPU 上运行 TensorFlow ,则可以采用多塔式方式构建模型,其中每个塔都分配有不同的 GPU。 例如:

创建计算图

c = []

for d in [‘/device:GPU:2’, ‘/device:GPU:3’]:

with tf.device(d):

a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3])

b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2])

c.append(tf.matmul(a, b))

with tf.device(‘/cpu:0’):

sum = tf.add_n(c)

创建一个 log_device_placement 设置为 True 的会话

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

运行这个操作

print(sess.run(sum))

你将会看到以下的输出:

Device mapping:

/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Tesla K20m, pci bus

id: 0000:02:00.0

/job:localhost/replica:0/task:0/device:GPU:1 -> device: 1, name: Tesla K20m, pci bus

id: 0000:03:00.0

/job:localhost/replica:0/task:0/device:GPU:2 -> device: 2, name: Tesla K20m, pci bus

id: 0000:83:00.0

/job:localhost/replica:0/task:0/device:GPU:3 -> device: 3, name: Tesla K20m, pci bus

id: 0000:84:00.0

Const_3: /job:localhost/replica:0/task:0/device:GPU:3

Const_2: /job:localhost/replica:0/task:0/device:GPU:3

MatMul_1: /job:localhost/replica:0/task:0/device:GPU:3

Const_1: /job:localhost/replica:0/task:0/device:GPU:2

Const: /job:localhost/replica:0/task:0/device:GPU:2

MatMul: /job:localhost/replica:0/task:0/device:GPU:2

AddN: /job:localhost/replica:0/task:0/cpu:0

[[ 44. 56.]

[ 98. 128.]]

翻译自:

https://www.tensorflow.org/programmers_guide/using_gpu

多GPU使用详解的更多相关文章

  1. [CB]Intel 2018架构日详解:新CPU&新GPU齐公布 牙膏时代有望明年结束

    Intel 2018架构日详解:新CPU&新GPU齐公布 牙膏时代有望明年结束 北京时间12月12日晚,Intel在圣克拉拉举办了架构日活动.在五个小时的演讲中,Intel揭开了2021年CP ...

  2. Win10 TensorFlow(gpu)安装详解

    Win10 TensorFlow(gpu)安装详解 写在前面:TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,其命名来源于本身的运行原理.Tensor(张量)意味着 ...

  3. (转)Win10 TensorFlow(gpu)安装详解

    Win10 TensorFlow(gpu)安装详解 写在前面:TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,其命名来源于本身的运行原理.Tensor(张量)意味着 ...

  4. GPU虚拟化技术详解

    GPU虚拟化技术详解 GPU英文名称为Graphic Processing Unit,GPU中文全称为计算机图形处理器,1999年由NVIDIA公司提出. 一.GPU概述 GPU这一概念也是相对于计算 ...

  5. 详解Paint的setXfermode(Xfermode xfermode)

    一.setXfermode(Xfermode xfermode) Xfermode国外有大神称之为过渡模式,这种翻译比较贴切但恐怕不易理解,大家也可以直接称之为图像混合模式,因为所谓的“过渡”其实就是 ...

  6. Chrome开发者工具详解(3)-Timeline面板

    Chrome开发者工具详解(3)-Timeline面板 注: 这一篇主要讲解面板Timeline,参考了Google的相关文档,主要用于公司内部技术分享.. Timeline面板 Timeline面板 ...

  7. 2013款Mac Pro“神秘”主机详解

    2013款Mac Pro"神秘"主机详解 自2013年12月下旬新款Mac Pro在美国发布以来,经过一个月的等待终于在国内已到货,笔者亲自测试了这个号称"神秘" ...

  8. GLSL-几何着色器详解跟实例(GS:Geometry Shader)[转]

    [OpenGL4.0]GLSL-几何着色器详解和实例(GS:Geometry Shader) 一.什么是几何着色器(GS:Geometry Shader) Input Assembler(IA)从顶点 ...

  9. 莱特币ltc在linux下的多种挖矿方案详解

    莱特币ltc在linux下的多种挖矿方案详解 4.0.1 Nvidia显卡Linux驱动Nvidia全部驱动:http://www.nvidia.cn/Download/index.aspx?lang ...

随机推荐

  1. ElasticSearch系列四 CURD

    1: ES 类似JPA操作 1.1 编写实体类 1.2 编写映射文件 xxx.json 1.3编写repository继承 ElasticSearchrepository 1.4 编写admin 的C ...

  2. Git的安装与TortoiseGit的安装和汉化

    下载Git 进入https://git-scm.com/downloads 可以看到如下界面 因为我是windows系统,选择windows即可. 有的朋友因为网络慢的一些原因不能很快下载下来,可以进 ...

  3. C++扬帆远航——11(斐波那契数列)

    /* * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:Feibo.cpp * 作者:常轩 * 微信公众号:Worldh ...

  4. Python——详解collections工具库

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天为大家介绍Python当中一个很好用也是很基础的工具库,叫做collections. collection在英文当中有容器的意思,所以顾 ...

  5. Object-C 银行卡,信用卡校验规则(Luhn算法)

    最近的项目中涉及到绑定用户的银行卡,借记卡.经过查找银行卡的校验规是采用 Luhn算法进行验证. Luhn算法,也被称作“模10算法”.它是一种简单的校验公式,一般会被用于身份证号码,IMEI号码,美 ...

  6. ArrayList集合不能使用foreach增加、删除、修改元素的原因

    大家先看两段代码 第一段代码: List<String> arrayList1 = new ArrayList<String>(); arrayList1.add(" ...

  7. ajax+lazyload时lazyload失效问题及解决

    最近写公司的项目的时候遇到一个关于图片加载的问题,所做的页面是一个商城的商品列表页,其中需要显示商品图片,名称等信息,因为商品列表可能会很长,所以其中图片需要滑到可以显示的区域再进行加载. 首先我的图 ...

  8. 沃土前端系列 - HTML常用标签

    html是什么 HTML是Hyper Text Markup Language的缩写,中文的意思是"超文本标记语言",它是制作网页的标准语言.由于网页中不仅包含普通文本,还包含超文 ...

  9. C/C++ 手动开O2

    手动O2比赛不能用,平时玩玩就好 #pragma GCC optimize (2) #pragma G++ optimize (2)

  10. 关于Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析

    1.Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service和 @Controller 其实这三个跟@Com ...