VS2015+CUDA configuration to write GPU running program

1 Install the software

The VS2015
CUDA
download and installation method will not be repeated here, if necessary, please refer to the previous article.

2 Project establishment and configuration

2.1 Project establishment

Use the usual method to build the project, there is no special requirement.

2.2 Project configuration

Right click on the project - Generate Dependencies - Generate Custom, the "Visual C++ Generate Custom File" dialog box will pop up, check CUDA xx, where xx is the version number of CUDA installed, confirm

3 CUDA source files and settings

Note: The CUDA code that needs to run on the GPU cannot be written in cpp, otherwise there will be a problem of '<<<' not understanding and cannot be compiled, it must be written in the .cu file. But the .cu file can
contain C/C++ code ;

3.1 Add CUDA source files

Right-click the project-Add-New Item-NVIDIA CUDA xx-Code, you can create a new CUDA C/C++ File with the extension of .cu, or CUDA C/C++ Header with the extension of .cuh

3.2 Compilation settings of CUDA source files

If the .cu file is added according to the above method, it will be automatically configured here.
If the .cu is added by other methods, the following configuration may be required:
Right-click on the .cu file-Properties-pop-up file "Property Page", General-item type select "CUDA C/C++", apply

3.3 Platform Selection of CUDA Source Files

Right click on the .cu file - Properties - Pop-up file "property page", CUDA C/C++, Common-Target Machine platform select 32-bit or 64-bit according to project requirements

4 CUDA common header files and libraries

4.1 CUDA common header files

CUDA common functions generally include the following header files to use

#include <cuda_runtime.h>
#include <device_launch_parameters.h>

These h files are generally located in C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vx.x\include. In fact, an environment variable, CUDA_PATH, has been added to the system when CUDA is installed, pointing to the installation path of CUDA, so in the project Properties-C/C++-General-Additional include directory: $(CUDA_PATH)\include can be added.

4.2 Common CUDA libraries

cudart.lib

Need to add in project properties-linker-general-additional library directory: $(CUDA_PATH)\lib\x64 (if x86 program, change x64 to win32)

5 CUDA basic functions

After the above configuration is completed, you can use CUDA to write the program running on the GPU. Here are a few basic functions about the GPU, which can be tested. The following code actually just fetches some attributes of the GPU, so it can be written in cpp. It does not need to be written in the cu file.

5.1 Get the number of GPUs in the system

int deviceCount;
cudaGetDeviceCount(&deviceCount);

5.2 Get specified GPU device information

cudaDeviceProp devProp;
cudaGetDeviceProperties(&devProp, 0);	//取第0个GPU设备属性

5.3 Select the GPU device according to the specified attribute requirements

//定义需要的设备属性
cudaDeviceProp devicePropDefined;
memset(&devicePropDefined, 0, sizeof(cudaDeviceProp));  //设置devicepropDefined的值
devicePropDefined.major = 5;
devicePropDefined.minor = 2;

int devicedChoosed;  //选中的设备ID
cudaChooseDevice(&devicedChoosed, &devicePropDefined);  //查找符合要求的设备ID
cudaError_t cudaError = cudaSetDevice(devicedChoosed); //设置选中的设备为下文的运行设备

5.4 View the currently selected GPU device

int devicedChoosed;  //选中的设备ID
cudaGetDevice(&devicedChoosed);  //获取当前设备ID

reference

https://www.cnblogs.com/skyfsm/p/9673960.html

Guess you like

Origin blog.csdn.net/hangl_ciom/article/details/121019806