TensorFlow调用CPU/GPU对比速度

一、GPU代码

import time
import tensorflow as tf

begin = time.time()

with tf.device('/gpu:0'):
    rand_t = tf.random_uniform([50,50],0,10,dtype=tf.float32,seed=0)
    a = tf.Variable(rand_t)
    b = tf.Variable(rand_t)
    c = tf.matmul(a,b)
    init = tf.global_variables_initializer()

sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) #强制使用GPU
sess.run(init)
print(sess.run(c))

end = time.time()
print(end-begin,'s')
2019-02-21 22:04:26.726268: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2019-02-21 22:04:27.112285: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\gpu\gpu_device.cc:1030] Found device 0 with properties: 
name: GeForce GTX 960M major: 5 minor: 0 memoryClockRate(GHz): 1.176
pciBusID: 0000:01:00.0
totalMemory: 2.00GiB freeMemory: 1.65GiB
2019-02-21 22:04:27.112967: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\gpu\gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 960M, pci bus id: 0000:01:00.0, compute capability: 5.0)
Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce GTX 960M, pci bus id: 0000:01:00.0, compute capability: 5.0
Variable_1: (VariableV2): /job:localhost/replica:0/task:0/device:GPU:0
Variable_1/read: (Identity): /job:localhost/replica:0/task:0/device:GPU:0
Variable: (VariableV2): /job:localhost/replica:0/task:0/device:GPU:0
Variable/read: (Identity): /job:localhost/replica:0/task:0/device:GPU:0
MatMul: (MatMul): /job:localhost/replica:0/task:0/device:GPU:0
random_uniform/sub: (Sub): /job:localhost/replica:0/task:0/device:GPU:0
random_uniform/RandomUniform: (RandomUniform): /job:localhost/replica:0/task:0/device:GPU:0
random_uniform/mul: (Mul): /job:localhost/replica:0/task:0/device:GPU:0
2019-02-21 22:04:27.264328: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\direct_session.cc:299] Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce GTX 960M, pci bus id: 0000:01:00.0, compute capability: 5.0

2019-02-21 22:04:27.266224: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\placer.cc:874] Variable_1: (VariableV2)/job:localhost/replica:0/task:0/device:GPU:0
2019-02-21 22:04:27.266854: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\placer.cc:874] Variable_1/read: (Identity)/job:localhost/replica:0/task:0/device:GPU:0
2019-02-21 22:04:27.267491: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\placer.cc:874] Variable: (VariableV2)/job:localhost/replica:0/task:0/device:GPU:0
2019-02-21 22:04:27.268118: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\placer.cc:874] Variable/read: (Identity)/job:localhost/replica:0/task:0/device:GPU:0
2019-02-21 22:04:27.268720: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\placer.cc:874] MatMul: (MatMul)/job:localhost/replica:0/task:0/device:GPU:0
2019-02-21 22:04:27.269363: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\placer.cc:874] random_uniform/sub: (Sub)/job:localhost/replica:0/task:0/device:GPU:0
2019-02-21 22:04:27.270043: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\placer.cc:874] random_uniform/RandomUniform: (RandomUniform)/job:localhost/replica:0/task:0/device:GPU:0
2019-02-21 22:04:27.272116: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\placer.cc:874] random_uniform/mul: (Mul)/job:localhost/replica:0/task:0/device:GPU:0
random_uniform: (Add): /job:localhost/replica:0/task:0/device:GPU:0
Variable_1/Assign: (Assign): /job:localhost/replica:0/task:0/device:GPU:0
Variable/Assign: (Assign): /job:localhost/replica:0/task:0/device:GPU:0
init: (NoOp): /job:localhost/replica:0/task:0/device:GPU:0
random_uniform/max: (Const): /job:localhost/replica:0/task:0/device:GPU:0
random_uniform/min: (Const): /job:localhost/replica:0/task:0/device:GPU:0
random_uniform/shape: (Const): /job:localhost/replica:0/task:0/device:GPU:0
2019-02-21 22:04:27.274185: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\placer.cc:874] random_uniform: (Add)/job:localhost/replica:0/task:0/device:GPU:0
2019-02-21 22:04:27.274958: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\placer.cc:874] Variable_1/Assign: (Assign)/job:localhost/replica:0/task:0/device:GPU:0
2019-02-21 22:04:27.275633: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\placer.cc:874] Variable/Assign: (Assign)/job:localhost/replica:0/task:0/device:GPU:0
2019-02-21 22:04:27.276352: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\placer.cc:874] init: (NoOp)/job:localhost/replica:0/task:0/device:GPU:0
2019-02-21 22:04:27.276909: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\placer.cc:874] random_uniform/max: (Const)/job:localhost/replica:0/task:0/device:GPU:0
2019-02-21 22:04:27.277621: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\placer.cc:874] random_uniform/min: (Const)/job:localhost/replica:0/task:0/device:GPU:0
2019-02-21 22:04:27.278221: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\placer.cc:874] random_uniform/shape: (Const)/job:localhost/replica:0/task:0/device:GPU:0
[[1405.2432 1441.7412 1364.38   ... 1480.2251 1279.0063 1620.0939]
 [1232.6589 1344.4458 1169.7092 ... 1205.1285 1040.5568 1421.9672]
 [1209.3167 1180.3208 1158.1396 ... 1200.0343 1014.0321 1222.5107]
 ...
 [1298.9648 1262.9236 1205.6917 ... 1396.4789 1090.7252 1437.241 ]
 [1118.2474 1209.0153 1077.7229 ... 1180.7023 1076.4692 1139.7421]
 [1200.8864 1297.2267 1260.0101 ... 1289.4294 1165.245  1433.4185]]
1.7576417922973633 s

二、CPU代码

import time
import tensorflow as tf

begin = time.time()

with tf.device('/cpu:0'):
    rand_t = tf.random_uniform([50,50],0,10,dtype=tf.float32,seed=0)
    a = tf.Variable(rand_t)
    b = tf.Variable(rand_t)
    c = tf.matmul(a,b)
    init = tf.global_variables_initializer()

sess = tf.Session()
sess.run(init)
print(sess.run(c))

end = time.time()
print(end-begin,'s')
2019-02-21 22:09:21.253923: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2019-02-21 22:09:21.603302: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\gpu\gpu_device.cc:1030] Found device 0 with properties: 
name: GeForce GTX 960M major: 5 minor: 0 memoryClockRate(GHz): 1.176
pciBusID: 0000:01:00.0
totalMemory: 2.00GiB freeMemory: 1.65GiB
2019-02-21 22:09:21.604014: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\gpu\gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 960M, pci bus id: 0000:01:00.0, compute capability: 5.0)
[[1405.2432  1441.7412  1364.38    ... 1480.2251  1279.0061  1620.0938 ]
 [1232.6588  1344.4459  1169.7092  ... 1205.1285  1040.5566  1421.9672 ]
 [1209.3167  1180.3208  1158.1396  ... 1200.0343  1014.03217 1222.5106 ]
 ...
 [1298.9648  1262.9236  1205.6917  ... 1396.479   1090.7252  1437.2411 ]
 [1118.2474  1209.0155  1077.7229  ... 1180.7023  1076.4692  1139.7421 ]
 [1200.8864  1297.2268  1260.0101  ... 1289.4296  1165.2449  1433.4185 ]]
0.5449635982513428 s

猜你喜欢

转载自blog.csdn.net/lly1122334/article/details/87869545