在Tensorflow中,GPU的运行速度比CPU慢,为什么?

时间:2019-02-16 08:15:57

标签: tensorflow gpu cpu

以下代码:

import time
import tensorflow as tf

tf.enable_eager_execution()


def time_matmul(x):
    start = time.time()
    for loop in range(10):
        tf.matmul(x, x)

    result = time.time() - start

    print("10 loops: {:0.2f}ms".format(1000 * result))


# Force execution on CPU
print("On CPU:")
with tf.device("CPU:0"):
    x = tf.random_uniform([1000, 1000])
    assert x.device.endswith("CPU:0")
    time_matmul(x)

# Force execution on GPU #0 if available
print("On GPU:")
if tf.test.is_gpu_available():
    with tf.device("GPU:0"):  # Or GPU:1 for the 2nd GPU, GPU:2 for the 3rd etc.
        x = tf.random_uniform([1000, 1000])
        assert x.device.endswith("GPU:0")
        time_matmul(x)

给出以下输出:

On CPU:
2019-02-16 11:12:33.724828: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2019-02-16 11:12:34.056651: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1432] Found device 0 with properties: 
name: GeForce GTX 1070 major: 6 minor: 1 memoryClockRate(GHz): 1.7715
pciBusID: 0000:01:00.0
totalMemory: 8.00GiB freeMemory: 6.63GiB
2019-02-16 11:12:34.056984: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1511] Adding visible gpu devices: 0
2019-02-16 11:12:34.501349: I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-02-16 11:12:34.501515: I tensorflow/core/common_runtime/gpu/gpu_device.cc:988]      0 
2019-02-16 11:12:34.501612: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1001] 0:   N 
2019-02-16 11:12:34.501855: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 6384 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1070, pci bus id: 0000:01:00.0, compute capability: 6.1)
10 loops: 206.00ms
On GPU:
2019-02-16 11:12:34.718164: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1511] Adding visible gpu devices: 0
2019-02-16 11:12:34.718377: I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-02-16 11:12:34.718540: I tensorflow/core/common_runtime/gpu/gpu_device.cc:988]      0 
2019-02-16 11:12:34.718641: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1001] 0:   N 
2019-02-16 11:12:34.718832: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/device:GPU:0 with 6384 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1070, pci bus id: 0000:01:00.0, compute capability: 6.1)
10 loops: 239.52ms

CPU为Core i7,GPU为GTX 1070

发生了什么事?

1 个答案:

答案 0 :(得分:0)

在具有100个循环的同一台Windows计算机上:

On CPU:
100 loops: 2100.54ms
On GPU:
100 loops: 229.00ms

在具有Core i5,相同的GPU模型和更好的RAM的Linux机器上,有10个循环:

On CPU:
10 loops: 112.44ms
On GPU:
10 loops: 113.44ms

在具有100个循环的同一台Linux计算机上:

On CPU:
100 loops: 1120.60ms
On GPU:
100 loops: 107.55ms

所以,问题出在内部数据传输