Build & Install OpenCL on Raspberry Pi 3

Muhammad Yunus
3 min readOct 15, 2021

OpenCL CPU + GPU using PoCL and VC4CL (VideoCore IV GPU)

OpenCL (Open Computing Language) is a framework for writing programs that execute across heterogeneous platforms consisting of central processing units (CPUs), graphics processing units (GPUs), digital signal processors (DSPs), field-programmable gate arrays (FPGAs) and other processors or hardware accelerators. [Wikipedia]

Since there is no official OpenCL in Raspberry Pi, but we can use several OpenCL Opensource Implementation like PoCL (CPU implementation) and VC4CL (VideoCore GPU Implementation).

PoCL is a portable open source (MIT-licensed) implementation of the OpenCL standard (1.2 with some 2.0 features supported). In addition to being an easily portable multi-device (truely heterogeneous) open-source OpenCL implementation, a major goal of this project is improving interoperability of diversity of OpenCL-capable devices by integrating them to a single centrally orchestrated platform. [PoCL Page]

VC4CL is an implementation of the OpenCL 1.2 standard for the VideoCore IV GPU (found in Raspberry Pi 1–3 models). The implementation consist of:

The VC4CL OpenCL runtime library, running on the host CPU to compile, run and interact with OpenCL kernels.

The VC4C compiler, converting OpenCL kernels into machine code. This compiler also provides an implementation of the OpenCL built-in functions.

The VC4CLStdLib, the platform-specific implementation of the OpenCL C standard library, is linked in with the kernel by VC4C. [V4CL Github]

Install VC4C, VC4CLStdLib & VC4CL

  • Install dependency libraries,
sudo apt-get update 
sudo apt-get upgrade -y
sudo apt-get install cmake git
sudo apt-get install libraspberrypi-dev
sudo apt-get install ocl-icd-opencl-dev ocl-icd-dev
sudo apt-get install libhwloc-dev ninja-build
sudo apt-get install dialog apt-utils libxml2-dev libncurses5
sudo apt-get install opencl-headers
sudo apt-get install clinfo
  • Install OpenCL compiler (LLVM/Clang)
sudo apt-get install clang clang-format clang-tidy libclang-dev
  • Check clang ,
clang --version
  • When this tutorial created, we are using LLVM/Clang version 7.
  • Download Source,
cd ~
mkdir OpenCL_GPU
cd OpenCL_GPU
git clone https://github.com/doe300/VC4CLStdLib.git
git clone https://github.com/doe300/VC4CL.git
git clone https://github.com/doe300/VC4C.git

Install VC4CLStdLib

cd ~/OpenCL_GPU/VC4CLStdLib
mkdir build
cd build
cmake ..
make
sudo make install
sudo ldconfig

Install VC4C

cd ~/OpenCL_GPU/VC4C
mkdir build
cd build
cmake ..
make
sudo make install
sudo ldconfig

Install VC4CL

cd ~/OpenCL_GPU/VC4CL
mkdir build
cd build
cmake ..
make
sudo make install
sudo ldconfig

Install PoCL

  • Download PoCL 1.7 source from github,
cd ~
mkdir OpenCL_CPU
cd OpenCL_CPU
git clone --single-branch --branch release_1_7 https://github.com/pocl/pocl.git
cd pocl
mkdir build
cd build
  • Configure & Build,
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/pocl/ ..
make
sudo make install
  • Create pocl.icd file in /etc/OpenCL/vendors/ path,
cd /etc/OpenCL/vendors/
sudo nano pocl.icd
  • Paste the following path,
/usr/local/pocl/lib/libpocl.so
  • Exit and save pocl.icd file.

Installation Test

  • Run clinfo ,
clinfo
  • Result should look like this,
  • Create small C program to query device info, called it with query_device_info.c,
  • Compile it using gcc + ocl-icd loader,
gcc query_device_info.c -o query_device_info `pkg-config --libs --cflags OpenCL`
  • Then run the compiled binary,
./query_device_info
  • Output should look like this,
1. Device: VideoCore IV GPU
1.1 Hardware version: OpenCL 1.2 VC4CL 0.4.9999 (4bf06c9)
1.2 Software version: 0.4.9999
1.3 OpenCL C version: OpenCL C 1.2
1.4 Parallel compute units: 1
1. Device: pthread-cortex-a53
1.1 Hardware version: OpenCL 1.2 pocl HSTR: pthread-armv6k-unknown-linux-gnueabihf-cortex-a53
1.2 Software version: 1.7
1.3 OpenCL C version: OpenCL C 1.2 pocl
1.4 Parallel compute units: 4
  • At this point, we are successfully build & install OpenCL in Raspberry Pi 3 via VC4CL (VideoCore IV GPU) and PoCL (ARM Cortex A53 CPU).

Source

--

--

Muhammad Yunus

IoT Engineer, Software Developer & Machine Learning Enthusiast