Skip to content

Chapter 2 Heterogeneous data-parallel computing

Programming Massively Parallel Processors 5e — English original

Chapter 2 Heterogeneous data-parallel computing

Data parallelism refers to the phenomenon that the computations to be performed on different parts of a dataset can be done independently of each other and thus can be done in parallel with each other. Many applications exhibit a rich amount of data parallelism that makes them amenable to scalable parallel execution where the speed of execution improves proportionally to the amount of execution resources in the system. It is therefore important for parallel programmers to be familiar with the concept of data parallelism and the parallel programming language constructs for writing code that exploit data parallelism. In this chapter, we will use the CUDA C++ language constructs to develop a simple data parallel program.

2.1 Data parallelism

When modern software applications run slowly, the problem is usually data – too much data to process. Image processing applications manipulate images or videos with millions to trillions of pixels. Scientific applications model fluid dynamics using billions of grid points. Molecular dynamics applications simulate interactions between thousands to billions of atoms. Airline scheduling must deal with thousands of flights, crews, and airport gates. Most of these pixels, particles, grid points, interactions, flights, and so on can usually be dealt with largely independently. For example, in image processing, converting a color pixel to grayscale requires only the data of that pixel. Blurring an image averages each pixel’s color with the colors of nearby pixels, requiring only the data of that small neighborhood of pixels. Even a seemingly global operation, such as finding the average brightness of all pixels in an image, can be broken down into many smaller computations that can be executed independently. Such independent evaluation of different pieces of data is the basis of data parallelism. Writing data parallel code entails (re)organizing the computation around the data such that one can execute the resulting independent computations in parallel to complete the overall job faster – often much faster. Let us illustrate the concept of data parallelism with a color-to-grayscale conversion example. Fig. 2.1 shows a color image (left side) consisting of many pixels, each Programming Massively Parallel Processors. https://doi.org/10.1016/B978-0-44-343900-1.00011-1 Copyright © 2027 Elsevier Inc. All rights are reserved, including those for text and data mining, AI training, and similar technologies.

Figure 2.1

FIGURE 2.1. Conversion of a color image to a grayscale image.

containing a red, green, and blue fractional value (r, g, b) varying from 0 (black) to 1 (full intensity). To convert the color image (left side of Fig. 2.1) to a grayscale image (right side), we compute the luminance value L for each pixel by applying the following weighted sum formula: L = r ∗ 0.299 + g ∗ 0.587 + b ∗ 0.114 (2.1)

RGB Color Image Representation In an RGB representation, each pixel of an image is specified as a tuple of (r, g, b) values. The format of an image’s row is (r g b) (r g b) . . . (r g b), as illustrated in the following conceptual picture.

Each tuple specifies a mixture of red (R), green (G), and blue (B). That is, for each pixel, the r, g, and b values represent the intensity (0 being dark and 1 being full intensity) of the red, green, and blue light sources when the pixel is rendered. If we consider the input to be an image organized as an array I of RGB values and the output to be a corresponding array O of luminance values, we get the simple FIGURE 2.2 Data parallelism in color-image-to-grayscale conversion – pixels can be calculated independently of each other.

computation structure shown in Fig. 2.2. For example, O[0] is generated by calculating the weighted sum the RGB values in I[0] according to the formula above; O[1] by calculating the weighted sum of the RGB values in I[1]; O[2] by calculating the weighted sum of the RGB values in I[2]; and so on. None of these per-pixel computations depends on each other. All of them can be performed independently. Clearly, color-to-grayscale conversion exhibits a rich amount of data parallelism as modern images commonly consist of millions of pixels. Of course, data parallelism in complete applications can be more complex and much of this book is devoted to teaching the ``parallel thinking’’ necessary to find and exploit data parallelism. Data Parallelism vs. Task Parallelism Data parallelism is not the only type of parallelism used in parallel programming. Task Parallelism has also been used extensively in parallel programming. Task parallelism is typically exposed through task decomposition of applications. For example, a simple application may need to do a vector addition and a matrix-vector multiplication. Each of these would be a task. Task parallelism exists if the two tasks can be done independently. I/O and data transfers are also common sources of tasks. In large applications, there is usually a larger number of independent tasks and therefore a larger amount of task parallelism. For example, in a molecular dynamics simulator, the list of natural tasks include vibrational force calculation, rotational force calculation, neighbor identification for non-bonding forces, nonbonding force calculation, velocity and position calculation, etc. In general, data parallelism is the main source of performance improvement for parallel programs. With large data sets, one can often find abundant data parallelism to be able to utilize massively parallel processors and allow application performance to grow with each generation of hardware that has more execution resources, a desirable property referred to as scalability. Nevertheless, task parallelism can also play an important role in achieving performance goals.

2.2 CUDA C++ program structure

