Chapter 6 Performance considerations
Chapter 6 Performance considerations
CHAPTER
Performance considerations
CONTENTS 6.1 Global memory access coalescing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 6.2 Hiding memory latency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 6.3 Vector loads and stores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 6.4 Shared memory bank conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 6.5 Thread coarsening . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 6.6 Loop unrolling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 6.7 Double buffering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 6.8 A checklist of optimizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 6.9 Optimization strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 6.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 6.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 The execution speed of a parallel program can vary greatly depending on the interactions between the resource demands of the program and the resource constraints of the hardware. Managing the interaction between parallel code and hardware resource constraints is important for achieving high performance in virtually all parallel programming models. It is a practical skill that requires deep understanding of the hardware architecture and is best learned with hands-on exercises in a parallel programming model designed for high-performance. So far, we have learned about the basic aspects of the GPU architecture and their implications on performance. In Chapter 4, we learned about the compute architecture of the GPU and related performance considerations such as control divergence and occupancy. In Chapter 5, we learned about the on-chip memory architecture of the GPU and the use of shared memory tiling and register tiling to alleviate global memory bandwidth limitations. In this chapter, we will learn more about the off-chip memory (DRAM) architecture and discuss related performance considerations such as memory coalescing, memory latency hiding, and vector loads and stores. We also introduce additional important optimizations such as avoiding shared memory bank conflicts, thread granularity coarsening, loop unrolling, and double-buffering. Finally, we wrap up this part of the textbook with a checklist of common performance optimizations that will serve as a guide for optimizing the performance of the parallel patterns discussed in the second and third parts of the book. Programming Massively Parallel Processors. https://doi.org/10.1016/B978-0-44-343900-1.00015-9 Copyright © 2027 Elsevier Inc. All rights are reserved, including those for text and data mining, AI training, and similar technologies.
Since different applications impose different demands on architectural resources, different architecture constraints may dominate and become the limiting factors of performance, commonly referred to as bottlenecks. One can often dramatically improve the performance of an application on a particular CUDA device, by trading one resource usage for another. This strategy works well if the resource constraint thus alleviated was the dominating constraint before the strategy was applied and the one thus exacerbated does not have negative effects on parallel execution. Without such understanding, performance tuning would be a guess work; plausible strategies may or may not lead to performance enhancements.
The execution speed of a parallel program can vary greatly depending on the interactions between the resource demands of the program and the resource constraints of the hardware. Managing the interaction between parallel code and hardware resource constraints is important for achieving high performance in virtually all parallel programming models. It is a practical skill that requires deep understanding of the hardware architecture and is best learned with hands-on exercises in a parallel programming model designed for high-performance.
So far, we have learned about the basic aspects of the GPU architecture and their implications on performance. In Chapter 4, we learned about the compute architecture of the GPU and related performance considerations such as control divergence and occupancy. In Chapter 5, we learned about the on-chip memory architecture of the GPU and the use of shared memory tiling and register tiling to alleviate global memory bandwidth limitations. In this chapter, we will learn more about the off-chip memory (DRAM) architecture and discuss related performance considerations such as memory coalescing, memory latency hiding, and vector loads and stores. We also introduce additional important optimizations such as avoiding shared memory bank conflicts, thread granularity coarsening, loop unrolling, and double-buffering. Finally, we wrap up this part of the textbook with a checklist of common performance optimizations that will serve as a guide for optimizing the performance of the parallel patterns discussed in the second and third parts of the book.
Since different applications impose different demands on architectural resources, different architecture constraints may dominate and become the limiting factors of performance, commonly referred to as bottlenecks. One can often dramatically improve the performance of an application on a particular CUDA device, by trading one resource usage for another. This strategy works well if the resource constraint thus alleviated was the dominating constraint before the strategy was applied and the one thus exacerbated does not have negative effects on parallel execution. Without such understanding, performance tuning would be a guess work; plausible strategies may or may not lead to performance enhancements.
6.1 Global memory access coalescing
One of the most important factors of CUDA kernel performance is accessing data in the global memory whose limited bandwidth can become the bottleneck. CUDA applications exploit massive data parallelism. Naturally, CUDA applications tend to process a massive amount of data from the global memory within a short period of time. In Chapter 5, we studied tiling techniques that leverage the shared memory to reduce the total amount of data that must be accessed from the global memory by a collection of threads in each thread block. In this chapter, we will further discuss memory coalescing techniques for moving data between global memory and shared memories or registers in an efficient manner. Memory coalescing techniques are often used in conjunction with tiling techniques to allow CUDA devices to reach their performance potential by efficiently utilizing the global memory bandwidth. The global memory of a CUDA device is implemented with DRAM as we saw in Chapter 5. In DRAM, data bits are stored in DRAM cells that are small capacitors, where the presence or absence of a tiny amount of electrical charge distinguishes between a 1 and a 0 value. Reading data from a DRAM cell requires the small capacitor to use its tiny electrical charge to drive a highly capacitive line leading to a sensor and to set off the sensor’s detection mechanism that determines whether a sufficient amount of charge is present in the capacitor to qualify as a 1''. This process takes tens of nanoseconds in modern DRAM chips (see Why is DRAM so slow?’’ sidebar). This is in sharp contrast with the sub-nanosecond clock cycle time of modern computing devices. Because this process is very slow relative to the desired data access speed (sub-nanosecond access per byte), modern DRAM designs use parallelism to increase their rate of data access, commonly referred to as memory access throughput. Why are DRAMs so slow? The following figure shows a DRAM cell and the path for accessing its content.
The decoder is an electronic circuit that drives a horizontal line connected to the outlet transistor gates of thousands of cells. It can take a long time for the horizontal line to be fully charged or discharged to the desired level to activate the outlet gate. A more formidable challenge is for the cell, once its outlet gate is activated, to drive the vertical line to the sense amplifiers and allow the sense amplifier to detect its content. This is based on electrical charge sharing. The gate lets out the tiny amount of electrical charge stored in the cell. The tiny amount of charge must cause sufficient change to the electrical potential of the large capacitance of the long bit line in order to trigger the detection mechanism of the sense amplifier. A good analogy would be for someone to hold a small cup of coffee at one end of a long hallway connecting many offices for another person to smell the aroma propagated through the hallway to determine the flavor of the coffee. One could speed up the process by using a larger, stronger capacitor in each cell. However, the DRAMs have been going in the opposite direction. The capacitors in each cell have been steadily reduced in size and thus reduced in their strength over time so that more bits can be stored in each chip. This is why the access latency of DRAMs has not decreased over time. Each time a DRAM location is accessed, a range of consecutive locations that include the requested location are accessed as well. Many sensors are provided in each DRAM chip and they all work in parallel. Each sensor senses the content of a bit within these consecutive locations. Once detected by the sensors, the data from all these consecutive locations can be transferred at high speed to the processor. These consecutive locations accessed and delivered are referred to as DRAM bursts. If an application makes focused use of data from a burst, the DRAM can supply the data at a much higher rate than if an application accesses a truly random sequence of locations scattered across many bursts. Modern GPUs also use SRAM-based L1 and L2 on-chip caches to help reduce the latencies and increase the throughput of global memory accesses. Since these caches are not a prominent part of the CUDA programming model, we have not covered these caches. However, their presence and behavior impact the observed performance of CUDA applications. Regarding the global memory access pattern, even when data has already been fetched from the DRAM and placed in the SRAM caches, making focused access to consecutive memory locations is still beneficial. When data in the L2 cache is accessed, a range of consecutive locations are moved from the L2 cache FIGURE 6.1 Placing matrix elements into a linear array based on row major order.
to the L1 cache. This granularity at which data is moved between different levels of cache is known as the cache block or cache line. If an application makes focused use of data from within a cache line, the caches can supply the data at a faster rate because fewer cache lines need to be moved. Recognizing the burst organization of modern DRAMs and the cache line organization of modern cache hierarchies, current CUDA devices employ a technique that allows programmers to achieve high global memory access efficiency by organizing memory accesses of threads into favorable patterns. This technique takes advantage of the fact that threads in a warp execute the same instruction at any given point in time. When all threads in a warp execute a load instruction, the hardware detects whether they access consecutive global memory locations. The most favorable access pattern is achieved when all threads in a warp access consecutive global memory locations in the same DRAM burst and/or cache line. In this case, the hardware combines, or coalesces, all these accesses into a consolidated access. For example, for a given load instruction of a warp, if thread 0 accesses global memory location X, thread 1 accesses location X + 1, thread 2 accesses location X + 2, and so on, all these accesses will be coalesced, or combined into a larger request for consecutive locations when accessing the DRAM or caches. Besides ensuring that threads in a warp access consecutive global memory locations, another consideration is the alignment of these accesses in global memory. When the starting address of these consecutive global memory locations is aligned to the beginning of a burst or cache line, this alignment ensures that the smallest number of memory transactions are needed to perform the coalesced access. On the other hand, if the starting address is not aligned, the unaligned access could result in additional memory traffic because the accesses data wall span a larger number of bursts or cache lines. To understand how to effectively use the coalescing hardware, we need to review how the memory addresses are formed in accessing C multi-dimensional array elements. Recall from Chapter 3 (Fig. 3.3, replicated as Fig. 6.1 for convenience) that multi-dimensional array elements in C and CUDA are placed into the linearly addressed memory space according to the row major convention. Recall that row major FIGURE 6.2 A coalesced access pattern.
refers to the fact that the placement of data preserves the structure of rows: all adjacent elements in a row are placed into consecutive locations in the address space. In Fig. 6.1, the four elements of row 0 are first placed in their order of appearance in the row. Elements in row 1 are then placed, followed by elements of row 2, followed by elements of row 3. It should be clear that M0,0 and M1,0 , though appear to be consecutive in the two-dimensional matrix, are placed four locations away in the linearly addressed memory. Let’s assume that a multi-dimensional array N similar to the array M in Fig. 6.1 is used as the second input matrix in matrix multiplication. In this case, consecutive threads in a warp that are assigned to consecutive output elements and will iterate through consecutive columns of this input matrix. The top-left part of Fig. 6.2 shows the code for this computation and the top-right part shows the logical view of the access pattern: consecutive threads iterating through consecutive columns. One can tell by inspecting the code that the accesses to N can be coalesced. The index of the array N is k*Width + col. The variables k and Width have the same value across all threads in the warp. The variable col is defined as blockIdx.x*blockDim.x+threadIdx.x, which means that consecutive threads (with consecutive threadIdx.x values) will have consecutive values of col, and will therefore access consecutive elements of N. The bottom part of Fig. 6.2 shows the physical view of the access pattern. In iteration 0, consecutive threads will access consecutive elements in row 0 which are adjacent in memory, shown as Loads for iteration 0'' in Fig. 6.2. In iteration 1, consecutive threads will access consecutive elements in row 1 which are also adjacent in memory, shown as Loads for iteration 1’’ in Fig. 6.2. This process continues for all rows. As we can see, the memory access pattern formed by the threads during this FIGURE 6.3 An un-coalesced access pattern.
process is a favorable one that can be coalesced. Indeed, in all the kernels we have implemented in the book so far, our memory accesses have been naturally coalesced. Now, assume that the matrix was stored in column-major order instead of rowmajor order. There could be various reasons why this might be the case. For example, we might be accessing the transpose of a matrix that is stored in row-major order. In linear algebra, we often need to use both the original and transposed forms of a matrix. It would be better to avoid creating and storing both forms. A common practice is to create the matrix in one form, say the original form. When the transposed form is needed, its elements can be accessed by accessing the original form and switching the roles of the row and column indices. This is equivalent to viewing the transposed matrix as being in the column-major layout. Regardless of the reason, let’s observe the memory access pattern achieved when the second input matrix to our matrix multiplication example is stored in column-major order. Fig. 6.3 illustrates how consecutive threads iterate through consecutive columns when the matrix is stored in column-major order. The top-left part of Fig. 6.3 shows the code and the top-right part shows the logical view of the memory accesses. The program is still trying to have each thread to access a column of matrix N. However, the index calculation for accessing N in the for-loop is adjusted to reflect the assumed column-major layout. One can tell by inspecting the code that the accesses to N are not favorable for coalescing. The index of the array N is col*Width+k. As before, col is defined as blockIdx.x*blockDim.x+threadIdx.x, which means that consecutive threads (with consecutive threadIdx.x values) will have consecutive values of col. However, in the index to N, col is multiplied by Width, which means that consecutive threads will access elements of N that are Width apart. Therefore, the accesses are not favorable for coalescing. In the bottom portion of Fig. 6.3, we can see that the physical view of the memory accesses is quite different from that in Fig. 6.2. In iteration 0, consecutive threads will logically access consecutive elements in row 0, but this time they are not adjacent in memory because of the column major layout. These loads are shown as ``Loads for iteration 0’’ in Fig. 6.3. Similarly, in iteration 1, consecutive threads will access consecutive elements in row 1 which are also not adjacent in memory. For a realistic matrix, there are typically hundreds or even thousands of elements in each dimension. The elements of N accessed in each iteration by neighboring threads can be hundreds or even thousands of elements apart. The hardware will determine that accesses to these elements are far away from each other and cannot be coalesced. There are various strategies for optimizing code to achieve memory coalescing when the computation is not naturally amenable to it. One strategy is to rearrange how threads are mapped to the data and another strategy is to rearrange the layout of the data itself. We will discuss these strategies in Section 6.8 and see examples of how they can be applied throughout this book. Yet another strategy is to transfer the data between global memory and shared memory in a coalesced manner and carry out the unfavorable access pattern in shared memory which is less sensitive to the data layout and provides faster access latency. We will also see example optimizations that use this strategy throughout this textbook, including an optimization that we will apply now to matrix-matrix multiplication where the second input matrix is in column-major layout. This optimization is called corner turning. Fig. 6.4 illustrates an example of how corner turning can be applied. In this example, M is an input matrix stored in row-major layout in global memory and N is an input matrix stored in column-major layout in global memory. They are multiplied to produce an output matrix P which is stored in row-major layout in global memory. The example illustrates how four threads that are responsible for the four consecutive elements at the top edge of the output tile load the input tile elements. The access to the input tile in matrix M is similar to that in Chapter 5. The four threads load the four elements at the top edge of the input tile. Each thread loads an input element whose local row and column indices within the input tile are the same as that of the thread’s output element within the output tile. These accesses are coalesced because consecutive threads access consecutive elements in the same row of M which are adjacent in memory according to the row major layout. On the other hand, the access to the input tile in matrix N needs to be different from that in Chapter 5. Fig. 6.4(a) shows what the access pattern would be like if we use the same arrangement as in Chapter 5. Even though the first four threads in the block are logically loading the four consecutive elements at the top edge of the input tile, the elements loaded by consecutive threads are far away from each other in the memory because of the column major layout of the N elements. In other words, consecutive threads that are responsible for consecutive elements in the same row of the output FIGURE 6.4 Applying corner turning to coalesce accesses to matrix N which is stored in column-major layout.
tile load non-consecutive locations in memory, which results in uncoalesced memory accesses. This problem can be solved by assigning the first four consecutive threads of the block to load the four consecutive elements at the left edge (the same column) in the input tile, as shown in Fig. 6.4(b). Intuitively, we are exchanging the roles of threadIdx.x and threadIdx.y when each thread calculates the linearized index for loading the N input tile. Since N is in column-major layout, consecutive elements in the same column are adjacent in memory. Hence, consecutive threads load input elements that are adjacent in memory, which ensures that the memory accesses are coalesced. The code can be written to place the tile of N elements into the shared memory in either column-major layout or row-major layout. Either way, after the input tile has been loaded, each thread can access its inputs with little performance penalty. This is because shared memory is implemented with SRAM technology and does not require coalescing. Note that we have been using a toy example illustrating the first four threads in the block when accessing 4 × 4 tiles. In reality, the tile size for corner turning will be the warp size of 32, so the first 32 threads in the block will access 32 consecutive elements on the left edge of the tile. While our example focuses on enabling the coalescing of global memory load operations, coalescing is also important for global memory store operations. In fact, the impact of uncoalesced stores can be more severe than the impact of uncoalesced loads. The reason is that uncoalesced stores generate partial store operations that perform read-modify-write accesses, which could require more memory bandwidth on GPUs with error correcting code (ECC) enabled. The main advantage of coalescing memory accesses is that it reduces global memory traffic by combining multiple memory accesses into a single access. Accesses FIGURE 6.5 Reducing traffic congestion in highway systems.
can be combined when they take place at the same time and access adjacent memory locations. Traffic congestion does not only arise in computing. Most of us have experienced traffic congestion in highway systems, as illustrated in Fig. 6.5. The root cause of highway traffic congestion is that there are too many cars all squeezing through a road that is designed for a much smaller number of vehicles. When congestion occurs, the travel time for each vehicle is greatly increased. Commute time to work can easily double or triple during traffic congestion. Most solutions for reduced traffic congestion involve reduction of cars on the road. Assuming that the number of commuters is constant, people need to share rides in order to reduce the number of cars on the road. A common way to share rides is carpooling, where a group of commuters take turns to drive the group to work in one vehicle. Governments usually have policies to encourage carpooling. In some countries, the government simply disallows certain classes of cars to be on the road on a daily basis. For example, cars with odd license plates may not be allowed on the road on Monday, Wednesday, or Friday. This encourages people whose cars are allowed on different days to form a carpool group. In other countries, the government may provide incentives for behavior that reduces the number of cars on the road. For example, in certain countries, some lanes of congested highways are designated as carpool lanes; only cars with more than two or three people are allowed to use these lanes. There are also countries where the government makes gasoline so expensive that people form carpools to save money. All these measures for encouraging carpooling are designed to overcome the fact that carpooling requires extra effort, as we show in Fig. 6.6. Carpooling requires workers who wish to carpool to compromise and agree on a common commute schedule. The top half of Fig. 6.6 shows a good schedule pattern for carpooling. Time goes from left to right. Worker A and Worker B have similar schedule for sleep, work, and dinner. This allows these two workers to easily go to work and return home in one car. Their similar schedules allow them to more easily agree on a common departure time and return time. This is however, not the case of the schedules shown in the bottom half of Fig. 6.6. Worker A and Worker B have very different habit in this case. Worker A parties until sunrise, sleeps during the day, and goes to work in the evening. Worker B sleeps at night, goes to work in the morning, FIGURE 6.6 Carpooling requires synchronization among people.
and returns home for dinner at 6 pm. The schedules are so wildly different that there is no way these two workers can coordinate a common time to drive to work and return home in one car. Memory coalescing is very similar to carpooling arrangements. We can think of the data as the commuters and the DRAM access requests as the vehicles. When the rate of DRAM requests exceeds the provisioned access throughput of the DRAM system, traffic congestion rises, and the arithmetic units become idle. If multiple threads access data from the same DRAM location, they can potentially form a ``carpool’’ and combine their accesses into one DRAM request. This, however, requires the threads to have similar execution schedule so that their data accesses can be combined into one. Threads in the same warp are the perfect candidates because they all execute a load instruction simultaneously by virtue of SIMD execution. Note that modern CPUs also recognize the DRAM burst organization in their cache memory design. A CPU cache line typically maps to a fraction of, one, or even multiple DRAM bursts. Applications that make full use of bytes in each cache line they touch tend to achieve much higher performance than those that randomly access memory locations. The techniques presented in this chapter can be adapted to help CPU programs achieve higher speed by reducing the congestion in accessing DRAM.
6.2 Hiding memory latency
As we explained in Section 6.1, DRAM bursting is a form of parallel organization: multiple locations are accessed in the DRAM core array in parallel. However, bursting alone is not sufficient to realize the level of DRAM access bandwidth required by modern processors. DRAM systems typically employ two more forms of parallel organization – banks and channels. At the highest level, a processor contains one or more channels. Each channel is a memory controller with a bus that connects a set of DRAM banks to the processor. Fig. 6.7 illustrates a processor that contains four FIGURE 6.7 Channels and banks in DRAM systems.
channels, each with a bus that connects four DRAM banks to the processor. In real systems, a processor typically has one to eight channels and each channel is connected a large number of banks. The data transfer bandwidth of a bus is defined by its width and clock frequency. Modern double data rate (DDR) buses perform two data transfers per clock cycle, one at the rising edge and one at the falling edge of each clock cycle. For example, a 64-bit DDR5-6400 bus with a clock frequency of 3.2 GHz has a bandwidth of 8 B × 2 × 3.2 GHz = 51.2 GB/sec. This seems to be a large number, but is often too small for modern CPUs and GPUs. For example, a modern CPU might require a memory bandwidth of at least 200 GB/s whereas a GPU might require more than 1000 GB/s, which would translate into four buses for the CPU and 20 buses for the GPU. Such large numbers of buses consume too much power when the processor needs to drive the wires on traditional circuit boards and have motivated the adoption of High Bandwidth Memory (HBM) technology. HBM co-packages the processors with DRAM to drastically reduce the size and capacity of connections between the two and enhance the clock frequency as well as the number of buses that connect the processor and the DRAM. For example, HBM3E supports a bus clock frequency of 4.9 GHz/s and 16 64-bit buses, resulting in 1 TB/s memory bandwidth. For each channel, the number of banks connected to it is determined by the number of banks required to fully utilize the data transfer bandwidth of the bus. This is illustrated in Fig. 6.8. Each bank contains an array of DRAM cells, the sensing amplifiers for accessing these cells, and the interface for delivering bursts of data to the bus (Section 6.1). Fig. 6.8(a) illustrates the data transfer timing when a single bank is connected to a channel. It shows the timing of two consecutive memory read accesses to the DRAM cells in the bank. Recall from Section 6.1 that each access involves long latency for the decoder to enable the cells and for the cells to share their stored charge with the sensing amplifier. This latency is shown as the light gray section at the left end of the time frame. Once the sensing amplifier completes its work, the burst data is delivered through the bus. The time for transferring the burst data through the bus is shown as the left dark section of the time frame in Fig. 6.8(a). The second memory read access FIGURE 6.8 Banking improves the utilization of data transfer bandwidth of a channel.
will incur a similar long access latency (light section between the dark sections of the time frame) before its burst data can be transferred (right dark section). In reality, the access latency (light gray sections) is much longer than the data transfer time (dark section). It should be apparent that the access-transfer timing of a one-bank organization would grossly underutilize the data transfer bandwidth of the channel bus. For example, if the ratio of DRAM cell array access latency to the data transfer time is 20:1, the maximal utilization of the channel bus would be 1/21 = 4.8%. That is a 16 GB/s channel would deliver data to the processor at a rate no more than 0.76 GB/s. This would be totally unacceptable. This problem is solved by connecting multiple banks to a channel bus. When two banks are connected to a channel bus, an access can be initiated in the second bank while the first bank is serving another access. Therefore, one can overlap the latency for accessing the DRAM cell arrays. Fig. 6.8(b) shows the timing of a two-bank organization. We assume that the bank 0 started at a time earlier than the window shown in Fig. 6.8. Shortly after the first bank starts accessing its cell array, the second bank also starts access its cell array. When the access in bank 0 is complete, it transfers the burst data (leftmost dark section of the time frame). Once bank 0 completes its data transfer, bank 1 can transfer its burst data (second dark section). This pattern repeats for the next accesses. From Fig. 6.8(b), we can see that by having two banks, we can potentially double the utilization of the data transfer bandwidth of the channel bus. In general, if the ratio of the cell array access latency and data transfer time is R, we need to have at least R + 1 banks if we hope to fully utilize the data transfer bandwidth of the channel bus. For example, if the ratio is 20, we will need at least 21 banks connected to each channel bus. In general, the number of banks connected to each channel bus needs to be larger than R for two reasons. One is that having more banks reduces FIGURE 6.9 Distributing array elements into channels and banks.
the probability of multiple simultaneous accesses targeting the same bank, a situation referred to as a bank conflict. Since each bank can serve only one access at a time, the cell array access latency can no longer be overlapped for these conflicting accesses. Having a larger number of banks increases the probability that these accesses will be spread out among multiple banks. The second reason is that the size of each cell array is set to achieve reasonable latency and manufacturability. This limits the number of cells that each bank can provide. One may need many banks just to be able to support the memory capacity required. Fig. 6.9 shows a toy example of distributing the elements of an array M to channels and banks. We assume a small burst size of two elements (eight bytes). The distribution is done by hardware design. The addressing of the channels and banks are such that the first eight bytes of the array (M[0] and M[1]) are stored in Bank 0 of Channel 0, the next eight bytes (M[2] and M[3]) in Bank 0 of Channel 1, the next eight bytes (M[4] and M[5]) in Bank 0 of Channel 2, and the next eight bytes (M[6] and M[7]) in Bank 0 of Channel 3. At this point, the distribution wraps back to Channel 0 but will use Bank 1 for the next eight bytes (M[8] and M[9]). This way, elements M[10] and M[11] will be in Bank 1 of Channel 1, M[12] and M[13] in Bank 1 of Channel 2, and M[14] and M[15] in Bank 1 of Channel 3. Although not shown in the figure, any additional elements will be wrapped around and start with Bank 0 of Channel 0. For example, M[16] and M[17] will be stored in Bank 0 of Channel 0, M[18] and M[19] will be in Bank 0 or Channel 1, and so on. The distribution scheme illustrated in Fig. 6.9, often referred to as interleaved data distribution, spreads the elements across the banks and channels in the system. This scheme ensures that even relatively small arrays are spread out nicely. That is, we only assign enough elements to fully utilize the DRAM burst of Bank 0 of Channel 0 before moving on to Bank 0 of Channel 1. In our toy example, as long as we have at least 16 elements, the distribution will involve all the channels and banks for storing the elements. There is an important connection between the parallel execution of threads and the parallel organization of the DRAM system. In order to achieve the memory access bandwidth specified for device, there must be a sufficient number of threads FIGURE 6.10 A small example of matrix multiplication (replicated from Fig. 5.5).
making simultaneous memory accesses. This observation reflects another benefit of maximizing occupancy. Recall that in Chapter 4, we saw that maximizing occupancy ensures that there are enough threads resident on the SMs to hide core pipeline latency, thereby utilizing the instruction throughput efficiently. As we see now, maximizing occupancy also has the additional benefit of ensuring that there are enough memory access requests made to hide DRAM access latency, thereby utilizing the memory bandwidth efficiently. Of course, to achieve the best bandwidth utilization, these memory accesses must be evenly distributed across channels and banks, and each access to a bank must also be a coalesced access. We now illustrate the interaction between parallel thread execution and the parallel memory organization. We will use the example in Fig. 5.5, replicated as Fig. 6.10. We assume that the matrix multiplication will be performed with 2 × 2 thread blocks and 2 × 2 tiles. During phase 0 of the kernel’s execution, all four thread blocks load their first tile. The M elements involved in each tile are shown in Fig. 6.11. Row 2 shows the M elements accessed in Phase 0, with their 2D indices. Row 3 shows the same M elements with their linearized indices. Assume that all thread blocks are executed in parallel. We see that each block will make two coalesced accesses. According to the distribution in Fig. 6.9, these coalesced accesses in Phase 0 by all thread blocks will be made to the banks on Channel 0 and the banks on Channel 2. These accesses will be done in parallel to take advantage of two channels as well as improving the utilization of the data transfer bandwidth of each channel. We also see that Block0,0 and Block0,1 will load the same M elements. Most of the modern devices are equipped with caches that will combine these accesses into FIGURE 6.11 M elements loaded by thread blocks in each phase.
one as long as the execution timings of these blocks are sufficiently close to each other. In fact, the cache memories in GPU devices are mainly designed to combine such accesses and reduce the number of accesses to the DRAM system. Rows 4 and 5 of Fig. 6.11 show the M elements loaded during phase 1 of the kernel execution. We see that the accesses are now done to the banks in Channel 1 and Channel 3. Once again, these accesses will be done in parallel. It should be clear to the reader that there is a symbiotic relationship between the parallel execution of the threads and the parallel structure of the DRAM system. On one hand, good utilization of the potential access bandwidth of the DRAM system requires that many threads simultaneously access data in the DRAM. On the other hand, the execution throughput of the device relies on good utilization of the parallel structure of the DRAM system, i.e., banks and channels. For example, if the simultaneously executing threads all access data in the same channel, the memory access throughput and the overall device execution speed will be greatly reduced. The reader is invited to verify that multiplying two larger matrices, such as 8 × 8 with the same 2 × 2 thread block configuration, will make use all the four channels in Fig. 6.9. On the other hand, an increased DRAM burst size would require multiplication of even larger matrices to fully utilize the data transfer bandwidth of all the channels.
FIGURE 6.12. A vector addition kernel that uses vector loads.
6.3 Vector loads and stores
We have seen in Section 6.2 that maximizing the occupancy of threads on the GPU allows these threads to issue enough memory accesses simultaneously to utilize the memory bandwidth efficiently. We can further increase the amount of memory that is accessed simultaneously by having each thread access a larger amount of continuous data in a single instruction. Such load and store instructions that access large amounts of continuous data per thread are referred to as vector loads and vector stores. For example, consider the vector addition kernel presented in Fig. 2.10 of Chapter 2. In this kernel, each thread loads two 4 B (i.e., 4-byte) floating point values and stores one 4 B floating point value. Since adjacent threads access adjacent elements in memory with each access, the memory accesses are coalesced. Moreover, since the kernel does not overutilize any resources, it achieves maximum occupancy and issues enough memory loads and stores simultaneously to utilize the memory bandwidth efficiently. However, the memory bandwidth utilization of this kernel can still be further improved. As is, each thread in the kernel executes one load or store instruction for every 4 B of memory that it accesses. Instead, we can use vector loads and stores to have each thread access 16 B of memory with each instruction. The code that embodies this optimization is shown in Fig. 6.12. In this example, each thread identifies its global index as before (line 02). However, the number of threads launched is only one quarter of the number of elements being added, and the index i refers to a chunk of four floating point values, not just a single one. To load a chunk of four floating point values, the pointers to the x and y arrays are cast to the float4 datatype before being dereferenced (lines 03-04). The float4 datatype represents four floating point values as one combined 16 B object. When a pointer to a float4 datatype is dereferenced, the compiler converts the access to a vector load or store that accesses the entire 16 B object. The loaded objects, x4 and y4, consist of four floating point values each that can be accessed via the fields .x, .y, .z, and .w. These fields are added individually and the results are placed in another float4 object z4 (lines 05-09). Finally, the pointer to the z array is cast to the float4 datatype while storing z4 (line 10). This cast ensures that the store is performed as a vector store. In the case where the total number of elements in the x and y arrays is not divisible by four, a boundary condition is needed to make sure that the boundary threadperforms scalar loads and stores instead of vector loads and stores to avoid accessing data that is out of bounds. A boundary condition is also needed to make sure that the out of bounds threads in the last thread block are not active. We leave these boundary conditions as an exercise to the reader. Using vector loads and stores reduces the instruction execution overhead associated with accessing global memory, allowing the memory bandwidth to be utilized more efficiently. In Fig. 6.12, two vector load instructions (lines 03-04) replace eight scalar load instructions, reducing the instruction execution overhead for loading the same amount of data by 75%. Vector loads and stores are also important in situations where kernels cannot execute at full occupancy, allowing such kernels to still be able to issue enough concurrent memory accesses to tolerate memory latency and fully utilize the memory bandwidth. We will see an example of such a situation in Chapter 15.
6.4 Shared memory bank conflicts
Unlike the global memory which is made from DRAM technology, the shared memory in GPUs is made from SRAM technology which has much shorter access latency. The shorter access latency allows the shared memory to avoid using the DRAM bursting technique and thus keep the banks narrow. For example, a typical shared memory structure on GPUs has an access latency of a few clock cycles and consists of 32 banks, with each bank being 32 bits (4 bytes) wide. Hence, when a GPU thread accesses a 32 bit integer or floating point value from shared memory, the thread alone utilizes the output of an entire bank. When the threads in the same warp access different values from shared memory, it is desirable for each thread to access a different memory bank so that the values can be served in parallel. For this reason, the shared memory is organized such that consecutive 4 B chunks are placed in different banks in an interleaved fashion, similar to the placement of data in DRAM banks and channels described in Section 6.2. For example, the first 4 B are placed in bank 0, the next 4 B in bank 1, the next 4 B in bank 2, etc. When the 32 banks are exhausted, the placement restarts at bank 0. Accordingly, if the 32 threads in the warp access 32 consecutive 4 B integer of floating point values, each thread will be served by a different bank in parallel. Unfortunately, if the accesses by threads in the same warp are less adjacent to each other, these accesses may hit the same bank at the same time, which results in a bank conflict. When a shared memory bank conflict occurs, the bank cannot serve multiple threads simultaneously. Instead, accesses to the same bank need to be served one at a time. The hardware detects when the shared memory accesses by threads in the same warp exhibit a bank conflict and divides these accesses into multiple conflict-free accesses. For example, consider the corner turning optimization in Fig. 6.4(b) in Section 6.1. Although the threads load from global memory in a coalesced manner, their stores to shared memory have a strided pattern. The code for writing to the shared memory looks as follows:
shared float a[TILE_DIM][TILE_DIM]; // TILE_DIM = 32 a[threadIdx.x][threadIdx.y] = …;
Assuming a block dimension of 32 × 32, threads in the same warp have the same threadIdx.y value and consecutive threadIdx.x values. Accordingly, these threads write to consecutive elements in the same column. Thread 0 writes to a[0][0], thread 1 writes to a[1][0], thread 2 writes to a[2][0], etc. Since array a is stored in row major order, the linear index of a[i][j] is i × 32 + j . Hence, the linear indices of a[0][0], a[1][0], a[2][0], etc. are 0, 32, 64, etc. That is, the threads in the same warp access elements that are 32 positions (128 B) apart. Now assume that the element at index 0 is located in bank 0. In this case, the elements at indices 1 to 31 are located in banks 1 to 31. After reaching the last bank, the order restarts and the subsequent element at index 32 is located in bank 0. Hence, threads 0 and 1 both write to bank 0. Similarly, the element at index 64 written to by thread 2 is also located at bank 0 (64 mod 32). In fact, all threads in the same warp write to the same bank causing a 32-way bank conflict! The hardware serializes these writes, performing 32 successive accesses to the same bank, while the other banks are not used. To avoid bank conflicts, we need to rearrange the data such that elements accessed by threads in the same warp are placed in different banks. Such a rearrangement can be done adding an extra column to the tile, which effectively adds extra elements between consecutive rows. These extra unused elements that are added to modify the data layout are referred to as padding. The following code shows how padding can be added to the corner turning example: shared float a[TILE_DIM][TILE_DIM + 1]; // TILE_DIM = 32 a[threadIdx.x][threadIdx.y] = …;
The key change is that the number of columns in a is now TILE_DIM + 1 instead of TILE_DIM. Let us observe the impact of this change on how the shared memory banks are accessed. Since array a now has 33 columns, the linear index of a[i][j] is now i × 33 + j . Accordingly, thread 0 accesses a[0][0] at linear index 0, which is placed in bank 0. Thread 1 accesses a[1][0] at linear index 1 × 33 + 0 = 33, which is placed in bank 1 (33 mod 32). Thread 2 accesses a[2][0] at linear index 2 × 33 + 0 = 66, which is placed in bank 2 (66 mod 32). It is clear that the bank conflicts are eliminated. Each of the 32 threads in the warp accesses a different bank. These accesses are served by the banks in parallel without the need for the hardware to serialize the accesses. As is the case with many performance optimization techniques, padding comes with cost and limitations. The most obvious cost is that it consumes additional shared memory resources. Another important cost of padding is that the calculation of the linearized index for accessing a padded array can become more expensive than when the original dimensions of the array are of power of two. For example, when the number of columns is 32, which is a 25 , the multiplication i × 32 in the linearized index calculation can be simplified into a shift operation i » 5. However, when thenumber of columns is padded into 33, the multiplication will need to be implemented as i × (32 + 1) = i » 5 + i. The number of additional operations needed depends on the number of additional columns introduced by padding. The observed benefit of padding an array depends on the access patterns and the original dimension of the array. For example, if the number of columns in the original array were 16 rather than 32, the bank conflicts among threads in the same warp would have spread across two banks, with a 16-way bank conflict on each bank. Thus, the performance benefit of padding would have been lower. This is intuitive as the smaller stride means that the accesses within the same warp are closer to each other.
6.5 Thread coarsening
So far, in all the kernels that we have seen, work has been parallelized across threads at the finest granularity. That is, each thread was assigned the smallest possible unit of work. For example, in the vector addition kernel in Chapter 2, each thread was assigned one output element. In the RGB to grayscale conversion and the image blur kernels in Chapter 3, each thread was assigned one pixel in the output image. In the matrix multiplication kernels in Chapter 3 and Chapter 5, each thread was assigned one element in the output matrix. The advantage of parallelizing work across threads at the finest granularity is that it enhances transparent scalability, as discussed in Chapter 4. If the hardware has enough resources to perform all the work in parallel, then the application has exposed enough parallelism to fully utilize the hardware. Otherwise, if the hardware does not have enough resources to perform all the work in parallel, the hardware can simply serialize the work by executing it in multiple waves, delaying the execution of some of the thread blocks until sufficient resources become available due to the completion of the ones that are currently running. The disadvantage of parallelizing work at the finest granularity comes when there is an overhead associated with parallelizing that work. This overhead can take many forms such as redundant loading of data by different thread blocks, redundant work, synchronization overhead, instruction execution overhead, and others. When the threads are executed in parallel by the hardware, this parallelization overhead is often worth incurring. However, if the hardware ends up serializing the work due to insufficient resources, then the overhead has been incurred unnecessarily. In this case, it is better for the programmer to partially serialize the work and reduce the overhead of parallelization. This can be done by assigning each thread multiple units of work, which is often referred to as thread coarsening. For example, consider the following code where each thread in the kernel is responsible for performing some computation foo on different data distinguished by index i: unsigned int i = blockIdx.x*blockDim.x + threadIdx.x; foo(i);
Here, each thread performs foo only once for one value of i. Alternatively, thread coarsening could be applied where each thread could be assigned to perform foo multiple times for multiple values of i. We refer to the number of units of work that the thread is assigned to perform as the coarsening factor. The following code shows how thread coarsening could be applied to the previous code example with a coarsening factor of 4: unsigned int iStart = 4*(blockIdx.x*blockDim.x + threadIdx.x); for(unsigned int c = 0; c < 4; ++c) { unsigned int i = iStart + c; foo(i); } Here, the global thread index is multiplied by 4 such that each thread receives the starting value of 4 consecutive integers. For example, thread 0 starts at an iStart of 0 and is responsible for integers 0, 1, 2, and 3; thread 1 starts at an iStart of 4 and is responsible for integers 4, 5, 6, and 7; etc. Next, each thread executes a loop that iterates over its 4 assigned values. This loop is referred to as a coarsening loop. Finally, in each loop iteration, the thread calculates the index i corresponding to the unit of work that iteration is responsible for and calls foo. While we introduce thread coarsening using a toy example in this chapter, we will see more realistic examples of how to apply thread coarsening to real kernels throughout this book. We have already seen an example of thread coarsening in Section 6.3 where each thread of the vector addition kernel is assigned to add four pairs of elements instead of one pair to benefit from vector loads and stores. Here, the overhead of parallelizing vector addition across more threads is the instruction execution overhead associated with the load and store instructions. If the threads actually run in parallel, this overhead may be worth paying. However, if there are excess threads and the hardware serializes them, then it is preferable to serialize the additions ourselves within the threads as we have done in Fig. 6.12. Serializing multiple additions in a single thread enables us to combine the memory accesses into vector loads and stores, thereby reducing the instruction execution overhead associated with accessing memory. Another example where thread coarsening could be beneficial is the tiled matrix multiplication kernel in Chapter 5. In this example, parallelizing the computation across many thread blocks means that the same input tiles are redundantly loaded by different thread blocks. Using fewer thread blocks and having each block process larger output tiles enables each input tile to be loaded fewer times. We will visit this example in depth in Chapter 15 where we apply thread coarsening to matrix multiplication to improve data reuse. Thread coarsening is a powerful optimization that can result in substantial performance improvement for many applications. It is an optimization that is commonly applied, and that we will see recur in many chapters. However, there are several pitfalls to avoid when applying thread coarsening. The first pitfall is that one must becareful not to apply the optimization when it is unnecessary. Recall that thread coarsening is beneficial if there is an overhead incurred when parallelizing a computation. Not all computations exhibit such an overhead. For some computations, threads operate completely independently without any coordination or sharing of data. Applying thread coarsening to such computations is not expected to make a substantial performance difference. The second pitfall to avoid when applying thread coarsening is not to apply too much coarsening such that the hardware resources become underutilized. Recall that exposing as much parallelism as possible to the hardware enables transparent scalability. It provides the hardware with the flexibility of parallelizing or serializing work depending on the amount of execution resources it has. When programmers coarsen threads, they reduce the amount of parallelism exposed to the hardware. If the coarsening factor is too high, not enough parallelism will be exposed to the hardware resulting in some parallel execution resources being unutilized. For example, if a kernel that has 1056 blocks runs on a GPU that can execute 264 blocks simultaneously, the kernel would have 4 waves that fully utilize the GPU. However, if the threads in this kernel are coarsening by a factor of 8, then the kernel would only have 132 blocks or 0.5 waves. In this case, half the GPU resources would be unutilized, which would likely negatively impact performance. Programmers should also be careful to pick a coarsening factor that does not result in partial waves. For example, if an uncoarsened kernel that has 1584 blocks runs on a GPU that can execute 264 blocks simultaneously, the kernel would have 6 waves that fully utilize the GPU. If the threads in this kernel are coarsening by a factor of 4, then the kernel would have 396 blocks or 1.5 waves. Hence, after the first full wave completes, the second partial wave underutilizes the GPU resources and has a tail effect as discussed in Chapter 4. In practice, different devices have different amounts of execution resources, so the best coarsening factor is usually device-specific and dataset-specific and needs to be retuned for different devices and datasets. Hence, when thread coarsening is applied, scalability becomes less transparent. That is, the kernel may need to be recompiled from one generation of GPU to another generation using a different coarsening factor to achieve the best possible level of performance. The third pitfall to avoid when applying thread coarsening is not to increase resource consumption to the extent that it substantially reduces occupancy and hurts performance. Depending on the kernel, thread coarsening may require using more registers per thread or more shared memory per thread block because the thread and thread block are responsible for performing more work. In such cases, if programmers use too many registers or too much shared memory, the occupancy can be reduced. The programmer must be careful that the performance penalty from reducing occupancy is not more detrimental than the performance benefit that thread coarsening may offer.
6.6 Loop unrolling
Recall from Chapter 1 that GPUs follow throughput-oriented design as opposed to latency-oriented design, which means that GPU cores are optimized to execute many threads concurrently at the expense of long latencies experienced by individual threads. In Chapter 4, we discussed an important optimization for tolerating latency by maximizing the occupancy of threads on an SM. Maximizing occupancy ensures that when a warp encounters a stall while waiting for the output of a long-latency instruction, there is a high probability of finding another warp that is ready to execute and keep the SM cores busy. In this section, we discuss another important optimization for tolerating latency, i.e., loop unrolling. Loop unrolling is an optimization that transforms a loop by replicating the body of the loop by some factor and reducing the number of loop iterations by the same factor. For example, consider the following loop: for(unsigned int i = 0; i < 16; ++i) { A(i) B(i) } where A(i) and B(i) are some operations or instructions performed in loop iteration i. If we apply loop unrolling with an unrolling factor of four, the loop will be transformed as follows: for(unsigned int i = 0; i < 16; i += 4) { A(i) B(i) A(i + 1) B(i + 1) A(i + 2) B(i + 2) A(i + 3) B(i + 3) } There are two advantages of loop unrolling that relate to tolerating latency. The first advantage of loop unrolling is that by reducing the number of loop iterations, the number of branch instructions is reduced. While CPUs have sophisticated branch prediction to reduce the latency of branch instructions, GPUs do not have such sophisticated branch prediction. When a GPU thread encounters a branch instruction, it stalls until the outcome of the branch is resolved. Such stalls are reduced when the number of branch instructions is reduced by loop unrolling. The second advantage of loop unrolling is that it exposes more independent instructions that can be reordered to avoid stalling. In our example, if B(i) depends on A(i), then when a thread executes A(i), it needs to stall and wait for A(i) to complete before it can execute B(i). Alternatively, if we have other operations that we can perform that do not need to wait for A(i), we can place those operations in between A(i) and B(i) to avoid having to stall. In our example, if A(i + 1) is independent of A(i) and B(i), then the body of the unrolled loop can be further transformed as follows:
for(unsigned int i = 0; i < 16; i += 4) { A(i) A(i + 1) A(i + 2) A(i + 3) B(i) B(i + 1) B(i + 2) B(i + 3) } In this case, moving A(i + 1), A(i + 2), and A(i + 3) in between A(i) and B(i) ensures that the thread has plenty of instructions to execute while waiting for A(i) to finish so it can execute B(i). Reordering instructions in such a way to avoid stalling is part of an important optimization called instruction scheduling. In practice, loop unrolling and instruction scheduling are applied automatically and aggressively by the compiler at the compiler’s discretion, and the programmer does not need to worry about them. In our example, since the loop body is small and the loop bound is constant and small, the compiler will most likely fully unroll the loop, eliminating all branches and exposing the largest number of instructions to schedule. If the programmer would like to have control over loop unrolling, this control can be exerted by placing the directive #pragma unroll N before the loop to instruct the compiler to unroll the loop by a factor of N. For example, the directive can be applied as follows: #pragma unroll 4 for(unsigned int i = 0; i < 16; ++i) { A(i) B(i) } In this code, the loop will only be unrolled by a factor of 4. Setting the unrolling factor to 1 instructs the compiler not to perform any unrolling. Throughout this book, we will not annotate loops with the #pragma unroll directive except where it is particularly important to emphasize that a loop is expected to be unrolled. A common category of loops that benefit from loop unrolling are loops that apply thread coarsening (see Section 6.5). Loop unrolling is essential to making thread coarsening efficient. For example, consider the following code: int x; foo(x);
In this code, each thread is responsible for processing some variable x which is placed in a register. If we apply thread coarsening to this code, each thread will be responsible for multiple x’s. The code after coarsening can be written as follows (assuming a coarsening factor of four):
int x[4]; for(unsigned int c = 0; c < 4; ++c) { foo(x[c]); } However, recall from Chapter 5 that local arrays such as x[4] are placed in global memory by default. It is necessary to place arrays in memory so that they can be accessed with a variable index, as is done with the access x[c]. Unfortunately, placing x[4] in global memory can introduce memory access overhead that outweighs the benefits of thread coarsening. However, the thread coarsening loop has a small and constant loop bound (which is typical of thread coarsening loops) so it can be fully unrolled. In this case, the code with the fully unrolled coarsening loop would look as follows: int x[4]; foo(x[0]); foo(x[1]); foo(x[2]); foo(x[3]);
Since all accesses to the local array x have constant indices after unrolling, as opposed to variable indices, the compiler can promote the local array x from memory to registers. Hence, not only does loop unrolling reduce branches and expose more instructions for scheduling, it also exposes opportunities for propagating constants and promoting variables from memory to registers for more efficient access.
6.7 Double buffering
Recall from Chapter 5 that threads in the same thread block use __syncthreads() to perform barrier synchronization in order to coordinate their accesses to shared memory. Consider the following example code where threads in the same block pass data to each other during a computation: for(unsigned int i = 0; i < N; ++i) { … = buffer[anotherThreadID]; __syncthreads(); buffer[myThreadID] = …; __syncthreads(); } In this example, in every loop iteration, every thread reads some value in memory from a location that was previously written by another thread, then writes a new value to memory at a location corresponding to its own ID. This new value may itself be read by other threads in future iterations. The code example includes two barrier synchronization calls to ensure that the code executes correctly. The second barrier synchronization ensures that all the threads finish writing their values to memorybefore other threads read them. In other words, the barrier enforces a read-after-write dependence. This dependence is also referred to as a true dependence because it is unavoidable. We must wait for a value to be written before we can read it. On the other hand, the first barrier synchronization ensures that all threads finish reading the values they need before other threads overwrite them on the next iteration. In other words, the barrier enforces a write-after-read dependence. This dependence is also referred to as a false dependence because we do not really need to wait to read a value before writing another value. The only reason we are forced to wait is because the value being read and the value being written occupy the same memory location. If these two values did not occupy the same memory location, the write-after-read or false dependence would be eliminated, and hence, the first barrier synchronization would not be needed. Based on this observation, we can eliminate the second barrier synchronization by using different memory buffers on different loop iterations. One way to do so is to allocate a different buffer for each iteration. However, this approach would require a large amount of memory resources which is undesirable. A better approach is to simply allocate two buffers and alternate between them each iteration. This optimization is referred to as double-buffering because it requires allocating two buffers in total that keep trading places. The following code example shows how double-buffering can be applied to the previous example: for(unsigned int i = 0; i < N; ++i) { … = inBuffer[anotherThreadID]; outBuffer[myThreadID] = …; __syncthreads(); swap(inBuffer, outBuffer); } In this example, two different buffers are allocated, one pointed to by inBuffer and another pointed to by outBuffer. Since the read from memory is performed from inBuffer and the write is performed to outBuffer, there is no risk that the write will overwrite a value that another thread is reading. Accordingly, the call to __syncthreads() that separates these two memory accesses can be eliminated. However, after the write, the most up to date data is now located in outBuffer, so this buffer is the one we should read from in the next iteration. Swapping the two buffers at the end of the loop body guarantees that the right buffer is read from on the next iteration. This example is only a toy example, and we will see more concrete examples of how to apply double-buffering to real kernels throughout this book.
6.8 A checklist of optimizations
Throughout this first part of the textbook, we have covered various common optimizations that CUDA programmers apply to improve the performance of their code.
FIGURE 6.13. A checklist of optimizations.
We consolidate these optimizations into a single checklist that is shown in Fig. 6.13. This checklist is not an exhaustive one, but it contains many of the universal optimizations that are common across different applications and that programmers should first consider. In the second and third parts of the textbook, we apply the optimizations in this checklist to various parallel patterns and applications to understand how theymanifest in different contexts. In this section, we give a brief overview of each optimization and the strategies for applying it. The optimizations in Fig. 6.13 are categorized into three major categories based on the aspect of performance that they try to improve. The compute utilization category refers to optimizations that aim to reduce idleness in the GPU cores by making sure that cores have useful work to do instead of remaining idle. The memory utilization category refers to optimizations that improve the efficiency of how the memory subsystem is used to access data. The synchronization latency category refers to optimizations that reduce the need for threads to wait on barrier synchronizations. While some optimizations may fall into multiple categories, we place them in the category corresponding to the primary intent of when they are typically used. The final optimization, thread coarsening, is labeled as general because the category it falls into differs based on the context in which it is applied. The compute utilization category consists of three main optimizations. The first optimization is tuning the occupancy of threads on SMs. This optimization was introduced in Chapter 4 where the importance of having many more threads than cores was emphasized as a way for having enough work available to hide long-latency operations in the core pipeline. To maximize occupancy, programmers can tune the resource usage of their kernels to ensure that the maximum number of blocks or registers allowed per SM do not limit the number of threads that can be assigned to the SM simultaneously. In Chapter 5, shared memory was also introduced as another resource whose usage should be carefully tuned so as not to limit occupancy. In Section 6.2 of this chapter, the importance of maximizing occupancy was discussed as a means for also hiding memory latency, not just core pipeline latency. Having many threads executing simultaneously ensures that enough memory accesses are generated to fully utilize the memory bandwidth. Throughout this book, we will apply various optimizations that increase the shared memory or register usage of a kernel, and we will assume that the programmer can adequately tune their usage so as not to adversely affect occupancy. In Chapter 15, we will see an example of where the benefit of using more resources may outweigh the decrease in occupancy, and where other optimizations are used to improve compute utilization that compensate for the reduction in occupancy. The second optimization for improving compute utilization is loop unrolling. In Section 6.6 of this chapter, we saw that loop unrolling reduces the number of long-latency branch instructions, exposes more independent instructions that can be scheduled to reduce stalls, and may convert local array indices to constants which allows them to be promoted to registers. Throughout this book, we will assume that the compiler does an adequate job at unrolling loops where profitable. In Chapter 11, which covers the prefix sum pattern, and Chapter 15, which covers advanced optimizations for matrix multiplication, we will emphasize how loop unrolling is essential to enable register tiling after thread coarsening. In Chapter 15, we will also comment on how loop unrolling is critical for compensating for the kernel executing with low occupancy. The third optimization for improving compute utilization is reducing control divergence. Control divergence was introduced in Chapter 4, where the importance ofthreads in the same warp taking the same control path was emphasized as a means for ensuring that all cores are productively utilized during SIMD execution. So far, the kernels we have looked at in this part of the textbook have not exhibited control divergence, except the inevitable divergence at boundary conditions. However, we will see many examples in the second and third parts of the textbook where control divergence can be a significant detriment to performance. There are multiple strategies that can be employed for reducing control divergence. One strategy is to rearrange how the work and/or data is distributed across the threads to ensure that threads in one warp are all used before using threads in other warps. We will see an example of this strategy in Chapter 10, which covers the reduction pattern. The strategy of rearranging how work and/or data is distributed across threads can also be used to ensure that threads in the same warp have similar workload. We will see an example of this in Chapter 18, which covers graph traversal, where we discuss the tradeoffs between vertex-centric and edge-centric parallelization schemes. Another strategy for reducing control divergence is to rearrange how the data is laid out to ensure that threads in the same warp which process adjacent data have similar workload. We will see an example of this strategy in Chapter 17, which covers sparse matrix computation and storage formats, particularly when discussing the JDS format. The memory utilization category in Fig. 6.13 consists of six main optimizations. The first optimization for improving memory utilization is using coalesced global memory accesses by ensuring that threads in the same warp access adjacent memory locations within the same cache line and/or DRAM burst. This optimization was introduced in Section 6.1 of this chapter, where the hardware’s ability to combine accesses to adjacent memory locations into a single memory request was emphasized as a way for reducing global memory traffic and improving utilization of DRAM bursts and cache lines. So far, the kernels we have looked at in this part of the textbook have exhibited coalesced accesses naturally. However, we will see many examples in the second and third parts of the textbook where memory access patterns are more irregular, thereby requiring more effort to achieve coalescing. There are multiple strategies that can be employed for achieving coalescing in applications with irregular access patterns. One strategy is to load data from global memory to shared memory in a coalesced manner, then perform the irregular accesses on shared memory. We have already seen an example of this strategy in Section 6.1, namely corner turning. We will see another example of this strategy in Chapter 12, which covers the filtering pattern, and Chapter 14, which covers the sorting pattern. In these patterns, threads write out results to an array in a scattered manner. Instead, they can collaborate to perform their scattered accesses in the shared memory, then write the result from the shared memory to the global memory with more coalescing enabled for elements with nearby destinations. This optimization is sometimes referred to as packing. We will also see an example of this strategy in Chapter 13, which covers the merge pattern. In this pattern, threads in the same block need to perform a binary search in the same array, so they collaborate to load that array from global memory to shared memory in a coalesced manner, and then each thread performs the binary search in shared memory.
Another strategy for achieving coalescing in applications with irregular access patterns is to rearrange how the threads are mapped to the data elements. We will see an example of this strategy in Chapter 10, which covers the reduction pattern, and Chapter 15, which covers advanced optimizations for matrix multiplication. Yet another strategy for achieving coalescing in applications with irregular access patterns is to rearrange how the data itself is laid out. We will see an example of this strategy in Chapter 17, which covers sparse matrix computation and storage formats, particularly when discussing the ELL and JDS formats. Other common examples of rearranging the layout of the data are transposing a matrix or converting an array of structures into a structure of arrays. The second optimization for improving memory utilization is tiling data that is reused within a thread block by placing it in shared memory and accessing it repetitively from there, such that it only needs to be transferred between global memory and the SM once. Shared memory tiling was introduced in Chapter 5 in the context of matrix multiplication, where threads processing the same output tile collaborate to load the corresponding input tiles to the shared memory then access these input tiles repeatedly from the shared memory. We will see shared memory tiling applied again in most of the parallel patterns in the second and third parts of the textbook. We will observe the challenges of applying tiling when the input and output tiles have different dimensions. This challenge arises in Chapter 7, which covers the convolution pattern, and Chapter 8, which covers the stencil pattern. We will also see more sophisticated applications of shared memory tiling to matrix multiplication in Chapter 15 and to sequence alignment using dynamic programming in Chapter 16. The third optimization for improving memory utilization is tiling data that is reused within a warp or thread by placing it in registers and accessing it repetitively from there, such that it only needs to be transferred between global or shared memory and registers once. Register tiling was applied in Chapter 3 and Chapter 5 where each thread used a local variable or register to accumulate a result before writing it to global memory. One can think of the collective set of registers across all threads as jointly forming a tile of the output data. We will see register tiling again in Chapter 8, where a shared memory input tile is temporarily moved from shared memory to registers and back for efficient access and preservation of shared memory capacity. We will also see register tiling in Chapter 11, which covers the prefix sum pattern, and Chapter 15, which covers advanced optimizations for matrix multiplication, where each thread is responsible for multiple output elements with multiple corresponding input elements that it reuses. The fourth optimization for improving memory utilization is using vector loads and stores to access larger chunks of memory with less instruction execution overhead. Vector loads and stores were introduced in Section 6.3 of this chapter and applied in the context of vector addition. The optimization can be applied to most computations discussed in this book. Due to the simplicity of the optimization, we will not apply it throughout the book. We will only mention it briefly where it is particularly relevant. The fifth optimization for improving memory utilization is avoiding shared memory bank conflicts. This optimization was introduced in Section 6.4 of this chapter in the context of corner turning. The main strategies to avoid bank conflicts are to rearrange how the threads are mapped to the data or to rearrange the data layout itself, often by adding padding elements. We will see an example of this optimization again in Chapter 11, which covers the prefix sum pattern, and in Chapter 15, which covers advanced optimizations for matrix multiplication, where threads make strided accesses to shared memory when they load their portions of the input data. The sixth optimization for improving memory utilization is privatization. This optimization has not been introduced yet, but it is mentioned here for completeness. Privatization relates to when multiple threads or blocks need to update a common (public) output. To avoid the overhead of updating the same data concurrently, a private copy of the data can be created and partially updated, then a final update can be made from the private copy to the public copy when done. We will see an example of privatization in Chapter 9, which covers the histogram pattern, where multiple threads need to update the same histogram counters. We will also see examples of privatization in Chapter 12, which covers the filtering pattern, and Chapter 18, which covers graph traversal, where multiple threads need to insert entries into the same output list. The synchronization latency category in Fig. 6.13 consists of two main optimizations. The first optimization for reducing synchronization latency is using warp-level primitives. Warp-level primitives were briefly mentioned in Chapter 4 as a means for threads within the same warp to synchronize and share data together without performing the more expensive block-wide barrier synchronization and accessing shared memory. The main strategy for reducing synchronization overhead with warplevel primitives is to decompose a block-wide computation across warps such that operations requiring barrier synchronization are first performed at the warp-level, then the warp-level results are consolidated at the block-level. We will see examples of using warp-level primitives to optimize computations that perform frequent synchronization in Chapter 10, Chapter 11, and Chapter 12 which cover the reduction, prefix sum, and filter patterns, respectively. We will also visit warp-level primitives in Chapter 15, which covers advanced optimizations for matrix multiplication, where warp-level primitives will be used to assist with register tiling. The second optimization for reducing synchronization latency is double-buffering. Double buffering was first introduced in Section 6.7 of this chapter. Double-buffering eliminates false (write-after-read) dependences by using different buffers for the writes and the preceding reads, thereby allowing the barrier synchronizations that enforce the false dependences to be removed. We will see double-buffering applied in real contexts in Chapter 11, which covers the prefix sum pattern, and Chapter 15, which covers advanced optimizations for matrix multiplication. The final optimization in Fig. 6.13 is thread coarsening, which is a general optimization whose performance objective depends on the computation at hand. Thread coarsening was first introduced in Section 6.5 of this chapter, where multiple units of parallel work are assigned to each thread to reduce the overhead of parallelization if the hardware is going to serialize the threads anyways. In the second and third parts of this textbook, we will see thread coarsening applied in different contexts with adifferent overhead of parallelization each time. In Chapter 8, which covers the stencil pattern, and Chapter 15, which covers advanced optimizations for matrix multiplication, thread coarsening is applied to increase the size of the output tile, thereby reusing input data loaded from memory to compute more output values. In this case, the overhead of having finer-grain parallelism with smaller output tiles is needing to load the input data redundantly by more thread blocks. In Chapter 9, which covers the histogram pattern, thread coarsening helps reduce the number of private copies that need to be committed to the public copy in the context of the privatization optimization. In Chapter 10, which covers the reduction pattern, and Chapter 11, which covers the prefix sum pattern, thread coarsening is applied to reduce the overhead from synchronization and control divergence. Also in Chapter 11, which covers the prefix sum pattern, thread coarsening helps reduce the redundant work performed by the parallel algorithm compared to the sequential algorithm. In Chapter 12, which covers the filtering pattern, and Chapter 14, which covers the sorting pattern, thread coarsening helps improve memory coalescing. Again, the checklist in Fig. 6.13 is not intended to be an exhaustive one, but it contains the major types of optimizations that are common across different computation patterns. These optimizations appear in multiple chapters in the second and third parts of the textbook. We will also see other optimizations that appear in specific chapters. For example, in Chapter 7, which covers the convolution pattern, we introduce the use of constant memory.
6.9 Optimization strategy
We have seen in Section 6.8 that different optimizations target different aspects of performance. Hence, when deciding which optimization to apply to a specific kernel, it is important to first understand what resource is limiting the performance of that kernel. The resource that limits the performance of a kernel is often referred to as a performance bottleneck. Optimizations typically use more of one resource to reduce the burden on another resource. If the optimization applied does not target the bottleneck resource, one may not see any benefit from the optimization. Worse yet, if the optimization applied uses more of the bottleneck resource, the optimization may even hurt performance. For example, shared memory tiling increases the use of shared memory to reduce the pressure on the global memory bandwidth. This optimization is great when the bottleneck resource of a kernel is the global memory bandwidth. However, if the performance of a kernel is limited by occupancy and occupancy is already constrained by the use of too much shared memory, then applying shared memory tiling is likely to make things worse. Performance optimization is an iterative process whereby one identifies the performance bottleneck of a kernel, applies an optimization to alleviate that bottleneck, identifies the new bottleneck, alleviates it, and keeps repeating. To identify the performance bottleneck of a kernel, GPU computing platforms typically provide variousprofiling tools. We refer readers to the CUDA documentation for more information on how to use profiling tools to identify the performance bottlenecks of their kernels [1]. Performance bottlenecks may be hardware-specific, meaning that the same kernel may encounter different bottlenecks on different devices. For this reason, the process of identifying performance bottlenecks and applying performance optimizations requires a good understanding of the GPU architecture and the architectural differences across different GPU devices. An important skill is knowing when to stop the iterative optimization process because the kernel’s performance can no longer be further improved. In Chapter 5, the roofline model was discussed as means for assessing how far a kernel’s performance is from the hardware limit. By estimating how many computations a kernel performs and how much data it accesses from memory, the kernel’s computational intensity can be derived to infer whether the kernel is compute-bound or memory-bound. After that, the kernel’s actual resource utilization reported by the profiler can be compared to the hardware’s limit using the roofline model. A kernel that is close to the hardware limit has little room for further optimization. On the other hand, a kernel that has not reached the hardware limit can likely be further optimized.
6.10 Summary
In this chapter, we covered the off-chip memory (DRAM) architecture of a GPU and discussed related performance considerations such as global memory access coalescing and hiding memory latency with memory parallelism. We then presented a few more important optimizations, which are thread granularity coarsening, loop unrolling, and double-buffering. With the insights presented in this chapter and earlier chapters, readers should be able to reason about the performance of any kernel code they come across. We concluded this part of the textbook by presenting a checklist of common performance optimizations that are widely used to optimize many computations. We will continue to study practical applications of these optimizations in the parallel computation patterns and application case studies in the next two parts of the book.
6.11 Exercises
global void foo_kernel(float* a, float* b, float* c, float* d, float* e) { unsigned int i = blockIdx.x*blockDim.x + threadIdx.x;
shared float a_s[256];
shared float bc_s[4*256];
a_s[threadIdx.x] = a[i];
for(unsigned int j = 0; j < 4; ++j) {
References
bc_s[j*256 + threadIdx.x] = b[j*blockDim.x*gridDim.x + i] + c[i*4 + j];
}
__syncthreads();
d[i + 8] = a_s[threadIdx.x];
e[i*8] = bc_s[threadIdx.x*4]; }
For each of the following memory accesses, specify whether they are coalesced, uncoalesced, or if coalescing is not applicable. a. The access to array a of line 05 b. The access to array a_s of line 05 c. The access to array b of line 07 d. The access to array c of line 07 e. The access to array bc_s of line 07 f. The access to array a_s of line 10 g. The access to array d of line 10 h. The access to array bc_s of line 11 i. The access to array e of line 11 2. Write a matrix multiplication kernel function that uses corner turning, corresponding to the design illustrated in Fig. 6.4. 3. For tiled matrix multiplication in Chapter 5, out of the possible range of values for BLOCK_SIZE, for what values of BLOCK_SIZE will the kernel completely avoid uncoalesced accesses to global memory? (You need to consider only square blocks.) 4. Implement a vector addition kernel that uses vector loads and handles the boundary conditions correctly. 5. Consider the following shared memory array and access to the array by a block with one warp (32 threads): shared float a[1024]; a[threadIdx.x*stride] = …;
Assume that a[0] is placed in bank 0. For each of the following values of stride, specify the banks accessed by the warp and the number of bank conflicts on each bank (if any): a. stride = 32 b. stride = 31 c. stride = 24 d. stride = 16 e. stride = 12 f. stride = 8 g. stride = 7 References [1] NVIDIA, Nsight compute, https://docs.nvidia.com/nsight-compute/index.html, 2023.
Part 2: Parallel patterns
References
[1] NVIDIA, Nsight compute, https://docs.nvidia.com/nsight-compute/index.html, 2023. Chapter 15. Advanced optimizations for matrix multiplication