东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 1689|回复: 1
打印 上一主题 下一主题

[C/C++] ubuntu18.04上opencl环境搭建(Nvidia cuda都好了)

[复制链接]

1366

主题

1857

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14459
QQ
跳转到指定楼层
楼主
发表于 2021-7-27 11:03:39 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式


ubuntu18.04上opencl环境搭建(Nvidia cuda都好了)


我是先玩cuda的thrust后来研究一下opencl的,所以系统显卡的驱动与cuda都搞定了的!


第一步:安装clinfo工具查看显卡对OpenCL的支持情况:
sudo apt-get install clinfo
clinfo
  1. jiang@jiang-Ubuntu:~/jjj_opencl_works/build$ clinfo
  2. Number of platforms                               1
  3.   Platform Name                                   NVIDIA CUDA
  4.   Platform Vendor                                 NVIDIA Corporation
  5.   Platform Version                                OpenCL 1.2 CUDA 11.1.114
  6.   Platform Profile                                FULL_PROFILE
  7.   Platform Extensions                             cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_fp64 cl_khr_byte_addressable_store cl_khr_icd cl_khr_gl_sharing cl_nv_compiler_options cl_nv_device_attribute_query cl_nv_pragma_unroll cl_nv_copy_opts cl_nv_create_buffer cl_khr_int64_base_atomics cl_khr_int64_extended_atomics cl_khr_device_uuid
  8.   Platform Extensions function suffix             NV

  9.   Platform Name                                   NVIDIA CUDA
  10. Number of devices                                 1
  11.   Device Name                                     GeForce RTX 2070
  12.   Device Vendor                                   NVIDIA Corporation
  13.   Device Vendor ID                                0x10de
  14.   Device Version                                  OpenCL 1.2 CUDA
  15.   Driver Version                                  455.45.01
  16.   Device OpenCL C Version                         OpenCL C 1.2
  17.   Device Type                                     GPU
  18.   Device Topology (NV)                            PCI-E, 01:00.0
  19.   Device Profile                                  FULL_PROFILE
  20.   Device Available                                Yes
  21.   Compiler Available                              Yes
  22.   Linker Available                                Yes
  23.   Max compute units                               36
  24.   Max clock frequency                             1620MHz
  25.   Compute Capability (NV)                         7.5
  26.   Device Partition                                (core)
  27.     Max number of sub-devices                     1
  28.     Supported partition types                     None
  29.   Max work item dimensions                        3
  30.   Max work item sizes                             1024x1024x64
复制代码



第二步:安装开发包
sudo apt-get install ocl-icd-opencl-dev
jiang@jiang-Ubuntu:~/jjj_opencl_works/build$ sudo apt-get install ocl-icd-opencl-dev
正在读取软件包列表... 完成
正在分析软件包的依赖关系树      
正在读取状态信息... 完成      
下列软件包是自动安装的并且现在不需要了:
  linux-hwe-5.4-headers-5.4.0-72 linux-hwe-5.4-headers-5.4.0-73 linux-hwe-5.4-headers-5.4.0-74
  qml-module-qtquick-dialogs qml-module-qtquick-privatewidgets
使用'sudo apt autoremove'来卸载它(它们)。
将会同时安装下列软件:
  opencl-c-headers
建议安装:
  libpoclu-dev
下列【新】软件包将被安装:
  ocl-icd-opencl-dev opencl-c-headers
升级了 0 个软件包,新安装了 2 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。
需要下载 31.0 kB 的归档。
解压缩后会消耗 267 kB 的额外空间。
您希望继续执行吗? [Y/n]


第三步:
cmake 配置:
  1. CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
  2. PROJECT(main_cl)
  3. #FIND_PACKAGE(CUDA REQUIRED)
  4. FIND_PACKAGE(OpenCL REQUIRED)
  5. ADD_EXECUTABLE(main_cl cl获取支持的平台信息.c)
  6. #TARGET_LINK_LIBRARIES(main_cl)
  7. #target_link_libraries(main_cl -lboost_system -lboost_thread)
  8. target_link_libraries(main_cl OpenCL)
