file:cuda_test.cu
#include <stdio.h>
__global__ void add(int x, int y, int* z)
{
*z = x + y;
printf("This is just a test for CUDA C!\n");
}
int main(void)
{
int c;
int *dev_c;
cudaMalloc((void**)&dev_c,sizeof(int)); //在GPU上申请内存
add<<<1, 1>>>(20, 500, dev_c); //运行GPU kernel函数
cudaMemcpy(&c, dev_c, sizeof(int), cudaMemcpyDeviceToHost);//把结果从GPU内存拷贝到CPU内存
printf("sum=%d\n", c);
cudaFree(dev_c); //释放GPU内存
return 0;
}
在linux上编译方法:
$ nvcc duda_test.cu
$ ./a.out
输出结果如下:
This is just a test for CUDA C!
sum=520