Optimizing Parallel Reduction in CUDA

Similar documents
Optimizing Parallel Reduction in CUDA. Mark Harris NVIDIA Developer Technology

Optimizing Parallel Reduction in CUDA. Mark Harris NVIDIA Developer Technology

ECE/ME/EMA/CS 759 High Performance Computing for Engineering Applications

CS671 Parallel Programming in the Many-Core Era

Advanced CUDA Optimizations

CS 314 Principles of Programming Languages

CUDA Technical Training

Computer Architecture

Global Memory Access Pattern and Control Flow

CUDA. Sathish Vadhiyar High Performance Computing

Optimization Strategies Global Memory Access Pattern and Control Flow

CUDA Programming. Week 1. Basic Programming Concepts Materials are copied from the reference list

CS516 Programming Languages and Compilers II

GPU programming: CUDA basics. Sylvain Collange Inria Rennes Bretagne Atlantique

Lecture 2: CUDA Programming

CS/EE 217 GPU Architecture and Parallel Programming. Lecture 10. Reduction Trees

Lecture 6. Programming with Message Passing Message Passing Interface (MPI)

Reductions and Low-Level Performance Considerations CME343 / ME May David Tarjan NVIDIA Research

Card Sizes. Tesla K40: 2880 processors; 12 GB memory

Warps and Reduction Algorithms

CUDA Performance Considerations (2 of 2)

Hardware/Software Co-Design

Plan. CS : High-Performance Computing with CUDA. Matrix Transpose Characteristics (1/2) Plan

CS/EE 217 Midterm. Question Possible Points Points Scored Total 100

Generating Performance Portable Code using Rewrite Rules

CS : High-Performance Computing with CUDA

Fundamental Optimizations in CUDA Peng Wang, Developer Technology, NVIDIA

AMS 148 Chapter 8: Optimization in CUDA, and Advanced Topics

Lecture 7. Using Shared Memory Performance programming and the memory hierarchy

4.10 Historical Perspective and References

Tesla Architecture, CUDA and Optimization Strategies

Hands-on CUDA Optimization. CUDA Workshop

GPU programming: CUDA. Sylvain Collange Inria Rennes Bretagne Atlantique

Lecture 5. Performance Programming with CUDA

Introduction to GPGPU and GPU-architectures

CS 179: GPU Computing LECTURE 4: GPU MEMORY SYSTEMS

Lecture 3: CUDA Programming & Compiler Front End

PPAR: CUDA basics. Sylvain Collange Inria Rennes Bretagne Atlantique PPAR

CUDA Performance Optimization. Patrick Legresley

Slide credit: Slides adapted from David Kirk/NVIDIA and Wen-mei W. Hwu, DRAM Bandwidth

Implementation of Adaptive Coarsening Algorithm on GPU using CUDA

Convolution Soup: A case study in CUDA optimization. The Fairmont San Jose 10:30 AM Friday October 2, 2009 Joe Stam

GPU Programming. Performance Considerations. Miaoqing Huang University of Arkansas Fall / 60

2006: Short-Range Molecular Dynamics on GPU. San Jose, CA September 22, 2010 Peng Wang, NVIDIA

CME 213 S PRING Eric Darve

CUDA. More on threads, shared memory, synchronization. cuprintf

GPU MEMORY BOOTCAMP III

Advanced CUDA Optimizations. Umar Arshad ArrayFire

ECE 498AL. Lecture 12: Application Lessons. When the tires hit the road

Real-time Graphics 9. GPGPU

Motivation OpenACC and OpenMP for Accelerators Cray Compilation Environment (CCE) Examples

CSCI 402: Computer Architectures. Parallel Processors (2) Fengguang Song Department of Computer & Information Science IUPUI.

CUDA Optimizations WS Intelligent Robotics Seminar. Universität Hamburg WS Intelligent Robotics Seminar Praveen Kulkarni

Understanding and modeling the synchronization cost in the GPU architecture

Lecture 7. Overlap Using Shared Memory Performance programming the memory hierarchy

Programming in CUDA. Malik M Khan

