Khadas Docs

Amazing Khadas, always amazes you!

User Tools

Site Tools



Edge2 OpenCL

Khadas Edges and VIMs are powered by ARM Mali GPUs.

We can make use of the available processing power offered to accelerate computational tasks like physics simulations, audio processing, neural networks, etc.

Fenix images are already bundled with the necessary OpenCL libraries to get started, we also provide some demos to make use of the GPU.

To use OpenCL you will need to use one of the following platforms to make sure the drivers are present.

board Linux Kernel (BSP) OS
Mali G52-MP4 - Bifrost 2nd gen
Ubuntu 22.04
Mali G31-MP2 - Bifrost 1st gen
Ubuntu 22.04
Mali G52-MP8 - Bifrost 2nd gen
Ubuntu 22.04
Mali G610-MP4 - Valhall 3rd gen
5.10 Ubuntu 22.04

Check OpenCL capabilities and details

$ clinfo

VIM3 has OpenCL capabilities for both NPU and GPU, and by default the OpenCL lib in /usr/lib is for NPU. To use the GPU for proper acceleration follow the below steps to replace the correct library for the GPU.

# Move the NPU OpenCL lib
$ sudo mv /usr/lib/ /usr/lib/
# Symlink the right OpenCL lib for Mali GPU
$ sudo ln -s /usr/lib/aarch64-linux-gnu/ /usr/lib/

Note: With the 4.9 kernel, there is only OpenCL 2.0 capability and PyOpenCL will break, migrating to 5.15 kernel images will resolve it.

Get source code

Clone the examples sravansenthiln1/opencl-demos

$ git clone
$ cd opencl-demos

The example demos consist of C++ and Python examples to try.

Setup the necessary headers and packages

Install the OpenCL headers

$ sudo apt install opencl-headers opencl-clhpp-headers

Install the Python OpenCL library

$ sudo apt install python3-pip
$ pip3 install numpy pyopencl

Run Examples

Taking the Neural Network examples in C++

Enter the example directory

$ cd c++/neural_network

Compile the application

$ make

Run the application

$ ./main

Taking the Neural Network examples in Python

Enter the example directory

$ cd python/neural_network

Run the application

$ python3

Improving OpenCL performance

Memory optimizations

Mali GPUs share the same memory as the rest of the system, copying into new Cl buffers can waste memory, You can optimize this by copying the specifying CL_MEM_ALLOC_HOST_PTR in your allocation, and using the host_ptr attribute to specify the system memory.

This way you can use the same memory buffers for the system and OpenCL.

Increasing device operation frequency

To make sure you are getting the most performance from the CPU and the GPU together, you can force the maximum operating frequency.

Forcing the system to operate at maximum frequency will require the device to have adequate cooling, Leaving it running without maintained temperature can reduce the board life span.

On VIM3/3L/4:

$ echo 2 | sudo tee /sys/class/mpugpu/scale_mode

On Edge2:

$ echo performance | sudo tee /sys/class/devfreq/fb000000.gpu/governor

Further guides to optimize on Mali GPUs

2023/10/26 02:58 · sravan
Last modified: 2023/12/26 03:08 by sravan