Our next step is to learn how to write a program to exploit data parallelism for faster execution in heterogeneous computing systems. Without loss of generality, we will use code examples of CUDA C++ programs to illustrate the algorithms and techniques in this book. From our experience, concepts learned based on CUDA C++ can be easily extended to other programming languages and platforms. CUDA C++1 extends the popular C++ programming language with minimal new syntax and with library functions to let programmers target heterogeneous computing systems containing both CPUs and GPUs. As the name implies, CUDA C++ is built on NVIDIA’s CUDA platform. CUDA is currently the most mature framework for massively parallel computing. It is broadly used in the high-performance computing industry, with essential tools such as compilers, debuggers, and profilers available on the most common operating systems. The structure of a CUDA C++ program reflects the co-existence of a host (CPU) and one or more devices (GPUs) in the computer. Each CUDA C++ source file can have a mixture of both host code and device code. By default, any traditional C++ program is a CUDA C++ program that contains only host code. One can add device code into any source file. The device code is clearly marked with special CUDA C++ keywords. The device code includes functions, or kernels, whose code is executed in a data-parallel manner. The execution of a CUDA C++ program is illustrated in Fig. 2.3. The execution starts with host code (CPU serial code) whose execution is illustrated as a single curly arrow that represents a thread. When a kernel, which is a piece of device code (device parallel kernel in Fig. 2.3), is called, a large number of threads are launched on a device to execute the kernel. All the threads that are launched by a kernel call are collectively called a grid. These threads are the primary vehicle of parallel execution in a CUDA platform. Fig. 2.3 shows the execution of two grids of threads. Each grid is shown as multiple blocks of threads. We will discuss how these grids and blocks are organized soon. When all threads of a grid complete their execution, the grid terminates and the execution continues on the host until another grid is launched. As is implied in Fig. 2.3, heterogeneous computing applications can manage overlapped CPU and GPU execution to take advantage of both CPUs and GPUs. Threads A thread is a simplified view of how a processor executes a sequential program in modern computers. A thread consists of the code of the program, the point in the code that is being executed, and the values of its variables and data structures. The execution of a thread is sequential as far as a user is concerned. One 1 In the code examples in this book, we use C-style programming for simplicity and introduce C++ fea tures where needed. However, readers should be aware that CUDA programming has evolved towards C++ to support modern generic programming and other object-oriented programming practices.

Figure 2.3

FIGURE 2.3. Execution of a CUDA program.

can use a source-level debugger to monitor the progress of a thread by executing one statement at a time, looking at the statement that will be executed next and checking the values of the variables and data structures as the execution progresses. Threads have been used in programming for many years. If a programmer wants to start parallel execution in an application, the programmer creates and manages multiple threads using thread libraries or special languages. In CUDA, the execution of each device thread is sequential as well. A CUDA program initiates parallel execution by calling kernel functions, which causes the underlying runtime mechanisms to launch a grid of device threads that process different parts of the data in parallel. Launching a grid typically generates many threads to exploit data parallelism. In the color-to-grayscale conversion example, each thread could be used to compute one pixel of the output array. In this case, the number of threads that ought to be generated by the grid launch is equal to the number of pixels in the image. For large images, a large number of threads, i.e., millions, could be generated. In practice, one might use each thread to compute multiple output pixels to enhance execution and data access efficiency, as we will discuss later in this book. CUDA programmers can assume that these threads take very few clock cycles to generate and schedule due to efficient hardware support. This assumption contrasts with traditional CPU threads that typically take thousands of clock cycles to generate and schedule. In Chapter 3, we show how to implement color-tograyscale conversion and image blur kernels. In this chapter, we will use vector addition as a running example for simplicity.

Figure 2.4

FIGURE 2.4. A simple traditional vector addition C++ code example.

2.3 A vector addition example

Vector addition is arguably the simplest possible data parallel computation – the parallel equivalent of Hello World'' from sequential programming. Before we show the kernel code for vector addition, it is helpful to first review how a conventional vector addition (host code) function works. Fig. 2.4 shows a simple C++ program that consists of a main function and a vector addition function. In all our examples, whenever there is a need to distinguish between host and device data, we will suffix the names of variables that are used by the host with _h’’ and those of variables that are used by a device with _d'' to remind ourselves of the intended usage of these variables. Since we only have host code in Fig. 2.4, we only see variables suffixed with _h’’. Assume that the vectors to be added are stored in arrays A and B that are allocated and initialized in the main function. The output vector is in array C, which is also allocated in the main function. For brevity, we do not show the details of how A, B, and C are allocated or initialized in the main function. The pointers to these arrays are passed to the vecAdd function, along with the variable N that contains the length of the vectors. Note that the parameters of the vecAdd function are suffixed with ``_h’’ to emphasize that they are used by the host. Pointers in the C++ Language The function arguments A, B, and C in Fig. 2.4 are pointers. In the C and C++ languages, a pointer can be used to access variables and data structures. While a floating-point variable V can be declared with: float V;

a pointer variable P can be declared with: float *P;

By assigning the address of V to P with the statement P = &V, we make P ``point to’’ V. *P becomes a synonym for V. For example, U = *P assigns the value of V to U. For another example, *P = 3 changes the value of V to 3. An array in a C or C++ program can be accessed through a pointer that points to its 0th element. For example, the statement P = &(A[0]) makes P point to the 0th FIGURE 2.5 Outline of a revised vecAdd (host code) function that moves the work to a device.

