英伟达的显卡首先要下载安装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 表示一个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("缓存大小: %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);

		//查询是否支持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;
}

在本机上执行输出:



posted on 2017-02-16 07:17  未雨愁眸  阅读(1664)  评论(1编辑  收藏  举报