Go Cuda in Windows under the Software development environment to build

Source: Internet
Author: User
Tags cuda toolkit nvcc

Citation: http://www.makaidong.com/yaoyuanzhi/archive/2010/11/13/1876215.html

In this paper, we use Visual Studio 2005 as an example to demonstrate CUDA installation and software development environment, as well as CUDA and MFC to the implementation of the joint.

1. CUDA installation Package

Cuda is free to use, the CUDA installation packages under various operating systems can be downloaded for free on http://www.nvidia.cn/object/cuda_get_cn.html. CUDA offers 3 installation packages, namely: Driver, toolkit and SDK. The SDK includes many examples of programs and libraries of functions. Toolkit includes the basic tools for Cuda . Install driver, then Toolkit, and finally SDK in order.

CUDA Toolkit

After installation , there are 6 folders in the Nvidia_cuda_toolkit installation directory, respectively:

Bin: Tool programs and dynamic link libraries

Doc: Related Documents

Include:header Header File Package

LIB: Program Library

Open64: Cuda compiler based on Open64

SRC: Part of the original code

During the installation, Toolkit automatically set 3 environment variables: cuda_bin_path, Cuda_inc_path, and Cuda_lib_path respectively, corresponding to the tool library, the header file library and the library, the default path is the bin under the current installation folder, Include and Lib three folders.

Cuda SDK

The SDK can choose to install as needed (recommended installation, because many of the example programs and libraries in the SDK are useful.)

Cuda Display

For computers that do not have an NV video card installed, the display installation package does not need to be installed, and the program can run in emulation mode.

2. Using Cuda in Visual Studio2005

Cuda's main tool is NVCC, which executes the required program to compile and execute the CUDA program. This article describes several ways to configure Cuda NVCC in a Windows environment based on the VS 2005ide environment.

2.1 NV Self-Adapting template method

Create a new folder under the project directory in the CUDA SDK installation directory, named for the project name you want to create, such as Test1. Under the project folder, locate the template folder that comes with the SDK and copy all the files under the template folder to Test1.

Change the template in the file name of all files under Copy to Test1 folder to test (that is, your project file name). Test.sln and test.vcproj are engineering files for the VS 8 series, Test_vc7.sln and test_vc7.vcproj are engineering files for the VS 7 series, and you can select the project files to change according to your version of Visual Studio. The other two can be deleted. In Visual Studio C + + 2005 Express, you can delete Test_vc7.sln and test_vc7.vcproj.

Open *.cu, *.sln, and *.vcproj with other text editing software like Notepad, and use the Find and replace function to change all the template in the above file to test.

Using the *.sln file to open the entire project, you can change the code arbitrarily, compile the run.

Modify the output file path (optional, if not changed, the executable file is output to the bin directory on the top level two directory), such as using a dynamic link library in the Cuda SDK, to copy the corresponding dynamic link library to the same directory as the executable file.

Summary: This approach is an NV company's standard approach for using VS-Build Cuda for Windows (see \sdk\doc\cuda_sdk_release_notes_windows.txt), using the company-provided templates to change to the work you want to build

This article from: Macaidong Blog Reprint please specify the source URL: http://www.makaidong.com

Process, you can also refer to the above methods in the SDK project library to find other projects similar to the one you want to build the project to make changes.

2.2 Creating a. cu file in the Win32 project

First build a C + + Win32 Console application, empty project, and add a new source file, with Main.cu as an example.

P1. Building a Win32 Project

Set to Empty Project

P3. Creating a Main.cu file dedicated to Cuda programs

Right-click on Main.cu in Solution Explorer and select Property. In the dialog box that opens, select General, and the option to determine the tool is the custom build tool.

Confirm that the tool option is the custom build tool.

Select Custom Bulid step to set the mode parameters separately in command line. There are several compilation methods to set command line parameters.

(1), Release mode: "$ (Cuda_bin_path) \nvcc.exe"-ccbin "$ (VCInstallDir) bin"-c-dwin32-d_console-d_mbcs-xcompiler/ehsc,/ W3,/nologo,/wp64,/o2,/zi,/mt-i "$ (cuda_inc_path)"-O $ (configurationname) \$ (inputname). obj $ (inputfilename)

(2), Debug mode: "$ (Cuda_bin_path) \nvcc.exe"-ccbin "$ (VCInstallDir) bin"-c-d_debug-dwin32-d_console-d_mbcs-xcompiler/ Ehsc,/w3,/nologo,/wp64,/od,/zi,/rtc1,/mtd-i "$ (cuda_inc_path)"-O $ (configurationname) \$ (inputname). obj $ ( InputFileName)

If you do not have an NV video card installed on your computer, you will need to add two additional settings when using analog mode. Create a new two modes emurelease and Emudebug.

P6. New Mode (1)

P6. New Mode (2)

The command line for the newly created emurelease and Emudebug are set to:

(1), Emurelease mode: "$ (Cuda_bin_path) \nvcc.exe"-ccbin "$ (VCInstallDir) bin"-deviceemu-c-dwin32-d_console-d_mbcs- Xcompiler/ehsc,/w3,/nologo,/wp64,/o2,/zi,/mt-i "$ (cuda_inc_path)"-O $ (configurationname) \$ (inputname). obj $ ( InputFileName)

