高通平台下安卓opencl小例子

http://blog.csdn.net/wcj0626/article/details/26272019

先到高通的qdn下载adreno GPU SDK,里面有OpenCL的例子。
https://developer.qualcomm.com/software/adreno-gpu-sdk

例子在以下目录:
AdrenoSDK\Development\Samples\OpenCL

1、从高通官网下载Adreon SDK(需要注册为会员)
2、解压以后,把文件夹Development/Inc/内的CL文件夹拷贝到:Android-ndk-r9d/platforms/android-19/arch-arm/usr/include/(请自行选择拷贝到哪个版本下)
3、连接手机到电脑,在终端运行:adb pull /system/vendor/lib/libOpenCL.so,此时会把手机里的libOpenCL.so拷贝到电脑当前文件夹下,把该文件拷贝到:android-ndk-r9d/platforms/android-19/arch-arm/usr/lib/(请自行选择拷贝到哪个版本下)
4、新建安卓工程,NDK开发。不知道NDK怎么弄,请移步这里
5、Java代码:
【高通平台下安卓opencl小例子】
[java]view plain copy

  1. public class MainActivity extends ActionBarActivity {
  2. static
  3. {
  4. System.loadLibrary("ocl");
  5. }
  6. public native Stringtestopencl();
  7. public native String getPlatformName();
  8. public native String getDeviceName();
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. TextView testView2=(TextView)findViewById(R.id.textView2);
  14. TextView testView4=(TextView)findViewById(R.id.textView4);
  15. TextView testView6=(TextView)findViewById(R.id.textView6);
  16. testView6.setText(testopencl());
  17. testView2.setText(getPlatformName());
  18. testView4.setText(getDeviceName());
  19. }
  20. }

6、NDK代码:ocl.cpp文件


