Pip install error with PyCUDA

Problem

  • I tried to install PyCUDA using pip:
$ sudo pip install pycuda
  • The installation tries to compile a few C++ files and it failed on the very first file with this error:
In file included from src/cpp/cuda.cpp:1:0:
src/cpp/cuda.hpp:14:18: fatal error: cuda.h: No such file or directory
#include <cuda.h>
                ^
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

Investigation

  • This error was strange because I had set CUDA_ROOT and had added the bin path of CUDA installation to PATH environment variable. So, the installer should have found cuda.h which I could see was present in $CUDA_ROOT/include

  • To see what was happening, I tried the same command with verbosity:

$ sudo pip -vvv install pycuda
  • Now I could see that it was failing to find nvcc.

  • On downloading the source code of PyCUDA and checking setup.py, I saw that the check for nvcc was used to figure out the CUDA_ROOT and CUDA_INC_DIR.

  • The reason nvcc was not visible was that CUDA_ROOT was set for my user, but this PATH is not visible when a command is run under sudo, as described here. The solution was to make the CUDA bin path visible to sudo.

Solution

To make the $CUDA_ROOT/bin available in PATH for sudo, we can follow the steps described here. For example, on my system with CUDA 7.0 I followed these steps:

  • Created a new file /etc/profile.d/cuda.sh and added this line:
export PATH=/usr/local/cuda-7.0/bin:$PATH
  • Opened root shell without resetting PATH and ran the pip installation:
$ sudo su -
$ pip install pycuda

This worked and PyCUDA was installed successfully! 🙂

Tried with: PyCUDA 2015.1.2, CUDA 7.0 and Ubuntu 14.04

Advertisements

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

PyCUDA error: cuInit failed

Problem

After installing PyCUDA, I tried to run a sample PyCUDA program and got this error:

$ python hello_cuda.py
Traceback (most recent call last):
  File "hello_cuda.py", line 1, in <module>
    import pycuda.autoinit
  File "/usr/local/lib/python2.7/dist-packages/pycuda/autoinit.py", line 4, in <module>
    cuda.init()
pycuda._driver.Error: cuInit failed: unknown

Solution

The error refers to line 4 in /usr/local/lib/python2.7/dist-packages/pycuda/autoinit.py. The lines relevant to that error in the file are:

import pycuda.driver as cuda
cuda.init()

So, CUDA initialization is failing for some reason. After a bit of Googling, I found that this means that the necessary (CUDA? NVIDIA?) modules were not being loaded into the kernel. NVIDIA provides a modprobe package that can auto-load modules into the kernel as needed.

I installed this package:

$ sudo apt-get install nvidia-modprobe

CUDA was able to initialized immediately after this package was installed, no reboot was needed.

Tried with: PyCUDA 2014.1, CUDA 5.5, NVIDIA 331 drivers, GTX Titan and Ubuntu 14.04