View
309
Download
11
Embed Size (px)
Citation preview
©SIProp Project, 2006-2008 1
How to Build & Use OpenCL on Android Studio
Noritsuna [email protected]
©SIProp Project, 2006-2008 2
Goal
Making OpenCL programming Environment on Android Studio
Android Studio 1.3 or laterC Language (Android NDK)OpenCL 1.1 (ARM-Mali)
©SIProp Project, 2006-2008 3
Why?
OpenCL source code is separated “host source code” as C Language file & “kernel(device) source code” as CL file.
But Android’s APK can NOT include “kernel(device) source code” as CL file in APK file.
SolutionConvert CL file to const char* in Single C header file
©SIProp Project, 2006-2008 4
Setup NDK on Android Studio1.3
RequitedGradle2.5 or later
AS1.3’s Gradel is 2.4. MUST update 2.4 -> 2.5 .
Setup NDKSettings->System Setting->Android SDK
Modify “build.gradle”http://tools.android.com/tech-docs/new-build-system/gradle-experimental
InformationsAndroid NDK Support for Android Studio
http://tools.android.com/tech-docs/android-ndk-preview
NDK Sampleshttps://github.com/googlesamples/android-ndk
©SIProp Project, 2006-2008 5
Setup OpenCL on Android Studio1.3
Setup Header files of OpenCLDownload from ARM-Mali’s OpenCL Dev Site
http://malideveloper.arm.com/resources/sdks/mali-opencl-sdk/
Copy “[ARM-Mali dir]/include/CL/*.h” to “[your project dir]/app/main/src/jni/CL/”
Load Shared Lib of OpenCLCopy “libOpenCL.so” from your Android Device
to “[your Android Studio Dir]/sdk/ndk-bundle/platforms/android-[your target system]/arch-arm/usr/lib/”
©SIProp Project, 2006-2008 6
Setup OpenCL on Android Studio1.3
Setup Header files & Shared Lib on Android Studio
Add “Cflags” & “ldLibs” in “build.gradle”
1. build.gradle : app, android.ndk section
1. android.ndk {2. moduleName = "openclsample-jni"3. CFlags += "-I${file("src/main/jni/CL")}".toString()4. ldLibs = ["android", "log", "OpenCL"]5. (snip)
©SIProp Project, 2006-2008 7
Setup Plugin “OpenCL CL Generator”
Install Plugin to Android StudioSelect “Setting->plugins”Click “Browse Repositories”Search Plugin by “OpenCL” as keyword
Target Plugin URLhttps://plugins.jetbrains.com/plugin/7888?pr=androidstudio
Click “Install Plugin”Restart Android Studio
©SIProp Project, 2006-2008 8
Use Plugin “OpenCL CL Generator”
Click “Tools->Generate OpenCL’s CL files header”
©SIProp Project, 2006-2008 9
How to Use Plugin: Host Side
Host side programming: standard OpenCL
CL file(kernel side source code) is loaded as “file”.
1. #include “opencl_cl_files.h”2. (snip)3. /* Create Kernel Program from the source */4. program = clCreateProgramWithSource(context, 1, (const char **)&CLCL_HELLO,
(const size_t *)&strlen(CLCL_HELLO), &ret);
1. FILE *fp;2. char fileName[] = "./hello.cl";3. char *source_str;4. size_t source_size;5. fp = fopen(fileName, "r");6. source_str = (char*)malloc(MAX_SOURCE_SIZE);7. source_size = fread( source_str, 1, MAX_SOURCE_SIZE, fp);8. fclose( fp );9. (snip)10. /* Create Kernel Program from the source */11. program = clCreateProgramWithSource(context, 1, (const char **)&source_str, (const
size_t *)&source_size, &ret);
Host side programming: with Android Plugin
CL file is loaded as “const char*”.
©SIProp Project, 2006-2008 10
How to Use Plugin: Kernel Side
1. Converted Original File: opencl_cl_files.h
1. const char *CLCL_HELLO = 2. "#pragma OPENCL EXTENSION
cl_khr_byte_addressable_store : enable\n"3. "\n"4. "__kernel void hello(__global char*
string)\n"5. "{\n"6. " string[0] = 'H';\n"7. " string[1] = 'e';\n"8. " string[2] = 'l';\n"9. " string[3] = 'l';\n"10. " string[4] = 'o';\n"11. " string[5] = '\\0';\n"12. "}\n"13. "";
1. Kernel Source Code File: hello.cl
1. #pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable
2. __kernel void hello(__global char* string)3. {4. string[0] = 'H';5. string[1] = 'e';6. string[2] = 'l';7. string[3] = 'l';8. string[4] = 'o';9. string[5] = '\0';10. }
Kernel side programming1. Write Source Code as CL file(Kernel Source
Code)2. Convert CL file(Kernel Source Code) to
Original Header File as “opencl_cl_files.h”
©SIProp Project, 2006-2008 11
Setup libOpenCL.so on Android
If you(or your Android Device) don’t have libOpenCL.so in “/system/lib” or “/system/vendor/lib”, you can download libOpenCL.so.
Support GPUsadreno-3xx: Qualcomm's Adreno 3xx GPUsmali-t6xx: ARM's Mali-T6xx GPUs
Download from: https://github.com/madeye/opencl-android-proprietary
SetupCopy All SO files to “/system/lib” or “/system/vendor/lib” on your Android Device.
©SIProp Project, 2006-2008 13
Download the Sample Project for AS
The Sample Project for AS that I use in this slide.
https://github.com/noritsuna/OpenCLCLGenerator_Sample
Features“build.gradle” for NDK SupportSetup PluginOpenCL Headers & SO libThe Sample Source Code for OpenCL