\chapterauthor{Author Name1}{Affiliation text1}
\chapterauthor{Author Name2}{Affiliation text2}


\chapter{Introduction to CUDA}
\label{chapter2}

\section{Introduction}\label{intro}
In this chapter  we give some simple examples on CUDA  programming.  The goal is
not to provide an exhaustive presentation of all the functionalities of CUDA but
rather giving some basic elements. Of  course, readers that do not know CUDA are
invited to read other books that are specialized on CUDA programming (for example: \cite{Sanders:2010:CEI}).


\section{First example}

This first example is  intented to show how to build a  very simple example with
CUDA.   The goal  of this  example is  to performed  the sum  of two  arrays and
putting the  result into a  third array.   A cuda program  consists in a  C code
which calls CUDA kernels that are executed on a GPU. The listing of this code is in Listing~\ref{ch2:lst:ex1}


As GPUs have  their own memory, the first step consists  in allocating memory on
the GPU. A call to \texttt{cudaMalloc} allows to allocate memory on the GPU. The
first  parameter of  this  function  is a  pointer  on a  memory  on the  device
(i.e. the GPU). In this example, \texttt{d\_} is added on each variable allocated
on the GPU meaning this variable  is on the GPU. The second parameter represents
the size of the allocated variables, this size is in bits.

In this example, we  want to compare the execution time of  the additions of two
arrays in  CPU and  GPU. So  for both these  operations, a  timer is  created to
measure the  time. CUDA proposes to  manipulate timers quick  easily.  The first
step is  to create the timer, then  to start it and  at the end to  stop it. For
each of these operations a dedicated functions is used.

In  order to  compute  the  same sum  with  a GPU,  the  first  step consits  in
transferring the data from the CPU (considered as the host with CUDA) to the GPU
(considered as the  device with CUDA).  A call  to \texttt{cudaMalloc} allows to
copy the content of an array allocated in the host to the device when the fourth
parameter is set to  \texttt{cudaMemcpyHostToDevice}. The first parameter of the
function is the destination array, the  second is the source array and the third
is the number of elements to copy (exprimed in bytes).

Now the GPU contains the data needed to perform the addition. In sequential such
addition is  achieved out with a  loop on all the  elements.  With a  GPU, it is
possible to perform  the addition of all elements of the  arrays in parallel (if
the   number  of   blocks   and   threads  per   blocks   is  sufficient).    In
Listing\ref{ch2:lst:ex1}     at    the     beginning,    a     simple    kernel,
called \texttt{addition} is defined to  compute in parallel the summation of the
two arrays. With CUDA, a  kernel starts with the keyword \texttt{\_\_global\_\_}
which  indicates that  this  kernel  can be  call  from the  C  code. The  first
instruction  in  this  kernel  is   used  to  computed  the  \texttt{tid}  which
representes the  thread index.  This thread  index is computed  according to the
values    of    the    block    index    (it   is    a    variable    of    CUDA
called  \texttt{blockIdx}\index{CUDA~keywords!blockIdx}). Blocks of  threads can
be decomposed into  1 dimension, 2 dimensions or 3  dimensions. According to the
dimension of data  manipulated, the appropriate dimension can  be useful. In our
example, only  one dimension  is used.  Then  using notation \texttt{.x}  we can
access to the first dimension (\texttt{.y} and \texttt{.z} allow respectively to
access      to      the     second      and      third     dimension).       The
variable \texttt{blockDim}\index{CUDA~keywords!blockDim} gives  the size of each
block.



\lstinputlisting[label=ch2:lst:ex1,caption=A simple example]{Chapters/chapter2/ex1.cu}

\putbib[Chapters/chapter2/biblio]