Advanced CUDA Programming. Dr. Timo Stich

Lecture 11 Parallel Computation Patterns Reduction Trees

More CUDA. Advanced programming

Fast Tridiagonal Solvers on GPU

CUDA Memory Model. Monday, 21 February Some material David Kirk, NVIDIA and Wen-mei W. Hwu, (used with permission)

COLLECTIVE COMMUNICATION AND BARRIER SYNCHRONIZATION ON NVIDIA CUDA GPU

Convolution Soup: A case study in CUDA optimization. The Fairmont San Jose Joe Stam

Unrolling parallel loops

2/17/10. Administrative. L7: Memory Hierarchy Optimization IV, Bandwidth Optimization and Case Studies. Administrative, cont.

CUDA OPTIMIZATION WITH NVIDIA NSIGHT ECLIPSE EDITION. Julien Demouth, NVIDIA Cliff Woolley, NVIDIA


NVIDIA CUDA. Fermi Compatibility Guide for CUDA Applications. Version 1.1

Lecture 9. Thread Divergence Scheduling Instruction Level Parallelism

CUDA. Schedule API. Language extensions. nvcc. Function type qualifiers (1) CUDA compiler to handle the standard C extensions.

Portable and Productive Performance on Hybrid Systems with OpenACC Compilers and Tools

CUDA OPTIMIZATION WITH NVIDIA NSIGHT ECLIPSE EDITION

GPU Programming. Parallel Patterns. Miaoqing Huang University of Arkansas 1 / 102

CUDA Performance Optimization Mark Harris NVIDIA Corporation

LDetector: A low overhead data race detector for GPU programs

CUDA and GPU Performance Tuning Fundamentals: A hands-on introduction. Francesco Rossi University of Bologna and INFN

PARSEC Benchmark Suite: A Parallel Implementation on GPU using CUDA

Shared Memory. Table of Contents. Shared Memory Learning CUDA to Solve Scientific Problems. Objectives. Technical Issues Shared Memory.

A Case for a Flexible Scalar Unit in SIMT Architecture

CSE 599 I Accelerated Computing - Programming GPUS. Memory performance

CUDA Advanced Techniques 3 Mohamed Zahran (aka Z)

Identifying Performance Limiters Paulius Micikevicius NVIDIA August 23, 2011

Profiling & Tuning Applications. CUDA Course István Reguly

High Performance Computing on GPUs using NVIDIA CUDA

INTRODUCTION TO CUDA PROGRAMMING BHUPENDER THAKUR

CSE 591: GPU Programming. Using CUDA in Practice. Klaus Mueller. Computer Science Department Stony Brook University

Introduction to Parallel Computing with CUDA. Oswald Haan

Dense Linear Algebra. HPC - Algorithms and Applications

CUDA Performance Considerations (2 of 2) Varun Sampath Original Slides by Patrick Cozzi University of Pennsylvania CIS Spring 2012

GPU Performance Optimisation. Alan Gray EPCC The University of Edinburgh

Parallel Systems Course: Chapter IV. GPU Programming. Jan Lemeire Dept. ETRO November 6th 2008

Real-time Graphics 9. GPGPU

Programmer's View of Execution Teminology Summary

Administrative. L8: Writing Correct Programs, cont. and Control Flow. Questions/comments from previous lectures. Outline 2/10/11

CS6963: Parallel Programming for GPUs Midterm Exam March 25, 2009

Leveraging Matrix Block Structure In Sparse Matrix-Vector Multiplication. Steve Rennich Nvidia Developer Technology - Compute

Memory. Lecture 2: different memory and variable types. Memory Hierarchy. CPU Memory Hierarchy. Main memory

Atomic Operations. Atomic operations, fast reduction. GPU Programming. Szénási Sándor.

CUDA Optimization with NVIDIA Nsight Visual Studio Edition 3.0. Julien Demouth, NVIDIA

From Hello World to Exascale

Transcription:

Optimizing Parallel Reduction in CUDA Mark Harris NVIDIA Developer Technology http://developer.download.nvidia.com/assets/cuda/files/reduction.pdf

