Using OpenCL in the new Android Studio

I am trying to write a short application that uses OpenCL 1.2 (not for distribution, so universal access is not an issue) for computation. However, OpenCL is giving me a bit of trouble with Android. I have a libopencl.so file and 1.2 headers on my computer from the AMD SDK. The question is how to use them correctly?

Where in the gradle build file (newest version) do I specify this, or can someone recommend me a good place to go to reading? Or, what do I need to understand theoretically, so that I can figure it out myself - what is needed to correctly reference the OpenCL libraries?

I already tried the answer mentioned here: Android Studio fatal error: CL / cl.h No such file or directory

apply plugin: 'com.android.model.application'

model {
    android {
        compileSdkVersion = 21
        buildToolsVersion ="22.0.1"

        defaultConfig.with {
            applicationId = "com.example.thing"
            minSdkVersion.apiLevel = 14
            targetSdkVersion.apiLevel = 14
        }
    }
    /*
     * native build settings
     */
    android.ndk {
        moduleName = "otherThing"

        ldLibs += "log"
        ldLibs += "android"
        //cFlags "~/OpenCL1-2HeaderFiles"
    }
    android.buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles  += file('proguard-rules.txt')
        }
    }

    android.productFlavors {
        create("arm") {
            ndk.abiFilters += "armeabi"
        }
        create("arm7") {
            ndk.abiFilters += "armeabi-v7a"
        }
        create("arm8") {
            ndk.abiFilters += "arm64-v8a"
        }
        create("x86") {
            ndk.abiFilters += "x86"
        }
        create("x86-64") {
            ndk.abiFilters += "x86_64"
        }
        create("mips") {
            ndk.abiFilters += "mips"
        }
        create("mips-64") {
            ndk.abiFilters += "mips64"
        }
        create("all")
    }
}

      

thank

+3


source to share


2 answers


This is a rather complicated process. I'll try to keep it short.

  • To run the app on Android, AMD's libopencl.so will not work. The OpenCL library must be compatible with your device. For android armv7 device you need the OpenCL library provided by the chip vendor and compiled with armv7 toolchain. To run on an Intel Atom device, you need the library provided by the chip vendor and compiled with targeting x86_32 arch.

Basically, for your question, you have two paths:

[Method 1] grab libOpenCL.so or libGLES_mali.so or libPVROCL.so from your devices. The above lib names correspond to Adreno, Mali and PowerVR GPUs. Use these libraries to compile your code.

[Method 2] . In your native code, open the OpenCL library with dlopen, then use dlsym to map the clBlaBla API to your own name. For example, you can map clGetDeviceInfo to myCLGetDeviceInfo. Then, in your own code, you can call myClGetDeviceInfo instead. Thus, you need to know the path to the opencl library on your devices. For example, on Adreno GPUs this would be /system/vendor/lib/libOpenCL.so. For more information, read another post. Does Android support OpenCL?



Method 2 is more general and elegant. Method 1 is easier and faster if you know exactly what devices your application will run on. For example, if you clearly know that your application is only for testing on adreno gpus, you can simply grab libOpenCL.so from your test device. Then use it to link your application. This link shows you how to do this and provides sample code. You can start from there.

In terms of using android studio. I can only comment on this: if you understand the need to compile an OpenCL application on Android. Compiling using android studio or directly using ndk-build makes no difference. Syntax can be matched one-to-one from one to the other.

For my personal experience, I've written a lot of Android.mk files and I'm used to it. So even though I am now using Android Studio for development. The method in the article you linked is actually correct.

However, I still use ndk-build to compile my own code as usual. This way I can have complete control over what happened there and I feel more comfortable. After I got the native .so library, I put it in the jniLibs folder under src / main (your folder structure may be different). jniLibs is the default folder where android studio will find native libraries. You can also specify your own folder in the gradle script (tons of articles online, search for gradle ndk). Of course, all of the above operations are done with a script (ndk-build, copy the library to some folder). Once I have it all, I create an app using android studio. Generally, you will not be debugging JAVA code and modifying native code at the same time,therefore, doing the above is completely okay and quite comfortable. If you want to automate the whole thing, you can ask gradle to run the ndk build script you wrote before it builds the app. Or you can ask gradle to execute the ndk-build command however you like (this is actually used by many developers).

Hope this is helpful. Any further discussion is appreciated.

+3


source


Thank you so much for your answer, Robert! I needed something to work, so what I ended up was going through the device it installed by checking

static const char *default_so_paths[] = { // Android
                                            "/system/lib/libOpenCL.so", "/system/vendor/lib/libOpenCL.so",
                                            "/system/vendor/lib/egl/libGLES_mali.so",
                                            "/system/vendor/lib/libPVROCL.so",
                                            "/data/data/org.pocl.libs/files/lib/libpocl.so",
                                          // Linux
                                            "/usr/lib/libOpenCL.so",            "/usr/local/lib/libOpenCL.so",
                                            "/usr/local/lib/libpocl.so",
                                            "/usr/lib64/libOpenCL.so", "/usr/lib32/libOpenCL.so",
                                            "libOpenCL.so"
                                          };

      



And then a mapping of the APIs that I think I should have seen how to do this sometime.

I finally got it working, but I wanted to thank you for this comprehensive answer and, well, most of your answers on this forum on this topic. They were all very helpful.

+2


source







All Articles