英伟达的显卡首先要下载安装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”保存起来,下次直接这个属性页就可以了,不用每次都重复配置。以下是测试程序:
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- #include <CL/cl.h>
-
-
- int main()
- {
-
- cl_platform_id *platforms;
-
-
- 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";
-
-
-
-
- err = clGetPlatformIDs(5, NULL, &num_platforms);
- if (err < 0)
- {
- perror("Couldn't find any platforms.");
- exit(1);
- }
- printf("本机上支持OpenCL的环境数量: %d\n", num_platforms);
-
-
- platforms = (cl_platform_id*)
- malloc(sizeof(cl_platform_id) * num_platforms);
-
- clGetPlatformIDs(num_platforms, platforms, NULL);
-
-
- 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("缓存大小: %d\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);
-
-
- if (strstr(ext_data, icd_ext) != NULL)
- platform_index = i;
- std::cout << "平台ID = " << platform_index << std::endl;
-
- 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;
- }
在本机上执行输出: