环境 WSL2 + Ubuntu 22.04 + CUDA Toolkit 12.2

我们来编写一个Hello World程序。

代码:

snippet.c
/*
*hello_world.cu
*/
#include<stdio.h>
__global__ void hello_world(void)
{
    printf("GPU: Hello world!\n");
}
int main(int argc,char **argv)
{
    printf("CPU: Hello world!\n");
	hello_world<<<1,10>>>();
	cudaDeviceReset();//if no this line ,it can not output hello world from gpu
	return 0;
}

编译:

root@FF-ACER:~/demo# nvcc -arch sm_60 hello_world.cu -o hello_world

运行:

snippet.bash
root@FF-ACER:~/demo# ./hello_world
CPU: Hello world!
GPU: Hello world!
GPU: Hello world!
GPU: Hello world!
GPU: Hello world!
GPU: Hello world!
GPU: Hello world!
GPU: Hello world!
GPU: Hello world!
GPU: Hello world!
GPU: Hello world!