1. tensorflow版本和CUDA版本匹配
dlerror: cusparse64_11.dll not found. Cannot detetct
遇到如上问题,是因为tensorflow-gpu版本过高,降低tensorflow版本即可
如果是2.0以上的tensorflow,按下面列表安装(2021年3月更新官网链接)
TF | CUDA | cudnn |
---|---|---|
2.0 | 10.0 | 7.6 |
2.1 | 10.1 | 7.6 |
2.2 | 10.1 | 7.6 |
2.3 | 10.1 | 7.6 |
2.4 | 11.0 | 8.0 |
可以看到tensorflow-gpu 2.4 对应了11.0的CUDA,所以降低tensorflow-gpu版本即可
下面命令可以显示可安装版本,如果出错,升级pip。
pip install tensorflow-gpu==
降低tensorflow-gpu版本到对应自己的cuda即可使用gpu。
2. GPU测试
2.1 测试所有设备
from tensorflow.python.client import device_lib
import tensorflow as tf
print(device_lib.list_local_devices())
print(tf.test.is_built_with_cuda())
测试结果:
2.2 测试gpu效果
import timeit
import matplotlib.pyplot as plt
import tensorflow as tf
'''
以矩阵A[10,n]和矩阵B[n,10]的乘法运算(分别在cpu和gpu上运行)来测试,
'''
def cpu_gpu_compare(n):
with tf.device('/cpu:0'): # 指定操作用cpu计算
cpu_a = tf.random.normal([10, n]) # 生成符合高斯分布的随机数矩阵,通过改变n大小,增减计算量
cpu_b = tf.random.normal([n, 10])
print(cpu_a.device, cpu_b.device)
with tf.device('/gpu:0'):
gpu_a = tf.random.normal([10, n])
gpu_b = tf.random.normal([n, 10])
print(gpu_a.device, gpu_b.device)
def cpu_run():
with tf.device('/cpu:0'): # 矩阵乘法,此操作采用cpu计算
c = tf.matmul(cpu_a, cpu_b)
return c
def gpu_run():
with tf.device('/gpu:0'): # 矩阵乘法,此操作采用gpu计算
c = tf.matmul(gpu_a, gpu_b)
return c
# 第一次计算需要热身,避免将初始化时间计算在内
cpu_time = timeit.timeit(cpu_run, number=10)
gpu_time = timeit.timeit(gpu_run, number=10)
print('warmup:', cpu_time, gpu_time)
# 正式计算10次,取平均值
cpu_time = timeit.timeit(cpu_run, number=10)
gpu_time = timeit.timeit(gpu_run, number=10)
print('run_time:', cpu_time, gpu_time)
return cpu_time, gpu_time
# n_list1 = range(1, 2000, 5)
# n_list2 = range(2001, 10000, 100)
n_list3 = range(100000, 1000000, 50000)
# n_list = list(n_list1) + list(n_list2)
n_list = n_list3
time_cpu = []
time_gpu = []
for n in n_list:
t = cpu_gpu_compare(n)
time_cpu.append(t[0])
time_gpu.append(t[1])
plt.plot(n_list, time_cpu, color='red', label='cpu')
plt.plot(n_list, time_gpu, color='green', linewidth=1.0, linestyle='--', label='gpu')
plt.ylabel('耗时', fontproperties='SimHei', fontsize=20)
plt.xlabel('计算量', fontproperties='SimHei', fontsize=20)
plt.title('cpu和gpu计算力比较', fontproperties='SimHei', fontsize=30)
plt.legend(loc='upper right')
plt.show()
测试结果: