GPU上的Tensorflow Matmul计算比CPU慢 [英] Tensorflow matmul calculations on GPU are slower than on CPU
问题描述
我第一次尝试使用GPU计算,当然希望能够大幅度提高速度.但是,使用张量流中的一个基本示例,实际上更糟:
I'm experimenting with GPU computations for the first time and was hoping for a big speed-up, of course. However with a basic example in tensorflow, it actually was worse:
在cpu:0上,十次运行的平均时间为2秒,gpu:0耗时2.7秒,而gpu:1比cpu:0差3秒,即降低了50%.
On cpu:0, each of the ten runs takes on average 2 seconds, gpu:0 takes 2.7 seconds and gpu:1 is 50% worse than cpu:0 with 3 seconds.
代码如下:
import tensorflow as tf
import numpy as np
import time
import random
for _ in range(10):
with tf.Session() as sess:
start = time.time()
with tf.device('/gpu:0'): # swap for 'cpu:0' or whatever
a = tf.constant([random.random() for _ in xrange(1000 *1000)], shape=[1000, 1000], name='a')
b = tf.constant([random.random() for _ in xrange(1000 *1000)], shape=[1000, 1000], name='b')
c = tf.matmul(a, b)
d = tf.matmul(a, c)
e = tf.matmul(a, d)
f = tf.matmul(a, e)
for _ in range(1000):
sess.run(f)
end = time.time()
print(end - start)
我在这里观察到什么?运行时间可能主要由在RAM和GPU之间复制数据主导吗?
What am I observing here? Is run time maybe mainly dominated by copying data between RAM and GPU?
推荐答案
用于生成数据的方式在CPU上执行(random.random()
是常规的python函数,而不是TF-one).同样,执行一次10^6
次将比一次运行请求10^6
个随机数要慢.
将代码更改为:
The way you use to generate data is executed on CPU (random.random()
is a regular python function and not TF-one). Also, executing it 10^6
times will be slower than requesting 10^6
random numbers in one run.
Change the code to:
a = tf.random_uniform([1000, 1000], name='a')
b = tf.random_uniform([1000, 1000], name='b')
以便数据将在GPU上并行生成,而不会浪费时间将其从RAM传输到GPU.
so that the data will be generated on a GPU in parallel and no time will be wasted to transfer it from RAM to GPU.
这篇关于GPU上的Tensorflow Matmul计算比CPU慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!