Toybrick

标题: 3399pro 中的opencl 怎么使用 [打印本页]

作者: keguo    时间: 2019-12-26 10:22
标题: 3399pro 中的opencl 怎么使用
从某个教程里面copy 了一个例子
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <alloca.h>

  5. #ifdef APPLE
  6. #include <OpenCL/cl.h>
  7. #else
  8. #include <CL/cl.h>
  9. #endif

  10. void loadProgramSource(const char** files,
  11.                        size_t length,
  12.                        char** buffer,
  13.                        size_t* sizes) {
  14.            /* Read each source file (*.cl) and store the contents into a temporary datastore */
  15.            for(size_t i=0; i < length; i++) {
  16.               FILE* file = fopen(files[i], "r");
  17.               if(file == NULL) {
  18.                  perror("Couldn't read the program file");
  19.                  exit(1);   
  20.               }
  21.               fseek(file, 0, SEEK_END);
  22.               sizes[i] = ftell(file);
  23.               rewind(file); // reset the file pointer so that 'fread' reads from the front
  24.               buffer[i] = (char*)malloc(sizes[i]+1);
  25.               buffer[i][sizes[i]] = '\0';
  26.               fread(buffer[i], sizeof(char), sizes[i], file);
  27.               fclose(file);
  28.            }
  29. }

  30. int main(int argc, char** argv) {

  31.    /* OpenCL 1.1 data structures */
  32.    cl_platform_id* platforms;
  33.    cl_program program;
  34.    cl_device_id device;
  35.    cl_context context;

  36.    /* OpenCL 1.1 scalar data types */
  37.    cl_uint numOfPlatforms;
  38.    cl_int  error;

  39.    /*
  40.       Get the number of platforms
  41.       Remember that for each vendor's SDK installed on the computer,
  42.       the number of available platform also increased.
  43.     */
  44.    error = clGetPlatformIDs(0, NULL, &numOfPlatforms);
  45.    if(error != CL_SUCCESS) {                       
  46.       perror("Unable to find any OpenCL platforms");
  47.       exit(1);
  48.    }

  49.    platforms = (cl_platform_id*) alloca(sizeof(cl_platform_id) * numOfPlatforms);
  50.    printf("Number of OpenCL platforms found: %d\n", numOfPlatforms);

  51.    error = clGetPlatformIDs(numOfPlatforms, platforms, NULL);
  52.    if(error != CL_SUCCESS) {                       
  53.       perror("Unable to find any OpenCL platforms");
  54.       exit(1);
  55.    }
  56.    // Search for a CPU/GPU device through the installed platforms
  57.    // Build a OpenCL program and do not run it.
  58.    for(cl_uint i = 0; i < numOfPlatforms; i++ ) {
  59.        // Get the GPU device
  60.        error = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_GPU, 1, &device, NULL);
  61.        if(error != CL_SUCCESS) {
  62.           // Otherwise, get the CPU
  63.           error = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_CPU, 1, &device, NULL);
  64.        }
  65.         if(error != CL_SUCCESS) {
  66.             perror("Can't locate any OpenCL compliant device");
  67.             exit(1);
  68.         }
  69.         /* Create a context */
  70.         context = clCreateContext(NULL, 1, &device, NULL, NULL, &error);
  71.         if(error != CL_SUCCESS) {
  72.             perror("Can't create a valid OpenCL context");
  73.             exit(1);
  74.         }

  75.         /* Load the two source files into temporary datastores */
  76.         const char *file_names[] = {"simple.cl", "simple_2.cl"};
  77.         const int NUMBER_OF_FILES = 2;
  78.         char* buffer[NUMBER_OF_FILES];
  79.         size_t sizes[NUMBER_OF_FILES];
  80.         loadProgramSource(file_names, NUMBER_OF_FILES, buffer, sizes);

  81.         /* Create the OpenCL program object */
  82.         program = clCreateProgramWithSource(context, NUMBER_OF_FILES, (const char**)buffer, sizes, &error);                               
  83.             if(error != CL_SUCCESS) {
  84.               perror("Can't create the OpenCL program object");
  85.               exit(1);   
  86.             }
  87.         /* Build OpenCL program object and dump the error message, if any */
  88.         char *program_log;
  89.         const char options[] = "-cl-finite-math-only -cl-no-signed-zeros";  
  90.         size_t log_size;
  91.         printf("build-options:%s\n", argv[1]);
  92.         error = clBuildProgram(program, 1, &device, argv[1], NULL, NULL);               
  93.         // Uncomment the line below, comment the line above; re-build the program to use build options statically
  94.         // error = clBuildProgram(program, 1, &device, options, NULL, NULL);               
  95.             if(error != CL_SUCCESS) {
  96.               // If there's an error whilst building the program, dump the log
  97.               clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
  98.               program_log = (char*) malloc(log_size+1);
  99.               program_log[log_size] = '\0';
  100.               clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG,
  101.                     log_size+1, program_log, NULL);
  102.               printf("\n=== ERROR ===\n\n%s\n=============\n", program_log);
  103.               free(program_log);
  104.               exit(1);
  105.             }
  106.    
  107.         /* Clean up */
  108.         for(i=0; i< NUMBER_OF_FILES; i++) { free(buffer[i]); }
  109.         clReleaseProgram(program);
  110.         clReleaseContext(context);
  111.    }
  112. }