element of array A. P[i] becomes a synonym for A[i]. In fact, the array name A is in itself a pointer to its 0th element. In Fig. 2.4, passing an array name A as the first argument in the function call to th vecAdd makes the function’s first parameter A_h point to the 0 element of A. As a result, A_h[i] in the function body can be used to access A[i] for the array A defined in the main function. See [1] for an easy-to-follow explanation of the detailed usage of pointers in C. The vecAdd function in Fig. 2.4 uses a for-loop to iterate through the vector elements. In the i th iteration, output element C_h[i] receives the sum of A_h[i] and B_h[i]. The vector length parameter n is used to control the loop so that the number of iterations matches the length of the vectors. The function reads the elements of A and B and writes the elements of C through the pointers A_h, B_h, and C_h, respectively. When the vecAdd function returns, the subsequent statements in the main function can access the new contents of C. A straightforward way to execute vector addition in parallel is to modify the vecAdd function and move its calculations to a device. The structure of such a modified vecAdd function is shown in Fig. 2.5. Part 1 of the function allocates space in the device (GPU) memory to hold copies of the A, B, and C vectors, and copies the A and B vectors from the host memory to the device memory. Part 2 calls a vector addition kernel to launch a grid of threads on the device. Part 3 copies the sum vector C from the device memory to the host memory and frees the three arrays from the device memory.

Note that the revised vecAdd function is essentially an outsourcing agent that ships input data to a device, activates the calculation on the device, and collects the results from the device. The agent does so in such a way that the main program does not need to even be aware that the vector addition is now actually done on a device. In practice, such ``transparent’’ outsourcing model can be very inefficient because of all the copying of data back and forth. One would often keep large and important data structures on the device and simply invoke device functions that operate on the data structures from the host code. For now, however, we will use the simplified transparent model to introduce the basic CUDA C++ program structure. The details of the revised function, as well as the way to compose the kernel function, will be the topic of the rest of this chapter.

2.4 Device global memory and data transfer

In current CUDA systems, devices are often hardware cards that come with their own Dynamic Random-Access Memory (DRAM) called device global memory, or simply global memory. For example, the NVIDIA Hopper H100 GPU comes with 80 GB or 94 GB of global memory. Calling it ``global’’ memory distinguishes it from other types of device memory that are also accessible to programmers. Details about the CUDA memory model and the different types of device memory are discussed in 5. For the vector addition kernel, before calling the kernel, the programmer needs to allocate space in the device global memory and transfer data from the host memory to the allocated space in the device global memory. These activities correspond to Part 1 of Fig. 2.5. Similarly, after device execution, the programmer needs to transfer the output data from the device global memory back to the host memory and free up the allocated space in the device global memory that is no longer needed. These activities correspond to Part 3 of Fig. 2.5. The CUDA runtime system (typically running on the host) provides API functions to perform these activities on behalf of the programmer. From this point on, we will simply say that data is transferred from host to device as shorthand for saying that the data is copied from the host memory to the device global memory. The same holds for the opposite direction. In Fig. 2.5, Part 1 and Part 3 of the vecAdd function need to use the CUDA API functions to allocate device global memory for A, B, and C, transfer A and B from host to device, transfer C from device to host after the vector addition, and free the device global memory for A, B, and C. We will explain the memory allocation and free functions first. Fig. 2.6 shows two API functions for allocating and freeing device global memory. The cudaMalloc function can be called from the host code to allocate a piece of device global memory for an object. The reader should notice the striking similarity between cudaMalloc and the malloc function from the standard C runtime library, which is also part of C++. This similarity is intentional; CUDA C++ is C++ with minimal extensions. CUDA C++ uses the standard C and C++ runtime library malloc function to manage the host memory and adds cudaMalloc as an extension to the C++ FIGURE 2.6 CUDA API functions for managing device global memory.

runtime library. By keeping the interface as close to the original C and C++ runtime libraries as possible, CUDA C++ minimizes the time that a C or C++ programmer spends to relearn the use of these extensions. The first parameter to the cudaMalloc function is the address of a pointer variable that will be set to point to the allocated object. The address of the pointer variable should be cast to (void**) because the function expects a generic pointer; the memory allocation function is a generic function that is not restricted to any particular type of objects.2 This parameter allows the cudaMalloc function to write the address of the allocated memory into the provided pointer variable regardless of its type. The host code that calls kernels passes this pointer value to the kernels that need to access the allocated memory object. The second parameter to the cudaMalloc function gives the size of the data to be allocated, in number of bytes. The usage of this second parameter is consistent with the size parameter to the C malloc function. Note that cudaMalloc has a slightly different format from the C malloc function. The C malloc function returns a pointer to the allocated object. It takes only one parameter that specifies the size of the allocated object. The cudaMalloc function writes to the pointer variable whose address is given as the first parameter. As a result, the cudaMalloc function takes two parameters. The two-parameter format of cudaMalloc allows it to use the return value to report any errors in the same way as other CUDA API functions. The cudaFree function strongly resembles the C free function. It takes one input parameter, the pointer to the memory to be freed, and frees the allocated space from the device memory. CUDA C++ also has more advanced library functions for allocating and deallocating space in the host and device memories. These alternative methods allow for faster copying of data, or eliminate the need to copy the data explicitly and allow it to be copied automatically on-demand. Such methods provide programs with improved performance or convenience when managing the device memory. We discuss these other forms of memory allocation in Chapter 23 and in Appendix C. We now use the following simple code example to illustrate the use of cudaMalloc and cudaFree: 2 The fact that cudaMalloc returns a generic object makes the use of dynamically allocated multidimen sional arrays more complex. We address this issue in Chapter 3.

