A modular, portable OpenCL C++ project supporting Linux, macOS, Windows, and Android. It features runtime-loadable OpenCL kernels, clean host/kernel separation, and optional auto-download of OpenCL headers.
.
├── CMakeLists.txt # Project build script
├── toolchain\_android.cmake # Android toolchain file
├── include/ # Project headers
├── src/ # Host C++ code (main.cpp, platform\_model.cpp, etc.)
├── kernels/ # OpenCL .cl kernels
├── opencl\_headers/ # OpenCL headers (auto-cloned if missing)
├── libs/ # Android native libs (contains libOpenCL.so)
├── build/ # Build output
- Cross-platform OpenCL C++ host code
- Modular, CMake-based build system
- Auto-cloning of Khronos OpenCL-Headers (optional)
- Android NDK toolchain support
- Platform-aware messages and diagnostics
- CMake ≥ 3.10
- Git (only if using auto-clone)
- OpenCL SDK / runtime (platform-dependent)
- Android only:
- Android NDK
libOpenCL.sopulled from device if not locally present
mkdir build
cd build
cmake .. -DDEBUG=ON
makemacOS uses
-framework OpenCL, Linux/Windows use-lOpenCL.
mkdir -p build/android
cd build/android
cmake ../.. \
-DCMAKE_TOOLCHAIN_FILE=../../toolchain_android.cmake \
-DANDROID_ABI=arm64-v8a \
-DANDROID_PLATFORM=android-24 \
-DENABLE_AUTO_CLONE_OPENCL_HEADERS=ON \
-DDEBUG=ON
make
⚠️ Make surelibs/arm64-v8a/libOpenCL.soexists. If not, pull it from a connected Android device using:
adb pull /data/local/tmp/libOpenCL.so libs/arm64-v8a/libOpenCL.so./opencl_app kernels/vector_add.clThis executes a vector addition example where:
- A = [2, 2, ..., 2]
- B = [3, 3, ..., 3]
- C = A + B
[INFO] Input vector A: 2 2 2 2 2 2 2 2 2 2
[INFO] Input vector B: 3 3 3 3 3 3 3 3 3 3
[INFO] Reading back results from device...
[RESULT] C = 5 5 5 5 5 5 5 5 5 5
[INFO] Cleaning up OpenCL resources...
[INFO] Execution complete.
| Option | Description |
|---|---|
-DOPENCL_HEADERS_DIR=/path |
Set path to OpenCL headers (should contain CL/cl.h) |
-DENABLE_AUTO_CLONE_OPENCL_HEADERS=ON |
Clone OpenCL-Headers from Khronos if not present |
-DDEBUG=ON |
Enable debug messages in both CMake and C++ runtime |
-
Missing OpenCL headers
- Set
-DOPENCL_HEADERS_DIR=/your/path - Or use
-DENABLE_AUTO_CLONE_OPENCL_HEADERS=ON
- Set
-
Missing
libOpenCL.soon Android-
Pull from a real Android device:
adb pull /data/local/tmp/libOpenCL.so libs/arm64-v8a/libOpenCL.so
-
Ensure your Android device is connected and USB debugging is enabled.
-
-
OpenCL runtime not found
- On Linux, ensure
libOpenCL.sois available via driver or SDK. - On macOS, no extra install is needed (uses system framework).
- On Linux, ensure
Licensed under the MIT License. See the LICENSE file.
---
Would you like me to also generate a `README.android.md` or embed commands for auto-checking `.so` availability with fallback in the script?