pycuda 按理说直接pip安装就可,但是如果你遇到问题,要么从源码编译安装,windows的话,可以从:
https://www.lfd.uci.edu/~gohlke/pythonlibs/#pycuda
这里安装,选择根据Python版本和CUDA版本选择就可
验证代码
我从这个博客:
pycuda测试、对比及分析 https://blog.csdn.net/u013468614/article/details/90137814
找个代码:
import pycuda.autoinit
import pycuda.driver as drv
import numpy as np
import time
from pycuda.compiler import SourceModule
mod = SourceModule('''
__global__ void Text_GPU(float *A , float *B, float *K, size_t N){
int bid = blockIdx.x;
int tid = threadIdx.x;
__shared__ float s_data[2];
s_data[tid] = (A[bid*2 + tid] - B[bid*2 + tid]);
__syncthreads();
if(tid == 0)
{
float sum_d = 0.0;
for(int i=0;i<N;i++)
{
sum_d += (s_data[i]*s_data[i]);
}
K[bid] = exp(-sum_d);
}
}
''')
multiply_them = mod.get_function("Text_GPU")
tic = time.time()
A = np.random.random((1000,20)).astype(np.float32)
B = np.random.random((1000,20)).astype(np.float32)
K = np.zeros((1000,), dtype=np.float32)
N = 20
N = np.int32(N)
multiply_them(
drv.In(A), drv.In(B), drv.InOut(K), N,
block=(20,1,1), grid=(1000,1))
toc = time.time()
print("time cost is:"+str(toc-tic))
直接运行一下,如果返回是这样的:
time cost is:0.009005308151245117
xxx.py:6: UserWarning: The CUDA compiler succeeded, but said the following:
kernel.cu
mod = SourceModule('''
这是输出,有个warning,不过无伤大雅,说明安装OK
安装问题
如果遇到这个问题:
pycuda: nvcc compitalation of kernel.cu failed
stackoverflow 的大佬说
I edited compiler.py to output the stdout of the command call. I got “nvcc fatal : Cannot find compiler ‘cl.exe’ in PATH”.
大佬debug, 发现实际的问题是
nvcc fatal : Cannot find compiler 'cl.exe' in PATH
他给出的方式是在程序最前方添加这几行代码:
import os
if os.system("cl.exe"):
os.environ['PATH'] += ';'+r"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\bin\HostX64\x64"
if os.system("cl.exe"):
raise RuntimeError("cl.exe still not found, path probably incorrect")
这一步操作就是将 cl.exe
的路径添加到环境变量中,你要自己把那个路径改成你的
如果你没找到 cl.exe
的位置,或者不想临时添加环境变量,那么可以这样操作(直接添加到windows环境变量中):
https://blog.csdn.net/HaoZiHuang/article/details/125795675
有参考自:
https://stackoverflow.com/questions/42286339/pycuda-nvcc-compitalation-of-kernel-cu-failed
https://blog.csdn.net/weixin_41521681/article/details/105899660