float *A_d int size = n*sizeof(float); cudaMalloc((void**)&A_d, size); … cudaFree(A_d);

This code is a continuation of the example in Fig. 2.5. For clarity, we suffix a pointer variable with ``_d’’ to indicate that it points to an object in the device global memory. The first argument passed to cudaMalloc is the address of pointer A_d (i.e., &A_d) cast to a void pointer. When cudaMalloc returns, A_d will point to the device global memory region allocated for the A vector. The second argument passed to cudaMalloc is the size of the region to be allocated. Since size is in number of bytes, the programmer needs to translate from the number of elements in an array to the number of bytes when determining the value of size. For example, when allocating space for an array of n single-precision floating-point elements, the value of size would be n times the size of a single-precision floating number, which is 4 bytes in computers today. Therefore, the value of size would be n*4. After the computation, cudaFree is called with pointer A_d passed as an argument to free the storage space for the A vector from the device global memory. Note that cudaFree does not need to change the value of A_d; it only needs to use the value of A_d to return the allocated memory back to the available pool. Thus, only the value and not the address of A_d is passed as an argument. The addresses in A_d, B_d, and C_d point to locations in the device global memory. These addresses should not be dereferenced in the host code. They should only be used in calling API functions and kernel functions. Dereferencing a device global memory pointer in host code can cause exceptions or other types of run-time errors since these addresses are located in the device global memory and are not accessible by the user host code. Now that we have discussed the declaration of the A_d pointer variable, and its use in cudaMalloc and cudaFree calls, the reader should be able to complete Part 1 of the vecAdd example in Fig. 2.5 with similar declarations of the B_d and C_d pointer variables as well as their corresponding cudaMalloc calls. Furthermore, Part 3 in Fig. 2.5 can be completed with the cudaFree calls for B_d and C_d. Once the host code has allocated space in the device global memory for the data objects, it can request that data be transferred from host to device and vice versa. This transfer is accomplished by calling one of the CUDA API functions. Fig. 2.7 shows such an API function, cudaMemcpy. The cudaMemcpy function takes four parameters. The first parameter is a pointer to the destination location for the data object to be copied. The second parameter points to the source location. The third parameter specifies the number of bytes to be copied. The fourth parameter indicates the location of the memory involved in the copy: from host to host, from host to device, from device to host, and from device to device. For example, the memory copy function can be used to copy data from one location in the device global memory to another location of the device global memory. The vecAdd function calls the cudaMemcpy function to copy the host memory arrays pointed to by A_h and B_h to the device memory arrays pointed to by A_d and B_d FIGURE 2.7 CUDA API function for data transfer between host and device.

Figure 2.8

FIGURE 2.8. A more complete version of the vecAdd function.

before adding them. The vecAdd function also calls the cudaMemcpy function to copy the device memory array pointed to by C_d to the host memory array pointed to by C_h after the vector addition is done. Assume that the values of A_h, B_h, A_d, B_d, and size have already been set as we discussed before, the three cudaMemcpy calls are shown below. cudaMemcpy(A_d, A_h, size, cudaMemcpyHostToDevice); cudaMemcpy(B_d, B_h, size, cudaMemcpyHostToDevice); … cudaMemcpy(C_h, C_d, size, cudaMemcpyDeviceToHost);

The two symbolic constants, cudaMemcpyHostToDevice and cudaMemcpyDeviceToHost, are recognized, predefined constants of the CUDA programming environment. Note that the same cudaMemcpy function can be used to transfer data in both directions by properly ordering the source and destination pointers and using the appropriate constant for the transfer type. To summarize, the main function in Fig. 2.4 calls vecAdd, which is also executed on the host. The vecAdd function, outlined in Fig. 2.5, allocates space in device global memory, requests data transfers, and calls the kernel that performs the actual vector addition. We refer to this type of host code as a stub for calling a kernel. We show a more complete version of the vecAdd function in Fig. 2.8.

Compared to Fig. 2.5, the vecAdd function in Fig. 2.8 is complete for Part 1 and Part 3. Part 1 allocates device global memory for A_d, B_d, and C_d and transfers A_h to A_d and B_h to B_d. This is done by calling the cudaMalloc and cudaMemcpy functions. The readers are encouraged to write their own function calls with the appropriate parameter values and compare their code with that shown in Fig. 2.8. Part 2 calls the kernel and will be described in the following subsection. Part 3 copies the vector sum data from device to host so that the values will be available in the main function. This is accomplished with a call to the cudaMemcpy function. It then frees the memory for A_d, B_d, and C_d from the device global memory, which is done by calls to the cudaFree function. Error Checking and Handling in CUDA C++ Programs It is important for a program to check for and handle errors. With good error checking and handling, one can catch the errors when they happen and detect them close to the root cause of the problems. CUDA API functions return flags that indicate whether an error has occurred when they served the request. Most errors are due to inappropriate argument values used in the call. For brevity, we will not show error checking code in our examples. For example, Fig. 2.8 shows a call to cudaMalloc as follows: cudaMalloc((void **) &A_d, size);

