(做到这一步发现很简单)
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 是否需要第一步中的安装CUDA? -> 用一台未配置的电脑试试
2 例子中的加速函数参数等应当如何配置? -> 继续学习pycuda tutorial
原文:https://www.cnblogs.com/guesswhoiscoming/p/13619825.html