NDK: 25.2
ubuntu: 20.04
device: Redmi K40s
1 安装NDK
从下面的链接下载NDK,并解压:
https://developer.android.google.cn/ndk/downloads/
记住解压后的路径
如果你是在Android Studio里面下载的ndk,那么安装路径可以参加下面
/home/peter/Android/Sdk/ndk/25.2.9519653
2 编写测试程序
mkdir opencl
cd opencl
touch main.cpp
gedit main.cpp
main.cpp
#include<iostream>
#include<CL/opencl.h>
using namespace std;
//根据平台ID得到平台名字
string getPlatformName(const cl_platform_id pid) {
size_t param_value_size;
clGetPlatformInfo(pid, CL_PLATFORM_NAME, 0, NULL, ¶m_value_size);
char *param_value = new char[param_value_size];
clGetPlatformInfo(pid, CL_PLATFORM_NAME, param_value_size, param_value, NULL);
return param_value;
}
int main()
{
cl_uint num_platforms;
clGetPlatformIDs(0, NULL, &num_platforms);
cl_platform_id* platforms = new cl_platform_id[num_platforms];
clGetPlatformIDs(num_platforms, platforms, NULL);
for (cl_uint i = 0; i < num_platforms; i++) {
string platname = getPlatformName(platforms[i]);
cout << "<" << i << "> " << "Platform name is :" << platname << endl;
}
cout << "hello" << endl;
return 0;
}
3 复制include头文件
我之前在ubuntu中安装了nvidia 的 cuda-driver,默认安装了opencl的头文件,可以在/usr/include
中找到CL文件夹,将其复制到项目目录下
mkdir include
cd include
cp /usr/include/CL .
4 复制lib库文件
由于linux的架构和Android手机的架构不同,所以linux x86的库文件并不使用于Android手机,所以我们需要从安卓手机上将库文件拷贝到linux中,才能进行交叉编译
Andrioid 的库文件一般位于 /system/lib
vendor/iib64
system/vendor/lib64
下
需要拷贝的库有
5 编写CMakeLists.txt
在opencl
文件夹下新建CMakeLists.txt
cmake_minimum_required(VERSION 2.8.3)
project(opencl)
set(CMAKE_CXX_STANDARD 11)
include_directories(include)
link_directories("/home/peter/temp/opencl/lib")
add_executable(main main.cpp)
target_link_libraries(main
OpenCL
cutils
log
utils
base
c++
processgroup
vndksupport
cgrouprc
dl_android)
6 编译运行
mkdir build
cd build
当前opencl
文件夹的组织如下
cmake .. -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake -DANDROID_ABI="arm64-v8a" -DANDROID_NATIVE_API_LEVEL=android-26
make -j4
adb push main /data/local/tmp
adb shell
cd /data/local/tmp
./main