(2), Emudebug mode: "$ (Cuda_bin_path) \nvcc.exe"-ccbin "$ (VCInstallDir) bin"-deviceemu-c-d_debug-dwin32-d_console- D_mbcs-xcompiler/ehsc,/w3,/nologo,/wp64,/od,/zi,/rtc1,/mtd-i "$ (cuda_inc_path)"-O $ (configurationname) \$ ( InputName). obj $ (inputfilename)

3.1.4, for all set modes, are included in the outputs of custom build step $ (ConfigurationName) \$ (inputname). obj.

P7. Setting outputs

Right-click Project, select Property, and then select Linker to modify the following settings for all modes:

(1), general/enable incremental Linking:no

(2), General/additional library directories:$ (Cuda_lib_path)

(3), input/additional Dependencies:cudart.lib

P8. Setting linker parameters (1)

P9. Setting linker parameters (2)

Set header file path tools, options, pop-up the following dialog box

Select Include files from the drop-down box on the right, click the Add button, and the C:\Program Files\nvidia Corporation\nvidia Cuda sdk\common\inc selected in the header file search path.

Select link Library file path

Above the library header file path selection, the right drop-down box to select the library file, select the path c: \ Programfiles\nvidia Corporation\nvidia Cuda sdk\common\lib.

Complete, you can write the Cuda program, and directly compile the execution (compiled in the same way as C + +)

Summary: This approach requires neither a template nor the use of third-party software. However, the operation is quite complex and is only valid for a single project that is configured. If you re-establish a project and reconfigure it again.

Note that when you need to use the dynamic-link library in the Cuda SDK (as a DLL suffix), you place the dynamic-link library and the resulting executable file under the same path.

If it is the driver API, you need to add "Cuda.lib cudart.lib", header file # include, #include.

2.3 Using the Cuda_vs_wizard plugin

Download the Cuda_vs_wizard plugin and install it.

Add the paths of the four folders (debug, Emudebug, Release, Emurelease) in the Bin\win32 folder under the Cuda installation directory under the bin and Cuda SDK installation directory to the path variable in the environment variable.

Use VC directly to establish the project. Select Cudawinapp in the project options.

If you are running in emulation mode, you need to set the configuration to EMU mode in the configuration properities by right-clicking the property in the solution.

Analog mode settings (1)

Analog mode setting (2).

Summary: Although this approach uses third-party tools, it only needs to be configured once, and the configuration is simple, and each subsequent Cuda program can be generated directly. If it is a project to establish a predefined header file, the CUDA program will be built with a sample.cu, you can choose to change or delete the sample.cu. cu file on sample.cu basis. If it is an empty project, it is not. The method for building a method. cu file is established. cpp file or. txt file is renamed to. cu files. Recommended use.

2.4 Syntax highlighting settings.

Locate the doc\syntax_highlighting\ file Usertypt.dat in the Cuda SDK installation directory and copy it to \Common7\IDE under the Microsoft Visual Studio installation directory.

In Visual Studio, make the following settings: add cu in the tools-> options->text editor-> file extension. Edit the way Microsoft Visual C + +.

P16. Setting highlighting

Restart Visual Studio to finish highlighting the settings.

??

3. Configure the build rule. Copy the cuda,rules in the C/common directory under the SDK directory to the Vc\vcprojectdefaults subdirectory under the VS installation directory. Build a Win32 project and right-click on the project, select Custombuildrules, tick before cuda build rule. Create a new. cu file in the project, and right-click the property to see if the custom build rule is already cuda build.

3. Cuda in MFC in the example of the method of 3.1 in the SDK program compilation

For example, our SDK installation path "C:\Program Files\nvidia Corporation\nvidia cuda sdk\projects\devicequery" can be found in Devicequery's engineering files.

When we copy this folder to a different directory, there will be an error finding the file, such as "Cutil.h not found". Why does this happen? We note that the reference to it in Devicequery, # include <cutil.h>, is in the system directory. This system directorycan be found cutil.h header files in the C: \ Program files\ nvidia Corporation\nvidia Cuda sdk\common\inc.

By looking at the properties of the DEVICEQUERY.CU, watch "command line":

"$ (Cuda_bin_path) \nvcc.exe"-ccbin "$ (VCInstallDir) bin"-c-d_debug-dwin32-d_console-d_mbcs-xcompiler/ehsc,/w3,/ Nologo,/wp64,/od,/zi,/rtc1,/mtd-i "$ (cuda_inc_path)"-i./-ihttp://www.makaidong.com/common/inc-o $ ( ConfigurationName) \devicequery.obj devicequery.cu

Note the Red Underline section, and then go back two folders, and then enter the INC folder in the Commom folder. Compare the previous two paths, which happens to be an access to the Decicequery.sin file to cutil.h.

When we copy the project files to a new directory, such an access method will inevitably expire. So we need to do some of the following settings.

