2

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
Vitality
  • 20,705
  • 4
  • 108
  • 146
  • I don't see any python runtime errors in this question, just IDE limitations. Does the code actually run? And if it does not, what python runtime error is emitted? – talonmies Jan 17 '19 at 19:23
  • @talonmies Thank you for your interest in the question. I have edited my question with a simpler test case. If i try to run modules from the `pycuda.driver` it rises issues. – Vitality Jan 18 '19 at 08:04
  • If I operate from the command prompt `>>>` and run `import pycuda.driver as cuda` and `cuda.init()`, then `python` just exits. – Vitality Jan 18 '19 at 08:23

2 Answers2

1

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.

talonmies
  • 70,661
  • 34
  • 192
  • 269
Vitality
  • 20,705
  • 4
  • 108
  • 146
0

If your code runs without AttributeErrors then it is likely that Visual Studio is giving false positives. If so, ignore them. This happens because doing static analysis on dynamic code doesn't always do the right thing.

BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • 1
    What does Python say when you try to run it? What is the stack trace? – BoarGules Jan 17 '19 at 16:03
  • I have edited the question with a simpler test case. Basically, if I try to run modules from the `pycuda.driver`, `VSCode` stops the execution and raises issues. – Vitality Jan 18 '19 at 08:05
  • It's very difficult respond to "VScode raises issues". Try running your program from the command line and report what the Python interpreter says the problem is. – BoarGules Jan 18 '19 at 08:08
  • 1
    If I operate from the command prompt `>>>` and run `import pycuda.driver as cuda` and `cuda.init()`, then `python` just exits. – Vitality Jan 18 '19 at 08:24
  • And if you run your program from a console command prompt? In other words, `python.exe myprogram.py` – BoarGules Jan 18 '19 at 08:32