PG-Strom on Mac OSX

Installing pg-strom on Mac requires a few hacks, mostly related to CUDA files. In this article I will present a step-by-step process that will allow you to harness the power an Nvidia GPU and significantly speed up PostgreSQL queries on your Mac.

Xcode & Macports

Since we’re going to build PostgreSQL from source, you need to ensure your console tools are up to date. The first step is to download or update Xcode to the most recent version available for your OSX.

Next, open Terminal and run the following command:

➜  ~ xcode-select --install

Follow the wizard to complete installation. Afterwards, you should have the “gcc” available at your disposal:

➜  ~ gcc -v 
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

I also recommend Macports, as they might come it handy should PostgreSQL complain about missing dependancies.

CUDA

Download and install the latest version of CUDA libraries. OSX version comes as a nifty package with an installation wizard. Once it’s in place, you should see a CUDA icon in your System Preferences:

Cuda Icon

We now have to symlink a “lib64” directory, which is where pg-strom expects CUDA libraries to reside. Go to:

➜  ~ cd /usr/local/cuda

And run the following command (it will ask you for your account password):

➜  cuda sudo ln -s lib lib64
Password:

That’s it, CUDA is now good to go.

PostgreSQL

We’re now going to download, build and install PostgreSQL database. Once it’s ready, it will require a data directory to store all of its files and configs. You can put postgres and its data anywhere you want, in my case I’m going to use ~/postgres for the database and ~/postgres_data for its files.

To download postgres sources we need git. It should be available by default in your OSX or you can get the latest version using Macports:

➜  ~ sudo port install git

Let’s prepare postgres directories:

➜  ~ mkdir ~/postgres
➜  ~ mkdir ~/postgres_data

I will use ~/src for the source code:

➜  ~ cd ~/src
➜  src git clone https://github.com/postgres/postgres.git
Cloning into 'postgres'...
remote: Counting objects: 589103, done.
remote: Compressing objects: 100% (131/131), done.
remote: Total 589103 (delta 65), reused 0 (delta 0), pack-reused 588972
Receiving objects: 100% (589103/589103), 283.13 MiB | 6.63 MiB/s, done.
Resolving deltas: 100% (480916/480916), done.
Checking connectivity... done.

We’re going to build version 9.5, as per pg-strom instructions:

➜  src cd postgres 
➜  postgres git:(master) git checkout REL9_5_STABLE
Branch REL9_5_STABLE set up to track remote branch REL9_5_STABLE from origin.
Switched to a new branch 'REL9_5_STABLE'

Be sure to substitute “/Users/tompaw/postgres” with whatever directory you decided to use for postgres binaries:

➜  postgres git:(REL9_5_STABLE) ./configure --prefix=/Users/tompaw/postgres --disable-rpath --enable-debug --enable-cassert

We’re now ready to build:

➜  postgres git:(REL9_5_STABLE) make

[...]

All of PostgreSQL successfully made. Ready to install.

And install:

➜  postgres git:(REL9_5_STABLE) make install

[...]

PostgreSQL installation complete.

The final step is to add our new postgres installation to PATH (again – make sure you use your own directory name):

➜  postgres git:(REL9_5_STABLE) export PATH=/Users/tompaw/postgres/bin:$PATH

Our database is now ready.

PG-Strom

In a similar fashion, I’m going to check out pg-strom sources into my ~/src directory:

➜  ~ cd ~/src       
➜  src git clone https://github.com/pg-strom/devel pg_strom
Cloning into 'pg_strom'...
remote: Counting objects: 9687, done.
remote: Compressing objects: 100% (122/122), done.
remote: Total 9687 (delta 80), reused 0 (delta 0), pack-reused 9565
Receiving objects: 100% (9687/9687), 7.98 MiB | 3.83 MiB/s, done.
Resolving deltas: 100% (7270/7270), done.
Checking connectivity... done.

At this moment, the latest usable commit is from June the 5th 2016. As pg-strom is still in infancy, you will need to keep track of its repository and update your installation quite frequently:

