In the previous article, cudA-1 documented the process of installing CUDA in centos7. In this article, we’ll start with a simple code example to get a feel for the basic style of CUDA programming.

Cut the crap, on the code:

// 001_hello.cu

#include <stdio.h>
#include <unistd.h>


// the __global__ modifier tells the compiler that the function is running on the device (GPU) rather than the host (CPU)
__global__ void kernel(void)
{
    printf("Hello world! \n");
}

int main(void)
{
    while(1)
    {
        kernel<<<1.1> > > (); sleep(1);
    }   

    return 0;
}
Copy the code

How does the code compile? Super simple, just replace GCC with NVCC.

nvcc 001_hello.cu -o e_002
Copy the code

Kernel <<<1,1>>>();

As you can see, almost all of the syntax is exactly the same as that of C.

Execution Result:



After playing with the basic CUDA syntax, the next article will show you how to get GPU attributes, run a parallel computing example, and compare the CPU and GPU power.