tensorflow - 为什么不同数据类型的 GPU 性能变化如此之大
问题描述
用一段代码来测试CPU和GPU的性能差异,我发现性能差异很有趣。
float64 比 float16 好,但 gpu 不如 CPU。
这里的任何人都可以分享一些关于为什么的见解吗?
我的硬件是 Xeon E5-1650 + Quadro K620。
这是我的测试代码。
from __future__ import print_function
import matplotlib
import matplotlib.pyplot as plt
import tensorflow as tf
import time
def get_times(maximum_time):
device_times = {
"/gpu:0":[],
"/cpu:0":[]
}
matrix_sizes = range(500,50000,50)
for size in matrix_sizes:
for device_name in device_times.keys():
print("####### Calculating on the " + device_name + " #######")
shape = (size,size)
data_type = tf.float16
with tf.device(device_name):
r1 = tf.random_uniform(shape=shape, minval=0, maxval=1, dtype=data_type)
r2 = tf.random_uniform(shape=shape, minval=0, maxval=1, dtype=data_type)
dot_operation = tf.matmul(r2, r1)
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as session:
start_time = time.time()
result = session.run(dot_operation)
time_taken = time.time() - start_time
print(result)
device_times[device_name].append(time_taken)
print(device_times)
if time_taken > maximum_time:
return device_times, matrix_sizes
device_times, matrix_sizes = get_times(1.5)
gpu_times = device_times["/gpu:0"]
cpu_times = device_times["/cpu:0"]
plt.plot(matrix_sizes[:len(gpu_times)], gpu_times, 'o-')
plt.plot(matrix_sizes[:len(cpu_times)], cpu_times, 'o-')
plt.ylabel('Time')
plt.xlabel('Matrix size')
plt.show()
解决方案
推荐阅读
- java - 在Java中读取括号内的内容?
- sql - 如何让最后一行在 Oracle SQL 中显示聚合函数
- r - 按观察出现的时间过滤
- java - 测试覆盖仅调用另一个方法的方法的最佳方法是什么?
- python - 使用循环从更大的列表中保存新列表
- python - 使用用户输入来引用python中另一个文件中的函数
- javascript - JAVASCRIPT:ml5.js:后端 webgl 的初始化失败 t.initializeBackend @ tf-core.esm.js:17
- html - 更改窗口(浏览器)大小时如何防止div移动?
- python - 在打开文件函数的长字符串切片中切片
- hyperledger-fabric - Hyperledger Fabric 2.X 中的开发模式是否仍然存在问题?