Parallel Reduction Tree-based approach used within each thread block 3 1 7 0 4 1 6 3 4 7 5 9 11 14 25 Need to be able to use multiple thread blocks To process very large arrays To keep all multiprocessors on the GPU busy Each thread block reduces a portion of the array But how do we communicate partial results between thread blocks? 3

Solution: Kernel Decomposition Avoid global sync by decomposing computation into multiple kernel invocations 3 1 7 0 4 1 6 3 4 7 5 9 11 14 25 3 1 7 0 4 1 6 3 4 7 5 9 11 14 25 3 1 7 0 4 1 6 3 4 7 5 9 11 14 25 3 1 7 0 4 1 6 3 4 7 5 9 11 14 25 3 1 7 0 4 1 6 3 4 7 5 9 11 14 25 3 1 7 0 4 1 6 3 4 7 5 9 11 14 25 3 1 7 0 4 1 6 3 4 7 5 9 11 14 25 3 1 7 0 4 1 6 3 4 7 5 9 11 14 25 Level 0: 8 blocks 3 1 7 0 4 1 6 3 4 7 5 9 11 14 25 Level 1: 1 block In the case of reductions, code for all levels is the same Recursive kernel invocation 5

What is Our Optimization Goal? We should strive to reach GPU peak performance Choose the right metric: GFLOP/s: for compute-bound kernels Bandwidth: for memory-bound kernels Reductions have very low arithmetic intensity 1 flop per element loaded (bandwidth-optimal) Therefore we should strive for peak bandwidth Will use G80 GPU for this example 384-bit memory interface, 900 MHz DDR 384 * 1800 / 8 = 86.4 GB/s Jinx (m2090 nodes) ~ 100 to 150 GB/s? 6

Parallel Reduction: Interleaved Addressing (shared memory) 10 1 8-1 0-2 3 5-2 -3 2 7 0 11 0 2

Parallel Reduction: Interleaved Addressing (shared memory) 10 1 8-1 0-2 3 5-2 -3 2 7 0 11 0 2 Step 1 Stride 1 Step 2 Stride 2 Step 3 Stride 4 Step 4 Stride 8 0 2 4 6 8 10 12 14 11 1 7-1 -2-2 8 5-5 -3 9 7 11 11 2 2 0 4 8 12 18 1 7-1 6-2 8 5 4-3 9 7 13 11 2 2 0 8 24 1 7-1 6-2 8 5 17-3 9 7 13 11 2 2 0 41 1 7-1 6-2 8 5 17-3 9 7 13 11 2 2 8

Reduction #1: Interleaved Addressing global void reduce0(int *g_idata, int *g_odata) { extern shared int sdata[]; // each thread loads one element from global to shared mem unsigned int tid = threadidx.x; unsigned int i = blockidx.x*blockdim.x + threadidx.x; sdata[tid] = g_idata[i]; syncthreads(); // do reduction in shared mem for(unsigned int s=1; s < blockdim.x; s *= 2) { if (tid % (2*s) == 0) { sdata[tid] += sdata[tid + s]; syncthreads(); // write result for this block to global mem if (tid == 0) g_odata[blockidx.x] = sdata[0]; 7

Performance for 4M element reduction Kernel 1: interleaved addressing with divergent branching Time (2 22 ints) Bandwidth 8.054 ms 2.083 GB/s Note: Block Size = 128 threads for all tests Jinx: Kernel ~ Bandwidth Kernel 1 ~ 10.6 GB/s 10

Reduction #1: Interleaved Addressing global void reduce1(int *g_idata, int *g_odata) { extern shared int sdata[]; // each thread loads one element from global to shared mem unsigned int tid = threadidx.x; unsigned int i = blockidx.x*blockdim.x + threadidx.x; sdata[tid] = g_idata[i]; syncthreads(); // do reduction in shared mem for (unsigned int s=1; s < blockdim.x; s *= 2) { if (tid % (2*s) == 0) { sdata[tid] += sdata[tid + s]; syncthreads(); Problem: highly divergent warps are very inefficient, and % operator is very slow // write result for this block to global mem if (tid == 0) g_odata[blockidx.x] = sdata[0]; 9

