测试Theano使用GPU并行计算,以验证环境搭建是否成功

一、前提

安装好了cuda和theano

二、测试

(1)创建一个test.py文件
添加如下内容

from theano import function, config, shared, sandbox
import theano.tensor as T
import numpy
import time

vlen = 10 * 30 * 768  # 10 x #cores x # threads per core
iters = 1000

rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
f = function([], T.exp(x))
print(f.maker.fgraph.toposort())
t0 = time.time()
for i in range(iters):
    r = f()
t1 = time.time()
print("Looping %d times took %f seconds" % (iters, t1 - t0))
print("Result is %s" % (r,))
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
    print('Used the cpu')
else:
    print('Used the gpu')

保存。
(2)创建.theanorc文件
文件中添加如下内容

[global]
floatX=float32
device=cpu

终端输入命令

sudo vi ~/.theanorc

这里先测试cpu,测试完成后,修改成device=gpu,对比速度.
(3)执行文件
终端输入

python test.py

(4)测试结果
1.cpu
这里写图片描述
会看到cpu测试用了3秒多
2.gpu
这里写图片描述
相比cpu,gpu、使用了仅仅0.8秒
3.若无问题,即环境搭建成功

三、测试失败的解决办法

提示:
ERROR (theano.sandbox.cuda): Failed to compile cuda_ndarray.cu: libcublas.so.7.5: cannot open shared object file: No such file or directory
WARNING (theano.sandbox.cuda): CUDA is installed, but device gpu is not available (error: cuda unavilable) TensorType(float32, vector)>)]
Looping 1000 times took 3.337314 seconds
Result is [ 1.23178029 1.61879337 1.52278066 …, 2.20771813 2.29967761
1.62323284]

解决办法:
(a)检查cuda的环境变量是否配好
(b)终端输入命令

sudo ldconfig /usr/local/cuda/lib64
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值