Use Cuda and thrust in Visual Studio

Source: Internet
Author: User
Tags visual studio 2010

Http://cuda.it168.com/a2011/0622/1208/000001208129_all.shtml

[It168] I am creating some new Cuda prototype projects to figure out how to best use Cuda 4.0. I will write it as a quick tutorial, shows you how to use Cuda in Visual Studio 2010 and the latest C ++ 0x feature to write a simple application.

Because the Cuda SDK currently requires that the project be compiled using the v90 toolset (Visual Studio 2008), the solution requires two projects, one DLL project that includes the Cuda target v90, and the other is the target v100 (Visual Studio 2010) A project that contains C ++ code.

Install dependency

Make sure you have installed the following dependent software packages

● Visual Studio 2010 and 2008 SP1 (required by Cuda)

● Parallel nsight 1.51

● Cuda 4.0 RC or 3.2, and thrust

The examples referenced in this article all use Cuda 4.0 RC. If you do not install Cuda 4.0, they can also work in the 3.2 Environment.

Set Solution

To create a solution that contains two projects, you need two projects because one target is the v100 (Visual Studio 2010) compiler, which allows you to use the latest C ++ 0x language features, another goal is the v90 (Visual Studio 2008) compiler because Cuda needs it.

1) create a Win32 console application named helloworld. Keep the default settings in all settings windows of the Wizard. This project will contain the main entry point of the application and all windows-specific code, for example, parallel patterns Library (PPL) is used to manage threads ).


▲Figure 1 Create A Win32 console application

2) create a second Win32 project named helloworldcuda. This dll will contain your Cuda code. In the application settings window, select DLL for the application type and select the check box for the blank project.


▲Figure 2 create a Win32 Project


▲Figure 3 in the additional options, select "Blank Project"

Configure a Cuda Project

Many settings need to be configured in the helloworldcuda project.

3) configure the helloworldcuda Project

3.1) Select the "project" * "generate custom" menu option and select the Cuda 4.0 Project in the dialog box. This step adds support for the Cuda C/C ++ file, however, before the project property generation settings window is displayed, you still need one. cu rule file. If you do not have Cuda 4.0, use the 3.2 rule.


▲Figure 4 select Cuda 4.0 in the generate custom dialog box

3.2) Add two new files to the project named hello. cpp c ++ file (. CPP) and a hello. h header file (. h), set. rename the CPP file to hello. cu, your solution tree structure should look like this:


▲Figure 5 solution Tree Structure

3.3) Select the hello. Cu file and open its Properties window. On the "General" tab, change "project type" to "Cuda C/C ++ ".


▲Figure 6 set the project type to Cuda C/C ++

3.4) Select a project and open the Properties window (Alt + press Enter). On the "General" tab, set the "platform toolset" field to "v90". If you cannot set this field, it may be because Visual Studio 2008 is not installed. Remind me again that Cuda needs it.


▲Figure 7 set the platform tool set

3.5) Open the "linking program" * "general" attribute window, and add "$ (cuda_path_v4_0) \ Lib \ $ (Platform);" in the "additional library directory" field );".


▲Figure 8 set the additional library directory

Now you can see the Cuda C/C ++ Properties tab.

3.6) Open the "link program" * "input" attribute window and add "cudart. Lib;" in the "add dependency" field ;".


▲Figure 9 set additional dependencies

3.7) Make sure that your project is always generated in the correct order. Right-click the helloworld project, select "project dependency", and select the helloworldcuda check box next to it, this forces the helloworldcuda project to be generated before helloworld.

4) at this point, the generated solution should have no warning or error.

Add some cuda/thrust code

Now it is time to add some code. We need to add some cuda code in helloworldcuda DLL and output it so that the helloworld application can execute it.

5) configure the helloworld project. It must be linked to helloworldcuda and be able to access the corresponding header file.

5.1) Open the "linking program" * "general" attribute window, and add ".. \ $ (configuration); $ (cuda_path_v4_0) \ Lib \ $ (Platform );".


▲Figure 10 set the additional library directory

5.2) Open the "link program" * "input" attribute window, and add "cudart. Lib; helloworldcuda. Lib;" in the "Additional dependency" field ;".


▲Figure 11 configure additional dependencies

5.3) Open the "C/C ++" * "general" attribute window, and add ".. \ helloworldcuda \; $ (cuda_path_v4_0) \ include ;".


▲Figure 12 add an included directory

5.4) Click the "project" * "project dependency" menu option and select the helloworldcuda check box to make the Cuda project a dependency for the Win32 main application project.


▲Figure 13 make helloworldcuda a dependency for the helloworld Project

6) Now it's time to write some code. Cuda 4.0 introduces thrust, so I plan to use thrust in the example given. If you do not use 4.0, You need to download the latest thrust library, copy it to the Cuda SDK including the file "% cuda_path % \ include \ thrust.

The example is a hello World application, so the code is very simple. It is a variant of the first example on the thrust project homepage.

Add the following class declaration to hello. in the H file, most of the Code fixes the compilation warning. It is necessary to declare a class constructed using host_vector, and then there are some methods to execute the Cuda code and return the result.

Related Article

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.