Hello World from GPU-Build a CUDA programming environment based on Win10+VS2019+CUDA 11.0

sequence

  CPU and GPU The two cores of modern computers, the combination of GPU computing and CPU computing, has greatly improved the original computing performance. The complementarity of the two functions has enabled the rapid development of CPU+GPU heterogeneous parallel computing. In order to support the use of CPU+GPU heterogeneous architecture to execute applications, the nuclear plant designed a general-purpose parallel computing platform and programming model called CUDA.
  CUDA Toolkit official download portal!

  • Operating system version: Win10
  • Visual Studio版本:Visual Studio 2019
  • CUDA version: CUDA Toolkit 11.0 RC

1. CUDA installation

1.1. Check the version of CUDA supported by the computer graphics card:

NVIDIA Control Panel -> Help -> System Information -> Components:
Check the version supported by CUDA

1.2. Check the installed version of nvcc after successful installation

nvcc version

1.3, configure environment variables

CUDA_PATH: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0
CUDA_PATH_V11_0: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0

  The above two items are the configuration of environment variables that have been automatically generated after the installation of Cuda. We need to add the following variables by ourselves:

CUDA_BIN_PATH: %CUDA_PATH%\bin
CUDA_LIB_PATH: %CUDA_PATH%\lib\x64
CUDA_SDK_PATH: C:\ProgramData\NVIDIA Corporation\CUDA Samples\v11.0
CUDA_SDK_BIN_PATH: %CUDA_SDK_PATH%\bin\win64
CUDA_SDK_LIB_PATH: %CUDA_SDK_PATH%\common\lib\x64

  And in the system variable Path, add the following four pieces of information:

%CUDA_BIN_PATH%
%CUDA_LIB_PATH%
%CUDA_SDK_BIN_PATH%
%CUDA_SDK_LIB_PATH%

1.4, installation verification

  To verify that the installation is successful, the path specified in the official guideline for the deviceQuery sample program is:
C:\ProgramData\NVIDIA Corporation\CUDA Samples\v10.2\bin\win64\Release
But in actual verification, the path of the program is:
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\extras\demo_suite
  execute deviceQuery.exe and bandwidthTest.exe to verify.
verification
The result of the two runs is PASS, then the environment configuration of the relevant system is completed. Next, run the test case after installing Cuda. The path of the test case is based on the path where you installed Cuda, such as: C:\ProgramData\NVIDIA Corporation\CUDA Samples\v11.0...

2. VS2019 configures the CUDA project (VS is installed by default, and the part of installing VS will not be repeated)

  This part will run the test case simplePrintf under the path 0_Simple\. You need to open the project first, and then complete the relevant configuration in VS.

2.1. Add the extension of .cu file

2.1.1. Tools -> Options -> Text Editor -> File Extension, add extension .cu and set the editor to: Microsoft Visual C++.
Configuration 1
2.1.2 Tools -> Options -> Projects and Solutions -> VC++ project settings, add the extension ".cu" to be included.
Configuration-2

2.2, configuration generation dependencies

2.2.1. Configure build customization: right-click the opened project -> build dependencies -> build customization -> check CUDA v11.0.
Configuration-3
2.2.2. Configure the configuration properties of the .cu file: right-click the .cu file -> set the properties of this type of file to CUDA c/c++.
Configuration-4
2.2.3. Run test cases. VS will call the nvcc compiler to compile the .cu code.
Configuration-5

OK, we have successfully built the basic environment, let's try how to use GPU to say Hello World!

3. GPU programming test demo

  Use VS to create a new empty project, and configure the project according to the above step 2.2.

#include "stdio.h"

/*
修饰符__global__表明这个函数是将会从CPU中调用,在GPU中进行执行。
并借此函数来启动内核函数。
*/
__global__ void hello_world_from_gpu(void)
{
    
    
	printf("Hello World from GPU\n");
	return;
}

int main(void)
{
    
    
	printf("Hello World from CPU\n");

	hello_world_from_gpu <<<1, 5 >>> ();
	/*
	三重尖括号里的参数表明的是相关的执行配置,用来表明使用多少线程来执行内核函数,
	在本例子中有5个GPU线程被系统所调用。
	*/
	cudaDeviceReset();
	/*
	执行完成后调用cudaDeviceReset()函数释放和清空与当前进程运行相关的资源。
	*/
	return 0;
}

Test results:
hw
Reference books:
1. "CUDA C Authoritative Programming Guide"
2. CUDA Installation Guide for Microsoft Windows
reference blog post:
1. Configure CUDA 10.1 + TensorFlow-GPU 1.14.0 in the Visual Studio 2019 environment for Win10 system
2. vs2017 configuration cuda project

Guess you like

Origin blog.csdn.net/qq_33475105/article/details/107151021