I was programming CUDA’s Hello World Program from the link below.

URL: https://cuda-tutorial.readthedocs.io/en/latest/tutorials/tutorial01/

Here is code. I could compile this code, but the program did not display message “Hello World from GPU!”.


__global__ void cuda_hello(){
    printf("Hello World from GPU!\n");
}

int main() {
    cuda_hello<<<1,1>>>(); 
    return 0;
}

I added the lines of code below after cuda_hello<<<1,1>>>().

cudaError_t cudaerr = cudaDeviceSynchronize();
if (cudaerr != cudaSuccess) {
	printf("Kernel Error: \"%s\".\n", 
	cudaGetErrorString(cudaerr));
} 

I compiled and executed the program again. I did not receive the error message, and I got “Hello World from GPU!” message!

Here is the entire code.

#include "cuda_runtime.h"
#include <stdio.h>

__global__ void cuda_hello(){
    printf("Hello World from GPU!\n");
}

int main() {
    cuda_hello<<<1,1>>>(); 

    cudaError_t cudaerr = cudaDeviceSynchronize();

    if (cudaerr != cudaSuccess) {
	printf("Kernel Error: \"%s\".\n", 
	cudaGetErrorString(cudaerr));
    }

    return 0;
}