[cpp]view plain copy
  1. #include
  2. #include
  3. #include
  4. #include
  5. #include
  6. #include"com_example_ocl_MainActivity.h"
  7. #define LEN(arr) sizeof(arr) / sizeof(arr[0])
  8. #define N 1024
  9. #define NUM_THREAD 128
  10. cl_uint num_device;
  11. cl_uint num_platform;
  12. cl_platform_id *platform;
  13. cl_device_id *devices;
  14. cl_int err;
  15. cl_context context;
  16. cl_command_queue cmdQueue;
  17. cl_mem buffer,sum_buffer;
  18. cl_program program ;
  19. cl_kernel kernel;
  20. const char* src[] = {
  21. "__kernel void redution(\n"
  22. "__global int *data,\n"
  23. "__global int *output,\n"
  24. "__local int *data_local\n"
  25. ")\n"
  26. " {\n"
  27. "int gid=get_group_id(0); \n"
  28. "int tid=get_global_id(0); \n"
  29. "int size=get_local_size(0); \n"
  30. "int id=get_local_id(0); \n"
  31. "data_local[id]=data[tid]; \n"
  32. "barrier(CLK_LOCAL_MEM_FENCE); \n"
  33. "for(int i=size/2; i>0; i>>=1){\n"
  34. "if(id
  35. "data_local[id]+=data_local[id+i]; \n"
  36. "}\n"
  37. "barrier(CLK_LOCAL_MEM_FENCE); \n"
  38. "}\n"
  39. "if(id==0){\n"
  40. "output[gid]=data_local[0]; \n"
  41. "}\n"
  42. " }\n"
  43. };
  44. int num_block;
  45. inttest()
  46. {
  47. int* in,*out;
  48. num_block=N/NUM_THREAD;
  49. in=(int*)malloc(sizeof(int)*N);
  50. out=(int*)malloc(sizeof(int)*num_block);
  51. for(int i=0; i
  52. in[i]=1;
  53. }
  54. Init_OpenCL();
  55. Context_cmd();
  56. Create_Buffer(in);
  57. Create_program();
  58. Set_arg();
  59. Execution();
  60. CopyOutResult(out);
  61. int sum=0;
  62. for(int i=0; i
  63. sum+=out[i];
  64. }
  65. return sum;
  66. }
  67. JNIEXPORT jstring JNICALL Java_com_example_ocl_MainActivity_testopencl (JNIEnv * env, jobject thisobject)
  68. {
  69. char result[10];
  70. sprintf(result,"%d\n",test());
  71. return env->NewStringUTF(result);
  72. }
  73. JNIEXPORT jstring JNICALL Java_com_example_ocl_MainActivity_getPlatformName(JNIEnv *env , jobject thisobject)
  74. {
  75. char buffer[1024];
  76. clGetPlatformInfo(platform[0],CL_PLATFORM_NAME,sizeof(buffer),buffer,NULL);
  77. return env->NewStringUTF(buffer);
  78. }
  79. JNIEXPORT jstring JNICALL Java_com_example_ocl_MainActivity_getDeviceName(JNIEnv *env , jobject thisobject)
  80. {
  81. char buffer[1024];
  82. clGetDeviceInfo(devices[0],CL_DEVICE_NAME,sizeof(buffer),buffer,NULL);
  83. return env->NewStringUTF(buffer);
  84. }
  85. void Init_OpenCL()
  86. {
  87. size_t nameLen1;
  88. char platformName[1024];
  89. err = clGetPlatformIDs(0, 0, &num_platform);
  90. platform=(cl_platform_id*)malloc(sizeof(cl_platform_id)*num_platform);
  91. err = clGetPlatformIDs(num_platform, platform, NULL);
  92. err=clGetDeviceIDs(platform[0],CL_DEVICE_TYPE_GPU,0,NULL,&num_device);
  93. devices=(cl_device_id*)malloc(sizeof(cl_device_id)*num_device);
  94. err=clGetDeviceIDs(platform[0],CL_DEVICE_TYPE_GPU,num_device,devices,NULL);
  95. }
  96. void Context_cmd()
  97. {
  98. context=clCreateContext(NULL,num_device,devices,NULL,NULL,&err);
  99. cmdQueue=clCreateCommandQueue(context,devices[0],0,&err);
  100. }
  101. void Create_Buffer(int *data)
  102. {
  103. buffer=clCreateBuffer(context,CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR,sizeof(int)*N,data,&err);
  104. sum_buffer=clCreateBuffer(context,CL_MEM_WRITE_ONLY,sizeof(int)*num_block,0,&err);
  105. }
  106. void Create_program()
  107. {
  108. program=clCreateProgramWithSource(context, LEN(src), src, NULL, NULL);
  109. err=clBuildProgram(program,num_device,devices,NULL,NULL,NULL);
  110. kernel = clCreateKernel(program, "redution", NULL);
  111. }
  112. void Set_arg()
  113. {
  114. err=clSetKernelArg(kernel,0,sizeof(cl_mem),&buffer);
  115. err=clSetKernelArg(kernel,1,sizeof(cl_mem),&sum_buffer);
  116. err=clSetKernelArg(kernel,2,sizeof(int)*NUM_THREAD,NULL);
  117. }
  118. void Execution()
  119. {
  120. const size_t globalWorkSize[1]={N};
  121. const size_t localWorkSize[1]={NUM_THREAD};
  122. err=clEnqueueNDRangeKernel(cmdQueue,kernel,1,NULL,globalWorkSize,localWorkSize,0,NULL,NULL);
  123. clFinish(cmdQueue);
  124. }
  125. void CopyOutResult(int*out)
  126. {
  127. err=clEnqueueReadBuffer(cmdQueue,sum_buffer,CL_TRUE,0,sizeof(int)*num_block,out,0,NULL,NULL);
  128. }

7、Android.mk代码:


[plain]view plain copy
  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)
  3. LOCAL_MODULE:= ocl
  4. LOCAL_SRC_FILES := ocl.cpp
  5. LOCAL_LDFLAGS += -llog-lOpenCL
  6. include $(BUILD_SHARED_LIBRARY)

8、执行结果:

高通平台下安卓opencl小例子
文章图片

搞定!!!

    推荐阅读