找不到pycuda.driver模块

时间:2019-01-17 15:29:49

标签: python cuda pycuda

我已经安装了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

但是,从下图可以看出,该模块存在

enter image description here

关于如何解决问题的任何建议?

编辑:简化的测试案例

如果我跑步

# --- 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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

问题是安装问题。

我刚刚卸载了以前通过pycuda安装的版本

python pip install pycuda

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

现在,一切正常。