#include <stdio.h>
#include <stdlib.h>
#include <cuda_runtime.h>
#include <cutil.h>
//
/ Init CUDA /
//
#if DEVICE_EMULATION
bool InitCUDA(void){return true;}
#else
bool InitCUDA(void)
{
int count = 0;
int i = 0;
cudaGetDeviceCount(&count);
if(count == 0) {
fprintf(stderr, “There is no device.\n”);
return false;
}
for(i = 0; i < count; i++) {
cudaDeviceProp prop;
if(cudaGetDeviceProperties(&prop, i) == cudaSuccess) {
if(prop.major >= 1) {
break;
}
}
}
if(i == count) {
fprintf(stderr, “There is no device supporting CUDA.\n”);
return false;
}
cudaSetDevice(i);
printf(“CUDA initialized.\n”);
return true;
}
#endif
//
/ Example /
//
global static void HelloCUDA(charresult,int num,clock_ttime)
{
int i = 0;
char p_HelloCUDA=“HelloCUDA”;
clock_t start=clock();
for(i = 0; i < num; i++) {
result[i] = p_HelloCUDA[i];
}
*time=clock()-start;
}
/* HelloCUDA /
/*******************************************************************/
int main(int argc, charargv)
{
char device_result = 0;
clock_t time=0;
char host_result[12] ={0};
clock_t time_used =0;
if(!InitCUDA()){
return 0;}
printf(“CUDA initialized.\n”);
CUDA_SAFE_CALL( cudaMalloc((void) &device_result, sizeof(char) * 11));
CUDA_SAFE_CALL( cudaMalloc((void) &time,sizeof(clock_t)));
HelloCUDA<<<1, 1, 0>>>(device_result,11,time);
CUDA_SAFE_CALL( cudaMemcpy(&host_result, device_result, sizeof(char) * 11, cudaMemcpyDeviceToHost));
CUDA_SAFE_CALL( cudaMemcpy(&time_used, time, sizeof(clock_t) , cudaMemcpyDeviceToHost));
CUDA_SAFE_CALL( cudaFree(device_result));
CUT_EXIT(argc, argv);}
可以运行,但是跳出来的结果是CUDA initialized
CUDA initialized