[AI-RESEARCH][02]Tensor Flow GPU運行環境測試
以下是Tensor Flow GPU運行環境測試碼
把該程式碼張貼到spyder運行即可
運行環境:
系統:windows 10 1903
CPU:7900X
GPU:2080 Ti
RAM:64GB
SSD:MX500 1TB
CUDA:cuda_10.0.130_411.31_win10
cuDNN:cudnn-forCUDA10.0-windows10-x64-v7.3.1.20
import tensorflow as tf
import time
import matplotlib.pyplot as plt
def performanceTest(device_name,size):
with tf.device(device_name):
W = tf.random.normal([size, size], name='W')
X = tf.random.normal([size, size], name='X')
mul = tf.matmul(W, X, name='mul')
sum_result = tf.reduce_sum(mul, name='sum')
startTime = time.time()
tfconfig = tf.ConfigProto(log_device_placement=True)
with tf.Session(config=tfconfig) as sess:
result = sess.run(sum_result)
takeTimes = time.time() - startTime
print(device_name, "matrix size:",size,"x",size, " time:",takeTimes)
return takeTimes
gpu_set = [];cpu_set = [];i_set = []
for i in range(0, 10000, 500):
g = performanceTest("/gpu:0",i)
c = performanceTest("/cpu:0",i)
gpu_set.append(g)
cpu_set.append(c)
i_set.append(i)
#%matplotlib inline
fig = plt.gcf()
fig.set_size_inches(6,4)
plt.plot(i_set, gpu_set, label='gpu')
plt.plot(i_set, cpu_set, label='cpu')
plt.title('CPU&GPU time-consuming')
plt.ylabel('Time')
plt.xlabel('matrix size')
plt.legend()
運行結果如下
這個程式碼主要在進行矩陣運算
我們發現隨著矩陣的大小增大,
所需的運行時間會增大
可以發現CPU時間增加的比例遠比GPU來得高
在這邊就可以體現GPU多核心運行的優勢所在

在這邊可以由“工作管理員”來看出GPU運行的蹤跡

留言
張貼留言
歡迎留下您的心靈足跡👍