复制代码

  1. jiang@jiang-Ubuntu:~/jjj_opencl_works/build$ cmake ..
  2. CMake Deprecation Warning at CMakeLists.txt:1 (CMAKE_MINIMUM_REQUIRED):
  3.   Compatibility with CMake < 2.8.12 will be removed from a future version of
  4.   CMake.

  5.   Update the VERSION argument <min> value or use a ...<max> suffix to tell
  6.   CMake that the project does not need compatibility with older versions.


  7. -- The C compiler identification is GNU 8.4.0
  8. -- The CXX compiler identification is GNU 8.4.0
  9. -- Detecting C compiler ABI info
  10. -- Detecting C compiler ABI info - done
  11. -- Check for working C compiler: /usr/bin/cc - skipped
  12. -- Detecting C compile features
  13. -- Detecting C compile features - done
  14. -- Detecting CXX compiler ABI info
  15. -- Detecting CXX compiler ABI info - done
  16. -- Check for working CXX compiler: /usr/bin/c++ - skipped
  17. -- Detecting CXX compile features
  18. -- Detecting CXX compile features - done
  19. -- Looking for CL_VERSION_2_2
  20. -- Looking for CL_VERSION_2_2 - found
  21. -- Found OpenCL: /usr/lib/x86_64-linux-gnu/libOpenCL.so (found version "2.2")
  22. -- Configuring done
  23. -- Generating done
  24. -- Build files have been written to: /home/jiang/jjj_opencl_works/build
复制代码

