Lesson 1 Hello World

来源:互联网 发布:竞价推广账户优化 编辑:程序博客网 时间:2024/05/16 03:15

1.准备好nvcc

装好CUDA后,我们在cmd里面运行


nvcc --version


如果找不到nvcc命令的话,需要自己在环境变量中path添加nvcc.exe的路径


2.接下来需要编写我们的cuda程序


//filename: helloworld.cu// This is the REAL "hello world" for CUDA!// It takes the string "Hello ", prints it, then passes it to CUDA with an array// of offsets. Then the offsets are added in parallel to produce the string "World!"// By Seamanj 04/11/2015 @NCCA #include <stdio.h>const int N = 7;const int blocksize = 7;__global__void hello(char *a, int *b){ printf("block %d thread %d is running!\n", blockIdx.x, threadIdx.x); a[threadIdx.x] += b[threadIdx.x];}int main(){ char a[N] = "Hello "; int b[N] = {15, 10, 6, 0, -11, 1, 0}; char *ad; int *bd; const int csize = N*sizeof(char); const int isize = N*sizeof(int); printf("%s\n", a); cudaMalloc( (void**)&ad, csize ); cudaMalloc( (void**)&bd, isize ); cudaMemcpy( ad, a, csize, cudaMemcpyHostToDevice ); cudaMemcpy( bd, b, isize, cudaMemcpyHostToDevice ); dim3 dimBlock( blocksize, 1 ); dim3 dimGrid( 1, 1 ); hello<<<dimGrid, dimBlock>>>(ad, bd); cudaMemcpy( a, ad, csize, cudaMemcpyDeviceToHost ); cudaFree( ad ); printf("%s\n", a); return EXIT_SUCCESS;}


3.编译helloworld.cu程序


在cmd中转到我们需要编译的文件夹中

输入 nvcc helloworld.cu

结果如下:




哇,坑爹的warning怎么去掉呢?

加选项, -Xcompiler /wd4819 

变成

nvcc -Xcompiler /wd4819 helloworld.cu

具体的选项可以参见http://blog.csdn.net/seamanj/article/details/49652649这篇文章里面的pro文件

或者查看官方文档http://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/#axzz3qY6vaFLz




由于没有指定输出名字,所以都命令为a.

执行a.exe看看


0 0
原创粉丝点击