OpenCL编译环境配置(VS+Nvidia)

来源:互联网 发布:天津广播电视网络招聘 编辑:程序博客网 时间:2024/05/21 17:36

英伟达的显卡首先要下载安装CUDA开发包,可以参考这里的步骤:   VS2015编译环境下CUDA安装配置

安装好CUDA之后,OpenCL的配置就已经完成了80%了,剩下的工作就是把OpenCL的路径添加到工程中。


1. 新建一个win32控制台应用程序,在工程的属性管理器Debug中添加一个属性页“OpenCL.props”,之后双击打开


2. 在C/C++ ->常规->附加包含目录 中添加CUDA的include文件夹路径,我的路径是“D:\Software\CUDA\Development\include”



3. 在链接器->常规->附加库目录 中添加lib文件夹路径,我的路径是“D:\Software\CUDA\Development\lib\Win32”



4. 在链接器->输入->附加依赖项 里添加lib文件 OpenCL.lib



经过以上4个步骤,OpenCL编译环境就已经配置好了,可以把属性页“OpenCL.props”保存起来,下次直接这个属性页就可以了,不用每次都重复配置。以下是测试程序:


[cpp] view plain copy
 print?
  1. #include <stdio.h>    
  2. #include <stdlib.h>    
  3. #include <iostream>    
  4. #include <CL/cl.h>    
  5.   
  6.   
  7. int main()  
  8. {  
  9.     //cl_platform 表示一个OpenCL的执行平台,关联到GPU硬件,如N卡,AMD卡  
  10.     cl_platform_id *platforms;  
  11.   
  12.     //OpenCL中定义的跨平台的usigned int和int类型  
  13.     cl_uint num_platforms;  
  14.     cl_int i, err, platform_index = -1;  
  15.   
  16.     char* ext_data;  
  17.     size_t ext_size;  
  18.     const char icd_ext[] = "cl_khr_icd";  
  19.   
  20.     //要使platform工作,需要两个步骤。1 需要为cl_platform_id结构分配内存空间。2 需要调用clGetPlatformIDs初始化这些数据结构。一般还需要步骤0:询问主机上有多少platforms    
  21.   
  22.     //查询计算机上有多少个支持OpenCL的设备  
  23.     err = clGetPlatformIDs(5, NULL, &num_platforms);  
  24.     if (err < 0)  
  25.     {  
  26.         perror("Couldn't find any platforms.");  
  27.         exit(1);  
  28.     }  
  29.     printf("本机上支持OpenCL的环境数量: %d\n", num_platforms);  
  30.   
  31.     //为platforms分配空间                                                   
  32.     platforms = (cl_platform_id*)  
  33.         malloc(sizeof(cl_platform_id) * num_platforms);  
  34.   
  35.     clGetPlatformIDs(num_platforms, platforms, NULL);  
  36.   
  37.     //获取GPU平台的详细信息   
  38.     for (i = 0; i < num_platforms; i++)  
  39.     {  
  40.         //获取缓存大小  
  41.         err = clGetPlatformInfo(platforms[i],  
  42.             CL_PLATFORM_EXTENSIONS, 0, NULL, &ext_size);  
  43.         if (err < 0)  
  44.         {  
  45.             perror("Couldn't read extension data.");  
  46.             exit(1);  
  47.         }  
  48.   
  49.         printf("缓存大小: %d\n", ext_size);  
  50.   
  51.         ext_data = (char*)malloc(ext_size);  
  52.   
  53.         //获取支持的扩展功能  
  54.         clGetPlatformInfo(platforms[i], CL_PLATFORM_EXTENSIONS,  
  55.             ext_size, ext_data, NULL);  
  56.         printf("平台 %d 支持的扩展功能: %s\n", i, ext_data);  
  57.   
  58.         //获取显卡的名称    
  59.         char *name = (char*)malloc(ext_size);  
  60.         clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME,  
  61.             ext_size, name, NULL);  
  62.         printf("平台 %d 是: %s\n", i, name);  
  63.   
  64.         //获取显卡的生产商名称    
  65.         char *vendor = (char*)malloc(ext_size);  
  66.         clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR,  
  67.             ext_size, vendor, NULL);  
  68.         printf("平台 %d 的生产商是: %s\n", i, vendor);  
  69.   
  70.         //获取平台版本   
  71.         char *version = (char*)malloc(ext_size);  
  72.         clGetPlatformInfo(platforms[i], CL_PLATFORM_VERSION,  
  73.             ext_size, version, NULL);  
  74.         printf("平台 %d 的版本信息: %s\n", i, version);  
  75.   
  76.         //查询显卡是独立的还是嵌入的   
  77.         char *profile = (char*)malloc(ext_size);  
  78.         clGetPlatformInfo(platforms[i], CL_PLATFORM_PROFILE,  
  79.             ext_size, profile, NULL);  
  80.         printf("平台 %d 是独立的(full profile)还是嵌入式的(embeded profile)?: %s\n", i, profile);  
  81.   
  82.         //查询是否支持ICD扩展  
  83.         if (strstr(ext_data, icd_ext) != NULL)  
  84.             platform_index = i;  
  85.         std::cout << "平台ID = " << platform_index << std::endl;  
  86.         /* Display whether ICD extension is supported */  
  87.         if (platform_index > -1)  
  88.             printf("平台 %d 支持ICD扩展: %s\n",  
  89.                 platform_index, icd_ext);  
  90.         std::cout << std::endl;  
  91.   
  92.         //释放空间    
  93.         free(ext_data);  
  94.         free(name);  
  95.         free(vendor);  
  96.         free(version);  
  97.         free(profile);  
  98.     }  
  99.   
  100.     if (platform_index <= -1)  
  101.         printf("No platforms support the %s extension.\n", icd_ext);  
  102.     getchar();  
  103.   
  104.     //释放资源  
  105.     free(platforms);  
  106.     return 0;  
  107. }  

在本机上执行输出: