第一个cuda程序

 随便写个纪念下。新建一个工程,右键单击该工程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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值