Chapter 2
Getting Started
We hope that Chapter 1 has gotten you excited to get started learning CUDA C.
Since this book intends to teach you the language through a series of coding
examples, you’ll need a functioning development environment. Sure, you could
stand on the sideline and watch, but we think you’ll have more fun and stay
interested longer if you jump in and get some practical experience hacking
CUDA C code as soon as possible. In this vein, this chapter will walk you
through some of the hardware and software components you’ll need in order to
get started. The good news is that you can obtain all of the software you’ll need
for free, leaving you more money for whatever tickles your fancy.
13
GettInG stArted
Chapter Objectives
Development Environment
Before embarking on this journey, you will need to set up an environment in which
you can develop using CUDA C. The prerequisites to developing code in CUDA C
are as follows:
• A CUDA-enabled graphics processor
• An NVIDIA device driver
• A CUDA development toolkit
• A standard C compiler
To make this chapter as painless as possible, we’ll walk through each of these
prerequisites now.
2.2.1 CUDA-ENABLED GRAPHICS PROCESSORS
Fortunately, it should be easy to find yourself a graphics processor that has
been built on the CUDA Architecture because every NVIDIA GPU since the 2006
release of the GeForce 8800 GTX has been CUDA-enabled. Since NVIDIA regularly
releases new GPUs based on the CUDA Architecture, the following will undoubt-
edly be only a partial list of CUDA-enabled GPUs. Nevertheless, the GPUs are all
CUDA-capable.
For a complete list, you should consult the NVIDIA websitewww.nvidia.com/cuda,
at
although it is safe to assume that all recent GPUs (GPUs from 2007 on) with more
than 256MB of graphics memory can be used to develop and run code written
with CUDA C.
14
nvironment
develoPment envIronment
Table 2.1 CUDA-enabled GPUs
GeForce GTX 480 GeForce 8300 mGPU Quadro FX 5600
GeForce GTX 470 GeForce 8200 mGPU Quadro FX 4800
GeForce GTX 295 GeForce 8100 mGPU Quadro FX 4800 for Mac
GeForce GTX 285 Tesla S2090 Quadro FX 4700 X2
GeForce GTX 285 for Mac Tesla M2090 Quadro FX 4600
GeForce GTX 280 Tesla S2070 Quadro FX 3800
GeForce GTX 275 Tesla M2070 Quadro FX 3700
GeForce GTX 260 Tesla C2070 Quadro FX 1800
GeForce GTS 250 Tesla S2050 Quadro FX 1700
GeForce GT 220 Tesla M2050 Quadro FX 580
GeForce G210 Tesla C2050 Quadro FX 570
GeForce GTS 150 Tesla S1070 Quadro FX 470
GeForce GT 130 Tesla C1060 Quadro FX 380
GeForce GT 120 Tesla S870 Quadro FX 370
GeForce G100 Tesla C870 Quadro FX 370 Low Profile
GeForce 9800 GX2 Tesla D870 Quadro CX
GeForce 9800 GTX+ QUADro mobile Quadro NVS 450
ProDUCtS
GeForce 9800 GTX Quadro NVS 420
Quadro FX 3700M
GeForce 9800 GT Quadro NVS 295
Quadro FX 3600M
GeForce 9600 GSO Quadro NVS 290
Quadro FX 2700M
GeForce 9600 GT Quadro Plex 2100 D4
Quadro FX 1700M
GeForce 9500 GT Quadro Plex 2200 D2
Quadro FX 1600M
GeForce 9400GT Quadro Plex 2100 S4
Quadro FX 770M
GeForce 8800 Ultra Quadro Plex 1000 Model IV
Quadro FX 570M
GeForce 8800 GTX GeForCe mobile
Quadro FX 370M ProDUCtS
GeForce 8800 GTS
Quadro FX 360M GeForce GTX 280M
GeForce 8800 GT
Quadro NVS 320M GeForce GTX 260M
GeForce 8800 GS
Quadro NVS 160M GeForce GTS 260M
GeForce 8600 GTS
Quadro NVS 150M GeForce GTS 250M
GeForce 8600 GT
Quadro NVS 140M GeForce GTS 160M
GeForce 8500 GT
Quadro NVS 135M GeForce GTS 150M
GeForce 8400 GS
Quadro NVS 130M GeForce GT 240M
GeForce 9400 mGPU
Quadro FX 5800 GeForce GT 230M
GeForce 9300 mGPU
Continued
15
GettInG stArted
Table 2.1 CUDA-enabled GPUs (Continued)
GeForce GT 130M GeForce 9700M GTS GeForce 9200M GS
GeForce G210M GeForce 9700M GT GeForce 9100M G
GeForce G110M GeForce 9650M GS GeForce 8800M GTS
GeForce G105M GeForce 9600M GT GeForce 8700M GT
GeForce G102M GeForce 9600M GS GeForce 8600M GT
GeForce 9800M GTX GeForce 9500M GS GeForce 8600M GS
GeForce 9800M GT GeForce 9500M G GeForce 8400M GT
GeForce 9800M GTS GeForce 9300M GS GeForce 8400M GS
GeForce 9800M GS GeForce 9300M G
2.2.2 nvIdIA devIce drIver
NVIDIA provides system software that allows your programs to communicate
with the CUDA-enabled hardware. If you have installed your NVIDIA GPU properly,
you likely already have this software installed on your machine. It never hurts
to ensure you have the most recent drivers, so we recommend that you visit
www.nvidia.com/cuda and click the Download Drivers link. Select the options that
match the graphics card and operating system on which you plan to do develop-
ment. After following the installation instructions for the platform of your choice,
your system will be up-to-date with the latest NVIDIA system software.
2.2.3 cudA develoPment toolKIt
If you have a CUDA-enabled GPU and NVIDIA’s device driver, you are ready to run
compiled CUDA C code. This means that you can download CUDA-powered appli-
cations, and they will be able to successfully execute their code on your graphics
processor. However, we assume that you want to do more than just run code
because, otherwise, this book isn’t really necessary. If you want to develop code
for NVIDIA GPUs using CUDA C, you will need additional software. But as prom-
ised earlier, none of it will cost you a penny.
You will learn these details in the next chapter, but since your CUDA C applica-
tions are going to be computing on two different processors, you are consequently
going to need two compilers. One compiler will compile code for your GPU, and
one will compile code for your CPU. NVIDIA provides the compiler for your GPU
code. As with the NVIDIA device driver, you can download the CUDA Toolkit at
http://developer.nvidia.com/object/gpucomputing.html.Click the CUDA Toolkit
link to reach the download page shown in Figure 2.1.
16
nvironment
develoPment envIronment
Figure 2.1 The CUDA download page
17
GettInG stArted
You will again be asked to select your platform from among 32- and 64-bit
versions of Windows XP, Windows Vista, Windows 7, Linux, and Mac OS. From the
available downloads, you need to download the CUDA Toolkit in order to build the
code examples contained in this book. Additionally, you are encouraged, although
not required, to download the GPU Computing SDK code samples, which contains
dozens of helpful example programs. The GPU Computing SDK code samples will
not be covered in this book, but they nicely complement the material we intend
to cover, and as with learning any style of programming, the more examples, the
better. You should also take note that although nearly all the code in this book will
work on the Linux, Windows, and Mac OS platforms, we have targeted the appli-
cations toward Linux and Windows. If you are using Mac OS X, you will be living
dangerously and using unsupported code examples.
2.2.4 stAndArd c comPIler
As we mentioned, you will need a compiler for GPU code and a compiler for
CPU code. If you downloaded and installed the CUDA Toolkit as suggested in the
previous section, you have a compiler for GPU code. A compiler for CPU code is
the only component that remains on our CUDA checklist, so let’s address that
issue so we can get to the interesting stuff.
wIndows
On Microsoft Windows platforms, including Windows XP, Windows Vista, Windows
Server 2008, and Windows 7, we recommend using the Microsoft Visual Studio C
compiler. NVIDIA currently supports both the Visual Studio 2005 and Visual Studio
2008 families of products. As Microsoft releases new versions, NVIDIA will likely
add support for newer editions of Visual Studio while dropping support for older
versions. Many C and C++ developers already have Visual Studio 2005 or Visual
Studio 2008 installed on their machine, so if this applies to you, you can safely
skip this subsection.
If you do not have access to a supported version of Visual Studio and aren’t ready
to invest in a copy, Microsoft does provide free downloads of the Visual Studio
2008 Express edition on its website. Although typically unsuitable for commercial
software development, the Visual Studio Express editions are an excellent way to
get started developing CUDA C on Windows platforms without investing money in
software licenses. So, head on over towww.microsoft.com/visualstudio if you’re
in need of Visual Studio 2008!
18
eview
eview
inux
Most Linux distributions typically ship with a version of the GNU C compiler
(gcc) installed. As of CUDA 3.0, the following Linux distributions shipped with
supported versions of gcc installed:
• Red Hat Enterprise Linux 4.8
• Red Hat Enterprise Linux 5.3
• OpenSUSE 11.1
• SUSE Linux Enterprise Desktop 11
• Ubuntu 9.04
• Fedora 10
If you’re a die-hard Linux user, you’re probably aware that many Linux software
packages work on far more than just the “supported” platforms. The CUDA
Toolkit is no exception, so even if your favorite distribution is not listed here, it
may be worth trying it anyway. The distribution’s kernel, gcc, and glibc versions
will in a large part determine whether the distribution is compatible.
MACINTOSH OS X
If you want to develop on Mac OS X, you will need to ensure that your machine
has at least version 10.5.7 of Mac OS X. This includes version 10.6, Mac OS X
“Snow Leopard.” Furthermore, you will need to install gcc by downloading
and installing Apple’s Xcode. This software is provided free to Apple Developer
Connection (ADC) members and can be downloaded from http://developer.apple.
com/tools/Xcode. The code in this book was developed on Linux and Windows
platforms but should work without modification on Mac OS X systems.
Chapter Review
If you have followed the steps in this chapter, you are ready to start developing
code in CUDA C. Perhaps you have even played around with some of the NVIDIA
GPU Computing SDK code samples you downloaded from NVIDIA’s website. If so,
we applaud your willingness to tinker! If not, don’t worry. Everything you need is
right here in this book. Either way, you’re probably ready to start writing your first
program in CUDA C, so let’s get started.
19