Installing PyCUDA on Mac OS X
Contents
You're looking at the generic instructions for installing PyCUDA on Mac, involving the system Python. If you'd like to use a different Python version (or are running into trouble with these insructions), check these subpages:
The process of installing PyCUDA on Mac OS X is very similar to the Linux one. In fact, you can use the shell (Terminal) for all operations. Below are the components used to build/use PyCUDA:
- Mac OS 10.5.7 (previous versions should work fine.)
Nvidia's CUDA toolkit. CUDA 2.2 is the latest one to date and it works fine in OS X.a
- A C++ compiler, preferably a Version 4.x gcc.
- MacOSX10.4u.sdk from Apple Developer Tools package.
- Boost 1.39.
NumPy 1.2.1.
MacPython 2.6.2. (Python 2.5.1 comes pre-installed on Mac OS X should work just fine.)
(You can install Xcode and not bother installing the C++ compiler and Mac OS SDK separately. However, Xcode will take 2+ GB of your drive.)
Pre-install Tips
Snow Leopard comes installed with Python 2.6.1. It may be in your best interest to stick with this version for now until someone else can show us how to run a newly installed version of MacPython in 32-bit mode. Read below why we want to run python in 32-bit, but basically it has to do with the fact that CUDA 2.2 currently only supports 32-bit mode, and hence PyCUDA as well.
Stick with Boost 1.39 instead of the latest (as of Jan 23, 2010 is 1.41). Follow the directions to install 1.39 at BoostInstallationHowto and pay attention to the Mac notes. The issue with running either the earlier or later versions of Boost are the build options you can specify. Again it goes back to the fact that we want to build 32-bit versions of all libraries we use on Snow Leopard so it is CUDA compatible. I also vaguely recall seeing numerous Boost compile warnings when using 1.41 and building PyCUDA. PyCUDA is probably preferring a particular version of Boost with given signatures, so 1.39 worked best for now.
Step 1: Install Boost
You may already have a working copy of the Boost C++ libraries. If so, make sure that it's version 1.35.0 or newer. If not, no problem, please follow the simple BoostInstallationHowto.
- If you are installing boost in /usr/local instead of $HOME/pool, you will need to run the commands as 'sudo', otherwise you will get permission denied errors.
- LD_LIBRARY_PATH in Mac OS X is called DYLD_LIBRARY_PATH.
- Make sure your bash shell startup file '.bash_profile' has an entry for the library path. Something like:
export DYLD_LIBRARY_PATH=/usr/local/cuda/lib:$HOME/pool/lib:$DYLD_LIBRARY_PATH
$HOME/pool/lib is where you installed Boost to. Change it if installed elsewhere.
Continue here when you're done installing Boost.
Step 2: Download and unpack PyCUDA
Download PyCUDA and unpack it:
$ tar xfz pycuda-VERSION.tar.gz
Step 3: Install Numpy
PyCUDA is designed to work in conjunction with numpy, Python's array package. Here's an easy way to install it, if you do not have it already:
$ cd pycuda-VERSION $ sudo "python ez_setup.py" # this will install setuptools $ sudo "easy_install numpy" # this will install numpy using setuptools
(If you're not sure, repeating these commands will not hurt.)
Step 4: Build PyCUDA
Next, just type:
$ cd pycuda-VERSION # if you're not there already $ python configure.py --boost-inc-dir=$HOME/pool/include/boost-1_39/ --boost-lib-dir=$HOME/pool/lib/ --boost-python-libname=boost_python-xgcc42-mt --cuda-root=/usr/local/cuda/
siteconf.py shoud look like:
1 BOOST_INC_DIR = ['/Users/k5patel/pool/include/boost-1_39/']
2 BOOST_LIB_DIR = ['/Users/k5patel/pool/lib/']
3 BOOST_COMPILER = 'gcc42'
4 BOOST_PYTHON_LIBNAME = ['boost_python-xgcc42-mt']
5 BOOST_THREAD_LIBNAME = ['boost_thread-xgcc42-mt']
6 CUDA_TRACE = False
7 CUDA_ROOT = '/usr/local/cuda/'
8 CUDA_ENABLE_GL = False
9 CUDADRV_LIB_DIR = []
10 CUDADRV_LIBNAME = ['cuda']
11
12 # not necessary with PyCUDA past 2010-01-23 or newer than 0.93.
13 CXXFLAGS = ['-m32']
14 LDFLAGS = ['-m32']
15
16 # if on Snow Leopard, include these two lines:
17 CXXFLAGS.extend(['-isysroot', '/Developer/SDKs/MacOSX10.6.sdk'])
18 LDFLAGS.extend(['-isysroot', '/Developer/SDKs/MacOSX10.6.sdk'])
19
20 # (Dear Lazyweb--can we detect Snow Leopard somehow? Thanks! -AK)
21
Note that gcc42 is a compiler tag that depends on the compiler with which you built boost. Check the contents of your boost library directory to find out what the correct tag is. The instructions above assume that the boost libraries were installed in $HOME/pool. Also note that you will (probably) have to change the value of :option:--cuda-root.
If you are getting the following error: ld: library not found for -lboost_python-xgcc40-mt and installed in /usr/lib, try to re-install boost somewhere else or just "cp -vf /usr/lib/libboost* ." and use --boost-lib-dir=./ with configure.py
Now it's time to build PyCUDA
$ sudo "make install"
ensure that there are no warnings around the line of "different architecture"
Once that works, congratulations! You've successfully built PyCUDA.
Step 5: Test PyCUDA
If you'd like to be extra-careful, you can run PyCUDA's unit tests:
$ cd pycuda-VERSION/test $ python test_driver.py
If it says "OK" at the end, that means your installation of PyCUDA was successful. Reward yourself by checking out the tutorial.
Notes about Snow Leopard
Python on Snow Leopard defaults to a 64-bit executable. You can check this by typing:
1 import sys
2 print sys.maxint
3
If it's ~2 billion, you're running Python in 32-bit mode. If it's a huge number, you've got a 64-bit Python, which conflicts with your 32-bit CUDA/PyCUDA drivers and can cause your error message.
You can force Python on Snow Leopard to run in 32-bit by setting a special environment variable:
VERSIONER_PYTHON_PREFER_32_BIT=yes
Or you can make this change permanent and global by writing this to the defaults:
defaults write com.apple.versioner.python Prefer-32-Bit 1
Fatal Python error: Interpreter not initialized (version mismatch?)
If for some reason you are unable to get Boost to link against the correct python, you can use install_name_tool to relink it against the correct one. For example, the following command will relink from the System Python (2.6.1) to the MacPython (2.6.4) on Snow Leopard:
% install_name_tool -change /System/Library/Frameworks/Python.framework/Versions/2.6/Python /Library/Frameworks/Python.framework/Versions/2.6/Python libboost_python.dylib
