Skip to content
Davor Sluga edited this page Jul 4, 2018 · 26 revisions

Gpu_cholesky_decompose branch of stan_math library provides experimental GPU support. It is based on OpenCL, an open-source framework for writing programs that utilize a platform with heterogeneous hardware. OpenCL is used to speed-up time consuming routines used by Stan. Currently, matrix multiplication and Cholesky decomposition and their derivatives are supported. Other routines will be available in the future. These routines are suitable for programs which require solving large NxM matrices (N>600) such as algorithms that utilize large covariance matrices.

Requirements

Users must have suitable hardware (e.g. Nvidia or AMD gpu), valid OpenCL driver, SDK and a suitable C/C++ compiler installed on their computer.

Installation

Linux

The following guide is for Ubuntu, but it should be similar for any other Linux distribution. You should have the GNU compiler suite or clang compiler installed beforehand.

Install the Nvidia CUDA toolkit and clinfo tool if you have a Nvidia GPU

apt update
apt install nvidia-cuda-toolkit clinfo

Those with AMD devices can install the OpenCL driver available through

apt install -y libclc-amdgcn mesa-opencl-icd clinfo

If your device is not supported by the current drivers available you can try Paulo Miguel PPA

add-apt-repository ppa:paulo-miguel-dias/mesa 
apt-get update
apt-get install libclc-amdgcn mesa-opencl-icd

MacOS

Mac's should already have the OpenCL driver installed if you have the appropriate hardware.

Windows

Install the latest Rtools suite if you don't already have it. During the installation make sure that the 64 bit toolchain is installed. You also need to verify that you have the System Enviroment variable Path updated to include the path to the g++ compiler (<Rtools installation path>\mingw_64\bin).

If you have a Nvidia card, install the latest Nvidia CUDA toolkit. AMD users should use AMD APP SDK.

Users can check that their installation is valid by running clinfo.

Setting up the Math Library to run on a GPU

To turn on GPU computation:

  1. Check and record what device and platform you would like to use with clinfo; you will need device and platform index.
  2. Open a text file called make/local; if it does not exist, create one.
  3. Add these lines to the make/local file:
STAN_OPENCL=true
OPENCL_DEVICE_ID=${CHOSEN_INDEX}
OPENCL_PLATFORM_ID=${CHOSEN_INDEX}

where the user will replace ${CHOSEN_INDEX} with the index of the device and platform they would like to use. In most cases these two will be 0. If you are using Windows append the following lines at the end of the make/local file in order to link with the appropriate OpenCL library:

  • Nvidia
CC = g++
LDFLAGS_OPENCL= -L"$(CUDA_PATH)\lib\x64" -lOpenCL
  • AMD
CC = g++
LDFLAGS_OPENCL= -L"$(AMDAPPSDKROOT)lib\x64" -lOpenCL

Running Tests with OpenCL

Once you have done the above step, runTests.py should execute with the GPU enabled. All tests will match the phrase *_opencl_* and tests can be filtered such as

./runTests.py test/unit -f opencl

Integration with CmdStan

Currently gp3_example branch of CmdStan has integrated OpenCL support. As of now the follofing OpenCL accelerated functions are supported, more will be added soon:

  • cholesky_decompose_gpu
  • multiply_matrix_gpu
  • inverse_gpu

In order to use these functions through CmdStan you need to first download the appropriate branch. You can do this by running the folllowing command (you need to have git installed beforehand): git clone --recurse-submodules -b gp3_example https://github.com/bstatcomp/cmdstan.git

Then you need to create the make/local file in the CmdStan folder as explained above. After that build the stan compiler by navigating to the CmdStan folder and running make build.

An example model is provided in examples/GP/, which uses OpenCL Cholesky decomposition. You can check if your OpenCL configuration works by trying to build it.

  • Linux and MacOS: make examples/GP/gp3
  • Windows: make examples/GP/gp3.exe