TensorFlow多GPU并行计算

本文介绍了如何在TensorFlow中指定使用特定的GPU或CPU进行计算,包括默认使用GPU #0,指定使用CPU #0,GPU #1,以及同时使用GPU #0和CPU #0。在单GPU环境下,尝试指定不存在的GPU会引发错误。
摘要由CSDN通过智能技术生成

1. TensorFlow指定特定GPU或者CPU进行计算:

说明:示例计算机为单CPU(编号为0),单GPU(编号为0),安装的TensorFlow为GPU版。

本文的结构如下:

  • 默认为GPU #0
  • 指定CPU #0
  • 指定GPU #1
  • 指定GPU #0 + CPU #0
1.1 默认为GPU #0
In [1]: import tensorflow as tf
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcublas.so locally
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcudnn.so locally
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcufft.so locally
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcurand.so locally
In [2]: with tf.Session() as sess:
   ...:     matrix1=tf.constant([[3.,3.]])
   ...:     matrix2=tf.constant([[2.],[2.]])
   ...:     product=tf.matmul(matrix1,matrix2)
   ...:     result=sess.run(product)
   ...:     print result
   ...:
I tensorflow/core/common_runtime/gpu/gpu_init.cc:102] Found device 0 with properties: 
name: GeForce GTX 970
major: 5 minor: 2 memoryClockRate (GHz) 1.266
pciBusID 0000:01:00.0
Total memory: 4.00GiB
Free memory: 3.62GiB
I tensorflow/core/common_runtime/gpu/gpu_init.cc:126] DMA: 0 
I tensorflow/core/common_runtime/gpu/gpu_init.cc:136] 0:   Y 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:838] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 970, pci bus id: 0000:01:00.0)
[[ 12.]]
1.2 指定GPU #0
In [1]: import tensorflow as tf
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcublas.so locally
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcudnn.so
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值