In practice, CUDA programmers surround the call with code that checks the returned error condition and prints out a relevant error message so that the user can be aware of the fact that an error has occurred. A simple version of such checking code is as follows: cudaError_t err = cudaMalloc((void **) &A_d, size); if (error != cudaSuccess) { printf("%s in %s at line %d \n", cudaGetErrorString(err), FILE, LINE); exit(EXIT_FAILURE); } This way, if the system is out of device memory, the user will be informed about the situation. Proper error checking can save many hours of debugging time. One could define a C++ macro to make the checking code more concise in the source code.

2.5 Kernel functions and threading

We are now ready to discuss more about the CUDA C++ kernel functions and the effect of calling these kernel functions. In CUDA, a kernel function specifies the FIGURE 2.9 All threads in a grid execute the same kernel code.

code to be executed by GPU threads during a parallel phase. Since all these threads execute the same code, CUDA C++ programming is an instance of the well-known Single-Program Multiple-Data (SPMD) [2] parallel programming style, a popular programming style for parallel computing systems. When a program’s host code calls a kernel, the CUDA runtime system running on the host launches a grid of GPU threads that are organized into a two-level hierarchy. Each grid is organized as an array of thread blocks, which will be referred to as blocks for brevity. All blocks of a grid have the same size. Each block can contain up to 1024 threads on current systems. Fig. 2.9 shows an example where each block consists of 256 threads. Each thread is represented by a curly arrow stemming from a box that is labeled with the thread’s index number in the block. The total number of blocks in the grid and the total number of threads in each block is specified by the host code when a kernel is called. The same kernel can be called with different numbers of blocks and threads in different parts of the host code. For a given grid of threads, the number of threads in a block is available in a built-in variable named blockDim. The blockDim variable’s type is a C++ struct with three unsigned integer fields (x, y, and z) which help the programmer organize the threads into a one-, two-, or three-dimensional array. For a one-dimensional organization, only the x field is used. For a two-dimensional organization, the x and y fields are used. For a three-dimensional structure, all three x, y, and z fields are used. The choice of dimensionality for organizing threads usually reflects the dimensionality of the data. This makes sense since the threads are created to process data in parallel, so it is only natural that the organization of the threads reflects the organization of the data. Built-in Variables Many programming languages have built-in variables. These variables have special meaning and purpose. The values of these variables are often pre-initialized by the runtime system and are typically read-only in the program. The programmers should refrain from redefining these variables for any other purposes.

In Fig. 2.9, each thread block is organized as a one-dimension array of threads because the data are one-dimensional vectors. The value of the blockDim.x variable indicates the total number of threads in each block, which is 256 in Fig. 2.9. In general, it is recommended that the total number of threads in a thread block is a multiple of 32 for hardware efficiency reasons. We will revisit this point later. CUDA kernels have access to two more built-in variables, threadIdx and blockIdx, that allow threads to distinguish themselves from each other and to determine the area of data each thread is to work on. The threadIdx variable gives each thread a unique coordinate within a block. In Fig. 2.9, since we are using a one-dimensional thread organization, only threadIdx.x is used. The threadIdx.x value for each thread is shown in the small shaded box of each thread in Fig. 2.9. The first thread in each block has value 0 in its threadIdx.x variable, the second thread has value 1, the third thread has value 2, etc. The blockIdx variable gives all threads in a block a common block coordinate. In Fig. 2.9, all threads in the first block have value 0 in their blockIdx.x variables, those in the second thread block value 1, and so on. Using an analogy with the telephone system, one can think of threadIdx.x as local phone number and blockIdx.x as area code (see Sidebar on Hierarchical Organizations). The two together gives each telephone line a unique phone number in the whole country. Similarly, each thread can combine its threadIdx and blockIdx values to create a unique global index for itself within the entire grid. In Fig. 2.9, a unique global index i is calculated with the C++ statement i = blockIdx.x * blockDim.x + threadIdx.x. Recall that blockDim.x is 256 in our example. The i values of threads in block 0 range from 0 to 255. The i values of threads in block 1 range from 256 to 511. The i values of threads in block 2 range from 512 to 767. That is, the i values of the threads in these three blocks form a continuous coverage of the values from 0 to 767. Since each thread uses i to access A, B, and C, these threads cover the first 768 iterations of the original loop. By launching a grid with a larger number of blocks, one can process larger vectors. By launching a grid with n or more threads, one can process vectors of length n. Hierarchical Organizations Like CUDA threads, many real-world systems are organized hierarchically. The U.S. telephone system is a good example. At the top level, the telephone system consists of areas'' each of which corresponds to a geographical area. All telephone lines within the same area have the same 3-digit area code’’. A telephone area is sometimes larger than a city. For example, many counties and cities of central Illinois are within the same telephone area and share the same area code 217. Within an area, each phone line has a seven-digit local phone number, which allows each area to have a maximum of about ten million numbers. One can think of each phone line as a CUDA thread, with the area code as the value of blockIdx and the seven-digital local number as the value of threadIdx. This hierarchical organization allows the system to have a very large number of phone FIGURE 2.10 A simple vector addition kernel function.

