现在的位置: 首页 > 综合 > 正文

第一个cuda程序

2014年01月04日 ⁄ 综合 ⁄ 共 1463字 ⁄ 字号 评论关闭

 随便写个纪念下。新建一个工程,右键单击该工程Custom Build Rules里面选择Cuda Runtime API Build Rule(v4.1)。另外点击该项目的右键,属性的linker->input右侧有Additional Dependencies添加cudart.lib cutil32D.lib。添加新项目比如cudatest.cpp,重命名为cudatest.cu

 

cudaError_t cudaGetDeviceCount
( int *  count  ) 

此函数返回计算能力>=1的设备数量

Returns in *count the number of devices with compute capability greater or equal to 1.0 that are available for execution. If there is no such device then
cudaGetDeviceCount() will return
cudaErrorNoDevice. If no driver can be loaded to determine if any such devices exist then
cudaGetDeviceCount() will return
cudaErrorInsufficientDriver.

Parameters:
count  - Returns the number of devices with compute capability greater or equal to 1.0
Returns:
cudaSuccess,
cudaErrorNoDevice,
cudaErrorInsufficientDriver
Note:
Note that this function may also return error codes from previous, asynchronous launches

 

#include<stdio.h>

bool InitCUDA()  
{  
	
	int count;  
	//获取计算能力>=1.0的设备数量
	cudaGetDeviceCount(&count);  
	printf("%d\n",count);

	if(count == 0)  
	{  
		fprintf(stderr, "There is no device.\n");  
		return false;  
	}  
	int i;  
	for(i = 0; i < count; i++)  
	{  //指定设备的属性
		cudaDeviceProp prop;  
		if(cudaGetDeviceProperties(&prop, i) == cudaSuccess)  
		{  
			//输出设备名称
			char *c=prop.name;
			printf("%s\n",c);
			//定义设备计算能力的主要修订编号
			printf("%d",prop.major);
			//定义设备计算能力的次要修订编号
			printf("%d",prop.minor);
			if(prop.major >= 1)  
			{  
				break;  
			}  
			
		}  
	}  
	if(i == count)  
	{  
		fprintf(stderr, "There is no device supporting CUDA 1.x.\n");  
		return false;  
	}  
	//把设备设置为调用主线程的当前设备
	cudaSetDevice(i);  
	return true;  
}  
int main()  
{  
	if(!InitCUDA())  
	{  
		return 0;  
	}  
	printf("HelloWorld, CUDA has been initialized.\n");  
	int j;
	scanf("%d",&j);
	return 0; 
	
	
}  

结果输出显卡名称,我的显卡为8600M GT,因此输出了8600MGT

抱歉!评论已关闭.