In the last article [CUDA-1] centos7 installed CUDA , recorded the process of installing cuda in centos7, in this article, we first use a simple example code to experience the basic style of CUDA programming.
Less nonsense, the code:
// 001_hello.cu
#include <stdio.h>
#include <unistd.h>
// __global__ 修饰符,将告诉编译器,函数在设备(GPU)上运行而不是在主机(CPU)上运行
__global__ void kernel(void)
{
printf("Hello world!\n");
}
int main(void)
{
while(1)
{
kernel<<<1,1>>>();
sleep(1);
}
return 0;
}
How does the code compile? It's super simple, just replace gcc with nvcc.
nvcc 001_hello.cu -o e_002
In order to just see the occupation of the GPU, a loop has been used in the upload code to execute the code kernel <<< 1,1 >>> ();
you can find that almost all the syntax is exactly the same as the C language.
Execution result: After
experiencing the basic syntax of CUDA, in the next article, I will explain how to get the properties of the GPU, and run an example of parallel computing, and compare the differences in computing power between the CPU and GPU.