簡體   English   中英

GPU上的Tensorflow Matmul計算比CPU慢

[英]Tensorflow matmul calculations on GPU are slower than on CPU

我是第一次嘗試GPU計算,當然希望能夠大幅度提高速度。 但是,使用tensorflow中的一個基本示例實際上更糟:

在cpu:0上,十次運行中的每一次平均花費2秒,gpu:0花費2.7秒,而gpu:1比cpu:0差3秒,即降低50%。

這是代碼:

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之間復制數據主導?

用於生成數據的方式在CPU上執行( random.random()是常規的python函數,而不是TF-one)。 同樣,執行10^6次比一次運行請求10^6隨機數要慢。 將代碼更改為:

a = tf.random_uniform([1000, 1000], name='a')
b = tf.random_uniform([1000, 1000], name='b')

因此,數據將在GPU上並行生成,而不會浪費時間將其從RAM傳輸到GPU。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM