Installing PyCUDA on Mac OS X

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:

If you're running Snow Leopard (Mac OS 10.6), then you might benefit from following these slightly more up-to-date instructions:

Installing PyCUDA on Snow Leopard

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:

(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

Step 1: Download and unpack PyCUDA

Download PyCUDA and unpack it:

$ tar xfz pycuda-VERSION.tar.gz

Step 2: 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 distribute_setup.py" # this will install distribute
$ sudo "easy_install numpy" # this will install numpy using distribute

If you're not sure try import numpy at the Python prompt. Don't repeat these commands for the Mac system Python.

Step 3: Build PyCUDA

Next, just type:

$ cd pycuda-VERSION # if you're not there already
$ python configure.py --cuda-root=/usr/local/cuda/

siteconf.py shoud look like:

   1 CUDA_ROOT = '/usr/local/cuda'
   2 CUDA_ENABLE_GL = True
   3 CUDADRV_LIB_DIR = []
   4 CUDADRV_LIBNAME = ['cuda']
   5 
   6 # if on Snow Leopard, include these lines:
   7 CXXFLAGS = ["-arch", "x86_64", "-arch", "i386"]
   8 LDFLAGS = ["-arch", "x86_64", "-arch", "i386"]
   9 
  10 CXXFLAGS.extend(['-isysroot', '/Developer/SDKs/MacOSX10.6.sdk'])
  11 LDFLAGS.extend(['-isysroot', '/Developer/SDKs/MacOSX10.6.sdk'])

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.

Note that this step may automatically look for and, if necessary, install a number of Python packages from the Python package index. These packages are listed under install_requires in PyCUDA's install control file.

Step 4: 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

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. Either should be fine.

If you'd like to, 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

(BryanCatanzaro)

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 

Fatal Python error: pytest.py not found

You might have gotten this if you install PyCUDA before PyOpenCL. Simply install pytest before running the confidence tests.

$ cd pycuda-VERSION/test # if you're not there already
$ sudo easy_install -U pytest
$ python test_driver.py

See also: py.test 2.0.0 PyOpenCL Installation for Mac

(BogdanVacaliuc)

Installing PyCUDA on Mac OS X 10.7 Lion with CUDA 4.0

Installing PyCUDA on OS X 10.7 is a bit easier than with 10.6 and other versions. This is because many of the earlier PyCUDA dependencies now come preinstalled with 10.7 Below are the components used to build/use PyCUDA:

Step 1: Install the CUDA driver and toolkit

Do all of the following:

You can either type the following in the shell or add it to the ~/.bash_profile file (If ~/.bash_profile does not exist then create it and add the following lines)

$ export PATH=/usr/local/cuda/bin:$PATH
$ export DYLD_LIBRARY_PATH=/usr/local/cuda/lib:$DYLD_LIBRARY_PATH

Step 2: Get PyCUDA

Download PyCUDA. There are multiple ways to do this. I prefer letting OS X do the hard work by going here and letting the archiver unzip it by doubling clicking the download when it's finished.

Step 3: Install PyCUDA

Next, just type:

$ cd pycuda-VERSION # if you're not there already
$ python configure.py --cuda-root=/usr/local/cuda/

siteconf.py shoud look like (at least mine did):

   1 BOOST_INC_DIR = []
   2 BOOST_LIB_DIR = []
   3 BOOST_COMPILER = 'gcc43'
   4 USE_SHIPPED_BOOST = True
   5 BOOST_PYTHON_LIBNAME = ['boost_python']
   6 BOOST_THREAD_LIBNAME = ['boost_thread']
   7 CUDA_TRACE = False
   8 CUDA_ROOT = '/usr/local/cuda/'
   9 CUDA_ENABLE_GL = False
  10 CUDA_ENABLE_CURAND = True
  11 CUDADRV_LIB_DIR = ['${CUDA_ROOT}/lib', '${CUDA_ROOT}/lib64']
  12 CUDADRV_LIBNAME = ['cuda']
  13 CUDART_LIB_DIR = ['${CUDA_ROOT}/lib', '${CUDA_ROOT}/lib64']
  14 CUDART_LIBNAME = ['cudart']
  15 CURAND_LIB_DIR = ['${CUDA_ROOT}/lib', '${CUDA_ROOT}/lib64']
  16 CURAND_LIBNAME = ['curand']
  17 CXXFLAGS = []
  18 LDFLAGS = []
  19 
  20 # We need to remove the lib64 references from three lines because OSX 10.7 is only 64 bit, the CUDA toolkit only has one version and it is 64bit, it's just not called lib64.
  21 
  22 CUDADRV_LIB_DIR = ['${CUDA_ROOT}/lib', '${CUDA_ROOT}/lib64']
  23 to
  24 CUDADRV_LIB_DIR = ['${CUDA_ROOT}/lib']
  25 
  26 CUDART_LIB_DIR = ['${CUDA_ROOT}/lib', '${CUDA_ROOT}/lib64']
  27 to
  28 CUDART_LIB_DIR = ['${CUDA_ROOT}/lib']
  29 
  30 CURAND_LIB_DIR = ['${CUDA_ROOT}/lib', '${CUDA_ROOT}/lib64']
  31 to
  32 CURAND_LIB_DIR = ['${CUDA_ROOT}/lib']
  33 
  34 
  35 # We now need to change the CXXFLAGS and LDFLAGS to look like this:
  36 CXXFLAGS = ["-arch", "x86_64", "-arch", "i386"]
  37 LDFLAGS = ["-arch", "x86_64", "-arch", "i386"]
  38 # We also need to add these lines
  39 CXXFLAGS.extend(['-isysroot', '/Developer/SDKs/MacOSX10.6.sdk'])
  40 LDFLAGS.extend(['-isysroot', '/Developer/SDKs/MacOSX10.6.sdk'])

You will notice that we just added a line referring to 'MacOSX10.6.sdk'. You should look in the /Developer/SDKs/ folder and see two sdk files. OSX 10.7 should have:

(Because it's there and works with other builds, I have opted to use the 10.6 sdk)

After editing siteconf.py shoud look like (at least mine did):

   1 BOOST_INC_DIR = []
   2 BOOST_LIB_DIR = []
   3 BOOST_COMPILER = 'gcc43'
   4 USE_SHIPPED_BOOST = True
   5 BOOST_PYTHON_LIBNAME = ['boost_python']
   6 BOOST_THREAD_LIBNAME = ['boost_thread']
   7 CUDA_TRACE = False
   8 CUDA_ROOT = '/usr/local/cuda/'
   9 CUDA_ENABLE_GL = False
  10 CUDA_ENABLE_CURAND = True
  11 CUDADRV_LIB_DIR = ['${CUDA_ROOT}/lib']
  12 CUDADRV_LIBNAME = ['cuda']
  13 CUDART_LIB_DIR = ['${CUDA_ROOT}/lib']
  14 CUDART_LIBNAME = ['cudart']
  15 CURAND_LIB_DIR = ['${CUDA_ROOT}/lib']
  16 CURAND_LIBNAME = ['curand']
  17 CXXFLAGS = ["-arch", "x86_64", "-arch", "i386"]
  18 LDFLAGS = ["-arch", "x86_64", "-arch", "i386"]
  19 CXXFLAGS.extend(['-isysroot', '/Developer/SDKs/MacOSX10.6.sdk'])
  20 LDFLAGS.extend(['-isysroot', '/Developer/SDKs/MacOSX10.6.sdk'])

run make

$ sudo make

(This might take some time)

Now it's time to build PyCUDA

$ sudo "make install"

(This might also take some time)

ensure that there are no warnings around the line of "different architecture" (yes I copied this)

PyCUDA should now be built

Note that this step may automatically look for and, if necessary, install a number of Python packages from the Python package index. These packages are listed under install_requires in PyCUDA's install control file.

Step 4: Test PyCUDA

If you'd like to be extra-careful, you can run PyCUDA's unit tests: (I recommend this)

$ cd pycuda-VERSION/test
$ python test_driver.py

Step 5: Use PyCUDA

At this point installation should be done you should be able to successfully use PyCUDA. Happy Developing.

Installing PyCUDA on Mac OS X 10.8 Mountain Lion with CUDA 4.0

Here is the siteconf.py that enabled it to compile (upgrade from Lion):

   1 BOOST_INC_DIR = []
   2 BOOST_LIB_DIR = []
   3 BOOST_COMPILER = 'gcc43'
   4 USE_SHIPPED_BOOST = True
   5 BOOST_PYTHON_LIBNAME = ['boost_python']
   6 BOOST_THREAD_LIBNAME = ['boost_thread']
   7 CUDA_TRACE = False
   8 CUDA_ROOT = '/usr/local/cuda/'
   9 CUDA_ENABLE_GL = False
  10 CUDA_ENABLE_CURAND = True
  11 CUDADRV_LIB_DIR = ['${CUDA_ROOT}/lib']
  12 CUDADRV_LIBNAME = ['cuda']
  13 CUDART_LIB_DIR = ['${CUDA_ROOT}/lib']
  14 CUDART_LIBNAME = ['cudart']
  15 CURAND_LIB_DIR = ['${CUDA_ROOT}/lib']
  16 CURAND_LIBNAME = ['curand']
  17 CXXFLAGS = ["-arch", "x86_64", "-arch", "i386", "-mmacosx-version-min=10.7"]
  18 LDFLAGS = ["-F/Library/Frameworks","-framework CUDA","-arch",
  19 "x86_64", "-arch", "i386", "-mmacosx-version-min=10.7"]
  20 CXXFLAGS.extend(['-isysroot',
  21 '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk'])
  22 LDFLAGS.extend(['-isysroot',
  23 '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk'])

Optional: Install Boost

As of version 0.94, the Boost C++ libraries are no longer a dependency of PyCUDA.

If you would like to use multiple packages that use Boost.Python (e.g. both PyCUDA and PyOpenCL), you can still build PyCUDA against a separate Boost installation. If so, follow the BoostInstallationHowto. Continue here when you're done.

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.

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.

Continue here when you're done installing Boost.

This warning was added at some point--I'm not sure if it's still relevant now:

-- AndreasKloeckner 2010-09-21 00:16:32

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


CategoryPyCuda