OpenCL is not able to query a platform on an android device

Im using the Qualcomm OpenCL to optimize native C++ code in my android application. The linking in CMakeLists.txt is handled as follows:

add_library(opencl-lib SHARED IMPORTED)
set_target_properties( opencl-lib PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/libOpenCL.so)
target_link_libraries( demo opencl-lib )

My reproducible example:

#include <jni.h>
#include <string>
#include <iostream>
#include <vector>
#include <cmath>
#include <stdio.h>
#include <android/log.h>
#include "opencl-sdk/inc/CL/cl.h"
#include "opencl-sdk/inc/CL/opencl.h"

extern "C" JNIEXPORT jint JNICALL
Java_com_example_demo_MainActivity_getInt( JNIEnv* env, jobject /* this */) {
    cl_int err = 0;
    cl_uint num_platforms = 0;
    err = clGetPlatformIDs(0, NULL, &num_platforms);
    return 0;
}

The application crashes immediately after running it. I receive the following message in the Logcat:

libc, com.example.<app_name>, A Fatal signal 5 (SIGTRAP), code 1 (TRAP_BRKPT), fault addr 0x726e76a004 in tid 29952 (om.example.demo), pid 29952 (om.example.<app_name>)

It seems like OpenCL is not able to query a device at all.

When I comment everything in Java_com_example_demo_MainActivity_getInt() , I get a lot of type DEBUG logs, including

/data/app/~~6MgdG5XHBNcZSiF0nvJtPg==/com.example.demo-ZvGN17inQLVfZzeCRpFjJA==/base.apk!libOpenCL.so (clGetPlatformIDs+72) (BuildId: 4252b0a3fc7297cc93dfa7a31c208d1f)

Are there any work-arounds to this issue?

My guess is, that there is a problem with ld-android.so from /vendor/lib64/, which causes the app to crash at clGetPlatformIDs. I got the following log after the crash:

Fatal signal 5 (SIGTRAP), code 1 (TRAP_BRKPT), fault addr 0x726e6b1004 in tid 30364 (om.example.demo), pid 30364 (om.example.demo)
Cmdline: com.example.demo
pid: 30364, tid: 30364, name: om.example.demo  >>> com.example.demo <<<
#00 pc 0000000000001004  /data/app/~~bwptcAF2Mcxj1QrTS4VEAA==/com.example.demo-M6X_ed8wUlOuBtErB5lu1Q==/base.apk!ld-android.so (__loader_dlopen+4) (BuildId: daa6dd2d221674856cad38a0130d0b16)
#01 pc 00000000000010e0  /data/app/~~bwptcAF2Mcxj1QrTS4VEAA==/com.example.demo-M6X_ed8wUlOuBtErB5lu1Q==/base.apk!libdl_android.so (android_get_exported_namespace+12) (BuildId: a506d7cbefe663d77d99d39034bddf74)
#02 pc 0000000000001194  /data/app/~~bwptcAF2Mcxj1QrTS4VEAA==/com.example.demo-M6X_ed8wUlOuBtErB5lu1Q==/base.apk!libvndksupport.so (android_load_sphal_library+308) (BuildId: fa178c0474c2bbfb9380bc25fc0547ec)
#03 pc 0000000000021c70  /data/app/~~bwptcAF2Mcxj1QrTS4VEAA==/com.example.demo-M6X_ed8wUlOuBtErB5lu1Q==/base.apk!libOpenCL.so (clGetPlatformIDs+72) (BuildId: 4252b0a3fc7297cc93dfa7a31c208d1f)
#04 pc 0000000000005d30  /data/app/~~bwptcAF2Mcxj1QrTS4VEAA==/com.example.demo-M6X_ed8wUlOuBtErB5lu1Q==/base.apk!libdemo.so (Java_com_example_demo_MainActivity_getInt+32) (BuildId: a0b5246c34b8e042c29bfe224a864d949ab56d04)
#07 pc 0000000000000eac  /data/app/~~bwptcAF2Mcxj1QrTS4VEAA==/com.example.demo-M6X_ed8wUlOuBtErB5lu1Q==/base.apk (com.example.demo.MainActivity.onCreate$lambda$0+48)
#09 pc 0000000000000e1c  /data/app/~~bwptcAF2Mcxj1QrTS4VEAA==/com.example.demo-M6X_ed8wUlOuBtErB5lu1Q==/base.apk (com.example.demo.MainActivity.$r8$lambda$KC6AjQoJlmGj4vROYZUNl9WDJyU+0)
#11 pc 0000000000000dd4  /data/app/~~bwptcAF2Mcxj1QrTS4VEAA==/com.example.demo-M6X_ed8wUlOuBtErB5lu1Q==/base.apk (com.example.demo.MainActivity$$ExternalSyntheticLambda0.onClick+4)
#17 pc 00000000002e7432  /data/app/~~bwptcAF2Mcxj1QrTS4VEAA==/com.example.demo-M6X_ed8wUlOuBtErB5lu1Q==/base.apk (com.google.android.material.button.MaterialButton.performClick+22)

hi,

Try to compile with the 32 version. Some architecture do not have openCL 64.

Hi, thanks for your response. Im using a Samsung Tab S8 Ultra which has a 64-bit architecture. Using the 32 bit version won’t work here.

we can run 32 on 64 but not the inverse ;))

I receive ld: error: /Users/terdev/AndroidStudioProjects/demo/app/src/main/cpp/opencl-sdk/libOpenCL.so is incompatible with aarch64linux when using the 32-bit version of OpenCL.so

The general problem here is that ld-android is not able to load vendor drivers for OpenCL. I don’t know why this is the case, since OpenCL benchmarks work well on my device… I have contacted someone who works on GPU drivers at Qualcomm regarding this issue.

if the code you show is you C code. Why do you use extern “C”. This should be in the .h for JNI function declaration not in cpp code.

You declare the function that you call from java code in .h and the C code function implementation in .cpp

But if you have a libc error it means that at least one library is missing. You must copy it from your smartphonne to your android libs directorie.

it is the first time you try to run openCL on smartphonne ?