➜  src cd pg_strom 
➜  pg_strom git:(master) git checkout 8fb4aee6e036b1f1eba8f4d2e270ff90c0a15302
Note: checking out '8fb4aee6e036b1f1eba8f4d2e270ff90c0a15302'.

To satisfy pg-strom dependancies, we need the rpm tool:

➜  pg_strom git:(8fb4aee) sudo port install rpm

(I have no idea why this is required, but your build will otherwise fail. Hopefully this will be resolved in future releases.)

There is no “configure” step here. Pg_strom uses pg_config tool to determine postgres location (which is why we had to update our PATH) and the fixed path for CUDA libraries (which is why he had to symlink “lib64” at the very beginning):

➜  pg_strom git:(8fb4aee) make

[...]

test src/pg_strom.control -ef pg_strom.control || cp -f src/pg_strom.control pg_strom.control

You can ignore warnings and notices during the compilation.

We now have to add CUDA libraries to rpath, due to how the .so files of pg_strom are linked:

➜  pg_strom git:(8fb4aee)  install_name_tool -add_rpath /usr/local/cuda/lib pg_strom.so

Finally, we’re ready to install pg_strom:

➜  pg_strom git:(8fb4aee)  make install
sort: invalid option -- V
Try `sort --help' for more information.
sort: invalid option -- V
Try `sort --help' for more information.
/bin/sh /Users/tompaw/postgres/lib/pgxs/src/makefiles/../../config/install-sh -c -d '/Users/tompaw/postgres/lib'
/bin/sh /Users/tompaw/postgres/lib/pgxs/src/makefiles/../../config/install-sh -c -d '/Users/tompaw/postgres/share/extension'
/bin/sh /Users/tompaw/postgres/lib/pgxs/src/makefiles/../../config/install-sh -c -d '/Users/tompaw/postgres/share/extension'
/bin/sh /Users/tompaw/postgres/lib/pgxs/src/makefiles/../../config/install-sh -c -d '/Users/tompaw/postgres/bin'
/usr/bin/install -c -m 755  pg_strom.so '/Users/tompaw/postgres/lib/pg_strom.so'
/usr/bin/install -c -m 644 .//pg_strom.control '/Users/tompaw/postgres/share/extension/'
/usr/bin/install -c -m 644 .//./src/pg_strom--1.0.sql  '/Users/tompaw/postgres/share/extension/'
/usr/bin/install -c -m 755 ./src/gpuinfo '/Users/tompaw/postgres/bin/'

That’s all. Let’s see if our setup works.

Testing PG-Strom

Since it’s a brand new postgres installation, the first step is to initialize its data directory (remember to use your own path):

➜  ~ initdb -D /Users/tompaw/postgres_data

[...]

Success. You can now start the database server using:

 pg_ctl -D /Users/tompaw/postgres_data -l logfile start

We now need to add pg-strom to psql configuration:

➜  ~ vim ~/postgres_data/postgresql.conf

Required changes are:

shared_preload_libraries = '$libdir/pg_strom'
shared_buffers = 1024MB

(This is a reasonable minimum for shared_buffers, feel free to adjust it to suit your needs.)

Finally, let’s launch the PostgreSQL daemon:

➜  ~ pg_ctl -D /Users/tompaw/postgres_data start
server starting

LOG:  PG-Strom version 0.9devel built for PostgreSQL 9.5
LOG:  CUDA Runtime version: 7.5.0
LOG:  GPU0 GeForce GTX 680MX (8 SMs, 719MHz), L2 512KB, RAM 2047MB (256bits, 2500MHz), capability 3.0, NOT SUPPORTED
FATAL:  No supported CUDA devices, PG-Strom was disabled

We’re almost there. Installation is now complete, but we need a more powerful GPU to take advantage of CUDA.

If only there was a nice and easy way to add one to a Macintosh…

3 thoughts on “PG-Strom on Mac OSX”

  1. install_name_tool -add_rpath /usr/local/cuda/lib pg_strom.so

    error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/install_name_tool: can’t open file: pg_strom.so (No such file or directory)

    I keep getting this message on this step…..?

Leave a Reply

Your email address will not be published. Required fields are marked *