复制代码
编译指令如下
  1. gcc build_opencl_program.c -o build_opencl_program -lOpenCL
复制代码
运行错误
  1. ./build_opencl_program
  2. Number of OpenCL platforms found: 1
  3. Failed creating base context during opening of kernel driver.
  4. Kernel module may not have been loaded
  5. Failed creating base context during opening of kernel driver.
  6. Kernel module may not have been loaded
  7. Can't locate any OpenCL compliant device: Bad address
  8. enCL @localhost opencl]# ./build_opencl_program e
  9. Number of OpenCL platforms found: 1
  10. Failed creating base context during opening of kernel driver.
  11. Kernel module may not have been loaded
  12. Failed creating base context during opening of kernel driver.
  13. Kernel module may not have been loaded
  14. Can't locate any OpenCL compliant device: Bad address
复制代码
有相关OpenCL 在3399pro中的使用说明吗?
clinfo 打印出的信息如下
  1. [root@localhost opencl]# clinfo
  2. Number of platforms                               1
  3.   Platform Name                                   ARM Platform
  4.   Platform Vendor                                 ARM
  5.   Platform Version                                OpenCL 1.2 v1.r18p0-01rel0.42aa65ae5fdfdec464479cb891075220
  6.   Platform Profile                                FULL_PROFILE
  7. Failed creating base context during opening of kernel driver.
  8. Kernel module may not have been loaded
  9.   Platform Extensions                             <printPlatformInfo:4: get CL_PLATFORM_EXTENSIONS size : error -6>
  10. Failed creating base context during opening of kernel driver.
  11. Kernel module may not have been loaded
  12. printPlatformInfo:488: number of devices : error -6
复制代码





作者: keguo    时间: 2019-12-26 11:13
按照这里的说法 http://t.rock-chips.com/forum.php?mod=viewthread&tid=97
  1. sudo dnf clean all
  2. sudo dnf -y update
复制代码

还是查看不到GPU的型号, 连接 -lmali 时,没有找到这个库
作者: keguo    时间: 2019-12-26 14:34
按照其他的贴子,libOpenCL 也已经链接到了libmali.so.r18p0

  1. ls libOpenCL.so.1.0.0 -lh
  2. lrwxrwxrwx 1 root root 16 Feb 13  2019 libOpenCL.so.1.0.0 -> libmali.so.r18p0
复制代码

作者: keguo    时间: 2019-12-27 10:56
opencl 已经安装到最新
  1. dnf install ocl-icd.aarch64
  2. Last metadata expiration check: 0:00:00 ago on Thu 26 Dec 2019 09:54:50 PM EST.
  3. Package ocl-icd-2.2.12-3.rockchip.fc28.aarch64 is already installed, skipping.
  4. Dependencies resolved.
  5. Nothing to do.
  6. Complete!
复制代码

作者: jefferyzhang    时间: 2019-12-27 12:27
你要link ocl-icd.aarch64提供的so
不是自己瞎写。。。
不知道它提供了什么so的话,可以用命令查下这个包装了什么so进去
作者: keguo    时间: 2019-12-27 14:29
本帖最后由 keguo 于 2019-12-27 14:30 编辑

从这个链接依赖来看,好像没有问题,最终都是链接到 libmali.so.r18p0



作者: keguo    时间: 2019-12-27 14:30
标题: “ocl-icd.aarch64提供的so” 是哪一个so
本帖最后由 keguo 于 2019-12-27 14:34 编辑


作者: keguo    时间: 2019-12-28 19:14
有没有谁来回答下这个opencl 问题,到底是用哪一个库
作者: jefferyzhang    时间: 2019-12-30 08:31
既然最终都是连接到那个库,那就是那个libmali.so.r18p这个库啊?
还有什么问题么?
作者: keguo    时间: 2019-12-30 08:43
程序跑起来是由问题的啊,clinfo clinfo 找不到设备啊
作者: keguo    时间: 2019-12-30 09:24
帖子什么时候解决的,我怎么不知道
作者: keguo    时间: 2019-12-30 18:14
系统被动过了,重装一次系统clinfo 就能用了
作者: jefferyzhang    时间: 2019-12-31 08:20
keguo 发表于 2019-12-30 18:14
系统被动过了,重装一次系统clinfo 就能用了

额,那不需要我再写sample了吧?




欢迎光临 Toybrick (https://t.rock-chips.com/) Powered by Discuz! X3.3