Pip install error with PyCUDA

Updated post here: https://codeyarns.github.io/tech/2015-07-31-pip-install-error-with-pycuda.html

How to install PyCUDA

PyCUDA enables a Python program to pass data to and call CUDA kernels from a Python program. Getting it to install and work correctly on Ubuntu took a bit of work.

  • I tried installing the nvidia-343 drivers for Ubuntu. But, it turns out that only 340.x or earlier drivers support the GTS 250 I use for display (not for compute). So, I reverted back and installed nvidia-331 drivers. Note that the nvidia-331-updates driver will not work with CUDA either. Do not ask me why! 🙂

  • Installing the CUDA toolkit was easier, just install nvidia-cuda-toolkit package.

  • PyCUDA is available in Ubuntu as a python-pycuda package. But, that is the very old 2013.1.1 version. Instead I installed it from the Python Package Index (PyPI):

$ sudo pip install pycuda

The install script kept complaining about the absence of a configure.py script, but it seemed to end with success.

Tried with: PyCUDA 2014.1, CUDA 5.5, NVIDIA 331 driver and Ubuntu 14.04

CUDA error: gcc 4.9 and up are not supported

Problem

I have seen this error while compiling a CUDA program. I have also seen this error on running a PyCUDA program:

#error -- unsupported GNU version! gcc 4.9 and up are not supported!

Solution

I was using GCC 4.9.2 on this system, but I did not know why CUDA had a problem with these newer versions. Turns out that it does not have any problem, this warning just needs to be choked in the relevant header file.

For CUDA, this header file was /usr/local/cuda/include/host_config.h. For PyCUDA, this header file was /usr/include/host_config.h.

This diff should fix this error:

--- host_config.h.bkp   2015-02-24 09:53:55.232620612 +0800
+++ host_config.h   2015-02-24 10:24:01.428654521 +0800
@@ -77,11 +77,11 @@

 #if defined(__GNUC__)

-#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 8)
+#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 9)

 #error -- unsupported GNU version! gcc 4.9 and up are not supported!

-#endif /* __GNUC__> 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 8) */
+#endif /* __GNUC__> 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 9) */

 #endif /* __GNUC__ */

Tried with: PyCUDA 2014.1, CUDA 6.5, GCC 4.9.2 and Ubuntu 14.04

PyCUDA error: cannot import name intern

Problem

Running a simple PyCUDA program, I got this error:

$ python hello_cuda.py 
Traceback (most recent call last):
  File "hello_cuda.py", line 5, in <module>
    from pycuda.compiler import SourceModule
  File "/usr/local/lib/python2.7/dist-packages/pycuda/compiler.py", line 1, in <module>
    from pytools import memoize
  File "/usr/local/lib/python2.7/dist-packages/pytools/__init__.py", line 5, in <module>
    from six.moves import range, zip, intern, input
ImportError: cannot import name intern

Solution

The error had nothing to do with PyCUDA or CUDA, but with the six module which helps with compatibility between Python2 and Python3 code. It needed an upgrade and it worked after this:

$ sudo pip install six --upgrade

Tried with: Six 1.9.0, PyCUDA 2014.1, CUDA 5.5 and Ubuntu 14.04