2022-09-18

Raspberry Pi Open CL

In my previous post part 1 and part 2, I have configured Rasberry Pi Cluster. Since I am using Raspberry Pi 3 and Raspberry Pi Zero 2, I found out that I am able to use OpenCL on these hardware. I am curious how OpenCL will perform, and to see if we can use the cluster to execute OpenCL code.

In this post I make a detour from the Raspberry Pi Cluster post and will show how I configure OpenCL on Rasberry Pi 3. Since the CPU is the same with Rasbperry Pi Zero 2, the steps will work on Raspberry Pi Zero 2. However, do take note that the Pi Zero 2 have less RAM. There are similar guide out there in the internet, however, these steps are the one I tried and work in my setup. As usual, the steps below are made possible by many people contributing to get each of the component to work. All this step are done directly at the Raspberry Pi itself.

In my previous posts, I have set the the Graphic Processing Unit (GPU) memory of the Raspberry Pi to a minimum value. Since we will be using OpenCL with the GPU of the Pi, we first need to increase the memory allocated to GPU. We do this by updating /boot/config.txt:

# set GPU mem to 64MB
gpu_mem=64

Then I installed the dependencies:

$ sudo apt-get install cmake git
$ sudo apt-get install build-essential dialog apt-utils libxml2-dev libncurses5
$ sudo apt-get install libhwloc-dev ninja-build
$ sudo apt-get install libraspberrypi-dev
$ sudo apt-get install ocl-icd-opencl-dev ocl-icd-dev
$ sudo apt-get install opencl-headers
$ sudo apt-get install --reinstall clinfo
$ sudo apt-get install llvm libclang-dev libbsd0  libc6  libedit2  libffi6  libgcc-s1 libncurses5  libstdc++6  libtinfo5 zlib1g llvm-dev clang clang-format clang-tidy
$ sudo apt install libclang-cpp-dev

Check the clang installation:

$ clang --version
$ clinfo

If needed (Read: if you encountered some problem, you can try this), add the user to the video group:

$ sudo usermod -a -G video $(whoami)

The next step is to download and compile the VC4CL from source. Many thanks to Doe300 and contributors to make this work with Raspberry Pi3 and Pi Zero 2. NOTE that at the time of writing, Raspberry Pi 4 is not supported. In this example, download and compile it at ~/rpi3opencl, but you can use other folder as needed.

$ mkdir -p ~/rpi3opencl
$ cd ~/rpi3opencl
$ git clone https://github.com/doe300/VC4CLStdLib.git
$ git clone https://github.com/doe300/VC4C.git
$ git clone https://github.com/doe300/VC4CL.git

First I build VC4CLStdLib:

$ cd ~/rpi3opencl/VC4CLStdLib
$ mkdir build
$ cd build
$ cmake -DCMAKE_INSTALL_PREFIX=/usr -DBUILD_DEBUG=OFF -DCROSS_COMPILE=OFF -DBUILD_DEB_PACKAGE=OFF ..
$ make
$ sudo make install
$ sudo ldconfig

Then I built VC4C. Note that this will take to compile this.

$ cd ~/rpi3opencl/VC4C
$ mkdir build
$ cd build
$ cmake -DCMAKE_INSTALL_PREFIX=/usr -DBUILD_DEBUG=OFF -DBUILD_DEB_PACKAGE=OFF ..
$ make
$ sudo make install
$ sudo ldconfig

Lastly, I compile the VC4CL:

$ cd ~/rpi3opencl/VC4CL 
$ mkdir build
$ cd build
$ cmake -DCMAKE_INSTALL_PREFIX=/usr -DBUILD_DEBUG=OFF -DBUILD_DEB_PACKAGE=OFF -DBUILD_ICD=ON -DIMAGE_SUPPORT=ON ..
$ make
$ sudo make install
$ sudo ldconfig

You need the also to create/update VC4CL.icd at /etc/OpenCL/vendors/ and update the installation location of the VC4CL in this file.

$ sudo mkdir /etc/OpenCL
$ sudo mkdir /etc/OpenCL/vendors
$ sudo nano /etc/OpenCL/vendors/VC4CL.icd

I put the following line in this file, then save, and exit:

/usr/local/lib/libVC4CL.so

You can check if the installation is correct by using clinfo command. If for some reason that the clinfo is not working for you, you can optionally compile it your own from source:

$ cd ~/rpi3opencl
$ git clone https://github.com/Oblomov/clinfo.git
$ cd ~/rpi3opencl/clinfo
$ make
$ sudo make install
$ sudo ldconfig

NOTE: For some reason, the OpenCL in my Raspberry Pi requires root access, so everytime you run OpenCL code in Raspberry Pi, you may need to run as root (maybe even when running clinfo)

$ sudo clinfo

Now you should be able to run OpenCL with Raspberry Pi GPU.

In addition of running the code in GPU, it is also possible to run the OpenCL code with CPU. The following steps will show hos to install PoCL to allos running OpenCL code in Raspberry Pi CPU. If you have followed the dependencies installation above, you should be ready to compile PoCL. In this example, we compile OpenCL in ~/rpi3opencl/opencl_cpu:

$ cd ~/rpi3opencl
$ mkdir opencl_cpu
$ cd opencl_cpu
$ git clone --single-branch --branch release_3_0 https://github.com/pocl/pocl.git
$ cd pocl
$ mkdir build
$ cd build
$ cmake -DCMAKE_INSTALL_PREFIX=/usr/local/pocl/ ..
$ make 
$ sudo make install && sudo ldconfig

As with VC4CL above, we need to add the pocl.icd file in /etc/OpenCL/vendors/ path:

$ sudo nano /etc/OpenCL/vendors/pocl.icd

and add the following line, then save and exit:

/usr/local/pocl/lib/libpocl.so

You can check using clinfo to check the installation. The clinfo should show both VC4CL and PoCL if the installation is correct.

$ sudo clinfo

Well, this post have only covered the installation of OpenCL for Raspberry Pi 3 and Pi Zero 2. I will cover the compilation and running of OpenCL code in the upcoming post.