lines while preserving locality'' for calling the same area. That is, when dialing a phone line in the same area, a caller only needs to dial the local number. As long as we make most of our calls within the local area, we seldom need to dial the area code. If we occasionally need to call a phone line in another area, we dial 1 and the area code, followed by the local number (this is the reason why no local number in any area should start with a 1). The hierarchical organization of CUDA threads also offers a form of locality. We will study this locality soon. Fig. 2.10 shows a kernel function for vector addition. Note that we do not use the “_h'' and _d’’ convention in the kernel code because there is no potential for confusion. We do not access any host memory from the kernel code in our examples.3 The syntax of a kernel follows the C++ standard with some notable extensions. First, there is a CUDA-specific keyword global in front of the declaration of the vecAddKernel function. This keyword indicates that the function is a kernel and that it can be called to generate a grid of threads on a device. In general, CUDA C++ extends the C++ language with three qualifier keywords that can be used in function declarations. The meaning of these keywords is summarized in Fig. 2.11. The global keyword indicates that the function being declared is a CUDA C++ kernel function. Note that there are two underscore characters on each side of the word ``global’’. Such a kernel function is executed on the device and can be called from the host. In CUDA systems that support dynamic parallelism (starting with the Kepler architecture), it can also be called from the device. The important feature is that calling such a kernel function results in a new grid of threads being launched on the device, i.e., the GPU. 3 In general, it is possible for the kernel code to access host memory through a mechanism called Unified Virtual Address (UVA). This mechanism allows the GPU to access host memory but at lower bandwidth than accessing its own device global memory, and is discussed in Appendix C.

Figure 2.11

FIGURE 2.11. CUDA C++ keywords for function declaration.

The device keyword in Fig. 2.11 indicates that the function being declared is a CUDA device function. A device function executes on a GPU device and can only be called from a kernel function or another device function. The device function is executed by the device thread that calls it and does not result in any new device threads being launched. The host keyword indicates that the function being declared is a CUDA host function. A host function is simply a traditional C++ function that executes on host and can only be called from another host function. By default, all functions in a CUDA program are host functions if they do not have any of the CUDA keywords in their declaration. This makes sense since many CUDA applications are ported from CPU-only execution environments. The programmer would add kernel functions and device functions during the porting process. The original functions remain as host functions. Having all functions to default into host functions spares the programmer the tedious work to change all original function declarations. Since all functions are host functions by default, the reader may wonder why the host keyword is needed. In some cases, a programmer may want a function to be available on both the host and the device. Labeling the function with the device keyword makes the function available only on the device. To make it available on both, one can use both the host and device keywords together in a function declaration. This combination tells the compilation system to generate two versions of object code for the same function. One is executed on the host and can only be called from a host function. The other is executed on the device and can only be called from a device or kernel function. This capability supports a common use case when the same function source code can be recompiled to generate a device version. Many user library functions will likely fall into this category. The second notable CUDA C++ extension used the vecAdd kernel function in Fig. 2.10 is the built-in variables threadIdx, blockIdx, and blockDim. Recall that all threads execute the same kernel code and there needs to be a way for them to distinguish themselves from each other and direct each thread towards a particular part of the data. These built-in variables are the means for threads to access hardware registers that provide the identifying coordinates to threads. Different threads will seedifferent values in their threadIdx.x, blockIdx.x, and blockDim.x variables. For readability, we will sometimes refer to a thread as thread blockIdx.x, threadIdx.x in our discussions. The global index computed using the built in variables (line 05 in Fig. 2.10) is written to an automatic (local) variable i of type int.4 In a CUDA kernel function, automatic variables are private to each thread. That is, a version of i will be generated for every thread. If the grid is launched with 10,000 threads, there will be 10,000 versions of i, one for each thread. The value assigned by a thread to its i variable is not visible to other threads. We will discuss these automatic variables in more details in Chapter 5. A quick comparison between Fig. 2.4 and Fig. 2.10 reveals an important insight for CUDA kernels. The kernel function in Fig. 2.10 does not have a loop that corresponds to the one in Fig. 2.10. The reader should ask where the loop went. The answer is that the loop is now replaced with the grid of threads. The entire grid forms the equivalent of the loop. Each thread in the grid corresponds to one iteration of the original loop. This is sometimes referred to as loop parallelism, where iterations of the original sequential code are executed by threads in parallel. Note that there is an if (i<n) statement in vecAddVecKernel in Fig. 2.10. This is because not all vector lengths can be expressed as multiples of the block size. For example, let’s assume that the vector length is 100. The smallest efficient thread block dimension is 32. Assume that we picked 32 as the block size. One would need to launch four thread blocks to process all the 100 vector elements. However, the four thread blocks would have 128 threads. We need to disable the last 28 threads in thread block 3 from doing work not expected by the original program. Since all threads are to execute the same code, all will test their i values against n, which is 100. With the if (i<n) statement, the first 100 threads will perform the addition whereas the last 28 will not. This allows the kernel to be called to process vectors of arbitrary lengths.

2.6 Calling kernel functions