-- Looking for CL_VERSION_2_2 - found
-- Found OpenCL: /usr/lib/x86_64-linux-gnu/libOpenCL.so (found version "2.2")



  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

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

  9. int main() {
  10.   /* Host data structures */
  11.   cl_platform_id *platforms;
  12.   //每一个cl_platform_id 结构表示一个在主机上的OpenCL执行平台,
  13.   //就是指电脑中支持OpenCL的硬件,如nvidia显卡,intel CPU和显卡,AMD显卡和CPU等
  14.   cl_uint num_platforms;

  15.   cl_int i, err, platform_index = -1;

  16.   /* Extension data */

  17.   char* ext_data;         

  18.   size_t ext_size;

  19.   const char icd_ext[] = "cl_khr_icd";

  20.   //要使platform工作,需要两个步骤。1 需要为cl_platform_id结构分配内存空间。
  21.   //2 需要调用clGetPlatformIDs初始化这些数据结构。一般还需要步骤0:询问主机上有多少platforms

  22.   /* Find number of platforms */

  23.   //返回值如果为-1就说明调用函数失败,如果为0标明成功
  24.   //第二个参数为NULL代表要咨询主机上有多少个platform,并使用num_platforms取得实际flatform数量。
  25.   //第一个参数为1,代表我们需要取最多1个platform。可以改为任意大如:INT_MAX整数最大值。
  26.   //但是据说0,否则会报错,实际测试好像不会报错。下面是步骤0:询问主机有多少platforms

  27.   err = clGetPlatformIDs(5, NULL, &num_platforms);    // 获取平台ids

  28.   if(err < 0) {
  29.     perror("调用函数clGetPlatformIDs失败Couldn't find any platforms.");   
  30.     exit(1);              
  31.   }                 

  32.   printf("我有opencl计算平台的数目: %d\n", num_platforms); //本人计算机上显示为2,有intel和nvidia两个平台

  33.   /* Access all installed platforms */

  34.   //步骤1 创建cl_platform_id,并分配空间   malloc分配的空间是 堆空间啊 还没到GPU上

  35.   platforms = (cl_platform_id*)malloc(sizeof(cl_platform_id) * num_platforms);

  36.   //步骤2 第二个参数用指针platforms存储platform  
  37.   clGetPlatformIDs(num_platforms, platforms, NULL);   // 再调函数获取平台ids 这下信息放进platforms里
  38.   /* Find extensions of all platforms */

  39.   //获取额外的平台信息。上面已经取得了平台id了,那么就可以进一步获取更加详细的信息了。

  40.   //一个for循环获取所有的主机上的platforms信息

  41.   for(i=0; i<num_platforms; i++)
  42.   {

  43.     /* Find size of extension data */

  44.     //也是和前面一样,先设置第三和第四个参数为0和NULL,然后就可以用第五个参数ext_size获取额外信息的长度了。

  45.     err = clGetPlatformInfo(platforms[i],     

  46.       CL_PLATFORM_EXTENSIONS, 0, NULL, &ext_size);    // 获取平台扩展信息

  47.     if(err < 0)

  48.     {
  49.       perror("函数clGetPlatformInfo调用失败.");
  50.       exit(1);
  51.     }

  52.     printf("平台详细信息The size of extension data is: %d\n", (int)ext_size);//我的计算机显示392

  53.     /* Access extension data */

  54.     //这里的ext_data相当于一个缓存,存储相关信息。

  55.     ext_data = (char*)malloc(ext_size);

  56.     //这个函数就是获取相关信息的函数,第二个参数指明了需要什么样的信息,如这里的CL_PLATFORM_EXTENSIONS表示是opencl支持的扩展功能信息。我计算机输出一大串,机器比较新(专门为了学图形学而购置的电脑),支持的东西比较多。

  57.     clGetPlatformInfo(platforms[i], CL_PLATFORM_EXTENSIONS,  

  58.       ext_size, ext_data, NULL);       // 再一次 获取平台扩展信息

  59.     printf("Platform %d supports extensions扩展信息: %s\n", i, ext_data);
  60.     //Platform 0 supports extensions信息:
  61.     //cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics
  62.     //cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics
  63.     //cl_khr_fp64 cl_khr_byte_addressable_store
  64.     //cl_khr_icd cl_khr_gl_sharing cl_nv_compiler_options
  65.     //cl_nv_device_attribute_query cl_nv_pragma_unroll cl_nv_copy_opts
  66.     //cl_nv_create_buffer cl_khr_int64_base_atomics cl_khr_int64_extended_atomics
  67.     //cl_khr_device_uuid

  68.     //这里是输出生产商的名字,比如我显卡信息是:NVIDIA CUDA

  69.     char *name = (char*)malloc(ext_size);

  70.     clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME,

  71.       ext_size, name, NULL);      

  72.     printf("Platform(CL_PLATFORM_NAME) %d name名字: %s\n", i, name);

  73.     //这里是供应商信息,我显卡信息:NVIDIA Corporation

  74.     char *vendor = (char*)malloc(ext_size);

  75.     clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR,  

  76.       ext_size, vendor, NULL);        

  77.     printf("Platform(CL_PLATFORM_VENDOR) %d vendor供应商: %s\n", i, vendor);

  78.     //最高支持的OpenCL版本,本机显示:OpenCL 1.2 CUDA 11.1.114

  79.     char *version = (char*)malloc(ext_size);

  80.     clGetPlatformInfo(platforms[i], CL_PLATFORM_VERSION,  

  81.       ext_size, version, NULL);      

  82.     printf("Platform(CL_PLATFORM_VERSION) %d 最高支持版本: %s\n", i, version);

  83.     //这个只有两个值:full profile 和 embeded profile

  84.     char *profile = (char*)malloc(ext_size);

  85.     clGetPlatformInfo(platforms[i], CL_PLATFORM_PROFILE,  

  86.       ext_size, profile, NULL);      
  87.     //OpenCL平台信息参数 FULL_PROFILE:支持OpenCL规范的所有功能。
  88.     // EMBEDDED_PROFILE:支持OpenCL嵌入式简档,是OpenCL规范的一个子集
  89.     printf("Platform(CL_PLATFORM_PROFILE) %d full profile or embeded profile?: %s\n", i, profile);

  90.     /* Look for ICD extension */

  91.     //如果支持ICD这一扩展功能的platform,输出显示,本机的Intel和Nvidia都支持这一扩展功能

  92.     if(strstr(ext_data, icd_ext) != NULL){
  93.         //strstr是C语言中的函数,作用是返回字符串中首次出现子串的地址
  94.         platform_index = i;
  95.     }

  96.     //std::cout<<"Platform_index = "<<platform_index<<std::endl;

  97.    printf("Platform_index is: %d\n", platform_index);

  98.     /* Display whether ICD extension is supported */

  99.     if(platform_index > -1)

  100.       printf("Platform %d supports(是否支持ICD扩展) the %s extension.\n",

  101.       platform_index, icd_ext);

  102.     //释放空间

  103.     free(ext_data);

  104.     free(name);

  105.     free(vendor);

  106.     free(version);

  107.     free(profile);

  108.   }

  109.   if(platform_index <= -1)

  110.     printf("No platforms support the %s extension.\n", icd_ext);

  111.   /* Deallocate resources */

  112.   free(platforms);

  113.   return 0;

  114. }
复制代码




opencl2.png (63.86 KB, 下载次数: 81)

opencl2.png

opencl3.png (67.91 KB, 下载次数: 77)

opencl3.png
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

0

主题

98

帖子

200

积分

中级会员

Rank: 3Rank: 3

积分
200
沙发
发表于 2021-11-23 19:34:32 | 只看该作者
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-6-24 03:16 , Processed in 0.198153 second(s), 22 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表