前期工作:了解系统配置,选择合适的软件 利用GPU-Z的工具查看。本人电脑主机配置示意图: 图1 Intel显卡,支持OpenCL。
硬件准备: AMD(A卡)、NVIDIA(N卡)、Intel(I卡)等商家提供的支持OpenCL的显卡。 软件准备: 相应商家显卡的最新驱动 SDK软件 A卡:AMPAPP SDK@默认:C:\Program Files(x86)\AMD APP I卡:OpenCL-drivers(非必需),opencl-sdk@默认:C:\Program Files(x86)\Intel\OpenCL SDK\6.3(版本号) N卡:CUDA toolkit(非必需),GPU Computing SDK@默认:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\5.8(版本号) 配置流程: Windows操作系统,VS2017+N卡(本人笔记本电脑配置) 1>默认地址安装SDK软件 2>VS2013下新建C++项目(Shift+Ctrl+N->空白项目C++) 3>右击工程目录下源程序文件夹,新建源文件(Shift+Ctrl+A) 4>右击项目名->属性(或者:工具->选项)->C/C++->常规->附加包含目录->编辑->复制或搜索添加C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\5.8 (版本号)\include 5>属性->链接器->常规->附加库目录->编辑->复制或搜索添加C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\5.8(版本号)\lib\win32(本人笔记本为32位,64位作相应调整) 6>链接器->输入->附加依赖项->编辑->输入添加OpenCL.lib 注:每次新建项目都要进行此项配置 绝大多数情况下2>-6>步不分先后顺序,但第4>步有时要在新建源文件后才可见。
Windows操作系统,VS2013+I卡(本人台式机配置流程) 1>默认地址安装软件 2>VS2013下新建C++项目(Shift+Ctrl+N->空白项目C++) 3>右击工程目录下源程序文件夹,新建源文件(Shift+Ctrl+A) 4>右击项目名->属性(或者:工具->选项)->C/C++->附加包含目录->编辑->复制或搜索添加C:\Program Files(x86)\Intel\OpenCL SDK\6.3(版本号)\include 5>属性->链接器->常规->附加库目录->编辑->复制或搜索添加C:\Program Files (x86)\Intel\OpenCL SDK\6.3(版本号)\lib\x86 (本人台式机是64位机,但安装VS时默认32位,所以64位机在先择相应的lib文件夹时因先查看选项窗口上平台信息,本人台式机平台:活动(Win32),选择x86,活动的是64位,选择x64,详见图2,如果不对应VS平台信息,配置失败) 6>链接器->输入->附加依赖项->编辑->输入添加OpenCL.lib 注:除OpenCL软件安装目录不同,其他配置过程与N卡相同。 A卡采用类似流程,从略。
图2
在源文件中编写以下程序,调看计算机的OpenCL设备信息。此段代码来自《OpenCL异构并行编程实战》随书代码,详见http://book.2cto.com/201510/57214.html
# include <stdio.h> # include <stdlib.h> # include <malloc.h> # ifdef APPLE # include <OpenCL/cl.h> # else # include <CL/cl.h> # endif void displayPlatformInfo(cl_platform_id id, cl_platform_info param_name, const char* paramNameAsStr) { cl_int error = 0; size_t paramSize = 0; error = clGetPlatformInfo(id, param_name, 0, NULL, ¶mSize); char* moreInfo = (char*)malloc(sizeof(char) * paramSize); error = clGetPlatformInfo(id, param_name, paramSize, moreInfo, NULL); if (error != CL_SUCCESS) { perror("Unable to find any OpenCL platform information"); return; } printf("%s: %s\n", paramNameAsStr, moreInfo); } int main() { /* OpenCL 1.1 data structures */ cl_platform_id* platforms; /* OpenCL 1.1 scalar data types */ cl_uint numOfPlatforms; cl_int error; /* Get the number of platforms Remember that for each vendor's SDK installed on the computer, the number of available platform also increased. */ error = clGetPlatformIDs(0, NULL, &numOfPlatforms); if (error != CL_SUCCESS) { perror("Unable to find any OpenCL platforms"); exit(1); } // Allocate memory for the number of installed platforms. // alloca(...) occupies some stack space but is automatically freed on return platforms = (cl_platform_id*)alloca(sizeof(cl_platform_id) * numOfPlatforms); printf("Number of OpenCL platforms found: %d\n", numOfPlatforms); error = clGetPlatformIDs(numOfPlatforms, platforms, NULL); if (error != CL_SUCCESS) { perror("Unable to find any OpenCL platforms"); exit(1); } // We invoke the API 'clPlatformInfo' twice for each parameter we're trying to extract // and we use the return value to create temporary data structures (on the stack) to store // the returned information on the second invocation. for (cl_uint i = 0; i < numOfPlatforms; ++i) { displayPlatformInfo(platforms[i], CL_PLATFORM_PROFILE, "CL_PLATFORM_PROFILE"); displayPlatformInfo(platforms[i], CL_PLATFORM_VERSION, "CL_PLATFORM_VERSION"); displayPlatformInfo(platforms[i], CL_PLATFORM_NAME, "CL_PLATFORM_NAME"); displayPlatformInfo(platforms[i], CL_PLATFORM_VENDOR, "CL_PLATFORM_VENDOR"); displayPlatformInfo(platforms[i], CL_PLATFORM_EXTENSIONS, "CL_PLATFORM_EXTENSIONS"); } return 0; }本人台式机结果输出如下:
Number of OpenCL platforms found: //有两个平台 CL_PLATFORM_PROFILE: FULL_PROFILE//平台简介 CL_PLATFORM_VERSION: OpenCL 2.0//平台版本:OpenCL 2.0 CL_PLATFORM_NAME: Intel(R) OpenCL//平台名称 CL_PLATFORM_VENDOR: Intel(R) Corporation//商家 CL_PLATFORM_EXTENSIONS: cl_intel_dx9_media_sharing cl_khr_3d_image_writes cl_khr_byte_addressable_store cl_khr_d3d11_sharing cl_khr_depth_images cl_khr_dx9_media_sharing cl_khr_gl_sharing cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_icd cl_khr_image2d_from_buffer cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_spir//拓展项 CL_PLATFORM_PROFILE: FULL_PROFILE//第二个平台简介 CL_PLATFORM_VERSION: OpenCL 2.1//平台版本:OpenCL 2.1 CL_PLATFORM_NAME: Experimental OpenCL 2.1 CPU Only Platform CL_PLATFORM_VENDOR: Intel(R) Corporation CL_PLATFORM_EXTENSIONS: cl_khr_icd 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_byte_addressable_store cl_khr_depth_images cl_khr_3d_image_writes cl_intel_exec_by_local_thread cl_khr_spir cl_khr_dx9_media_sharing cl_intel_dx9_media_sharing cl_khr_d3d11_sharing cl_khr_gl_sharing cl_khr_fp64 cl_khr_image2d_from_buffer注:运行时,计算机先加载OpenCL必要的.dll,而后才进入程序。需加载的.dll较多,加载的过程较长。