Having implemented the kernel function, the remaining step is to call that function from the host code to launch the grid. Fig. 2.12 shows the host code for calling the vector addition kernel. When the host code calls a kernel function, it sets the grid and thread block dimensions via execution configuration parameters. The configuration 4 Different C and C++ programmers may choose to use the int or the unsigned int type when declaringvariables intended to represent array indices, array sizes, loop iteration counters, and loop bounds. The use of unsigned int makes the intent clear that these variables should never be negative and may result in beneficial compiler warnings that prevent bugs. On the other hand, the use of int avoids bugs when performing subtraction or intermixing unsigned int variables with other int variables when performing arithmetic. Moreover, since the overflow of int variables is undefined, the compiler can take advantage of that fact when performing optimizations. Throughout this book, we sometimes used int and sometimes use unsigned int when declaring array indices, array sizes, loop iteration counters, and loop bounds. Readers can use either style in their own implementations.

Figure 2.12

FIGURE 2.12. A host code example with a vector addition kernel call statement.

Figure 2.13

FIGURE 2.13. A vector addition kernel call statement.

parameters are given between the <<<'' and »>’’ brackets before the traditional C++ function arguments. The first configuration parameter gives the number of blocks in the grid. The second specifies the number of threads in each block. In this example, there are 256 threads in each block. To ensure that we have enough threads in the grid to cover all the vector elements, we need to set the number of blocks in the grid to the ceiling division (rounding up the quotient to the immediate higher integer value) of the desired number of threads (n in this case) by the thread block size (256 in this case). There are many ways to perform a ceiling division. One way is to apply the C++ ceiling function to n/256.0. Using floating point value 256.0 ensures that we generate a floating value for the division so that the ceiling function can round it up correctly. For example, if we want 1000 threads, we would launch ceil(1000/256.0) = 4 thread blocks. As a result, the statement will launch 4*256 = 1024 threads. With the if (i < n) statement in the kernel as shown in Fig. 2.10, the first 1000 threads will perform addition on the 1000 vector elements. The remaining 24 will not. Fig. 2.13 shows the final host code in the vecAdd function. This source code completes the skeleton code in Fig. 2.5. Fig. 2.10 and Fig. 2.13 jointly illustrate a simple FIGURE 2.14 Overview of the compilation process of a CUDA C++ program.

2.7 Compilation

CUDA program that consists of both host code and a device kernel. The code is hardwired to use thread blocks of 256 threads each. The number of thread blocks used, however, depends on the length of the vectors (n). If n is 750, then three thread blocks will be used. If n is 4000, then 16 thread blocks will be used. If n is 2,000,000, then 7813 blocks will be used. Note that all the thread blocks operate on different parts of the vectors. They can be executed in any arbitrary order. Programmers must not make any assumptions regarding execution order. A small GPU with a small amount of execution resources may execute only one or two of these thread blocks in parallel. A larger GPU may execute 128 or 256 blocks in parallel. This gives CUDA kernels scalability in execution speed with hardware. That is, the same code runs at lower speed on small GPUs and higher speed on larger GPUs. We revisit this point in Chapter 4.

We have seen that implementing CUDA C++ kernels requires using various extensions that are not part of C++. Once these extensions are used in the code, it is no longer acceptable to a traditional C++ compiler. The code needs to be compiled by a compiler that recognizes and understands these extensions such as NVCC (NVIDIA CUDA Compiler). As shown at the top of Fig. 2.14, the NVCC compiler processes a CUDA C++ program, using the CUDA keywords to separate the host code and device code. The host code is straight C++ code, which is compiled with a standard C/C++ compiler and is run as a traditional CPU process. The device code which is marked with CUDA keywords that designate CUDA kernels and their associated device functions and data structures is compiled by NVCC into virtual binary files called PTXfiles. These PTX files are further compiled by a run-time component of NVCC into the real object files and executed on a CUDA-capable GPU device.

2.8 Summary

This chapter provided a quick, simplified overview of the CUDA C++ programming model. CUDA C++ extends the C++ language to support parallel computing. We discussed an essential subset of these extensions in this chapter. For the reader’s convenience, we summarize these extensions as follows. Function Declarations. CUDA C++ extends the C++ function declaration syntax to support heterogeneous parallel computing. The extensions are summarized in Fig. 2.11. Using one of __global__'', device’’, or __host__'', a programmer can instruct the compiler to generate a kernel function, a device function, or a host function. All function declarations without any of these keywords default to host functions. If both the host’’ and ``device’’ keywords are used in a function declaration, the compiler generates two versions of the function, one for the device and one for the host. Kernel Call and Grid Launch. CUDA C++ extends the C++ function call syntax with kernel execution configuration parameters surrounded by the «< and »> brackets. These execution configuration parameters are only used when calling a kernel function to launch a grid. We discussed the execution configuration parameters that define the dimensions of the grid and the dimensions of each block. The reader should refer to the CUDA Programming Guide [3] for more details of the kernel launch extensions as well as other types of execution configuration parameters. Built-in (Predefined) Variables. CUDA kernels can access a set of built-in readonly variables that allow each thread to distinguish itself from other threads and to determine the area of data to work on. We discussed the threadIdx, blockDim, and blockIdx variables in this chapter. In Chapter 3, we discuss more details of using these variables to manage multi-dimensional blocks and grids while processing multidimensional data. Runtime API. CUDA supports a set of Application Programming Interface (API) functions to provide services to CUDA C++ programs. The services that we discussed in this chapter are the cudaMalloc, cudaFree and cudaMemcpy functions. These functions are called by the host code to allocate device global memory, deallocate device global memory, and transfer data between host and device on behalf of the calling program, respectively. The reader is referred to the CUDA C++ Programming Guide [3] for other CUDA API functions. Our goal for this chapter is to introduce the core concepts of CUDA C++ and the essential CUDA extensions to C++ for writing a simple CUDA program. The chapter is by no means a comprehensive account of all CUDA features. More features will be covered in the remainder of the book. However, our emphasis will be on the key parallel computing concepts supported by these features. We will only introduce enough CUDA C++ features that are needed in our code examples for parallel programming techniques. In general, we encourage the reader to always consult the CUDA C++ Programming Guide [3] for more details of the latest features of CUDA.

