pycuda.driver 未找到模块

pycuda.driver module not found

我已经安装了 python 3.7.2 以及以下库:jupyterpandasnumpypytoolspycuda。我正在与 Visual Studio Code 合作。我正在尝试 运行 标准 pyCuda 示例:

# --- PyCuda initialization
import pycuda.driver as cuda
import pycuda.autoinit
from pycuda.compiler import SourceModule

# --- Create a 4x4 double precision array of random numbers
import numpy
a = numpy.random.randn(4,4)

# --- Demote array to single precision
a = a.astype(numpy.float32)

# --- Allocate GPU device memory
a_gpu = cuda.mem_alloc(a.nbytes)

# --- Memcopy from host to device
cuda.memcpy_htod(a_gpu, a)

# --- Define a device function that doubles the input device array
mod = SourceModule("""
  __global__ void doublify(float *a)
  {
    int idx = threadIdx.x + threadIdx.y*4;
    a[idx] *= 2;
  }
  """)

# --- Define a reference to the __global__ function and call it
func = mod.get_function("doublify")
func(a_gpu, block=(4,4,1))

# --- Copy results from device to host
a_doubled = numpy.empty_like(a)
cuda.memcpy_dtoh(a_doubled, a_gpu)

print(a_doubled)
print(a)

当我 运行 这段代码时,VSCode 表示

Module 'pycuda.driver' has no 'mem_alloc' member
Module 'pycuda.driver' has no 'memcpy_htod' member
Module 'pycuda.driver' has no 'memcpy_dtoh' member

但是从下图来看,好像是存在这个模块的

有什么解决问题的建议吗?

编辑:简化测试用例

如果我运行

# --- PyCuda initialization
import pycuda.driver as cuda

print("test")

然后 test 在控制台中发出。如果我运行

# --- PyCuda initialization
import pycuda.driver as cuda

# Initialize CUDA
cuda.init()

print("test")

控制台中未发出任何内容,VSCode 发出以下 problem

Module 'pycuda.driver' has no 'init' member

如果您的代码在没有 AttributeError 的情况下运行,那么 Visual Studio 可能会给出误报。如果是这样,请忽略它们。发生这种情况是因为对动态代码进行静态分析并不总能做出正确的事情。

问题是安装问题。

我刚刚卸载了之前通过

安装的pycuda版本
python pip install pycuda

并从 Christoph Golke 页面下载了预编译的二进制文件,同时注意兼容性。对我来说,正确的文件是 pycuda-2018.1.1+cuda100-cp37-cp37m-win_amd64 for python 3.7.2 64bits

现在,一切正常。