Installing PyCUDA on Snow Leopard

Tue Dec 20 2011

PREAMBLE: If you're running Mac OS 10.5 or 10.7, this little walk-through is not for you! Get outta here. Git! If you want a nice tutorial on how to install for 10.5, go to the official tutorial for how to install 10.5.  If you want 10.7, I'll have a new tutorial up when I install 10.7 on my work machine.

If you are running Mac OS 10.6, and want some of that gradient optimization and sparse matrix multiplication goodness that PyCUDA offers that (as far as I can tell) ain't in PyOpenCL, you're in for a fun time. Well, I'm being sarcastic, and not you specifically, more like me, I did most of the hair pulling, and I'd like to save you some hair.

Also, this used to be published at my blog, which was at http://alexspixels.com/, but now I'm trying out this whole "living document" thing at substance.io.

Let's go through this step by step.

Python Install

You should have Python on your system and NumPy as well. I'd highly recommend downloading the Enthought Python Distribution (EPD), and forgetting about the trials of getting an optimized NumPy installation working. Just don't worry about it, download the EPD, install it, and come back when you're done. 

CUDA Toolkits

We need to snag all of the drivers and toolkits that NVidia provides that PyCUDA depends upon. You can find them here, at this linky link. Scroll down to the bottom of that page, under "MAC OS X", and download everything that has a "download" link next to it. Go through all the installations of everything you downloaded. Okay. Now... 

Update your shell

What's that mean? Well, launch Terminal.app, and you'll need to edit your bash profile. You can do this by typing

open ~/.profile  

If you're running a different shell, I'll assume you know where your profile is, and you can open it up on your own. Your profile might look like this (or it might now, who knows):

We're going to have to update this to tell the computer where all the fancy things we installed now live. Add these three lines: 

export PATH="/usr/local/cuda/bin:${PATH}"
export DYLD_LIBRARY_PATH=/usr/local/cuda/lib:$HOME/pool/lib:${DYLD_LIBRARY_PATH}
export PYTHONPATH=$HOME/lib/python:$PYTHONPATH

So, your bash profile should now look like something like this:

Alright, that should do it. We're ready to start rolling. 

Get PyCUDA

 Open up Terminal.app, and issue these commands:

git clone http://git.tiker.net/trees/pycuda.git
python configure.py

What do those lines do? The first one grabs all the code we want to install. The last line writes up some files that'll help us install everything.

Configure PyCUDA

Okay, that primes the pump, but we're not quite ready to install PyCUDA yet. In terminal, run:

open siteconf.py

That'll open up your favorite text editor. On my system, I use TextMate, and this is what I see:

What's there by default is wrong. We need to add a few lines for our system (otherwise, PyCUDA will complain bitterly that it's not compiling on OS 10.5, and then it'll throw up on itself and crap out). So, we add these lines: 

# if on Snow Leopard, include these lines:
CXXFLAGS = ["-arch", "x86_64", "-arch", "i386"]
LDFLAGS = ["-arch", "x86_64", "-arch", "i386"]
CXXFLAGS.extend(['-isysroot', '/Developer/SDKs/MacOSX10.6.sdk'])
LDFLAGS.extend(['-isysroot', '/Developer/SDKs/MacOSX10.6.sdk'])

A sidenote, regarding Homebrew

If you've installed Python 2.7 using Homebrew, then your flags should be something slightly different. Check it;

CXXFLAGS = ["-arch", "x86_64", "-arch", "i386", "-mmacosx-version-min=10.6", 
"-I/usr/local/lib/python2.7/site-packages/numpy/core/include"]
LDFLAGS = ["-arch", "x86_64", "-arch", "i386", "-mmacosx-version-min=10.6"]

Thanks to Uznick for the tip!

Alrighty, now we've got to do one last thing. Even though we're grabbing PyCUDA from the freshest source we can find (the active git repository at tiker.net), we still need to do one last update. In your terminal (you didn't close it, right?), run:

git submodule init
git submodule update

In all likelihood, even if you've used git before, you've never used the "submodule" command. I hadn't either. There's some little helper packages within PyCUDA that need some attention. 

Okay, a sidenote: Lewis Anderson has informed me that on Python 2.7, he's found it necessary to force the system to compile all the C and C++ code with gcc and not clang. You can do that quite simply, by typing in your terminal:

export CC=/usr/bin/gcc-4.2
export CXX=/usr/bin/g++-4.2

Alright, the Home Stretch

Okay, in your terminal, type:

make

Simple, yeah? If there are no errors, you're going to be waiting for quite some time. I believe it's traditional to say "grab a soda," or something like that. I like orange juice, so that's what I drank. If there are any errors, please do see my little "error disclaimer" below.

When all that goodness of the make command has finished, with one set of keystrokes, enter:

sudo make install

KABLAMO

You've now got PyCUDA installed on your system. Convince yourself by launching your Python interpreter (I like iPython best), and import PyCUDA.

ipython
import pycuda

Optional Final Step

Do a jig!

ERROR DISCLAIMER:

If this doesn't work for you for some reason, feel free to email me at  alex (dot) bw (at) gmail (dot) com. I'd be happy to answer questions. Getting this library installed is kind of a hassle, so anything I can do to make the world a slightly less frustrating place, well, I'd love to do it.