|
ubuntu18.04上opencl环境搭建(Nvidia cuda都好了)
我是先玩cuda的thrust后来研究一下opencl的,所以系统显卡的驱动与cuda都搞定了的!
第一步:安装clinfo工具查看显卡对OpenCL的支持情况:
sudo apt-get install clinfo
clinfo
- jiang@jiang-Ubuntu:~/jjj_opencl_works/build$ clinfo
- Number of platforms 1
- Platform Name NVIDIA CUDA
- Platform Vendor NVIDIA Corporation
- Platform Version OpenCL 1.2 CUDA 11.1.114
- Platform Profile FULL_PROFILE
- 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
- Platform Extensions function suffix NV
- Platform Name NVIDIA CUDA
- Number of devices 1
- Device Name GeForce RTX 2070
- Device Vendor NVIDIA Corporation
- Device Vendor ID 0x10de
- Device Version OpenCL 1.2 CUDA
- Driver Version 455.45.01
- Device OpenCL C Version OpenCL C 1.2
- Device Type GPU
- Device Topology (NV) PCI-E, 01:00.0
- Device Profile FULL_PROFILE
- Device Available Yes
- Compiler Available Yes
- Linker Available Yes
- Max compute units 36
- Max clock frequency 1620MHz
- Compute Capability (NV) 7.5
- Device Partition (core)
- Max number of sub-devices 1
- Supported partition types None
- Max work item dimensions 3
- 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 配置:
- CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
- PROJECT(main_cl)
- #FIND_PACKAGE(CUDA REQUIRED)
- FIND_PACKAGE(OpenCL REQUIRED)
- ADD_EXECUTABLE(main_cl cl获取支持的平台信息.c)
- #TARGET_LINK_LIBRARIES(main_cl)
- #target_link_libraries(main_cl -lboost_system -lboost_thread)
- target_link_libraries(main_cl OpenCL)
复制代码
- jiang@jiang-Ubuntu:~/jjj_opencl_works/build$ cmake ..
- CMake Deprecation Warning at CMakeLists.txt:1 (CMAKE_MINIMUM_REQUIRED):
- Compatibility with CMake < 2.8.12 will be removed from a future version of
- CMake.
- Update the VERSION argument <min> value or use a ...<max> suffix to tell
- CMake that the project does not need compatibility with older versions.
- -- The C compiler identification is GNU 8.4.0
- -- The CXX compiler identification is GNU 8.4.0
- -- Detecting C compiler ABI info
- -- Detecting C compiler ABI info - done
- -- Check for working C compiler: /usr/bin/cc - skipped
- -- Detecting C compile features
- -- Detecting C compile features - done
- -- Detecting CXX compiler ABI info
- -- Detecting CXX compiler ABI info - done
- -- Check for working CXX compiler: /usr/bin/c++ - skipped
- -- Detecting CXX compile features
- -- Detecting CXX compile features - done
- -- Looking for CL_VERSION_2_2
- -- Looking for CL_VERSION_2_2 - found
- -- Found OpenCL: /usr/lib/x86_64-linux-gnu/libOpenCL.so (found version "2.2")
- -- Configuring done
- -- Generating done
- -- 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")
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #ifdef MAC
- #include <OpenCL/cl.h>
- #else
- #include <CL/cl.h>
- #endif
- int main() {
- /* Host data structures */
- cl_platform_id *platforms;
- //每一个cl_platform_id 结构表示一个在主机上的OpenCL执行平台,
- //就是指电脑中支持OpenCL的硬件,如nvidia显卡,intel CPU和显卡,AMD显卡和CPU等
- cl_uint num_platforms;
- cl_int i, err, platform_index = -1;
- /* Extension data */
- char* ext_data;
- size_t ext_size;
- const char icd_ext[] = "cl_khr_icd";
- //要使platform工作,需要两个步骤。1 需要为cl_platform_id结构分配内存空间。
- //2 需要调用clGetPlatformIDs初始化这些数据结构。一般还需要步骤0:询问主机上有多少platforms
- /* Find number of platforms */
- //返回值如果为-1就说明调用函数失败,如果为0标明成功
- //第二个参数为NULL代表要咨询主机上有多少个platform,并使用num_platforms取得实际flatform数量。
- //第一个参数为1,代表我们需要取最多1个platform。可以改为任意大如:INT_MAX整数最大值。
- //但是据说0,否则会报错,实际测试好像不会报错。下面是步骤0:询问主机有多少platforms
- err = clGetPlatformIDs(5, NULL, &num_platforms); // 获取平台ids
- if(err < 0) {
- perror("调用函数clGetPlatformIDs失败Couldn't find any platforms.");
- exit(1);
- }
- printf("我有opencl计算平台的数目: %d\n", num_platforms); //本人计算机上显示为2,有intel和nvidia两个平台
- /* Access all installed platforms */
- //步骤1 创建cl_platform_id,并分配空间 malloc分配的空间是 堆空间啊 还没到GPU上
- platforms = (cl_platform_id*)malloc(sizeof(cl_platform_id) * num_platforms);
- //步骤2 第二个参数用指针platforms存储platform
- clGetPlatformIDs(num_platforms, platforms, NULL); // 再调函数获取平台ids 这下信息放进platforms里
- /* Find extensions of all platforms */
- //获取额外的平台信息。上面已经取得了平台id了,那么就可以进一步获取更加详细的信息了。
- //一个for循环获取所有的主机上的platforms信息
- for(i=0; i<num_platforms; i++)
- {
- /* Find size of extension data */
- //也是和前面一样,先设置第三和第四个参数为0和NULL,然后就可以用第五个参数ext_size获取额外信息的长度了。
- err = clGetPlatformInfo(platforms[i],
- CL_PLATFORM_EXTENSIONS, 0, NULL, &ext_size); // 获取平台扩展信息
- if(err < 0)
- {
- perror("函数clGetPlatformInfo调用失败.");
- exit(1);
- }
- printf("平台详细信息The size of extension data is: %d\n", (int)ext_size);//我的计算机显示392
- /* Access extension data */
- //这里的ext_data相当于一个缓存,存储相关信息。
- ext_data = (char*)malloc(ext_size);
- //这个函数就是获取相关信息的函数,第二个参数指明了需要什么样的信息,如这里的CL_PLATFORM_EXTENSIONS表示是opencl支持的扩展功能信息。我计算机输出一大串,机器比较新(专门为了学图形学而购置的电脑),支持的东西比较多。
- clGetPlatformInfo(platforms[i], CL_PLATFORM_EXTENSIONS,
- ext_size, ext_data, NULL); // 再一次 获取平台扩展信息
- printf("Platform %d supports extensions扩展信息: %s\n", i, ext_data);
- //Platform 0 supports 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
- //这里是输出生产商的名字,比如我显卡信息是:NVIDIA CUDA
- char *name = (char*)malloc(ext_size);
- clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME,
- ext_size, name, NULL);
- printf("Platform(CL_PLATFORM_NAME) %d name名字: %s\n", i, name);
- //这里是供应商信息,我显卡信息:NVIDIA Corporation
- char *vendor = (char*)malloc(ext_size);
- clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR,
- ext_size, vendor, NULL);
- printf("Platform(CL_PLATFORM_VENDOR) %d vendor供应商: %s\n", i, vendor);
- //最高支持的OpenCL版本,本机显示:OpenCL 1.2 CUDA 11.1.114
- char *version = (char*)malloc(ext_size);
- clGetPlatformInfo(platforms[i], CL_PLATFORM_VERSION,
- ext_size, version, NULL);
- printf("Platform(CL_PLATFORM_VERSION) %d 最高支持版本: %s\n", i, version);
- //这个只有两个值:full profile 和 embeded profile
- char *profile = (char*)malloc(ext_size);
- clGetPlatformInfo(platforms[i], CL_PLATFORM_PROFILE,
- ext_size, profile, NULL);
- //OpenCL平台信息参数 FULL_PROFILE:支持OpenCL规范的所有功能。
- // EMBEDDED_PROFILE:支持OpenCL嵌入式简档,是OpenCL规范的一个子集
- printf("Platform(CL_PLATFORM_PROFILE) %d full profile or embeded profile?: %s\n", i, profile);
- /* Look for ICD extension */
- //如果支持ICD这一扩展功能的platform,输出显示,本机的Intel和Nvidia都支持这一扩展功能
- if(strstr(ext_data, icd_ext) != NULL){
- //strstr是C语言中的函数,作用是返回字符串中首次出现子串的地址
- platform_index = i;
- }
- //std::cout<<"Platform_index = "<<platform_index<<std::endl;
- printf("Platform_index is: %d\n", platform_index);
- /* Display whether ICD extension is supported */
- if(platform_index > -1)
- printf("Platform %d supports(是否支持ICD扩展) the %s extension.\n",
- platform_index, icd_ext);
- //释放空间
- free(ext_data);
- free(name);
- free(vendor);
- free(version);
- free(profile);
- }
- if(platform_index <= -1)
- printf("No platforms support the %s extension.\n", icd_ext);
- /* Deallocate resources */
- free(platforms);
- return 0;
- }
复制代码
|
|