1 \chapterauthor{Author Name1}{Affiliation text1}
2 \chapterauthor{Author Name2}{Affiliation text2}
5 \chapter{Introduction to CUDA}
8 \section{Introduction}\label{intro}
9 In this chapter we give some simple examples on CUDA programming. The goal is
10 not to provide an exhaustive presentation of all the functionalities of CUDA but
11 rather giving some basic elements. Of course, readers that do not know CUDA are
12 invited to read other books that are specialized on CUDA programming.
15 \section{First example}
17 This first example is intented to show how to build a very simple example with
18 CUDA. The goal of this example is to performed the sum of two arrays and
19 putting the result into a third array. A cuda program consists in a C code
20 which calls CUDA kernels that are executed on a GPU.
23 As GPUs have their own memory, the first step consists in allocating memory on
24 the GPU. A call to \texttt{cudaMalloc} allows to allocate memory on the GPU. The
25 first parameter of this function is a pointer on a memory on the device
26 (i.e. the GPU). In this example, \texttt{d\_} is added on each variable allocated
27 on the GPU meaning this variable is on the GPU. The second parameter represents
28 the size of the allocated variables, this size is in bits.
30 In this example, we want to compare the execution time of the additions of two
31 arrays in CPU and GPU. So for both these operations, a timer is created to
32 measure the time. CUDA proposes to manipulate timers quick easily. The first
33 step is to create the timer, then to start it and at the end to stop it. For
34 each of these operations a dedicated functions is used.
36 In order to compute the same sum with a GPU, the first step consits in
37 transferring the data from the CPU (considered as the host with CUDA) to the GPU
38 (considered as the device with CUDA). A call to \texttt{cudaMalloc} allows to
39 copy the content of an array allocated in the host to the device when the fourth
40 parameter is set to \texttt{cudaMemcpyHostToDevice}. The first parameter of the
41 function is the destination array, the second is the source array and the third
42 is the number of elements to copy (exprimed in bytes).