Install PyCUDA to Python managed by Pyenv under MacOS

Install PyCUDA to Python managed by Pyenv under MacOS

This post is a guide for newbie users of PyCUDA. We use Pyenv here and that means we can build different versions of working environments.

Install Git and Pyenv

In order to download the PyCUDA code, we need to install Git. In order to control multiple versions of Python and creat isolated environment for different projects, we need to use Pyenv. To install Pyenv, we need to install Brew first by running this in terminal:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Then run the command below to install Pyenv.

brew install git
brew install pyenv

The Most Important Trick

But here are some tricks that you must do to succeed your installment: In order to run Brew, you are forced to update your Xcode and its Command Line Tools to the latest version, or the brew would fail to run. So you need to keep a back up of your older version of Xcode which works with CUDA, and install the latest version of Xcode and Command Line Tools to run Brew for the installment of Pyenv. After this installment of Pyenv, you need to download the older version Command Line Tools working with your older version Xcode and CUDA. And DO NOT update in the app store.

With Pyenv installed and the right version of Xcode and Command Line Tools working with CUDA, we can begin our journey of PyCUDA installments.

Install Xcode, Command Line Tools and CUDA

As I posted in the earlier article, the latest version of CUDA cannot work with the newest version of Xcode. At this moment I am typing, the latest CUDA is still version 8.04, which requires XCode 7.3.1 and Command Line Tools for XCode 7.3.1. And HERE is the place you can download them.

Please make sure that your Xcode is the version that works with your CUDA. Then you need to install the CUDA and check the version of nvcc with following command int terminal:

nvcc --version

It will be OK if the output seems like this:

$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2016 NVIDIA Corporation
Built on Sun_Sep_18_22:16:08_CDT_2016
Cuda compilation tools, release 8.0, V8.0.46

Use Pyenv to install a version of Python

Using the following command in terminal to see the available versions of Python:

pyenv install --list

Here we use 3.5.2:

pyenv install 3.5.2

Then we have a brand new Python, which we can do anything inside without worrying about messing up with the system.

Install PyCUDA

Paste following command in terminal to get the source code of PyCUDA:

git clone --recursive http://git.tiker.net/trees/pycuda.git

Get into its folder and set the local Python to the one we installed using Pyenv:

cd pycuda
pyenv local 3.5.2

Then we use these commands to proceed the installation:

python configure.py
sudo make
sudo make install

If there is nothing wrong, we could use a piece of code from the examples of PyCUDA as a file named "test.py" :

# Sample source code from the Tutorial Introduction in the documentation.
import pycuda.gpuarray as gpuarray
import pycuda.driver as cuda
import pycuda.autoinit
import numpy
a_gpu = gpuarray.to_gpu(numpy.random.randn(4, 4).astype(numpy.float32))
a_doubled = (2*a_gpu).get()

print("original array:")
print(a_gpu)
print("doubled with gpuarray:")
print(a_doubled)

If you get similar output like this:

$ python test.py
original array:
[[ 0.27740544 -1.44831014  0.6379782   0.15358959]
 [-0.21130283 -0.19202329 -2.23594046  0.14036565]
 [-0.69078982 -0.44290611  1.2644769   1.55474603]
 [-1.08704031  2.22870898  0.85237521  0.15609477]]
doubled with gpuarray:
[[ 0.55481088 -2.89662027  1.27595639  0.30717918]
 [-0.42260566 -0.38404658 -4.47188091  0.28073129]
 [-1.38157964 -0.88581222  2.52895379  3.10949206]
 [-2.17408061  4.45741796  1.70475042  0.31218955]]

Something More

If you need to install PyCUDA to another version of Python, you just need to install another Python with Pyenv and change the local version of the path of pycuda. For example, we install a Python 3.5.1 and install the PyCUDA to it with commands below:

pyenv install 3.5.1
cd ~/pycuda
pyenv local 3.5.1
python configure.py
sudo make
sudo make install

Then everything will be OK.

Category
Tagcloud
Python Mount&Blade MayaVi Scholar Lens Library Linux QT Data Visualization Poem Hack RaspberryPi NAS Xcode IDE Geology VirtualBox Raspbian Chat Game GlumPy Photo Hate Camera Disease Video University ChromeBook RTL-SDR Microscope Programming Hardware Radio Memory Junck Conda VisPy Software Science Communicate Translation Pyenv Server Kivy Book Telescope Download CUDA Story Discuss Hackintosh DIY Moon Lesson Mac VTK GeoPython