Parallel Reduction: Interleaved Addressing (shared memory) 10 1 8-1 0-2 3 5-2 -3 2 7 0 11 0 2 Step 1 Stride 1 Step 2 Stride 2 Step 3 Stride 4 Step 4 Stride 8 0 2 4 6 8 10 12 14 11 1 7-1 -2-2 8 5-5 -3 9 7 11 11 2 2 0 4 8 12 18 1 7-1 6-2 8 5 4-3 9 7 13 11 2 2 0 8 24 1 7-1 6-2 8 5 17-3 9 7 13 11 2 2 0 41 1 7-1 6-2 8 5 17-3 9 7 13 11 2 2 8

Parallel Reduction: Interleaved Addressing (shared memory) 10 1 8-1 0-2 3 5-2 -3 2 7 0 11 0 2 Step 1 Stride 1 Step 2 Stride 2 Step 3 Stride 4 Step 4 Stride 8 0 1 2 3 4 5 6 7 11 1 7-1 -2-2 8 5-5 -3 9 7 11 11 2 2 0 1 2 3 18 1 7-1 6-2 8 5 4-3 9 7 13 11 2 2 0 1 24 1 7-1 6-2 8 5 17-3 9 7 13 11 2 2 0 41 1 7-1 6-2 8 5 17-3 9 7 13 11 2 2 New Problem: Shared Memory Bank Conflicts 12

Reduction #2: Interleaved Addressing Just replace divergent branch in inner loop: for (unsigned int s=1; s < blockdim.x; s *= 2) { if (tid % (2*s) == 0) { sdata[tid] += sdata[tid + s]; syncthreads(); With strided index and non-divergent branch: for (unsigned int s=1; s < blockdim.x; s *= 2) { int index = 2 * s * tid; if (index < blockdim.x) { sdata[index] += sdata[index + s]; syncthreads(); 11

Performance for 4M element reduction Kernel 1: interleaved addressing with divergent branching Kernel 2: interleaved addressing with bank conflicts Time (2 22 ints) Bandwidth 8.054 ms 2.083 GB/s Step Speedup Cumulative Speedup 3.456 ms 4.854 GB/s 2.33x 2.33x Jinx: Kernel ~ Bandwidth Speedup Kernel 1 ~ 10.6 GB/s Kernel 2 ~ 16.4 GB/s 1.55x 13

Parallel Reduction: Interleaved Addressing (shared memory) 10 1 8-1 0-2 3 5-2 -3 2 7 0 11 0 2 Step 1 Stride 1 Step 2 Stride 2 Step 3 Stride 4 Step 4 Stride 8 0 1 2 3 4 5 6 7 11 1 7-1 -2-2 8 5-5 -3 9 7 11 11 2 2 0 1 2 3 18 1 7-1 6-2 8 5 4-3 9 7 13 11 2 2 0 1 24 1 7-1 6-2 8 5 17-3 9 7 13 11 2 2 0 41 1 7-1 6-2 8 5 17-3 9 7 13 11 2 2 New Problem: Shared Memory Bank Conflicts 12

Array 0 1 2 3 4 5 6 7 8 9 Example: Array mapped to 4 banks. Maximum throughput occurs when delivering 1 word per bank. Shared memory 0 1 2 3 4 5 6 7 8 9

Parallel Reduction: Sequential Addressing (shared memory) 10 1 8-1 0-2 3 5-2 -3 2 7 0 11 0 2 Step 1 Stride 8 Step 2 Stride 4 Step 3 Stride 2 Step 4 Stride 1 0 1 2 3 4 5 6 7 8-2 10 6 0 9 3 7-2 -3 2 7 0 11 0 2 0 1 2 3 8 7 13 13 0 9 3 7-2 -3 2 7 0 11 0 2 0 1 21 20 13 13 0 9 3 7-2 -3 2 7 0 11 0 2 0 41 20 13 13 0 9 3 7-2 -3 2 7 0 11 0 2 Sequential addressing is conflict free 14

