用python执行以下例子的时候:(测试theano调用gpu执行程序)http://deeplearning.net/software/theano/tutorial/using_gpu.html
from theano import function, config, shared, tensor
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([], tensor.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, tensor.Elemwise) and
('Gpu' not in type(x.op).__name__)
for x in f.maker.fgraph.toposort()]):
print('Used the cpu')
else:
print('Used the gpu')
执行代码:
THEANO_FLAGS=device=cuda0 python gpu_tutorial1.py
结果报错:
ImportError : No module named pygpu
**
解决方法:(另外除了下面方法可以执行anaconda安装:conda install pygpu)
ps:我这里环境没有使用anaconda,所以尝试用源码安装
前提:以下的环境你安装好了
cmake >= 3.0 (cmake).
python
Cython >= 0.25
如果需要安装cmake请移步:
https://blog.csdn.net/l1028386804/article/details/50779092
** 解决步骤
1:下载源码
git clone https://github.com/Theano/libgpuarray.git
2:进入libgpuarray目录
cd
mkdir Build
cd Build
cmake .. -DCMAKE_BUILD_TYPE=Release
make
make install
cd ..
安装 pygpu:
# This must be done after libgpuarray is installed as per instructions above.
>python setup.py build
>python setup.py install
3:执行链接到系统环境中:
python setup.py build_ext -L MYPREFIX/lib−I M Y P R E F I X / l i b − I MY_PREFIX/include
注意这里的My_PREFIX是你安装的libgpuarray库目录。默认是在/usr/local下
sudo ldconfig
4:最后我按照官网测试例子:(test device是你环境下的gpu名称)
DEVICE=”” python -c “import pygpu;pygpu.test()”
发现还是不行,找不到libgpuarray
解决办法:
4.1:编辑环境变量文件
vim ~/.bashrc
4.2:添加内容如下:
export LIBGPUARRAY_PATH=/usr/local
export PATH= LIBGPUARRAYPATH/lib: L I B G P U A R R A Y P A T H / l i b : PATH
4.3:生效:
source ~/.bashrc
参考官网信息
http://deeplearning.net/software/libgpuarray/installation.html