Android studio uses opencl library (realme mobile phone)

Alas, after trying for many days, experiencing various error reports, searching for several days, and solving for five minutes, I can finally use opencl on Android Studio ! ! ! !

1. Check whether your mobile phone supports opencl, and download the opencl-z software

Link: https://pan.baidu.com/s/16irhsWaBLGXjy96zPCa7MQ

Extraction code: 1233

2. Start the operation

       1. Download platform-tools first, so that you can use the adb command.

       2. Enter the mobile phone with the adb shell command (the mobile phone has been connected and is in usb debugging mode)

       3. Enter cd /system/vendor/lib to see if there is libopencl.lib

       4. Pull the opencl.so file to the computer (anywhere), and put it in the specified directory (android studio)

adb pull /system/vendor/lib/libOpenCL.so d:/opencl

        5. Compile and run

                1.cmakelist.txt add library files

add_library(

        OpenCL

        SHARED

        IMPORTED)

set_target_properties(OpenCL PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../jinLibs/${ANDROID_ABI}/libOpenCL.so)



target_link_libraries(

        myopencldemo

        OpenCL

        ${log-lib})

                2. Add content to build.gradle under app

externalNativeBuild {

   cmake {

        abiFilters 'arm64-v8a'

    }

}

ndk{

     abiFilters 'arm64-v8a' // 指定编译

 }

If you do not specify the compilation, it seems that there will be an error error adding symbols: File in wrong format clang++.exe: error: linker command failed with exit code 1

                3. Use OpenCL in native-lib.cpp

//添加头文件

#include <android/log.h>

#include <android/native_window_jni.h>

#include <CL/cl.h>

#include <CL/cl_platform.h>

#define LOGD(...) __android_log_print(ANDROID_LOG_INFO,"David",__VA_ARGS__)

 //添加代码

    cl_platform_id *platforms;//查询后获得的平台列表,存放所有平台的ID

    cl_uint num_platforms;//当前可查询的平台的数量



    jint buffer;//创建一个buffer用以缓存平台数量,并传递;

    clGetPlatformIDs(0, NULL, &num_platforms);

    //当第二个参数为NULL时,函数将会查询当前可用平台的数量,并保存在第三个参数;

    platforms = new cl_platform_id[num_platforms];//查询后获得的平台列表,存放所有平台的ID

    clGetPlatformIDs(num_platforms ,platforms ,NULL);

    //获取平台数量后可以查询平台,将查询到的平台ID保存在第二个列表参数中

    buffer = num_platforms;

    LOGD("平台数:%d\n",buffer);

    LOGD("平台ID:%d\n",platforms);

LOGD("=============================================\n");

               4. That must be wrong. . . . , missing library files

          Introduced, and found that the library file is missing again. . . .

               So, let's see what files are missing. . . . . Under the linux system, the objdump command can be used to view

objdump -x libOpenCL.so | grep NEEDED

I finally found it all, happy, it should be stable this time! ! ! ! !

(Depending on the mobile phone, the required library may be different)

        6. Troubleshooting

       1、Fatal signal 4 (SIGILL), code 1 (ILL_ILLOPC), fault addr 0xc5614e64 in tid 22126

       One mistake, one glass of water, and half a day passes. This problem is said to be because of several situations.

The first null pointer problem; the second function has a return value, but it does not return at the end.

But, I haven't solved it, and I don't have this problem. It's such a simple program, isn't it. . .

        2. After a short operation, look at the logcat (terminal window adb logcat | findstr "program name"), and found these problems.

Access denied finding property "ro.odm.prev.product.name"

/Zygote: Unable to open libbeluga.so: dlopen failed: library "libbeluga.so"

I thought it was a permission problem, so I started to root the phone, and it took another night. Fortunately, it was solved, and the rooting was successful. I don’t understand, you can see

Realme x brush has root permission_czhunian's blog-CSDN blog

        3. After brushing, I feel that it is OK again. After compiling and running, well, there is a problem again. . . .

       UnsatisfiedLinkError: dlopen failed: cannot locate symbol "_Unwind_Resume"

  找到一个命令readelf -sW libnative-lib.so| grep _Unwind
  The  UND in the  Ndx column  means  Undefined symbol
   reference link: https://github.com/google/oboe/issues/966         and then? ? ? It's been half a day again, alas

        7. Give up at the end, it is impossible, create a new project and start from scratch

Here comes a new problem: A/libc: Fatal signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0xe1f7fb

Although it is not very similar to the first problem above, but after searching around, it seems that the cause is the same, that is, a null pointer exception, no return value or something, and it is an unresolved problem. . . , but learned a new knowledge

addr2line -f -e libnative-lib.so 0xe1f7fb90, check which function appears! ! !

-e output error code line number and file path -f output function name

addr2line under ndk

       

 But the question arises again, what is the output? ? ? , well, hopeless. . .

Search it up and find a nice solution, take a look at

The output of addr2line is ?: 0 possible reasons_qq_23101811's blog-CSDN blog_add2line display???

But life and death can't be decided, it's too good. . . .

Said to be compiled into debug, cmakelist.txt add command, add_definitions("-Wall -g")

But it still doesn't work, so it's said that it's broken, and I'm tired of doing it. . .

        8. Here we go again, create a new project and change your mobile phone

               It was found that the operation was successful, wc, Xida Puben, and the number of platforms was 0, md, why. . . .

        1. Let’s see why the previous one didn’t work. The current project compiles and runs, and it passes directly. After a comparison, it is found that

          

 The dynamic library cannot be introduced here, hey, why, obviously the dynamic library needs to be loaded, otherwise how can I find it! Wc, thinking about it later, it might be because the mobile phone has a default execution environment. She went to look for it by herself, but the previous one that she loaded by herself would cause errors.

        2. At first glance, the clGetPlatformIDs function returns CL_INVALID_VALUE (-30)

After starting various operations, the error is that the parameter is illegal, but it is simple, so it may be illegal. . .

depressed all night

        3. The Long March is over, and I am really happy! ! ! !

Change the operation search, ask Android studio to use opencl, found a problem, alas

Using OpenCL_gaussrieman123's Blog on Android Studio - CSDN Blog_opencl Android

Yes, because Google does not publicly support opencl , so there is no libOpenCL.so in the NDK. This library is placed in the library of each manufacturer. The more common location is "system/vendor/lib/libOpenCL.so ", pull out all the required dynamic libraries and load them as app private libraries, which can solve the compilation problem, but when using clGetPlatforms , no available platform will be found ; use stub to dynamically load libOpenCL.so , libopencl.c completes the work of adapting the platform and encapsulating the interface, mainly using dlopen and dlsym , the former is used to load the CL dynamic library supported by the vendor , and the latter is used to map the interface; here, the stub is directly compiled into a static library for use That's it.

The simple opencldemo, which took almost a week, can finally run successfully. I am worthy of the past few days, alas.

Code: GitHub - czhunian/opencldemo at master

Guess you like

Origin blog.csdn.net/qq_38295645/article/details/127602666