Reduction #3: Sequential Addressing Just replace strided indexing in inner loop: for (unsigned int s=1; s < blockdim.x; s *= 2) { int index = 2 * s * tid; if (index < blockdim.x) { sdata[index] += sdata[index + s]; syncthreads(); With reversed loop and threadid-based indexing: for (unsigned int s=blockdim.x/2; s>0; s>>=1) { if (tid < s) { sdata[tid] += sdata[tid + s]; syncthreads(); 15

Performance for 4M element reduction Kernel 1: interleaved addressing with divergent branching Kernel 2: interleaved addressing with bank conflicts Kernel 3: sequential addressing Time (2 22 ints) Bandwidth 8.054 ms 2.083 GB/s Step Speedup Cumulative Speedup 3.456 ms 4.854 GB/s 2.33x 2.33x 1.722 ms 9.741 GB/s 2.01x 4.68x Jinx: Kernel ~ Bandwidth Speedup (Cumulative speedup) Kernel 1 ~ 10.6 GB/s Kernel 2 ~ 16.4 GB/s 1.55x Kernel 3 ~ 22.3 GB/s 1.35x (2.10x) 16

Idle s Problem: for (unsigned int s=blockdim.x/2; s>0; s>>=1) { if (tid < s) { sdata[tid] += sdata[tid + s]; syncthreads(); Half of the threads are idle on first loop iteration! This is wasteful 17

Reduction #4: First Add During Load Halve the number of blocks, and replace single load: // each thread loads one element from global to shared mem unsigned int tid = threadidx.x; unsigned int i = blockidx.x*blockdim.x + threadidx.x; sdata[tid] = g_idata[i]; syncthreads(); With two loads and first add of the reduction: // perform first level of reduction, // reading from global memory, writing to shared memory unsigned int tid = threadidx.x; unsigned int i = blockidx.x*(blockdim.x*2) + threadidx.x; sdata[tid] = g_idata[i] + g_idata[i+blockdim.x]; syncthreads(); 18

Performance for 4M element reduction Kernel 1: interleaved addressing with divergent branching Kernel 2: interleaved addressing with bank conflicts Kernel 3: sequential addressing Kernel 4: first add during global load Time (2 22 ints) Bandwidth 8.054 ms 2.083 GB/s Step Speedup Cumulative Speedup 3.456 ms 4.854 GB/s 2.33x 2.33x 1.722 ms 9.741 GB/s 2.01x 4.68x 0.965 ms 17.377 GB/s 1.78x 8.34x Jinx: Kernel ~ Bandwidth Speedup (Cumulative speedup) Kernel 1 ~ 10.6 GB/s Kernel 2 ~ 16.4 GB/s 1.55x Kernel 3 ~ 22.3 GB/s 1.35x (2.10x) Kernel 4 ~ 37.5 GB/s 1.22x (4.33x) 19

Instruction Bottleneck At 17 GB/s, we re far from bandwidth bound And we know reduction has low arithmetic intensity Therefore a likely bottleneck is instruction overhead Ancillary instructions that are not loads, stores, or arithmetic for the core computation In other words: address arithmetic and loop overhead Strategy: unroll loops 20

Recall

Unrolling the Last Warp As reduction proceeds, # active threads decreases When s <= 32, we have only one warp left Instructions are SIMD synchronous within a warp That means when s <= 32: We don t need to syncthreads() We don t need if (tid < s) because it doesn t save any work Let s unroll the last 6 iterations of the inner loop 21

Reduction #5: Unroll the Last Warp device void warpreduce(volatile int* sdata, int tid) { sdata[tid] += sdata[tid + 32]; sdata[tid] += sdata[tid + 16]; sdata[tid] += sdata[tid + 8]; sdata[tid] += sdata[tid + 4]; sdata[tid] += sdata[tid + 2]; sdata[tid] += sdata[tid + 1]; // later for (unsigned int s=blockdim.x/2; s>32; s>>=1) { if (tid < s) sdata[tid] += sdata[tid + s]; syncthreads(); if (tid < 32) warpreduce(sdata, tid); Note: This saves useless work in all warps, not just the last one! Without unrolling, all warps execute every iteration of the for loop and if statement IMPORTANT: For this to be correct, we must use the volatile keyword! 22

Performance for 4M element reduction Kernel 1: interleaved addressing with divergent branching Kernel 2: interleaved addressing with bank conflicts Kernel 3: sequential addressing Kernel 4: first add during global load Kernel 5: unroll last warp Time (2 22 ints) Bandwidth 8.054 ms 2.083 GB/s Step Speedup Cumulative Speedup 3.456 ms 4.854 GB/s 2.33x 2.33x 1.722 ms 9.741 GB/s 2.01x 4.68x 0.965 ms 17.377 GB/s 1.78x 8.34x 0.536 ms 31.289 GB/s 1.8x 15.01x Jinx: Kernel ~ Bandwidth Speedup (Cumulative speedup) Kernel 1 ~ 10.6 GB/s Kernel 4 ~ 37.5 GB/s 1.22x (4.33x) Kernel 5 ~ 55.5 GB/s 1.48x (5.24x) 23

Complete Unrolling If we knew the number of iterations at compile time, we could completely unroll the reduction Luckily, the block size is limited by the GPU to 512 threads Also, we are sticking to power-of-2 block sizes So we can easily unroll for a fixed block size But we need to be generic how can we unroll for block sizes that we don t know at compile time? Templates to the rescue! CUDA supports C++ template parameters on device and host functions Won t cover today; see Harris s original slides. 24

Performance for 4M element reduction Kernel 1: interleaved addressing with divergent branching Kernel 2: interleaved addressing with bank conflicts Kernel 3: sequential addressing Kernel 4: first add during global load Kernel 5: unroll last warp Time (2 22 ints) Bandwidth 8.054 ms 2.083 GB/s Step Speedup Cumulative Speedup 3.456 ms 4.854 GB/s 2.33x 2.33x 1.722 ms 9.741 GB/s 2.01x 4.68x 0.965 ms 17.377 GB/s 1.78x 8.34x 0.536 ms 31.289 GB/s 1.8x 15.01x Kernel 6: completely unrolled 0.381 ms 43.996 GB/s 1.41x 21.16x Kernel 1 ~ 10.6 GB/s Kernel 5 ~ 55.5 GB/s 1.48x (5.24x) Kernel 6 ~ 62.7 GB/s 1.13x (5.92x) 28

Last idea (for today): Algorithmic cascading (coarsening)

E p T 1 P T P = 1 W +(p 1) D W 0 W 0 = (1) > 0 Last idea (for today): Algorithmic cascading (coarsening)

Work optimality E p T 1 P T P = 1 W +(p 1) D W 0 W 0 = (1) > 0 Last idea (for today): Algorithmic cascading (coarsening)

E p T 1 P T P = 1 W +(p 1) D W 0 W 0 = (1) > 0 Last idea (for today): Algorithmic cascading (coarsening)

E p T 1 P T P = 1 W +(p 1) D W 0 W 0 = (1) > 0 Weak scaling Last idea (for today): Algorithmic cascading (coarsening)

Reduction #7: Multiple Adds / Replace load and add of two elements: unsigned int tid = threadidx.x; unsigned int i = blockidx.x*(blockdim.x*2) + threadidx.x; sdata[tid] = g_idata[i] + g_idata[i+blockdim.x]; syncthreads(); With a while loop to add as many as necessary: unsigned int tid = threadidx.x; unsigned int i = blockidx.x*(blocksize*2) + threadidx.x; unsigned int gridsize = blocksize*2*griddim.x; sdata[tid] = 0; while (i < n) { sdata[tid] += g_idata[i] + g_idata[i+blocksize]; i += gridsize; syncthreads(); 32

Reduction #7: Multiple Adds / Replace load and add of two elements: unsigned int tid = threadidx.x; unsigned int i = blockidx.x*(blockdim.x*2) + threadidx.x; sdata[tid] = g_idata[i] + g_idata[i+blockdim.x]; syncthreads(); With a while loop to add as many as necessary: unsigned int tid = threadidx.x; unsigned int i = blockidx.x*(blocksize*2) + threadidx.x; unsigned int gridsize = blocksize*2*griddim.x; sdata[tid] = 0; Note: gridsize loop stride to maintain coalescing! while (i < n) { sdata[tid] += g_idata[i] + g_idata[i+blocksize]; i += gridsize; syncthreads(); 33

Performance for 4M element reduction Kernel 1: interleaved addressing with divergent branching Kernel 2: interleaved addressing with bank conflicts Kernel 3: sequential addressing Kernel 4: first add during global load Kernel 5: unroll last warp Kernel 6: completely unrolled Kernel 7: multiple elements per thread Time (2 22 ints) Bandwidth 8.054 ms 2.083 GB/s Step Speedup Cumulative Speedup 3.456 ms 4.854 GB/s 2.33x 2.33x 1.722 ms 9.741 GB/s 2.01x 4.68x 0.965 ms 17.377 GB/s 1.78x 8.34x 0.536 ms 31.289 GB/s 1.8x 15.01x 0.381 ms 43.996 GB/s 1.41x 21.16x 0.268 ms 62.671 GB/s 1.42x 30.04x Kernel 7 on 32M elements: 73 GB/s! 34

Performance for 4M element reduction Kernel 1: interleaved addressing with divergent branching Kernel 2: interleaved addressing with bank conflicts Kernel 3: sequential addressing Kernel 4: first add during global load Kernel 5: unroll last warp Kernel 6: completely unrolled Kernel 7: multiple elements per thread Time (2 22 ints) Bandwidth 8.054 ms 2.083 GB/s Step Speedup Cumulative Speedup Kernel 13.456 ~ 10.6 ms GB/s4.854 GB/s 2.33x 2.33x Kernel 6 ~ 62.7 GB/s 1.13x (5.92x) Kernel 71.722 ~ 80.4 ms GB/s 9.741 1.28x GB/s(7.58x) 2.01x 4.68x 0.965 ms 17.377 GB/s 1.78x 8.34x On 2^25 (~ 32 Mega-elems): 102 GB/s 0.536 ms 31.289 GB/s 1.8x 15.01x 0.381 ms 43.996 GB/s 1.41x 21.16x 0.268 ms 62.671 GB/s 1.42x 30.04x Kernel 7 on 32M elements: 73 GB/s! 34

template <unsigned int blocksize> device void warpreduce(volatile int *sdata, unsigned int tid) { if (blocksize >= 64) sdata[tid] += sdata[tid + 32]; if (blocksize >= 32) sdata[tid] += sdata[tid + 16]; if (blocksize >= 16) sdata[tid] += sdata[tid + 8]; if (blocksize >= 8) sdata[tid] += sdata[tid + 4]; if (blocksize >= 4) sdata[tid] += sdata[tid + 2]; if (blocksize >= 2) sdata[tid] += sdata[tid + 1]; template <unsigned int blocksize> global void reduce6(int *g_idata, int *g_odata, unsigned int n) { extern shared int sdata[]; unsigned int tid = threadidx.x; unsigned int i = blockidx.x*(blocksize*2) + tid; unsigned int gridsize = blocksize*2*griddim.x; sdata[tid] = 0; Final Optimized Kernel while (i < n) { sdata[tid] += g_idata[i] + g_idata[i+blocksize]; i += gridsize; syncthreads(); if (blocksize >= 512) { if (tid < 256) { sdata[tid] += sdata[tid + 256]; syncthreads(); if (blocksize >= 256) { if (tid < 128) { sdata[tid] += sdata[tid + 128]; syncthreads(); if (blocksize >= 128) { if (tid < 64) { sdata[tid] += sdata[tid + 64]; syncthreads(); if (tid < 32) warpreduce(sdata, tid); if (tid == 0) g_odata[blockidx.x] = sdata[0]; 35

Types of optimization Interesting observation: Algorithmic optimizations Changes to addressing, algorithm cascading 11.84x speedup, combined! Code optimizations Loop unrolling 2.54x speedup, combined 37