• windows OpenCL 环境配置


    系统信息

    GPU Nvidia GeForce GTX 1650
    CPU AMD R5 4600H
    operation system 版本 Windows 10 专业版 21H2

    根据官网Conformant Products - The Khronos Group Inc
    查询到硬件上对opencl的支持情况。查到GPU 1650支持opencl 3.0

    弯路(只想配置环境可跳过)

    KhronosGroup/OpenCL-SDK: OpenCL SDK (github.com)
    使用vcpkg 安装opencl
    先安装vcpkg vcpkg/README_zh_CN.md at 2022.04.12 · microsoft/vcpkg (github.com)

    使用vcpkg 安装opencl

    Package: opencl
    Version: 2.2
    Port-Version: 8
    Architecture: x86-windows

    安装提示信息如下

    The package opencl is compatible with built-in CMake targets via CMake v3.6 and prior syntax

    find_package(OpenCL REQUIRED)
    target_link_libraries(main PRIVATE ${OpenCL_LIBRARIES})
    target_include_directories(main PRIVATE ${OpenCL_INCLUDE_DIRS})
    

    and the CMake v3.7 and beyond imported target syntax

    find_package(OpenCL REQUIRED)
    target_link_libraries(main PRIVATE OpenCL::OpenCL)
    

    This package is only an OpenCL SDK. To actually run OpenCL code you also need to install an implementation.

    WINDOWS: implementations typically ship with the drivers of you CPU/GPU vendors.
    LINUX: implementations may be installed from your distro's repo or manually. There are too many to count.
    APPLE: consult your distribution vendor on the state of OpenCL support: https://support.apple.com/en-us/HT202823
    

    尝试了在CMake工程中使用vcpkg提供的库 - vcpkg_C++包管理器 - 博客园 (cnblogs.com)

    这里出现了

    
    PS D:\github_room\opencl\vcpkg> ./vcpkg integrate install
    Applied user-wide integration for this vcpkg root.
    
    All MSBuild C++ projects can now #include any installed libraries.
    Linking will be handled automatically.
    Installing new libraries will make them instantly available.
    
    CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=D:/github_room/opencl/vcpkg/scripts/buildsystems/vcpkg.cmake"
    

    在CMakeLists.txt中写

    SET(CMAKE_TOOLCHAIN_FILE E:/vcpkg/clean/vcpkg/scripts/buildsystems/vcpkg.cmake)
    

    这里提示,opencl的实现由cpu或gpu的厂商随驱动程序提供,那么需要找厂商文件或程序才是使用opencl的正途。

    安装CUDA Toolkit

    CUDA Toolkit | NVIDIA Developer
    我选择了下载整个包之后安装。安装cuda 11.6版本。
    安装好之后,已经提供了opencl环境。
    如opencl头文件位置在

    C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\include\CL\cl.h
    

    使用Clion

    clion 版本为2022.1 配置mingw作为c++开发环境。
    cmake 版本为3.23.1
    一种偷懒而快速的方式,CodeBlocks会一路安装好mingw,把路径给clion也用,即可配置工具链。

    代码文件与CMakeLists.txt

    main.cpp参考OpenCL编译环境配置(VS+Nvidia) - 未雨愁眸 - 博客园 (cnblogs.com)
    修改了两处

     if (strstr(ext_data, icd_ext) != NULL)
    

    需要再引入一个头文件

    #include <string.h>
    

    第二个问题:c++出现中文乱码怎么解决? - turtle的回答 - 知乎
    https://www.zhihu.com/question/421326588/answer/1738490344
    使用这个system("chcp 65001");

    #include <stdio.h>  
    #include <stdlib.h>  
    #include <string.h>  
    #include <iostream>  
    #include <CL/cl.h>  
      
      
    int main() {  
        system("chcp 65001");  
        //cl_platform 表示一个OpenCL的执行平台,关联到GPU硬件,如N卡,AMD卡  
        cl_platform_id *platforms;  
      
        //OpenCL中定义的跨平台的usigned int和int类型  
        cl_uint num_platforms;  
        cl_int i, err, platform_index = -1;  
      
        char *ext_data;  
        size_t ext_size;  
        const char icd_ext[] = "cl_khr_icd";  
      
        //要使platform工作,需要两个步骤。1 需要为cl_platform_id结构分配内存空间。2 需要调用clGetPlatformIDs初始化这些数据结构。一般还需要步骤0:询问主机上有多少platforms  
      
        //查询计算机上有多少个支持OpenCL的设备  
        err = clGetPlatformIDs(5, NULL, &num_platforms);  
        if (err < 0) {  
            perror("Couldn't find any platforms.");  
            exit(1);  
        }  
        printf("本机上支持OpenCL的环境数量: %d\n", num_platforms);  
      
        //为platforms分配空间  
        platforms = (cl_platform_id *)  
                malloc(sizeof(cl_platform_id) * num_platforms);  
      
        clGetPlatformIDs(num_platforms, platforms, NULL);  
      
        //获取GPU平台的详细信息  
        for (i = 0; i < num_platforms; i++) {  
            //获取缓存大小  
            err = clGetPlatformInfo(platforms[i],  
                                    CL_PLATFORM_EXTENSIONS, 0, NULL, &ext_size);  
            if (err < 0) {  
                perror("Couldn't read extension data.");  
                exit(1);  
            }  
      
            printf("缓存大小: %zd\n", ext_size);  
      
            ext_data = (char *) malloc(ext_size);  
      
            //获取支持的扩展功能  
            clGetPlatformInfo(platforms[i], CL_PLATFORM_EXTENSIONS,  
                              ext_size, ext_data, NULL);  
            printf("平台 %d 支持的扩展功能: %s\n", i, ext_data);  
      
            //获取显卡的名称  
            char *name = (char *) malloc(ext_size);  
            clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME,  
                              ext_size, name, NULL);  
            printf("平台 %d 是: %s\n", i, name);  
      
            //获取显卡的生产商名称  
            char *vendor = (char *) malloc(ext_size);  
            clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR,  
                              ext_size, vendor, NULL);  
            printf("平台 %d 的生产商是: %s\n", i, vendor);  
      
            //获取平台版本  
            char *version = (char *) malloc(ext_size);  
            clGetPlatformInfo(platforms[i], CL_PLATFORM_VERSION,  
                              ext_size, version, NULL);  
            printf("平台 %d 的版本信息: %s\n", i, version);  
      
            //查询显卡是独立的还是嵌入的  
            char *profile = (char *) malloc(ext_size);  
            clGetPlatformInfo(platforms[i], CL_PLATFORM_PROFILE,  
                              ext_size, profile, NULL);  
            printf("平台 %d 是独立的(full profile)还是嵌入式的(embeded profile)?: %s\n", i, profile);  
      
            //查询是否支持ICD扩展  
            if (strstr(ext_data, icd_ext) != NULL)  
                platform_index = i;  
            std::cout << "平台ID = " << platform_index << std::endl;  
            /* Display whether ICD extension is supported */  
            if (platform_index > -1)  
                printf("平台 %d 支持ICD扩展: %s\n",  
                       platform_index, icd_ext);  
            std::cout << std::endl;  
      
            //释放空间  
            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);  
        getchar();  
      
        //释放资源  
        free(platforms);  
        return 0;  
    }
    

    CMakeLists.txt

    cmake_minimum_required(VERSION 3.7)  
      
      
    project (hello)   
      
    add_executable(hello   
        main.cpp  
    )  
      
    find_package(OpenCL REQUIRED)  
    target_link_libraries(hello PRIVATE OpenCL::OpenCL)
    

    工具链为clion自动识别出的,

    cmake-ninja

    程序执行结果为

    D:\github_room\testopencl\cmake-build-debug\hello.exe
    Active code page: 65001
    本机上支持OpenCL的环境数量: 2
    缓存大小: 606
    平台 0 支持的扩展功能: cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_ato
    mics cl_khr_local_int32_extended_atomics cl_khr_fp64 cl_khr_3d_image_writes 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_d3d10_sharing cl_khr_d3d10_
    sharing cl_nv_d3d11_sharing cl_nv_copy_opts cl_nv_create_buffer cl_khr_int64_base_atomics cl_khr_int64_extended_atomics
    cl_khr_device_uuid cl_khr_pci_bus_info cl_khr_external_semaphore cl_khr_external_memory cl_khr_external_semaphore_win32
    cl_khr_external_memory_win32
    平台 0 是: NVIDIA CUDA
    平台 0 的生产商是: NVIDIA Corporation
    平台 0 的版本信息: OpenCL 3.0 CUDA 11.6.134
    平台 0 是独立的(full profile)还是嵌入式的(embeded profile)?: FULL_PROFILE
    平台ID = 0
    平台 0 支持ICD扩展: cl_khr_icd
    
    缓存大小: 124
    平台 1 支持的扩展功能: cl_khr_icd cl_khr_d3d10_sharing cl_khr_d3d11_sharing cl_khr_dx9_media_sharing cl_amd_event_callba
    ck cl_amd_offline_devices
    平台 1 是: AMD Accelerated Parallel Processing
    平台 1 的生产商是: Advanced Micro Devices, Inc.
    平台 1 的版本信息: OpenCL 2.1 AMD-APP (3075.13)
    平台 1 是独立的(full profile)还是嵌入式的(embeded profile)?: FULL_PROFILE
    平台ID = 1
    平台 1 支持ICD扩展: cl_khr_icd
    
    
    进程已结束,退出代码-1073741510 (0xC000013A: interrupted by Ctrl+C)
    
  • 相关阅读:
    经典的阿里前端笔试题
    Javascript之浏览器兼容EventUtil
    Javascript之对象的创建
    奇妙的CSS之CSS3新特性总结
    前端优化之无阻塞加载脚本
    正则表达式规则与常见的正则表达式
    全端工程师
    最全前端面试问题及答案总结--《转载》
    奇妙的CSS之布局与定位
    关于在django框架里取已登录用户名字的问题
  • 原文地址:https://www.cnblogs.com/lingr7/p/16255638.html
Copyright © 2020-2023  润新知