CUDA Programming: Hello, World
CUDA
MS Windows
C/C++
The customary ‘Hello, World’ program.
After deploying a CUDA command line development sandbox, I got the chance to put together a quick Hello World program to confirm that everything works fine. This involved spinning up Visual Studio to create a new solution and project based on the CUDA Runtime template; compiling the C/C++ code shown below; then testing the program, first within Visual Studio, then in the MSYS2 terminal.
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
void helloFromGPU(void) {
__global__ ("Hello, World from GPU!\n");
printf}
int main()
{
("Hello, World from the host!\n");
printf
<< <1, 10 >> > ();
helloFromGPU ();
cudaDeviceReset
return 0;
}
Conceptually, the host portion of the code provisions ten threads that each execute the kernel function helloFromGPU()
once to output the console message.
We use msbuild.exe
that came with the Visual Studio installation to compile the HelloWorldCUDA solution in the MSYS2 console.
$ MSBuild.exe HelloWorldCuda.sln -t:Rebuild -p:Configuration=Release
$ ./x64/Release/HelloWorldCuda.exe
Hello, World from the host!
Hello, World from GPU!
Hello, World from GPU!
Hello, World from GPU!
Hello, World from GPU!
Hello, World from GPU!
Hello, World from GPU!
Hello, World from GPU!
Hello, World from GPU!
Hello, World from GPU! Hello, World from GPU!