2.9 Exercises

\1. If we want to use each thread in a grid to calculate one output element of a vector addition, what would be the expression for mapping the thread/block indices to the data index (i) in our kernel function? a. i=threadIdx.x + threadIdx.y; b. i=blockIdx.x + threadIdx.x; c. i=blockIdx.x*blockDim.x + threadIdx.x; d. i=blockIdx.x * threadIdx.x; 2. Assume that we want to use each thread to calculate two adjacent elements of a vector addition. What would be the expression for mapping the thread/block indices to the data index (i) of the first element to be processed by a thread? a. i=blockIdx.x*blockDim.x + threadIdx.x +2; b. i=blockIdx.x*threadIdx.x*2; c. i=(blockIdx.x*blockDim.x + threadIdx.x)*2; d. i=blockIdx.x*blockDim.x*2 + threadIdx.x; 3. We want to use each thread to calculate two elements of a vector addition. Each thread block process 2*blockDim.x consecutive elements that form two sections. All threads in each block will first process a section first, each processing one element. They will then all move to the next section, each processing one element. Assume that variable i should be the index for the first element to be processed by a thread. What would be the expression for mapping the thread/block indices to data index of the first element? a. i=blockIdx.x*blockDim.x + threadIdx.x +2; b. i=blockIdx.x*threadIdx.x*2; c. i=(blockIdx.x*blockDim.x + threadIdx.x)*2; d. i=blockIdx.x*blockDim.x*2 + threadIdx.x; 4. For a vector addition, assume that the vector length is 8000, each thread calculates one output element, and the thread block size is 1024 threads. The programmer configures the kernel call to have a minimal number of thread blocks to cover all output elements. How many threads will be in the grid? a. 8000 b. 8196 c. 8192 d. 8200 5. If we want to allocate an array of v integer elements in CUDA device global memory, what would be an appropriate expression for the second argument of the call to cudaMalloc? a. n b. v c. n * sizeof(int) d. v * sizeof(int) 6. If we want to allocate an array of n floating-point elements and have a floatingpoint pointer variable A_d to point to the allocated memory, what would be an appropriate expression for the first argument of the cudaMalloc call?

a. n b. (void *) A_d c. *A_d d. (void **) &A_d 7. If we want to copy 3000 bytes of data from host array A_h (A_h is a pointer to element 0 of the source array) to device array A_d (A_d is a pointer to element 0 of the destination array), what would be an appropriate API call for this data copy in CUDA? a. cudaMemcpy(3000, A_h, A_d, cudaMemcpyHostToDevice); b. cudaMemcpy(A_h, A_d, 3000, cudaMemcpyDeviceToHost); c. cudaMemcpy(A_d, A_h, 3000, cudaMemcpyHostToDevice); d. cudaMemcpy(3000, A_d, A_h, cudaMemcpyHostToDevice); 8. How would one declare a variable err that can appropriately receive the returned value of a CUDA API call? a. int err; b. cudaError err; c. cudaError_t err; d. cudaSuccess_t err; 9. Consider the CUDA kernel and the corresponding host function that calls it in Fig. 2.15.

Figure 2.15

FIGURE 2.15. CUDA code for Exercise Question 9.

a. What is the number of threads per block? b. What is the number of threads in the grid? c. What is the number of blocks in the grid? d. What is the number of threads that execute the code on line 02? e. What is the number of threads that execute the code on line 04? 10. A new CUDA programmer is frustrated with CUDA. He has been complaining that CUDA is very tedious: he had to declare many functions that he plans to execute on both the host and the device twice, once as a host function and once as a device function. How would you help this new programmer?

References

References [1] Y.N. Patt, S.J. Patel, Introduction to Computing Systems: from Bits and Gates to C and Beyond, McGraw Hill Publisher, 2020, 2000, 2004. [2] M.J. e. Atallah, Algorithms and Theory of Computation Handbook, CRC Press, 1998. [3] NVIDIA, CUDA C++ programming guide. Release 13.0, https://docs.nvidia.com/cuda/ cuda-c-programming-guide, 2025.

CHAPTER

Multidimensional grids and data

References

[1] Y.N. Patt, S.J. Patel, Introduction to Computing Systems: from Bits and Gates to C and Beyond, McGraw Hill Publisher, 2020, 2000, 2004.

[2] M.J. e. Atallah, Algorithms and Theory of Computation Handbook, CRC Press, 1998.

[3] NVIDIA, CUDA C++ programming guide. Release 13.0, https://docs.nvidia.com/cuda/ cuda-c-programming-guide, 2025.