一 命令行安裝
比較簡單的方法是通過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/