1. First we need to add the four folders under the C:\Program Files\nvidia Corporation\nvidia cuda sdk\bin\win32 file directory to the environment variables respectively.

The specific way to do this is: "My Computer", properties, high -level environment variable path, note that the path is the full path, between each variable with ";" Separated.

2. Notice that the system variable nvsdkcuda_root is the C: \ Programfiles\nvidia Corporation\nvidia Cuda SDK.

Once we have transferred the project file directory, we only need to modify the command line. The previous Red Line section will be modified as follows:

"$ (Cuda_bin_path) \nvcc.exe"-ccbin "$ (VCInstallDir) bin"-c-d_debug-dwin32-d_console-d_mbcs-xcompiler/ehsc,/w3,/ Nologo,/wp64,/od,/zi,/rtc1,/mtd-i "$ (cuda_inc_path)"-i./-i "$ (nvsdkcuda_root)/common/inc"-O $ (configurationname) \ devicequery.obj devicequery.cu

3. In addition, we need to make corresponding changes to the "linker" of the "Project Properties". The program can then be compiled and run normally.

3.2 Application of CUDA program in MFC

This chapter builds an MFC project and then adds a call to the Cuda program in the CU file in the project. This chapter also provides reference to the program sample cppintegration in the SDK .

3.2.1 Configuring VisualStudio Environment Configuration Preparation

syntax highlighting : Copy the Usertype.dat file from D:\programming\cuda\sdk\doc\syntax_highlighting\visual_studio_8 to Microsoft Under the Visual Studio 8\Common7\IDE directory (appended to the original if it already exists).

Set VS2005 Environment (since this program will not only be in the CU use Cuda in Files function, which is also included in the CPP files, so you need to include these libraries):

Enter tools|options|projects and solutions|vc++directories add:

Include Files:

D:\programming\cuda\toolkit\include

D:\programming\cuda\sdk\common\inc

Library files:

D:\programming\cuda\toolkit\lib

D:\programming\cuda\sdk\common\lib

SOURCE Files:d:\Programming\cuda\sdk\common\src

text Editor Settings : Enter VC + + Project settings:c/c++ file extensions: Add *.cu, in text editor-file extension: Add CU    Corresponding editor to Microsoft VC + + editor.

Visual Assist X Setup (if required): Close all open Visual Studio, install VA, and then go to Registry Editor: Hkey_current_user\software\whole Tomato\vanet8 Locate the Extsource item on the right and add its value. cu;. Cuh, then close and open vs2005 again. )

3.2.2 Creating global functions and header files

First we establish global functions in the header and resource files for invocation:

The Testcuda function here uses the extern keyword to declare the C language extension.

We can see the global function when we look at the Class View:

3.2.3 Creating Cuda Code

To make this clear, we first created a filter named Cuda, and then we created two source code files, named First.cu and First_kernel.cu, in the filter. Then add the following code:

#include "stdio.h"

#include "cutil.h"

#include "First_kernel.cu"

extern "C" void Runtest (float *source,int datalen,float *result)

{

int count;

Cudagetdevicecount (&count);

if (count = = 0) {

fprintf (stderr, "There is no device.\n");

*result=-1;

}

int i;

for (i = 0; i < count; i++) {

CudaDeviceprop prop;

if (Cudagetdeviceproperties (&prop, i) = = cudasuccess) {

if (prop.major >= 1) {

Break

}

}

}

if (i = = count) {

fprintf (stderr, "There is no device supporting Cuda 1.x.\n");

*result=-1;

}

float * D_SOURCE,*D_RESULT;

Cudamalloc ((void**) &d_source,datalen*sizeof (float));

Cudamalloc ((void**) &d_result,sizeof (float));

cudamemcpy (d_source,source,datalen*sizeof (float), cudamemcpyhosttodevice);

Kernel<<<1,256,0>>> (D_source,datalen,d_result);

cudamemcpy (result,d_result,sizeof (float), cudamemcpydevicetohost);

Cudafree (D_source);

Cudafree (D_result);

}

and the kernel functions (that is, the parallel part) added in FIRST_KERNEL.CU:

#ifndef _first_kernel_h_

#define _first_kernel_h_

__global__ void Kernel (float *source,int len,float *result)

{

int i;

float sum;

sum=0;

for (i=0;i<len;i++)

sum+=* (Source+i);

*result=sum;

}

#endif

3.2.3 Modifying linker settings

The first is the First.cu property setting:

Set the tools in general to the custom build tool.

Then, in the command line, refer to setting in 3.1. It is important to note that "additional dependencies" are first_kernel.cu.

In the First_kernel.cu property setting, we need to exclude the file that holds the kernel function from the build.

Finally, set the properties of the project, and still refer to 3.1 for some changes in the properties of the linker in Project properties.

&nb

Search this article: CUDA in Windows under the Software development environment to build

This article links: http://www.makaidong.com/%E5%8D%9A%E5%AE%A2%E5%9B%AD%E6%B1%87/38217.shtml

Reprint Please specify source: Cuda under Windows software Development environment Construction-Blog Park

Go Cuda in Windows under the Software development environment to build

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.