Search code examples
pythoncudapycuda

pycuda.driver module not found


I have installed python 3.7.2 along with the following libraries: jupyter, pandas, numpy, pytools and pycuda. I'm working with Visual Studio Code. I'm trying to run the standard pyCuda example:

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

When I run this code, VSCode says that

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

However, from the below figure, it seems that the module exists

enter image description here

Any suggestion on how solving the problem?

EDIT: SIMPLIFIED TEST CASE

If I run

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

print("test")

then test is emitted in the console. If I run

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

# Initialize CUDA
cuda.init()

print("test")

nothing is emitted in the console and VSCode emits the following problem

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

Solution

  • The problem was an installation issue.

    I have just uninstalled the version of pycuda that I previously installed via

    python pip install pycuda
    

    and downloaded a precompiled binary from Christoph Golke page, while taking care of compatibility. For me, the correct file has been pycuda-2018.1.1+cuda100-cp37-cp37m-win_amd64 for python 3.7.2 64bits.

    Now, everything works correctly.