An Accurate GPU Performance Model for Effective Control ...lin.ma/publications/TCAD.pdf · 2...

14
1 An Accurate GPU Performance Model for Effective Control Flow Divergence Optimization Yun Liang, Member, IEEE Muhammad T. Satria, Kyle Rupnow, and Deming Chen, Senior Member, IEEE Abstract—Graphic processing units (GPUs) are composed of a group of single-instruction multiple data (SIMD) streaming multiprocessors (SMs). GPUs are able to efficiently execute highly data parallel tasks through SIMD execution on the SMs. However, if those threads take diverging control paths, all divergent paths are executed serially. In the worst case, every thread takes a different control path and the highly parallel architecture is used serially by each thread. This control flow divergence problem is well known in GPU development; code transformation, memory access redirection, and data layout reorganization are commonly used to reduce the impact of divergence. These techniques attempt to eliminate divergence by grouping together threads or data to ensure identical behavior. However, prior efforts using these techniques do not model the performance impact of any particular divergence or consider that complete elimination of divergence may not be possible. Thus, we perform analysis of the performance impact of divergence and potential thread regrouping algorithms that eliminate divergence or minimize the impact of remaining divergence. Finally, we develop a divergence optimization framework that analyzes and transforms the kernel at compile-time and regroups the threads at run-time. For the compute-bound applications, our proposed metrics achieve performance estimation accuracy within 6.2% of measured performance. Using these metrics, we develop thread regrouping algorithms, which consider the impact of divergence, and speed up these applications by 2.2X on average on NVIDIA GTX480. Index Terms—GPGPU, CUDA, control flow divergence, per- formance metric, thread regrouping. I. I NTRODUCTION G RAPHICS processing units (GPUs) have been widely used for computational acceleration for embedded sys- tems. A highly parallel architecture paired with a simplified programming model allows easy acceleration of data-parallel applications. Indeed, GPUs benefit a variety of embedded applications including electronics design automation, imaging, IP routing, audio, aerospace, military, and medical applica- tions [1]–[3]. Recent years have also seen a rapid adoption of GPUs in mobile devices like smartphones. Mobile devices typically use system-on-a-chip (SoC) that integrates GPUs with CPUs, memory controllers, and other application-specific Yun Liang is with Center for Energy-Efficient Computing and Applications (CECA), School of EECS, Peking University, China. Yun Liang is also with Collaborative Innovation Center of High Performance Computing, NUDT, Changsha, China. Yun Liang is the Corresponding Author. Muhammad T. Satria and Kyle Rupnow are with Advanced Digital Science Center (ADSC), Illinois at Singapore, Singapore. Deming Chen is with University of Illinois at Urbana Champaign, USA. Copyright (c) 2015 IEEE. Personal use of this material is permitted. However, permission to use this material for any other purposes must be obtained from the IEEE by sending an email to [email protected]. accelerators. The major SoCs with integrated GPUs available in the market include NVIDIA Tegra series with low power GPU, Qualcomm’s Snapdragon series with Adreno GPU, and Samsung’s Exynos series with ARM Mali GPU. In hardware, GPUs are composed hierarchically. A GPU is composed of multiple streaming multiprocessors (SMs), each of which has many streaming processors (SPs) used in a single-instruction multiple-data (SIMD) execution style. 1 An SM coordinates the SIMD style execution of many threads, divided into groups of 32 threads called warps. Threads within a warp start together at the same program address; each thread has its own program counter (PC) and register state. If all the threads in a warp execute the same instruction (same PC value), they may all execute in parallel. However, threads may follow different paths at conditional branches. When the threads within a warp diverge, the SM must coordinate serial execution of threads that follow different paths, and then later reconverge these threads. In the worst case, each thread in a warp takes a different control path; thus, only one thread of the warp is active at a time, and there is no actual parallel execution. Control flow divergence can significantly affect the performance of GPU applications. For example, Baghsorkhi et al. [4] demonstrated a prefix-scan benchmark with 33% of total execution time spent in control flow divergences, and some of the benchmarks used within this paper waste more execution latency on control flow divergence. Although control flow divergence is a well-known and well- studied problem, it cannot necessarily be entirely eliminated. Complex programs often have several divergence points at different stages of execution; in practice, it is not possible to entirely eliminate divergence through regrouping of threads or data inputs. Prior attempts to eliminate control flow divergence concentrated on thread regrouping techniques such as dynamic warp formation [5] or subdivision [6], memory access indi- rection, or input data transformations [7], [8]. Although each of these techniques is effective to reduce specific instances of control flow divergence, no prior technique models the performance impact of the transformations or prioritizes which instances of divergence are most important to eliminate. Prior elimination techniques simply assume that all instances of divergence are equally important and thus a reduction in percentage of divergent warps [7] or percentage of divergent branches [9] serve as their metrics for estimating improvement. However, these metrics are insufficient for estimating perfor- mance impact of divergence; divergences last for different 1 In this paper, we use NVIDIA GPUs and terminology, but the concepts also apply to other GPGPUs.

Transcript of An Accurate GPU Performance Model for Effective Control ...lin.ma/publications/TCAD.pdf · 2...

1

An Accurate GPU Performance Model for EffectiveControl Flow Divergence Optimization

Yun Liang, Member, IEEE Muhammad T. Satria, Kyle Rupnow, and Deming Chen, Senior Member, IEEE

Abstract—Graphic processing units (GPUs) are composed ofa group of single-instruction multiple data (SIMD) streamingmultiprocessors (SMs). GPUs are able to efficiently executehighly data parallel tasks through SIMD execution on the SMs.However, if those threads take diverging control paths, alldivergent paths are executed serially. In the worst case, everythread takes a different control path and the highly parallelarchitecture is used serially by each thread. This control flowdivergence problem is well known in GPU development; codetransformation, memory access redirection, and data layoutreorganization are commonly used to reduce the impact ofdivergence. These techniques attempt to eliminate divergence bygrouping together threads or data to ensure identical behavior.However, prior efforts using these techniques do not model theperformance impact of any particular divergence or consider thatcomplete elimination of divergence may not be possible. Thus, weperform analysis of the performance impact of divergence andpotential thread regrouping algorithms that eliminate divergenceor minimize the impact of remaining divergence. Finally, wedevelop a divergence optimization framework that analyzes andtransforms the kernel at compile-time and regroups the threadsat run-time. For the compute-bound applications, our proposedmetrics achieve performance estimation accuracy within 6.2% ofmeasured performance. Using these metrics, we develop threadregrouping algorithms, which consider the impact of divergence,and speed up these applications by 2.2X on average on NVIDIAGTX480.

Index Terms—GPGPU, CUDA, control flow divergence, per-formance metric, thread regrouping.

I. INTRODUCTION

GRAPHICS processing units (GPUs) have been widelyused for computational acceleration for embedded sys-

tems. A highly parallel architecture paired with a simplifiedprogramming model allows easy acceleration of data-parallelapplications. Indeed, GPUs benefit a variety of embeddedapplications including electronics design automation, imaging,IP routing, audio, aerospace, military, and medical applica-tions [1]–[3]. Recent years have also seen a rapid adoptionof GPUs in mobile devices like smartphones. Mobile devicestypically use system-on-a-chip (SoC) that integrates GPUswith CPUs, memory controllers, and other application-specific

Yun Liang is with Center for Energy-Efficient Computing and Applications(CECA), School of EECS, Peking University, China. Yun Liang is also withCollaborative Innovation Center of High Performance Computing, NUDT,Changsha, China. Yun Liang is the Corresponding Author.

Muhammad T. Satria and Kyle Rupnow are with Advanced Digital ScienceCenter (ADSC), Illinois at Singapore, Singapore.

Deming Chen is with University of Illinois at Urbana Champaign, USA.Copyright (c) 2015 IEEE. Personal use of this material is permitted.

However, permission to use this material for any other purposes must beobtained from the IEEE by sending an email to [email protected].

accelerators. The major SoCs with integrated GPUs availablein the market include NVIDIA Tegra series with low powerGPU, Qualcomm’s Snapdragon series with Adreno GPU, andSamsung’s Exynos series with ARM Mali GPU.

In hardware, GPUs are composed hierarchically. A GPUis composed of multiple streaming multiprocessors (SMs),each of which has many streaming processors (SPs) used in asingle-instruction multiple-data (SIMD) execution style.1 AnSM coordinates the SIMD style execution of many threads,divided into groups of 32 threads called warps. Threads withina warp start together at the same program address; each threadhas its own program counter (PC) and register state. If allthe threads in a warp execute the same instruction (same PCvalue), they may all execute in parallel. However, threadsmay follow different paths at conditional branches. When thethreads within a warp diverge, the SM must coordinate serialexecution of threads that follow different paths, and then laterreconverge these threads. In the worst case, each thread in awarp takes a different control path; thus, only one thread ofthe warp is active at a time, and there is no actual parallelexecution. Control flow divergence can significantly affect theperformance of GPU applications. For example, Baghsorkhiet al. [4] demonstrated a prefix-scan benchmark with 33% oftotal execution time spent in control flow divergences, andsome of the benchmarks used within this paper waste moreexecution latency on control flow divergence.

