How to Start CUDA

Referenced from NVIDIA CUDA Getting Started Guide for Microsoft Windows

NVIDIA CUDA Getting Started Guide for Microsoft Windows

 Introduction

CUDA™ enables dramatic increases in computing performance by harnessing the power of the graphics processing unit (GPU).

CUDA was developed with several design goals in mind:

  • Provide a small set of extensions to standard programming languages, like C, that enable a straightforward implementation of parallel algorithms. With CUDA C/C++, programmers can focus on the task of parallelization of the algorithms rather than spending time on their implementation.
  • Support heterogeneous computation where applications use both the CPU and GPU. Serial portions of applications are run on the CPU, and parallel portions are offloaded to the GPU. As such, CUDA can be incrementally applied to existing applications. The CPU and GPU are treated as separate devices that have their own memory spaces. This configuration also allows simultaneous computation on the CPU and GPU without contention for memory resources.

CUDA-capable GPUs have hundreds of cores that can collectively run thousands of computing threads. These cores have shared resources including a register file and a shared memory. The on-chip shared memory allows parallel tasks running on these cores to share data without sending it over the system memory bus.

This guide will show you how to install and check the correct operation of the CUDA development tools.

Compiling CUDA Programs

The project files in the CUDA Samples have been designed to provide simple, one-click builds of the programs that include all source code. To build the 32-bit or 64-bit Windows projects (for release or debug mode), use the provided
*.sln solution files for Microsoft Visual Studio 2008 or 2010 (and likewise for the corresponding versions of Microsoft Visual C++ Express Edition). You can use either the solution files located in each of the examples directories in

CUDA Samples\v5.0\C\<category>\<sample_name>

or the global solution files

Samples*.slnlocated in

CUDA Samples\v5.0\C

CUDA Samples are organized according to

<category>. Each sample is organized into one of the following folders: (0_Simple,1_Utilities,2_Graphics,3_Imaging,4_Finance,5_Simulations,6_Advanced,7_CUDALibraries).

Compiling Sample Projects

The

bandwidthTestproject is a good sample project to build and run. It is located in the

NVIDIA Corporation\CUDA Samples\v5.0\C\1_Utilities\bandwidthTestdirectory.

The output is placed in

CUDA Samples\C\v5.0\bin\win32\Release. (As mentioned previously, the

\win32segment of this address will be

\win64on 64-bit versions of Windows.)

This location presumes that you used the default installation directory structure.

Build the program using the appropriate solution file and run the executable. If all works correctly, the output should be similar to Figure 2.

Sample Projects

The sample projects come in two configurations: debug and release (where release contains no debugging information).

A few of the example projects require some additional setup. The

simpleD3D9example requires the system to have a Direct3D SDK installed and the Visual C++ directory paths (located in Tools > Options…) properly configured. Consult the Direct3D documentation for additional details.

These sample projects also make use of the

$CUDA_PATHenvironment variable to locate the CUDA Toolkit and a

.rulesfile to locate and configure the

nvcccompiler. The environment variable is set automatically and the

.rulesfile is installed automatically as part of the CUDA Toolkit installation process. The

.rulesfile is installed into

$VisualStudioInstallDir\VC\VCProjectDefaults. You can reference this

.rulesfile from your Visual Studio project files when building your own CUDA applications.

Build Customizations for New Projects

When creating a new CUDA application, the Visual Studio project file must be configured to include CUDA build customizations. To accomplish this, click File-> New | Project… NVIDIA-> CUDA->, then select a template for your CUDA Toolkit version. For example, selecting the “CUDA 5.0 Runtime” template will configure your project for use with the CUDA 5.0 Toolkit. The new project is technically a C++ project (.vcxproj) that is preconfigured to use NVIDIA’s Build Customizations. All standard capabilities of Visual Studio C++ projects will be available.

To specify a custom CUDA Toolkit location, under CUDA C/C++, select Common, and set the CUDA Toolkit Custom Dir field as desired. Note that the selected toolkit must match the version of the Build Customizations.

Build Customizations for Existing Projects

When adding CUDA acceleration to existing applications, the relevant Visual Studio project file must be updated to include CUDA build customizations. For Visual Studio 2010, this can be done using one of the following two methods:

  1. Open the Visual Studio 2010 project, right click on the project name, and select Build Customizations…, then select the CUDA Toolkit version you would like to target.
  2. Alternatively, you can configure your project always to build with the most recently installed version of the CUDA Toolkit. First add a CUDA build customization to your project as above. Then, right click on the project name and select Properties. Under CUDA C/C++, select Common, and set the CUDA Toolkit Custom Dir field to
    $(CUDA_PATH) .

While Option 2 will allow your project to automatically use any new CUDA Toolkit version you may install in the future, selecting the toolkit version explicitly as in Option 1 is often better in practice, because if there are new CUDA configuration options added to the build customization rules accompanying the newer toolkit, you would not see those new options using Option 2.

Note for advanced users: If you wish to try building your project against a newer CUDA Toolkit without making changes to any of your project files, go to the Visual Studio 2010 command prompt, change the current directory to the location of your project, and execute a command such as the following:

msbuild <projectname.extension> /t:Rebuild /p:CudaToolkitDir="drive:/path/to/new/toolkit/"