Installing CUDA 5.0 and Theano on Ubuntu 12.04 Precise
Theano is a very interesting Python library developed mainly for deep learning, which can run calculations on some NVIDIA GPUs by using the CUDA library. Setting up Theano to use the GPU can be a little tricky and take a bit of work.
Install the pre-reqs
sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev
Next, create a symlink to libglut, which will allow you to install the CUDA samples as described on Utkarsh Jaiswal’s blog
sudo ln -s /usr/lib/x86_64-linux-gnu/libglut.so.3 /usr/lib/libglut.so
Install CUDA
Download CUDA from the NVIDIA site and then install it:
sudo apt-get remove --purge nvidia*
chmod +x cuda_5.0.35_linux_64_ubuntu11.10-1.run
sudo service lightdm stop
sudo ./cuda_5.0.35_linux_64_ubuntu11.10-1.run
Install Theano
Get the latest released version of Theano:
sudo apt-get install python-dev libopenblas-dev liblapack-dev gfortran
sudo pip install --upgrade Theano
Create a ~/.theanorc file to enable the GPU:
[global]
floatX = float32
device = gpu
Test it out
Now run the sample program under “Testing Theano with GPU” in the Theano tutorial. It will hopefully tell you that it used your GPU.
A good benchmark to test out the speed of your setup is to run /usr/local/lib/python2.7/dist-packages/theano/misc/check_blas.py
Credits
Thanks to the Theano developers for providing this awesome library and to Andrew Ng, Samy Bengio, and the other Googlers who have been taking their time to teach the rest of us more machine learning concepts.
To have a faster implementation of blas, just install the atlas and atlas devel package. Numpy will start using it. So theano too.
Theano’s moved over to github, so your pip line should now read:
pip install –upgrade git+git://github.com/Theano/Theano.git
There is a problem if you don’t have X running on your system. Normally X loads the driver and creates the necessary devices. When you don’t run it you have to run this script as root at boot time (from the cuda docs):
#!/bin/bash
/sbin/modprobe nvidia
if [ “$?” -eq 0 ]; then
# Count the number of NVIDIA controllers found.
N3D=`/sbin/lspci | grep -i NVIDIA | grep “3D controller” | wc -l`
NVGA=`/sbin/lspci | grep -i NVIDIA | grep “VGA compatible controller” | wc -l`
N=`expr $N3D + $NVGA – 1`
for i in `seq 0 $N`; do
mknod -m 666 /dev/nvidia$i c 195 $i;
done
mknod -m 666 /dev/nvidiactl c 195 255
else
exit 1
fi