Although control flow divergence is a well-known and well-studied problem, it cannot necessarily be entirely eliminated.Complex programs often have several divergence points atdifferent stages of execution; in practice, it is not possible toentirely eliminate divergence through regrouping of threads ordata inputs. Prior attempts to eliminate control flow divergenceconcentrated on thread regrouping techniques such as dynamicwarp formation [5] or subdivision [6], memory access indi-rection, or input data transformations [7], [8]. Although eachof these techniques is effective to reduce specific instancesof control flow divergence, no prior technique models theperformance impact of the transformations or prioritizes whichinstances of divergence are most important to eliminate. Priorelimination techniques simply assume that all instances ofdivergence are equally important and thus a reduction inpercentage of divergent warps [7] or percentage of divergentbranches [9] serve as their metrics for estimating improvement.However, these metrics are insufficient for estimating perfor-mance impact of divergence; divergences last for different

1In this paper, we use NVIDIA GPUs and terminology, but the conceptsalso apply to other GPGPUs.

2

numbers of instructions, instruction latency varies, and threadblock scheduling can impact overlapping of threads’ execution,thus each divergence will have different importance to kernelexecution latency.

Performance models for GPU kernels are effective tech-niques for analyzing kernel behavior and predicting perfor-mance on a particular platform. Prior performance modelshave been used to estimate memory warp parallelism [10]and overall GPU performance [4]. However, these modelseither ignore divergence entirely, or use statistical modelingrather than thread-specific modeling, which is insufficient toguide thread regrouping for performance improvement. Forthese reasons, we develop an accurate performance modelfor control flow divergence that adds per-thread executioninformation in order to accurately estimate overall applicationperformance of candidate thread regrouping algorithms.

In this article, we first develop performance metrics thataccurately predict the performance impact of control flowdivergence for compute-bound GPU kernels. Specifically, ourperformance metrics combine per-thread basic block vector(BBV), instruction latencies, basic block instruction counts,number of simultaneously supported thread blocks, and threadblock scheduling policy. BBV captures the execution footprintof threads. More clearly, BBV combines the list of all thebasic blocks of the program with the count of how manytimes each basic block is executed. Then, we develop sev-eral thread regrouping algorithms to eliminate control flowdivergence based on the BBVs. Finally, we propose a di-vergence optimization framework that puts the models andalgorithms together. By comparing the relative improvementof the actual performance and metric, we demonstrate thatour proposed metrics correlate well with the performance onNVIDIA GTX480. Our divergence optimization demonstratesperformance speedup for a range of compute-bound, controlflow diverging applications. This paper advanced the state-of-the-art of control flow divergence modeling and optimizationfor GPUs with:• We develop novel control flow divergence metrics based

on the basic block vector (BBV), warp and thread blockperformance models, to predict control flow divergenceimpact on kernel performance.

• We develop thread regrouping algorithms to significantlyreduce control flow divergence based on our performancemetrics.

• We develop a divergence optimization framework thatperforms kernel analysis and transformations at compile-time and enable the thread regrouping at run-time.

We demonstrate that for compute-bound applications ourmetrics achieve performance estimation accuracy within 6.2%of the measured performance on NVIDIA GTX480 GPU. Ourdivergence optimization speedups these kernel execution by2.2X on average (up to 4.7X) on NVIDIA GTX480.

The rest of this paper is organized as follows: Section IIintroduces the architecture of GPU and CUDA programmingmodel. Section III presents background information on thecontrol flow divergence problem and software eliminationmechanism. Section IV introduces our proposed performancemetrics for estimating the performance impact of control flow

divergence. Section V presents thread regrouping algorithmsand Section VI describes the control flow divergence optimiza-tion framework. Section VII demonstrates the experimentalresults. Finally, Section VIII discusses the related work andSection IX concludes this paper and suggests future work.

II. GPU ARCHITECTURE AND PROGRAMMING MODEL

In this section, we begin by introducing the key architec-tural features of GPU platforms necessary for control flowdivergence performance modeling, and then describe the pro-gramming model.

A. NVIDIA GPU ArchitectureIn NVIDIA GPUs, each SM coordinates the single-

instruction multiple-data (SIMD) style execution of all of theSPs on it. The SPs are deeply pipelined with heterogenousfunctional unit latencies (depending on operation complexity),and use a static in-order pipeline without data bypassing tominimize the size and complexity of each SP. To maintainhigh throughput despite program data-dependencies, each SMcan support many simultaneous threads that share access tothe deeply pipelined SPs. Tasks such as instruction fetch anddecode, selection of the next instruction(s) to execute, andcoordination of branch reconvergence are handled at the SMlevel. An SM has a variety of architectural parameters thatmust be honored by the kernel implementation. Some of theseparameters are fixed, such as the limitation of 32 threads perwarp, the size of scheduling unit. Other parameters such asthreads per thread-block, thread-blocks per SM and sharedmemory per SM vary by architectures but have fixed limitsfor any particular architecture.

B. NVIDIA CUDA Programming ModelThe CUDA programming model is closely tied to the

underlying architecture; a GPU kernel is composed of manythreads hierarchically organized into groups that will executetogether in the GPU hardware. At the lowest level of thehierarchy, threads are grouped into warps, sets of 32 threads.Threads within a warp execute in SIMD style, only a singleinstruction may be fetched, decoded and executed within aparticular cycle, so divergent threads will be serialized.

At the next level of hierarchy is a thread block; a threadblock is composed of one or more warps, with an architecturallimit on warps per thread block. A thread block is theallocation unit for GPU execution, thread blocks are assignedto execute on one and only one SM. A single SM can typicallysupport execution of multiple thread blocks simultaneously,where the exact number of thread blocks supported is limitedby the resource demand per thread block, resource budget onan SM and architecture limits. There may be more than onethread blocks defined by a kernel than can be supported forsimultaneous execution. The thread blocks will be queued andassigned to SMs using a dynamic round-robin assignment toassign a waiting thread-block to the first available SM. In thecase that thread blocks have variable execution latency dueto control flow divergence, the ordering of thread blocks canhave an impact on load balancing among the SMs and thuson total kernel execution latency.

3

Input data: 1 12 1 12 1 12 1 12 tid : 0 1 2 3 4 5 6 7

(a) if (input[tid] > 10){ … } else { … }

(b) for ( int i = 0; i < input[tid]; i++){ … … }

Fig. 1. Program with divergences.

III. GPU CONTROL FLOW DIVERGENCE

We will now discuss the control flow divergence prob-lem and existing elimination mechanisms. Within a warp,threads execute together in a SIMD style. However, everythread has a unique identifier, program counter and registerstate; thus threads may follow different execution paths atconditional branches. When threads within a warp take dif-ferent (divergent) paths, each independent execution path willexecute serially, and will reconverge at a later convergencepoint [11]. In particular, branch reconvergence is handled bya combination of special instructions inserted by the compilerand a hardware waiting mechanism. During compilation, nvcccompiler detects cases that may diverge. Through static analy-sis, nvcc determines the reconvergence point – the programcounter that all threads will eventually reach regardless ofexecution path 2. The compiler inserts a special instructionto identify this PC value just before a divergence point. Then,a hardware mechanism implements a barrier at that PC value– no thread will continue until all have reached that PC value,thus guaranteeing reconvergence [11].

GPU kernels with control flow statements may divergeeither due to the input data, or systematic divergence based onthe thread IDs. Figure 1 illustrates branching scenarios basedon the input data. Due to different input data, threads with oddIDs go to the if path, while threads with even IDs take the elsepath (Figure 1 (a)). Figure 1 (b) shows a simple case whereevery thread has different loop bounds. In this case, there willbe a case that the some threads execute the loop while otherthreads do nothing.

Control flow divergence can seriously affect the perfor-mance. However, it is not trivial to eliminate the control flowdivergence. Thread regrouping based on input data ranges asdescribed by Zhang et al. [7] is not accurate enough: threadswith different input data may take the same execution path,and threads with identical input data may still have differentexecution paths due to thread IDs. Thus, we need an accuraterepresentation of execution paths for threads. Detailed programexecution paths may represent this behavior, but it may notbe efficient and can be extremely long. Therefore, we needa balance: a simple technique with a compact representationthat still accurately represents threads’ behavior. In addition,reducing the absolute amount of control flow divergencedoes not necessarily reduce the performance impact of di-

2Formally denoted the post-dominator

12 112 12 12111Input:

tid: 1 63 5 7420

16 3 5 7420D[]:

(a)

warp warp

(b)

tid: 1 63 5 7420warp warp

121 12 12 12111Input:

Fig. 2. Thread-data remapping: (a) reference redirection and (b) data layouttransformation. Warp size is assumed to be 4.

vergence: certain divergences will have higher performanceimpact than others. Therefore, we need an accurate estimateof the performance impact of thread regrouping together withefficient thread regrouping algorithms that can use a compactand accurate representation to guide selection of threads togroup together. We will discuss our proposed performancemetrics and thread regrouping algorithms in Section IV and V,respectively.

A. Elimination Mechanism

Zhang et. al. [7] proposed thread-data remapping, asoftware-based control flow divergence elimination mecha-nism. The basic idea is to switch the data sets that threadswork on so that threads in a warp may execute the same path.There are two options to realize this mechanism: referenceredirection (indirect access) and data layout transformation.Figure 2 illustrates the two options of thread-data remappingmechanism ((a) reference redirection and (b) data layouttransformation) with an example warp size of 4.

