Installing OpenCL on the Raspberry Pi 3

Wim Vanderbauwhede edited this page Feb 18, 2019 · 5 revisions

Installing OpenCL on the Raspberry Pi 3

There is no official OpenCL implementation for Arm CPUs, but fortunately there is pocl (Portable Computing Language), a portable open source implementation of the OpenCL standard (1.2 with some 2.0 features supported). This tutorial explains how to build pocl on the Raspberry Pi 3 B+ running Raspbian Stretch. This OpenCL implementation only supports the CPU on the Raspberry Pi 3, the GPU is not supported.

Prerequisites

Prepare the system

First update and upgrade your system packages using following commands. This takes quite long.

sudo apt update  
sudo apt upgrade  
sudo apt-get autoremove  

Then add the Debian Stretch repository to the package repository list. Create a file debian-stretch.list in the folder /etc/apt/sources.list.d/ with the following content:

deb [trusted=yes] http://ftp.de.debian.org/debian stretch main

Run sudo apt update again. You can ignore the the warning about missing public keys.

Install some dependencies

sudo apt install libhwloc-dev ocl-icd-opencl-dev libglew-dev libedit-dev zlib1g-dev

The first two of these, hwloc and ocl-icd, are OpenCL specific:

Portable Hardware Locality (hwloc)

The hwloc (Portable Hardware Locality) software package provides a portable abstraction (across OS, versions, architectures, ...) of the hierarchical topology of modern architectures, including NUMA memory nodes, sockets, shared caches, cores and simultaneous multithreading. It also gathers various system attributes such as cache and memory information as well as the locality of I/O devices such as network interfaces, InfiniBand HCAs or GPUs.

hwloc primarily aims at helping applications with gathering information about increasingly complex parallel computing platforms so as to exploit them accordingly and efficiently.

hwloc may also help many applications just by providing a portable CPU and memory binding API and a reliable way to find out how many cores and/or hardware threads are available.

OpenCL ICD loader

ICD stands for Installable Client Driver. The OpenCL ICD extension (cl_khr_icd) allows multiple implementations of OpenCL to co-exist on the same system. The OpenCL ICD Loader library allows applications to choose a platform from the list of installed platforms and dispatches OpenCL API calls to the underlying implementation. The Open Source implementation used by pocl is ocl-id. There is also an implementation provided by the Khronos Group.

Install LLVM and CLang

The LLVM project is a collection of modular and reusable compiler and toolchain technologies. Despite its name, LLVM has little to do with traditional virtual machines. The name "LLVM" itself is not an acronym; it is the full name of the project.

The Clang project provides a language front-end and tooling infrastructure for languages in the C language family (C, C++, Objective C/C++, OpenCL, CUDA, and RenderScript) for the LLVM project.

sudo apt install clang-3.8 libclang-3.8-dev

Install CMake ans SCons

CMake is an open-source, cross-platform family of tools designed to build, test and package software. CMake is used to control the software compilation process using simple platform and compiler independent configuration files, and generate native makefiles and workspaces that can be used in the compiler environment of your choice.

sudo apt install cmake

SCons is an Open Source software construction tool—that is, a next-generation build tool. Think of SCons as an improved, cross-platform substitute for the classic Make utility with integrated functionality similar to autoconf/automake and compiler caches such as ccache. In short, SCons is an easier, more reliable and faster way to build software.

sudo apt install scons

Build pocl from source

Get and unpack the archive

We use pocl version 0.13 as that is the one supporting the LLVM version from the Raspbian repository (3.8). If you want a more recent version you would have to build LLVM and Clang from source and get the pocl sources from GitHub.

wget http://portablecl.org/downloads/pocl-0.13.tar.gz
tar -zxvf pocl-0.13.tar.gz
cd pocl-0.13

Patch the cmake configuration

You can download the modified CMakeLists.txt file or change your local file:

On line 335 of CMakeLists.txt, add -std=gnu++11 as follows:

set(DEFAULT_KERNEL_CLANGXX_FLAGS "-std=gnu++11 -DVML_NO_IOSTREAM ${CLANGXX_STDLIB}")

Build pocl

mkdir build
cd build  
cmake -DLLC_HOST_CPU=cortex-a53 -DWITH_LLVM_CONFIG=/usr/bin/llvm-config-3.8 -DSTATIC_LLVM=1 -DENABLE_ICD=1 ..  
make  
sudo make install  

If you have a lot of time you can run the full test suite by running make check. For a quick test if everything works, build and run the trivial_opencl_program.c:

pi@raspberrypi:~ $ gcc -Wno-deprecated-declarations -o trivial_opencl_program trivial_opencl_program.c -lOpenCL
pi@raspberrypi:~ $ ./trivial_opencl_program
Success

Some useful tools

These tools are not essential but building them is very easy and quick.

First, create a directory bin in your $HOME directory. The default .profile will add this automatically to your $PATH.

cd ~
mkdir bin
. .profile

Oblomov's clinfo

clinfo is a simple command-line application that enumerates all possible (known) properties of the OpenCL platform and devices available on the system.

pi@raspberrypi:~ $ git clone https://github.com/Oblomov/clinfo.git  
pi@raspberrypi:~ $ cd clinfo  
pi@raspberrypi:~ $ make  

pi@raspberrypi:~ $ ./clinfo  

Krishnaraj Bhat's clpeak

clpeak is a synthetic benchmarking tool to measure peak capabilities of opencl devices. It only measures the peak metrics that can be achieved using vector operations and does not represent a real-world use case.

git clone https://github.com/krrishnarraj/clpeak.git  
cd clpeak  
mkdir build; cd build  
cmake ..  
make  
sudo cp clpeak  ~/bin

Raspbian Jessie on Raspberry Pi 3 B+ system information

Linux kernel version

pi@raspberrypi:~ $ uname -a
Linux raspberrypi 4.14.79-v7+ #1159 SMP Sun Nov 4 17:50:20 GMT 2018 armv7l GNU/Linux

C++ compiler version

pi@raspberrypi:~ $ g++ --version
g++ (Raspbian 6.3.0-18+rpi1+deb9u1) 6.3.0 20170516

Linker version

pi@raspberrypi:~ $ ldd --version
ldd (Debian GLIBC 2.24-11+deb9u3) 2.24
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.