PyCuda配置(二)下载PyCuda
一 命令行安装
比较简单的方法是通过apt-get下载,调用如下命令:
sudo apt-get install python-pycuda
这样可以安装python2对应版本的pycuda。
安装后用以下代码验证一下即可。
#!/usr/bin/env python
import pycuda.driver as cuda
import pycuda.autoinit
from pycuda.compiler import SourceModule
import numpy as np
from datetime import datetime
a = np.random.randn(4,4)
a = a.astype(np.float32)
mod = SourceModule("""
__global__ void doublify(float *a)
{
int idx = threadIdx.x + threadIdx.y*4;
a[idx] *= 2;
}
""")
startTime = datetime.now()
# CUDA method
a_gpu = cuda.mem_alloc(a.nbytes)
cuda.memcpy_htod(a_gpu, a)
a_doubled = np.empty_like(a)
cuda.memcpy_dtoh(a_doubled, a_gpu)
print "Cuda time is: ", datetime.now()-startTime
print a_doubled
startTime = datetime.now()
# np method
a_doubled_2 = a * 2
print "np method time is: ", datetime.now()-startTime
print a_doubled_2
二 下载源代码编译安装
1 下载pycuda
2 命令行进行如下指令
tar xzvf pycuda-VERSION.tar.gz
cd pycuda-VERSION
python configure.py --cuda-root=/where/ever/you/installed/cuda
或
python3 configure.py --cuda-root=/where/ever/you/installed/cuda
注意如果是安装python3的话,一般上述命令中的python要改为python3
问题
1 是否需要第一步中的安装CUDA? -> 用一台未配置的电脑试试
2 例子中的加速函数参数等应当如何配置? -> 继续学习pycuda tutorial
Credits:
https://blog.csdn.net/JohnJim0/article/details/100585885
https://blog.csdn.net/wj164/article/details/45648517
https://wiki.tiker.net/PyCuda/Installation/Linux/