In reference redirection, the thread’s access to data memoryis redirected by an indirection reference array D[] that pro-vides a new ID for all the threads. As illustrated in Figure 2(a), the new ID is D[tid], and threads will use their new IDs inaccessing the data memory and continuing the computation.This mapping option can be used to eliminate both threadID-dependent divergence and data-dependent divergence. Im-plementation of this mapping option in the code is by insertingadditional parameter in the kernel function. Then, duringkernel execution, all instances of thread ID in the kernelare replaced by the new ID. If thread ID is used as part ofkernel computation, the replacement will automatically correctthat portion as well. Reference redirection incurs overheaddue to the addition of instructions to fetch the referenceindirection. This mapping option may also hurt the memoryaccess bandwidth. In GPUs, memory accesses from a warpare coalesced if threads in a warp perform sequential accessto the memory. However, since threads get new IDs, thisredirection will break the sequential access pattern, generatinguncoalesced memory access as shown in Figure 2 (a).

Rather than changing the thread ID, the second mappingoption is by physically reorganizing the input data as shownin Figure 2 (b). For the entire data, we rearrange the inputdata based on the redirection array D[], so that the i-th item innew data is equal to D[i]-th item of old data. Unlike referenceredirection, we do not need to make any modification in thekernel function. Because the data are physically rearranged,there is no change in coalescing memory access. However,implementation of this mapping option requires additional

4

overhead to perform the input data rearrangement on the CPUbefore the GPU kernel call. Zhang et al. [7] demonstrated thatdata movement overhead can be hidden through pipeliningkernel calls between the CPU and GPU.

B. Limitations of Elimination Mechanism

Our work focuses on control flow divergence applications,which can be eliminated via thread-data remapping. However,this mechanism is not always safe. In case where the kernelneeds synchronization over the formed warps [12] or hasdependencies among threads [7] that disqualify work orderreorganization, thread-data remapping may destroy the syn-chronization and incorrectly evaluate dependencies. Such lim-itations exist for all thread divergence optimization techniques[7] [8] [12]. In some cases, these limitations can potentiallybe eliminated through programming language or compileroptimization techniques, but in other cases these limitationscannot be eliminated and must be used as a signal to notmodify that portion of the code. However, the problem ofdetermining when and how to eliminate synchronization anddependency limitations is beyond the scope of this article.

Our techniques are limited to applications with GPU kernelsthat are called with sufficient frequencies and have longexecution time. By eliminating the control flow divergence, theoverall execution time can be significantly improved. In oursystem, this is not a limitation because such kernels are exactlythe ones which are the most attractive targets for control flowdivergence elimination.

IV. PERFORMANCE MODEL

Two metrics have been widely used to measure control flowdivergence: divergent branches and divergent warps ratio. Di-vergent branches are measured by NVIDIA visual profiler [9].The divergent branches count is incremented each time abranch instruction causes at least one thread in the warp totake a different execution path than the other active threads [9].Therefore, the divergent branches count records the number ofdivergent branches in all warps. Instead of counting individualbranch instructions, divergent warps ratio [7] measures thepercentage of warps that have any control flow divergences.Both metrics quantify the amount of divergence for the GPUkernel, but (as we will demonstrate) neither metric representsthe performance impact of these divergences. It is possiblethat both metrics indicate a significant divergence problem,but the performance is only slightly impacted (e.g. frequentsingle instruction divergences that do not affect performance),or vice versa.

Control flow divergence is caused by different behavior(e.g., execution path) of the threads within a warp. Thus, toestimate the performance impact of control flow divergence,we need to represent thread-specific behavior accurately. Moreimportantly, thread-specific behavior information can be usedto guide the thread regrouping to minimize the impact ofcontrol flow divergence on performance. However, divergentbranches and divergent warps ratio are simple aggregate statis-tics; neither of them provide accurate performance estimation

A

B C

D E

F

start

end

A B C D E F

100 60 40 50 50 100

Basic Block Vector (BBV) (a) (b) Thread 1

A B C D E F

100 20 80 40 60 100

Thread 2

A B C D E F

10 6 4 5 5 10

Thread 3

… …

Fig. 3. (a) control flow graph of a program. (b) BBVs of threads.

of control flow divergence or thread-specific information forthread regrouping.

To accurately represent thread-specific behaviors, we usebasic block vector (BBV). A basic block is a sequential seriesof instructions with only one entry and one exit point. For theBBV, each thread has a set of counters, one per basic block inthe kernel. Then, each time the thread touches a basic block,that counter is incremented. BBV is a compact representationof thread-specific behavior: its size depends on the staticprogram size, not dynamic program size. This allows us todesign efficient performance models and thread regroupingalgorithms based on the BBV. As we will demonstrate in theexperiments, our performance models based on the BBV canaccurately distinguish the performance impact of control flowdivergence for different thread regrouping algorithms.

Figure 3 presents an example of control flow graph (CFG)of a GPU kernel and its BBV. The CFG consists of six basicblocks. For each thread, its BBV records the execution countsof basic blocks for it. For example, as shown in Figure 3 (b),the execution count of basic block B of thread 1, 2, 3 is 60,20, and 6, respectively. In this example, thread 1, 2, and 3 takedifferent paths at conditional branches (basic block A, B, C,F), leading to different execution counts of the basic blocksfor different threads.

A. BBV Estimation

To obtain precise BBV for the threads, we can executethe kernel program on the target architecture. However, thiswill be too slow. In this work, we apply regression modelingto efficiently obtain estimates of BBV for the threads. Foreach basic block b, we model its execution count BBV [b]as a weighted sum of input variables plus random noise. LetBBV [b] = BBVt1 [b], ..., BBVtN [b], where BBVti [b] denotesthe execution count of basic block b for thread ti. BBVti [b]is estimated as follows,

BBVti [b] = β0 + β1 · Iti[1] + ...+ βp · Iti [p] + βp+1 · tid (1)

where Iti = Iti [1], ..., Iti [p] denote the input variables forthread ti, and tid is the thread identifer for thread ti. Thesevariables are constant for a given input workload and thread.Let β = β0β1, ..., βp + 1 denote the corresponding set ofregression coefficients used in describing the linear function of

5

Listing 1 Code Instrumentation.__global__ void kernel(int * input, ...){{1. int tid = ...;2. ...3. for(int i = 0; i < input[tid]; i++) {4. if(tid = 1){ // instrumentation5. __prof_trigger(xx);6. }7. }8. if(input[tid] > 100){9. if(tid = 1){ // instrumentation10. __prof_trigger(xx);11. }12. }13.

input variables and noise in Equation 1. For each basic blockb, we employ a dataflow analysis to determine the set of inputvariables that impact the execution count of b. For example,in the sample code of Listing 1, the execution count of the forloop (line 3) and if statement (line 8) of a thread depends onits data in the input array.

We fit the model in Equation 1 using the method of leastsquare. More clearly, for each basic block b, we determine thep+ 2 coefficients (e.g., β0, ..., βp+1) based on a few observa-tions. The least square fitting model attempts to minimize thederivation of predicted and actual value. We obtain a numberof observations of BBV [b] through code instrumentation andprofiling. For the target thread and basic block, we insert a con-ditional if statement and a call to the NVIDIA’s profiler triggerprof trigger [13]. Listing 1 illustrates the code instrumentationusing an example. In this case, through instrumentation andprofiling, we can collect the execution counts for the loop (line3) and if statement (line 8) for thread 1. It is often that thenumber of inputs is smaller than the thread block size. Hence,we train the model based on the observations of the threads inone thread block and predict the BBV for the threads in otherthread blocks. For example, for application MC, we train themodel for all the basic blocks using the threads in one threadblock (32 threads) and then predict the BBV for other threads.The mean square error is smaller than 0.001. Note that theregression model is built offline but used online. When thekernel is launched, we feed the inputs of the threads into themodel shown by Equation 1 to derive the BBVs.

We then develop the performance metrics based on BBV.More specifically, the performance metrics leverage threecomponents: warp performance, thread-block performance,and kernel performance. At warp performance level, we modelper-instruction latencies, execution counts of basic blocks, andcontrol flow divergence; at thread-block performance level, wemodel the number of warps within a thread-block; at the kernelperformance level, we model the number of SMs and thread-block scheduling.

B. Performance Metrics

We develop two performance metrics, BBV-weighted andBBV-weighted-scheduled, which are different in terms ofthread block scheduling.

1) BBV-weighted: For each basic block, we can easily com-pute the number of instructions in it. However, each instructionhas different latency; a basic block with fewer instructions mayhave longer latencies than a basic block with more instructions.Thus, we apply instruction-specific latencies measured usingmicro-benchmarking [11] and store them to a table. Once wecollect the instructions of a basic block, we calculate the totalinstruction latency of the basic block based on the referencetable. For example, Listing 2 shows the instructions of a basicblock. Benchmarking results on NVIDIA GTX480 (details willbe presented in Section VII) show that mul instruction takes18 clocks, loading a constant variable needs 46 clock cycles,and both rem and div instructions in unsigned integer take264 clock cycles. Thus, the latency of this basic block is 610clock cycles.

Listing 2 A sample of basic block’s instructions

bb_32:PC=0xa38 mul.lo.s32 %r167, %r106, 8bb_32:PC=0xa40 ld.const.u32 %r168, [char_size]bb_32:PC=0xa48 mul.lo.u32 %r169, %r167, %r168bb_32:PC=0xa50 rem.u32 %r170, %r166, %r169bb_32:PC=0xa58 div.u32 %r171, %r170, %r168

The threads in a warp may have different behavior, leadingto different execution counts of a basic block for differentthreads in a warp. In BBV-weighted, for each basic block, wefind the maximum execution counts for any thread in the warp.For example, if one thread in warp 1 executes basic block b2 times, and another thread in the same warp executes basicblock b 5 times, then the maximum execution count is 5. Thetotal latency of basic block b in warp w is computed as themaximum execution counts of b in warp w multiplied by thelatency of basic block b as follows,

Latb|w = lat[b]×maxt∈w(BBVt[b]) (2)

where lat[b] is the total latency of all the instructions in threadblock b and BBVt[b] is the execution count of basic block bof thread t. The performance of warp w is then estimated asthe sum of latencies of all basic blocks.

Tw =∑b∈B

Latb|w (3)

where B is the set of basic blocks of the GPU kernel. Finally,the thread block latency estimate is the sum of the estimatedperformance of warps in the thread block (Equation 4), and thetotal kernel performance estimate is the sum of thread blocklatencies divided by the number of SMs available on the GPU(Equation 5), which corresponds to a static round-robin threadblock assignment with exactly equal thread block latencies.

TTb =∑w∈Tb

Tw (4)

Ttotal =

∑TTb

#SM(5)

If the threads in a warp have different execution countfor a basic block, then the threads within the warp mustdiverge. In BBV-weighted, the latency of thread block b inwarp w, Latb|w (Equation 2), is estimated using the maximum

6

StaticSchedule

SM0 SM1

tb0tb1

tb2

tb3

tb4

tb5

DynamicSchedule

SM0 SM1

tb0tb1

tb2

tb4

tb3

tb5

estimatedexecutiontime

estimatedexecutiontime

time

Fig. 4. Thread block schedule

execution counts of b in w. In other words, all the threads ina warp have to wait until the slowest thread completes.

2) BBV-weighted-scheduled: In BBV-weighted metric, weuse the sum of thread block latencies divided by the numberof SMs available as the estimate of kernel performance.This estimation assumes there is little variance in executiontime among thread blocks. However, due to control flowdivergence, the execution time of warps may vary, leadingto variation in thread blocks’ execution time. To accuratelymodel performance, we must therefore model the threadblock scheduling algorithm employed by the hardware. Theexact behavior of the scheduler has not been disclosed byNVIDIA, but previous research [14] indicated that a round-robin scheduling policy correlates well to the performance.Therefore, we develop BBV-weighted-scheduled metric, whichcombines the thread block performance estimation in BBV-weighted together with dynamic round-robin scheduling policyfor performance estimation.

In BBV-weighted-scheduled, for each thread block, we esti-mate its latency using Equation 4; the total kernel performanceis estimated using dynamic round-robin policy. After the initialthread block assignment to fill all the SMs, the rest of thethread blocks will be inserted into a queue and dispatchedto GPUs when there are available SMs. Figure 4 shows acomparison of static and dynamic round-robin schedule. Forexample, thread block 2 is scheduled to SM0 in static schedule.However, thread block 2 is scheduled to SM1 in dynamicschedule as SM1 finishes execution earlier than SM0. Thedynamic scheduling therefore performs a simple run-time loadbalancing between SMs. Note that in the example, we assumethe occupancy (the number of concurrent thread blocks on oneSM) is 1 for demonstration purposes. In reality, we use theoccupancy calculator to determine the occupancy. If one SMcan run more than one thread block in parallel, then multiplethread blocks will be assigned to it.

For the BBV-weighted and BBV-weighted-scheduled metrics,we do not intend to accurately predict the absolute perfor-mance, but to accurately estimate the performance impact ofcontrol flow divergence and the performance difference amongdifferent thread regrouping optimizations (section V). For allthe metrics, we assume the global memory bandwidth is notthe bottleneck on the performance; the long memory latencycan be hidden by the provided with sufficient occupancy.

As we will demonstrate, our metrics can accurately estimateperformance for compute-bound control flow divergence GPUapplications.

The estimation may be different from the actual perfor-mance for a few reasons. First, the BBV does not capturethe exact sequence of basic block executions. Thus, the con-trol flow divergence might be underestimated by the BBVs.Second, we assume the memory bandwidth is not bottleneckof the performance and not affected by the thread regroupingoptimizations. Finally, we do not model the instruction andwarp scheduling, and reconvergence points, etc. implementa-tion details, which are not disclosed by NVIDIA. Despite thesereasons, we will demonstrate that, in practice, the metrics wedevelop correlate well with the performance. Therefore, themetrics can be used to evaluate various control flow optimiza-tions without actually performing the thread regrouping, andre-running the application.

V. THREAD REGROUPING ALGORITHMS

In theory, we can enumerate all the possible thread re-grouping options and compare them using our performancemetrics in Section IV. However, in practice, exhaustivelytesting all the thread regrouping options is infeasible as thenumber of options grows exponentially with the number ofthreads of the kernel. Hence, in the following, we proposelow-complexity thread regrouping algorithms that can be usedonline to efficiently derive an optimized thread regroupingsolution.

At a high level, we wish to group together threads thatbehave similarly – optimally with no control flow divergence,but with minimized performance impact if control flow diver-gence is unavoidable. To achieve this goal, we use per-threadBBVs to represent thread behavior. We will examine threealternative thread regrouping algorithms, Sorting, Greedy, andGreedy Max. Moreover, these three algorithms will be used toevaluate the accuracies of our performance metrics.

Our thread regrouping algorithms start with N threadsand a parameter K, the number of groups to divide thethreads into. The size of the group is then calculated bygroupsize = dN/Ke. In situations where dN/Ke is not amultiple of warp size, there will be warps that are formed bytwo different groups and potentially will suffer from controlflow divergence. In order to avoid such situation, we set a fixedgroup size to be multiple of warp size: 32,64, and so on. Thus,the number of groups, K, is calculated by dN/groupsizee. Inthe following, we present the details of our thread regroupingalgorithms.

A. Sorting Algorithm

Our first thread regrouping algorithm is Sorting. We com-pare the threads based on the per-thread BBV. Each BBVis a vector of integers. We sort the BBVs in lexicographicalorder. More clearly, we compare element by element in twoBBVs that have the same index until one element is notequivalent to the other. The comparison result of two BBVs inlexicographical order is the comparison result of the first non-matching elements. For example, BBVt1 = {1, 20, 10, 20, 1}

7

is smaller than BBVt2 = {1, 30, 20, 30, 1}. After sorting,every groupsize consecutive threads form a group, where thelast group may be smaller if N threads cannot be equallydivided into K groups.

B. Greedy AlgorithmOur Greedy algorithm is an iterative method. At the begin-

ning, each thread forms an individual group. In each iteration,we calculate the estimated performance gain (described below)through merging and merge the two groups with the maximalgain. If the merged group size exceeds the groupsize, thenthe first groupsize threads form one group and the remainingthreads in the groups form a new group and will be used forfurther merging. This process is performed iteratively until thenumber of groups is equal to K.

To compute the estimated performance gain, we first definethe minimal and maximal execution counts of each basic blockb within a thread group g as follows,

Minbg = mint∈gBBVt[b] (6)

Maxbg = maxt∈gBBVt[b] (7)

Intuitively, we want to merge two thread groups which behavesimilarly to minimize any potential negative impact due tocontrol flow divergence. Thus, we merge two thread groupswhose BBVs have similar (preferably identical) executioncounts for each basic block. Because all the threads thatfollow the same BBV (or portion thereof) can simultaneouslymake progress, merging these threads into a group increasescomputation throughput with no change to total latency. Thus,we define the benefit of merging two thread groups g1 and g2as follows,

Benefit(g1, g2) =∑b∈B

(lat[b]×Minbg1∪g2) (8)

We do not want to merge two thread groups if there aresignificant differences in their BBVs since merging these twothread groups will cause the total warp latency to increasedue to control flow divergence. Thus, we define the cost ofmerging two threads groups g1 and g2 as follows,

Cost(g1, g2)

=∑b∈B

(lat[b]× (Maxbg1∪g2 −Minbg1∪g2)) (9)

We estimate the number of executions where two groups canexecute together as benefit, but the number of executionswhere they diverge as cost. For example, if one group executesbasic block b once and the other group executes basic block btwice, we estimate that the first time they can execute togetheras benefit, but the second execution as cost. This is becauseduring the second execution, one group is diverged, waitingfor the other group and making no forward progress. Finally,we define the overall gain as follows.

Gain(g1, g2) = Benefit(g1, g2)− Cost(g1, g2) (10)

For the threads with identical BBVs, merge provides benefitwith no cost, thus guaranteeing that identical threads willbe merged together before adding non-identical threads. Thedetails of our Greedy solution are presented in Algorithm 1.

Algorithm 1 Pseudo-code for the Greedy regrouping1: Let G be the set of thread groups2: Let E be the set of edges among groups3: for every gi, gj ∈ G do4: calculate gain(gi, gj)5: end for6: groups = 0;7: while ( groups < K - 1) do8: find edge(gm, gn) with the maximal gain9: merge(gm, gn) as gnew

10: for every group g ∈ G do11: remove edge (gm, g) from E12: remove edge (gn, g) from E13: insert edge (gnew, g) into E14: calculate gain(gnew, g)15: end for16: remove gm, gn from G17: insert gnew to G18: end whileFunction: Merge19: function MERGE(gm, gn)20: if size(gm) + size(gn) >= groupsize then21: gnew = gm ∪ gn22: the first groupsize threads form the group, g∗.23: gnew = gnew − g∗

24: groups = groups + 125: else26: gnew = gm ∪ gn27: end if28: end function

C. Greedy-Max Algorithm

Greedy algorithm tends to form a group of threads (e.g. 32threads) using similar threads. However, it does not optimizethe thread block ordering. The ordering of thread blocks has animpact on load balancing among the SMs and thus affects thetotal kernel execution latency under the dynamic round-robinthread block scheduling policy.

Figure 5 illustrates how the ordering of thread blocks affectsthe total kernel execution latency. In Figure 5 (a), there aretwo different thread block queues: queue Q1 is an unorderedqueue, while queue Q2 is a latency-based order queue (threadblocks are sorted in descending order of their latency). Figure 5(b) and (c) illustrate the round-robin scheduling policy thatassigns the thread blocks to the available SMs for Q1 andQ2, respectively. We assume the maximum number of threadblocks that can simultaneously execute on a SM is 1. In theFigure 5 (b), the assignment is scheduled as follows: at time 0(t0), thread block 0 (tb0) is assigned to SM0, tb1 is assignedto SM1 and tb2 is assigned to SM2. Since each SM can onlyhandle 1 thread block at a time, the rest of thread blocks willbe queued and wait for the first available SM. Then, tb1 inSM1 is done at t1, and thus instantly tb3 is assigned to SM1.When tb2 is complete at t2, tb4 is assigned to SM2. Thisscheduling mechanism is repeated until no more thread blocksleft in the queue. However, if we apply the same schedulingmechanism to Q2 (Figure 5 (c)), we find that the workloadis more balanced among SMs compared to Q1 and the finalexecution time of Q2 is less than Q1. Therefore, the orderingof thread blocks in the kernel has an impact on the overallperformance.

In our Greedy-Max algorithm, we still form groups ofthreads using overall gain metric (Equation 10), but we form

8

tb0

tb1

tb2

tb3

tb4

tb5

tb6

tb7

tb8

tb9

Q2

tb10tb11

Q1

tb0

tb1

tb2

tb3

tb4

tb5

tb6

tb7

tb8

tb9

tb10

tb11

(a)

(c)(b)

t(time)

estimated execution time

tb2

tb3

tb6

tb9

SM2

Q2

tb1

tb5

tb8

SM1

tb10

tb0

tb4

tb7

SM0

tb11

estimated execution time

Q1SM0

tb0

tb6

tb10

SM2

tb4

tb2

tb8

tb11

SM1

tb1

tb5

tb7

tb3

tb9

123

4

56

789

0t(time)

0

1

2

34

567

Fig. 5. The impact of thread block order to load balancing in SMs

the groups in the descending order of their latency. We definethe latency of thread t as follows,

Latt =∑b∈B

lat[b]×BBVt[b] (11)

where B is the set of basic blocks of the GPU kernel.The details of Greedy-Max are described in Algorithm 2. We

start with an empty thread group g. Initially, g is filled with thethread that has the maximum latency (Latt′ = maxt∈TLatt). Then, g is filled by either a thread that has identical BBVto an existing thread in g, or a thread that gives maximumperformance gain if merged with g. This step is repeated untilthe size of g is equal to groupsize. By doing this iteratively,we can build K groups. Compared to Greedy algorithm,Greedy-Max achieves load balancing via thread block orderingas shown in Figure 5.

Algorithm 2 Pseudo-code for Greedy-Max1: Let T be the per-thread BBV s list2: Let G be the set of thread groups and initially empty3: Let g is an empty thread group4: while ( size(G) < K) do5: let t′ s.t. Latt′ = maxt∈TLatt6: remove t′ from T and add it to g7: while ( size(g) < groupsize) do8: initial max gain9: identical = False

10: i = 011: while (not identical and i < size of T ) do12: if ti ∈ T match with any j ∈ g then13: identical = True14: max t = ti15: else16: calculate cur gain of edge(g, ti)17: if cur gain > max gain then18: max gain = cur gain19: max t = ti20: end if21: end if22: i = i+ 123: end while24: add max t into g25: remove max t from T26: end while27: add g into G28: clear g29: end while

D. Discussion

The Sorting algorithm regroups the threads by sorting theper-thread BBVs in lexicographical order. By doing this, ittends to group similar threads. However, it does not considerthe differences between basic blocks and model the warpperformance and thread block scheduling. Based on the warpperformance estimation by Equation 2 and 3 in Section IV,Greedy algorithm models the differences between basic blocks,and groups threads together using the gain metric (Equa-tion 10). However, it ignores the thread block latency andtheir ordering, which affects the load balancing among theSMs and the total latency. Our final thread group algorithm,Greedy-Max, not only models the thread performance andconsiders the thread similarities using gain metric, but alsoprioritizes the threads with high latency. Based on the dynamicround-robin thread block scheduling policy, ordering threadblocks in descending order of their latencies is helpful for loadbalancing among the SMs and optimizes the total latency. Inthe experiments, we will compare the performance speedup ofthe three thread regrouping algorithms.

VI. DIVERGENCE OPTIMIZATION FRAMEWORK

Our control flow divergence optimizations relies on syner-gistic collaboration of compile-time and run-time componentsas shown by Figure 6. At compile-time, we analyze the ker-nels, build the BBV estimation model, and enable the controlflow divergence using the elimination mechanism described inSection III. At run-time, we predict per-thread BBVs and callthread regrouping algorithms to regroup the threads.

Compile-time. Given a GPU kernel program, we first parsethe kernel code, build the control flow graph (CFG) at the

9

kernel<<<grid, thread,…>>>(…, redirect[]);

kernel(….., redirect[]);

Device code

Host code

BBV

Thread1

… BBV

Threadn

Kernel Analysis

BBV Estimation

Compile-time

Regrouping Algorithms

Code Modification

BBV prediction

Input data

Run-time

Binary Binary

redirect[]

transform();

Fig. 6. Divergence Optimization Overview

source code level and profile the latency for every instruction.Then, we build the regression model for BBV estimation foreach basic block as described in Section IV-A. Each GPUprogram contains a few inputs available offline. We profile thekernel using the profiler trigger [13] as shown in Listing 1.In general, one GPU kernel will launch thousands or evenmore number of threads. But, we observe that it is sufficientto profile the threads only in one thread block to build the BBVestimation model. Note that the BBV estimation model is builtat compile-time but used at run-time. At run-time, when thekernel is called with new inputs, we predict the BBVs usingthe BBV estimation model built offline.

We enable the divergence optimization through code trans-formation. Both the host and device code have to be slightlymodified. For the host code, we insert the thread regroupingfunction before the kernel call. For example, as shown byFigure 6, if the reference redirection mechanism is used,the redirect[] array is inserted and threads read their inputdata based on the redirect[] array (e.g. Figure 2 (a)); if thedata layout transformation mechanism is used, the transformfunction is called to reorganize the data. But, the redirect[]array or data layout transformation order are generated onlineby the thread regrouping algorithms. Finally, we compile themodified source code to binary.

Run-time. When the kernel is launched with its input data,we predict the per-thread BBV using the BBV estimationmodel. Then, we use the per-thread BBV to represent thethread behavior and invoke the thread regrouping algorithmsto produce the new thread grouping for improved control flowdivergence and application performance. Meanwhile, basedon the per-thread BBV, we can estimate the performanceimprovement potential using the performance models devel-oped in Section IV. If the thread regrouping leads to negativeor very marginal performance improvement, then the threadregrouping will be disabled.

The overhead incurred at run-time includes two parts —the overhead of the per-thread BBVs prediction using theBBV estimation model and the overhead of thread regroupingalgorithms. These overheads are very small compared to theGPU kernel time.

TABLE IISPECIFICATION OF TESTING MACHINE

GPU NVIDIA GTX 480

Architecture Fermi

SMs (Cores per SM) 15 (32)

Total CUDA Cores 480

GPU Clock per core 1.4 GHz

GPU RAM 1.5 GB

GPU Memory Clock 1.85 GHz

CPU Intel i5-750 @ 2.67 GHz

CPU RAM 4 GB

VII. EXPERIMENTS

A. Experiments Setup

Table I shows the list of benchmark applications used. Wecollect benchmarks from different sources that exhibit controlflow divergence. We include two real-world applications – 3Dsound localization [3] and stereo matching [20], which bothexhibit complex control flow divergence. For each applica-tion, Table I gives the transformation elimination mechanism(reference redirection or data layout transformation), the totalnumber of threads, and performance bottleneck. Performancebottleneck (memory-bound or compute-bound) is identified byusing the NVIDIA Visual Profiler [9]. In our optimizationframework, we rely on profiling to train the BBV estimationmodel. We evaluate our techniques using different profilingand evaluation inputs.

MarchingCubes (MC) benchmark is taken from the CUDASDK. MC implements marching cubes algorithm, which com-putes geometric iso-surface from a three dimensional volumedataset. Each parallel thread calculates one voxel (cube).Divergence in MC is caused by different threads calculatingdifferent number of vertices for each cube vertex index.Smith-Waterman (SW), CUDA-EC (EC), Needleman-Wunsch(NW) and MUMmer (MUM) benchmarks are all bioinformaticsapplications. SW aligns a query polypeptide sequence againsta protein database, while MUM aligns a set of DNA queriesagainst a single reference sequence. EC benchmark correctserrors in short reads from high-throughput sequencing. NWbenchmark calculates global alignment scores between a DNAquery and reads from DNA database. Divergences of thosefour benchmarks are caused by diversity in DNA or pro-tein sequence content being processed by threads. 3D soundlocalization (SL) finds the direction of the sound capturedby the acoustic microphone in 3D fashion [3]. SL exhibitscontrol flow divergence because different threads are assignedwith different workload (e.g., different number of 3D angles).Stereo matching (SM) is an important underlying technologyfor 3D-video application [20]. SM contains thread divergencebecause the grid building kernel selects global maxima basedon the cost value which differs among threads.

Although it seems intuitive that there are many benchmarksthat exhibit control flow divergence, few freely availablebenchmarks currently demonstrate this sort of behavior. Weexamined the benchmarks including the set of applicationsused for prior control flow divergence studies and all the

10

TABLE IBENCHMARK CHARACTERISTIC

Benchmarks Source Description Input EliminationMechanism Threads Performance Bottleneck

Marching Cubes (MC) CUDASDK

calculates geometriciso-surface from a

volume dataset [15]

float and intarrays

referenceredirection and

data layouttransformation

262144 Compute-bound

Smith-Waterman (SW) [16] local sequence alignmentalgorithm

8k proteinsequences

data layouttransformation 32768 Compute-bound

CUDA-EC (EC) [17] sequence error correction 6ksequences

data layouttransformation 24576 Memory-bound

Needleman-Wunsch(NW) [18] global sequence

alignment algorithm2k

sequencesdata layout

transformation 32768 Compute-bound

MUMmer (MUM) [19] fast genome alignmenttool

16ksequences

data layouttransformation 65536 Memory-bound

3D Sound Localization(SL) [3] finds the direction of

captured sound in 3D36m float

arraydata layout

transformation 17920 Compute-bound

Stereo Matcher (SM) [20] depth map based stereomatcher

two images450x375 px

referenceredirection 194560 Compute-bound

applications that demonstrate control flow divergence from theCUDA SDK.

Control flow divergence elimination is important for allthe generations of GPUs. In this paper, we use the widelyused NVIDIA Fermi-based GPUs to evaluate our techniques.In particular, all the experiments are performed on NVIDIAGTX480 GPU. The specification of our evaluation machine isshown by Table II. Using micro-benchmarks [11], we collectthe instruction latencies on the GTX480. Table III presents thelatency results. The latency table will be used for computingthe total latency of basic blocks for each application. GTX480contains caches. The latencies to constant and shared memoryare constant as they are always hits. However, for loads/storesto local and global memory, the latencies depend on theircache behavior (e.g., hits or misses). We empirically measurethe cache hit ratio using NVIDIA profiler and then computean average memory access latency using the cache hit ratio.

Each benchmark is compiled using NVIDIA’s CUDA com-piler, nvcc. Each basic block contains a list of instructions.We compute the total latency of each basic block by summingthe latencies of the instructions in it. With BBVs and eachbasic block’s latency as input, we can estimate the perfor-mance impact of control flow divergence. Then, each appli-cation is transformed using either the reference redirectiontransformation or the data layout transformation. Finally, weexecute both initial (without transformation) implementationand transformed implementation. We record the executiontime for both implementations and gather all performancestatistics including divergent branches, divergent warps, andestimated performance computed by our metrics. For thetransformed implementation, we include the runtime overheadin its measurement.

In the following, we perform two sets of experiments: first,we compare the prior metrics to our proposed metrics in termsof correlation to actual performance (subsection VII-B); sec-ond, we present the performance speedup of our optimizationframework (subsection VII-C).

TABLE IIIINSTRUCTION LATENCY (IN CLOCKS)

Operation Typeuint int float double

add, sub, mul 18 18 18 24div 264 300 984 1188

min, max 36 20 36 48mad 20 20 20 24

and, or, shl, shr 18 18 - -xor 1 18 - -rem 264 297 - -

sinf, cosf - - 40 -tanf - - 116 -abs - 36 36 -tex 220 220 220 -sqrt - - 208 -rsqrt - - 70 -rcp - - 228 -expf() - - 106 -

exp2f() - - 88 -logf(), log10f() - - 88 -

log2f() - - 70 -powf() - - 110 -

umul24() 36 - - -mul24() - 36 - -

fadd rn(), fadd rz(),fmul rn(), fmul rz() - - 18 -

fdividef() - - 88 -dadd rn() - - - 24

syncthreads 16 - - -

Operation Memory Typelocal constant global shared

load - 46 - 44store - - - 44

B. Metric Accuracy

As discussed in Section IV, divergent warps measure thepercentage of warps that have any control flow divergencewhile divergent branches count individual branch instructionsthat cause divergences. Note that both prior metrics (i.e.,divergent warps and divergent branches) and our metrics (i.e.,

11

-10%0%

10%20%30%40%50%60%70%80%90%

100%

Sort

ing

Gre

edy

Gre

edy-

Max

Sort

ing

Gre

edy

Gre

edy-

Max

Sort

ing

Gre

edy

Gre

edy-

Max

Sort

ing

Gre

edy

Gre

edy-

Max

Sort

ing

Gre

edy

Gre

edy-

Max

Sort

ing

Gre

edy

Gre

edy-

Max

Sort

ing

Gre

edy

Gre

edy-

Max

MC SW NW SL SM EC MUM

Perc

enta

ge Im

prov

emen

t Actual Performance Estimated by Divergent Warp Estimated by Divergent Branch

Fig. 7. Comparison of divergent warps and divergent branches metrics and actual performance improvement

BBV-weighted and BBV-weighted-scheduled) are not intendedto predict the absolute performance, but to estimate theperformance difference between different thread regroupingalgorithms. For each application, we use each of our threeregrouping algorithms, with the K (the number of groups)set to 32 (a warp size), and compare the actual performanceimprovement on GTX480 in percentage to the performanceimprovement in percentage predicted by the metrics.

We first examine the prior metrics. Table IV lists thedivergent warps, divergent branches, total warps and totalbranches for all the benchmarks. As shown, for some ap-plications, all the warps are divergent. For each application,we first measure the actual performance, divergent warps anddivergent branches of the original implementation (withoutthread regrouping). Then, for each application and threadregrouping algorithm pair, we measure the actual performance,divergent warps and divergent branches after applying thethread regrouping optimization. Figure 7 shows the percentageimprovement of actual performance, divergent warps anddivergent branches over the initial implementation withouttransformation for each combination of the applications andthread regrouping algorithms. We observe that divergent warpsand divergent branches give inaccurate estimation for mostapplications. For example, for the NW benchmark, divergentwarps estimate no improvement for all the algorithms, butactual performance improvement is about 70%; for the SLbenchmark, divergent branches give negative improvement forall the algorithms, but actual performance improvement isabout 20%. Our results in Figure 7 demonstrate that divergentwarps and divergent branches can not accurately estimate theperformance impact of control flow divergence.

Now we examine our proposed metrics that estimatethe performance impact of control flow divergence: BBV-weighted, BBV-weighted-scheduled. We separate the discus-sion for compute-bound and memory-bound (section VII-D)applications. For each application and thread regrouping algo-rithm pair, we estimate the performance using our proposedmetrics. We compare the estimation of the initial implemen-tation and the implementation with divergence optimization.Figure 8 presents the same performance data as in Figure 7,

TABLE IVDIVERGENT WARPS AND DIVERGENT BRANCHES

Benchmarks Divergent warps /Total warps

Divergent branches /Total branches

MC 8192 / 8192 47380 / 151820SW 1024 / 1024 29783 / 83517722EC 768 / 768 603954 / 52079573NW 1024 / 1024 97049370 / 1094483922

MUM 2048 / 2048 371967 / 1640166SL 36 / 560 102 / 163624SM 4760 / 6080 258875 / 3570480

TABLE VESTIMATION ERROR (IN %)

Benchmark Algorithms BBV-weighted

BBV-weighted-scheduled

MC

Sorting 20.9 7.6Greedy 20.1 6.2Gr.-Max 21.8 7.5

SW

Sorting 20.2 15.8Greedy 21.9 13.0Gr.-Max 2.6 7.5

NW

Sorting 5.8 1.4Greedy 7.5 2.0Gr.-Max 1.5 3.5

SL

Sorting 19.6 4.0Greedy 22.2 11.3Gr.-Max 22.0 7.2

SM

Sorting 1.5 0.5Greedy 1.4 0.9Gr.-Max 1.6 4.3

Average Error 12.7 6.2

but compares to our proposed metrics for five compute-boundapplications. For each metric, we show the improvement inpercentage. As shown in Figure 8, our metrics BBV-weightedand BBV-weighted-scheduled predict the performance well.Specifically, BBV-weighted-scheduled is more accurate thanBBV-weighted as it considers the latency variation among

12

0%

10%

20%

30%

40%

50%

60%

70%

80%

90%

100%

Sorting Greedy Greedy-Max

Sorting Greedy Greedy-Max

Sorting Greedy Greedy-Max

Sorting Greedy Greedy-Max

Sorting Greedy Greedy-Max

MC SW NW SL SM

Perc

enta

ge Im

prov

emen

t Actual Performance Estimated by BBV weighted scheduled Estimated by BBV weighted

Fig. 8. Comparison of our proposed metrics and actual performance improvement for compute-bound applications

0.00

0.50

1.00

1.50

2.00

2.50

3.00

3.50

4.00

4.50

5.00

MC SW NW SL SM EC MUM Average

Per

form

an

ce S

pee

du

p

Sorting Greedy Greedy-Max

Fig. 9. Speedup of Divergence Optimization

thread blocks and models the thread block scheduling. Ta-ble V presents the estimation error of each metric for allthe benchmarks. On average, BBV-weighted has 12.7% error,while BBV-weighted-scheduled improves the estimation errorto 6.2%. Our BBV-weighted-scheduled metric is correlatedwell with the actual performance. This suggests that program-mers can rely on these metrics to estimate the performanceimpact of control flow divergence and the effectiveness of thethread grouping algorithms.

C. Performance of Divergence Optimization

Figure 9 shows the performance speedup of our divergenceoptimization. We compare the performance of initial (withouttransformation) implementation and optimized implementationenabled by code transformation. For each application, wecompare three different regrouping algorithms (e.g., Sorting,Greedy, Greedy-Max). As indicated by our performance met-rics in Figure 8, applications MC, SL and SM have smallperformance improvement. Among the three thread regroupingalgorithms, Greedy-Max is the most effective algorithm. Inparticular, the Sorting algorithm achieves up to 3.5X speedup(on average 1.7X); the Greedy algorithm achieves up to2.7X speedup (on average 1.6X); the Greedy-Max algorithmachieves up to 4.7X speedup (on average 2.2X). Greedy-maxachieves the most speedup due to its accurate performancemodeling of control flow divergence impact on warps andthread block scheduling.

0%10%20%30%40%50%60%70%80%

Sorting Greedy Greedy-Max Sorting Greedy Greedy-Max

EC MUM

Perc

enta

ge Im

prov

emen

t

Actual Performance Estimated by BBV weighted scheduled Estimated by BBV weighted

Fig. 10. Comparison of our proposed metrics and actual performanceimprovement for memory-bound applications

D. Memory-bound Applications

When computation is not the critical path, computationoptimization through thread regrouping algorithms may notlead to expected overall performance improvement. Figure 10presents the percentage improvement of actual performanceand our metrics for the two memory-bound applications ECand MUM. For these two applications, the performance islimited by the achieved memory bandwidth. However, ourmetrics do not model the factors that affect memory bandwidthincluding memory coalescing, bank conflicts, cache conflicts,etc. and thus are not accurate.

VIII. RELATED WORK

In the recent years, we have witnessed the success ofGraphics Processing Units (GPUs) for high performance com-puting. However, performance tuning for GPUs was not atrivial task [21]. Analytical performance models have beenproposed to predict the performance improvement or identifythe performance bottlenecks [4], [10], [22]–[24]. Optimizationtechniques have also been proposed to tailor the applications tothe underlying architectural features and improve the resourceutilization. The state-of-the-art GPU performance optimizationtechniques focused on thread and warp scheduling, cacheoptimization, register allocation optimization, power and agingoptimization, multitasking,etc [25]–[32].

If the threads in a warp take different path at condi-tional branches, then execution of different paths have to

13

be serialized in the SIMD architecture. This is well knownas control flow divergence, which has significant impact onthe application performance. There are both software andhardware solutions for control flow divergence optimization.

Hardware Divergence Management. Fung [5] proposed ahardware implementation to dynamically regroup the threadsto improve the SIMD utilization. Their technique allowsdiverged threads with the same PC but originally from dif-ferent warps run together as a wider SIMD group. Meng [6]introduced dynamic warp subdivision (DWS) as an integratedframework for both control and memory divergence. DWSselectively divides warps into warp-splits and creates separateschedulers so that each warp-split can execute independently.By doing this, DWS enables divergent threads execute to-gether and hide each other’s latency. Rhu [33] proposed thedual-path execution model that extends the reconvergencestack model to support interleaved execution of differentpaths. Later, Aamodt [34] improves dual-path to multi-pathmicro-architecture that decouples divergence and reconver-gence tracking. There are also proposals that optimize theSIMD divergence through compaction [35] [36]. However, theabove hardware-based techniques require hardware changesand inevitably add complexity and hardware cost in the registerfile, scheduling logic, etc.

Software Divergence Management. Besides hardware-basedtechniques, software-based techniques for optimizing controlflow divergence have also been proposed. Software-based tech-niques can be applied to real GPUs without hardware changes.Zhang [7] [8] proposed to eliminate control-flow divergencethrough reference redirection or data layout transformation.However, their techniques simply assume that all instancesof divergence have the same impact on the performance anduse the percentage of divergent warps [7] or percentage ofdivergent branches [9] as their metrics for estimating perfor-mance improvement. However, these metrics are not accuratefor estimating performance impact of divergence as differentdivergences have different numbers of instructions, latencies,and also thread block scheduling can impact the overlapping ofthreads’ execution. In contrast, we build a more accurate modelby modeling these architectural features. Finally, Lee [37]implemented compiler algorithms to handle divergence usingpredicates at the intermediate representation level. In contrast,our optimization can be enabled through small code changesat source code level.

IX. CONCLUSION

Control flow divergence can significantly affect the perfor-mance of GPU applications. In this paper, we first present con-trol flow divergence metrics that can accurately estimate theperformance impact of control flow divergence for compute-bound GPU kernels. Our proposed BBV-weighted-scheduledmetric achieves high accuracy (6.2% error) for these work-loads. Thus, developers can rely on our metric to evaluatepotential control flow divergence optimizations and predictwhether a transformed program would have improved perfor-mance without actually performing the transformation of theapplication. Then, we represent threads’ behavior using per-thread BBVs and develop three thread regrouping algorithms

for control flow divergence optimization. We demonstrate thatour divergence optimization can reduce the impact of controlflow divergence, with good correlation between the actualperformance impact and the impact predicted by our metrics.Our best thread regrouping algorithm Greedy-Max achieves upto 4.7X speedup (on average 2.2X).

X. ACKNOWLEDGMENTS

This work was supported in part by the National NaturalScience Foundation of China under Grant 61300005, in part byHuman-Centered Cyber-physical Systems Programme at theAdvanced Digital Sciences Center from Singapore’s Agencyfor Science, Technology and Research (A*STAR) and CFAR,one of six centers of STARnet, a Semiconductor ResearchCorporation program sponsored by MARCO and DARPA.

REFERENCES

[1] V. Bertacco, D. Chatterjee, N. Bombieri, F. Fummi, S. Vinco, A. M.Kaushik, and H. D. Patel, “On the use of GP-GPUs for acceleratingcompute-intensive EDA applications,” in Proceedings of the Conferenceon Design, Automation and Test in Europe, ser. DATE ’13, 2013, pp.1357–1366.

[2] Y. Deng, B. D. Wang, and S. Mu, “Taming irregular EDA applicationson GPUs,” in Proceedings of the 2009 International Conference onComputer-Aided Design, ser. ICCAD ’09, 2009, pp. 539–546.

[3] Y. Liang, Z. Cui, S. Zhao, K. Rupnow, Y. Zhang, D. Jones, and D. Chen,“Real-time implementation and performance optimization of 3D soundlocalization on GPUs,” in Proceedings of the Conference on Design,Automation and Test in Europe, ser. DATE ’12, 2012, pp. 832–835.

[4] S. S. Baghsorkhi, M. Delahaye, S. J. Patel, W. D. Gropp, and W.-m. W.Hwu, “An adaptive performance modeling tool for GPU architectures,”in Proceedings of the 15th ACM SIGPLAN Symposium on Principles andPractice of Parallel Programming, ser. PPoPP ’10, 2010, pp. 105–114.

[5] W. W. L. Fung, I. Sham, G. Yuan, and T. M. Aamodt, “DynamicWarp Formation and Scheduling for Efficient GPU Control Flow,” inProceedings of the 40th Annual IEEE/ACM International Symposiumon Microarchitecture, ser. MICRO 40, 2007, pp. 407–420.

[6] J. Meng, D. Tarjan, and K. Skadron, “Dynamic warp subdivision forintegrated branch and memory divergence tolerance,” in Proceedings ofthe 37th annual international symposium on Computer architecture, ser.ISCA ’10, 2010, pp. 235–246.

[7] E. Z. Zhang, Y. Jiang, Z. Guo, and X. Shen, “Streamlining GPUapplications on the fly: thread divergence elimination through runtimethread-data remapping,” in Proceedings of the 24th ACM InternationalConference on Supercomputing, ser. ICS ’10, 2010, pp. 115–126.

[8] E. Z. Zhang, Y. Jiang, Z. Guo, K. Tian, and X. Shen, “On-the-flyelimination of dynamic irregularities for GPU computing,” in Proceed-ings of the sixteenth international conference on Architectural supportfor programming languages and operating systems, ser. ASPLOS XVI,2011, pp. 369–380.

[9] NVIDIA Visual Profiler. [Online]. Available: https://developer.nvidia.com/nvidia-visual-profiler

[10] S. Hong and H. Kim, “An analytical model for a GPU architecture withmemory-level and thread-level parallelism awareness,” in Proceedingsof the 36th annual international symposium on Computer architecture,ser. ISCA ’09, 2009, pp. 152–163.

[11] H. Wong, M.-M. Papadopoulou, M. Sadooghi-Alvandi, andA. Moshovos, “Demystifying GPU microarchitecture through micro-benchmarking,” in Performance Analysis of Systems Software (ISPASS),2010 IEEE International Symposium on, 2010, pp. 235–246.

[12] W. Fung, I. Sham, G. Yuan, and T. Aamodt, “Dynamic Warp Formation:Efficient MIMD Control Flow on SIMD Graphics Hardware,” ACMTransactions on Architecture and Code Optimization, vol. 6, no. 2, 2009.

[13] NVIDIA CUDA Programming Guide. [Online]. Available: http://docs.nvidia.com/cuda/cuda-c-programming-guide/

[14] S. Ryoo, “ Program Optimization Strategis for Data-Parallel Many-CoreProcessors ,” 2008.

[15] W. E. Lorensen and H. E. Cline, “Marching cubes: A high resolution 3dsurface construction algorithm,” SIGGRAPH Comput. Graph., vol. 21,no. 4, pp. 163–169, Aug. 1987.

14

[16] G. M. Striemer and A. Akoglu, “Sequence alignment with gpu: Per-formance and design challenges,” in IEEE International Symposium onParallel & Distributed Processing (IPDPS), 2009, pp. 1–10.

[17] CUDA-EC.NVIDIA Tesla Bio Bench. [Online]. Available: http://www.nvidia.com/object/ec on tesla.html

[18] T. R. P. Siriwardena and D. N. Ranasinghe, “Accelerating globalsequence alignment using cuda compatabile multi-core gpu,” in 5thInternational Conference on Information and Automation for Sustain-ability (ICIAFs), 2010, pp. 201–206.

[19] M. C. Schatz, C. Trapnell, A. L. Delcher, and A. Varshney, “High-throughput sequence alignment using Graphics Processing Units,” BMCbioinformatics, vol. 8, p. 474, 2007.

[20] D. Min, J. Lu, and M. Do, “A revisit to cost aggregation in stereomatching: How far can we reduce its computational redundancy?” inComputer Vision (ICCV), 2011 IEEE International Conference on, 2011,pp. 1567–1574.

[21] Y. Yang, P. Xiang, J. Kong, and H. Zhou, “A GPGPU compiler formemory optimization and parallelism management,” in Proceedings ofthe 31st ACM SIGPLAN Conference on Programming Language Designand Implementation, ser. PLDI ’10, 2010, pp. 86–97.

[22] L. Ma, K. Agrawal, and R. D. Chamberlain, “Theoretical analysis ofclassic algorithms on highly-threaded many-core GPUs,” in Proc. of the19th ACM SIGPLAN Symposium on Principles and Practice of ParallelProgramming (PPoPP), 2014, pp. 391–392.

[23] L. Ma, K. Agrawal, and R. D. Chamberlain, “Analysis of classicalgorithms on GPUs,” in Proc. of the 12th ACM/IEEE Int’l Conf. onHigh Performance Computing and Simulation (HPCS), 2014.

[24] Z. Cui, Y. Liang, K. Rupnow, and D. Chen, “An Accurate GPU Perfor-mance Model for Effective Control Flow Divergence Optimization,” inParallel Distributed Processing Symposium (IPDPS), 2012 IEEE 26thInternational, 2012, pp. 83–94.

[25] V. Narasiman, M. Shebanow, C. J. Lee, R. Miftakhutdinov, O. Mutlu,and Y. N. Patt, “Improving GPU performance via large warps and two-level warp scheduling,” in Proceedings of the 44th Annual IEEE/ACMInternational Symposium on Microarchitecture, ser. MICRO-44, 2011,pp. 308–317.

[26] M. Gebhart, D. R. Johnson, D. Tarjan, S. W. Keckler, W. J. Dally,E. Lindholm, and K. Skadron, “Energy-efficient mechanisms for manag-ing thread context in throughput processors,” in Proceedings of the 38thAnnual International Symposium on Computer Architecture, ser. ISCA’11, 2011, pp. 235–246.

[27] X. Xie, Y. Liang, G. Sun, and D. Chen, “An efficient compiler frameworkfor cache bypassing on GPUs,” in Proceedings of the InternationalConference on Computer-Aided Design, ser. ICCAD ’13, 2013, pp. 516–523.

[28] X. Xie, Y. Liang, Y. Wang, G. Sun, and T. Wang, “Coordinated staticand dynamic cache bypassing for GPUs,” in IEEE 21st InternationalSymposium on High Performance Computer Architecture, ser. HPCA,Feb 2015.

[29] X. Xie, Y. Liang, X. Li, Y. Wu, G. Sun, T. Wang, and D. Fan, “Enablingcoordinated register allocation and thread-level parallelism optimizationfor GPUs,” in Proceedings of the 48th Annual IEEE/ACM InternationalSymposium on Microarchitecture, ser. MICRO-48, 2015.

[30] Y. Liang, Z. Cui, K. Rupnow, and D. Chen, “Register and threadstructure optimization for GPUs,” in Proceedings of the 18th Asia andSouth Pacific Design Automation Conference (ASP-DAC), Jan. 2013, pp.461–466.

[31] X. Chen, Y. Wang, Y. Liang, Y. Xie, and H. Yang, “Run-time techniquefor simultaneous aging and power optimization in GPGPUs,” in Pro-ceedings of the 51st Annual Design Automation Conference, ser. DAC’14, 2014, pp. 168:1–168:6.

[32] Y. Liang, H. Huynh, K. Rupnow, R. Goh, and D. Chen, “Efficient GPUspatial-temporal multitasking,” Parallel and Distributed Systems, IEEETransactions on, vol. 26, no. 3, pp. 748–760, March 2015.

[33] M. Rhu and M. Erez, “The dual-path execution model for efficientGPU control flow,” in IEEE 19th International Symposium on HighPerformance Computer Architecture, ser. HPCA, Feb 2013.

[34] A. EITantawy, J. Ma, M. O’Connor, and T. Aamodt, “The dual-path execution model for efficient GPU control flow,” in IEEE 19thInternational Symposium on High Performance Computer Architecture,ser. HPCA, Feb 2013, pp. 248 – 259.

[35] A. S. Vaidya, A. Shayesteh, D. H. Woo, R. Saharoy, and M. Azimi,“SIMD divergence optimization through intra-warp compaction,” inProceedings of the 40th Annual International Symposium on ComputerArchitecture, ser. ISCA ’13, 2013, pp. 368–379.

[36] M. Rhu and M. Erez, “Maximizing SIMD resource utilization inGPGPUs with SIMD lane permutation,” in Proceedings of the 40th

Annual International Symposium on Computer Architecture, ser. ISCA’13, 2013, pp. 356–367.

[37] Y. Lee, V. Grover, R. Krashinsky, M. Stephenson, S. W. Keckler,and K. Asanovic, “Exploring the design space of SPMD divergencemanagement on data-parallel architectures,” in Proceedings of the 47thAnnual IEEE/ACM International Symposium on Microarchitecture, ser.MICRO-47, 2014, pp. 101–113.

Dr. Yun Liang obtained his BS in software engineeringfrom Tongji University, Shanghai, China and the Ph.D.degree in computer science from National University of Sin-gapore, in 2004 and 2010, respectively. He was a ResearchScientist with Advanced Digital Science Center, Universityof Illinois Urbana-Champaign, Urbana, IL, USA, from 2010to 2012. He has been an Assistant Professor with theschool of Eletronics Engineering and Computer Science,Peking University, Beijing, China, since 2012. His currentresearch interests include graphics processing unit (GPU)architecture and optimization, heterogeneous computing,

embedded system, and high level synthesis. Dr. Liang was a recipient of the BestPaper Award in International Symposium on Field-Programmable Custom ComputingMachines (FCCM’)11 and the best paper award nominations in International Conferenceon Hardware/Software Codesign and System Synthesis (CODES+ISSS)’08 and DesignAutomation Conference (DAC)’12. He serves a Technical Committee Member for AsiaSouth Pacific Design Automation Conference (ASPDAC), Design Automation and Testin Europe (DATE), International Conference on Compilers Architecture and Synthesis forEmbedded System (CASES), and International Conference on Parallel Architectures andCompilation Techniques (PACT). He is the TPC Subcommittee Chair for ASPDAC’13.

Mr. Muhammad Satria Muhammad Teguh Satria re-ceived his BS in Mathematics from the Bandung Instituteof Technology in 2006, and an MS in Computer Scienceand Information Engineering from National Taipei Univer-sity of Technology in 2009. Since 2009 he has workedin CUDA development projects for geothermal, computervision, space science and general computing projects. From2012 to 2014, Teguh worked at the Advanced DigitalSciences Center in Singapore.

Dr. Kyle Rupnow received his Ph.D. in ElectricalEngineering from the University of Wisconsin-Madison in2010. He previously received a Sandia National Labora-tories Excellence in Engineering Fellowship and an NSFGraduate Research Fellowship honorable mention. He isalso a recipient of the Gerald Holdridge award for TutorialDevelopment and the University of Wisconsin-MadisonCapstone Ph.D. teaching award. He is currently a researchscientist in ADSC, UIUC and an assistant professor inNanyang Technological University, Singapore.

Dr Deming Chen obtained his BS in computer sciencefrom University of Pittsburgh, Pennsylvania in 1995, andhis MS and PhD in computer science from University ofCalifornia at Los Angeles in 2001 and 2005 respectively.He has been an associate professor in the ECE departmentof University of Illinois, Urbana-Champaign since 2011.His current research interests include system-level and high-level synthesis, nano-systems design and nano-centric CADtechniques, GPU and reconfigurable computing, hardware/-software co-design, and computational biology. He is orhas been an associated editor for TCAD, TODAES, TVLSI,

TCAS-I. He obtained the NSF CAREER Award in 2008, and five Best Paper Awardsfor ASPDAC’09, SASP’09, FCCM’11, SAAHPC’11, and CODES+ISSS’13. He receivedthe ACM SIGDA Outstanding New Faculty Award in 2010 and IBM Faculty Award in2014 and 2015. He is a senior member of IEEE.