Advanced CUDA Optimizing to Get 20x Performance. Brent Oster

Similar documents
Advanced CUDA Optimizing to Get 20x Performance

L17: Lessons from Particle System Implementations

NVIDIA GTX200: TeraFLOPS Visual Computing. August 26, 2008 John Tynefield

CUDA Performance Optimization. Patrick Legresley

Fundamental CUDA Optimization. NVIDIA Corporation

Fundamental CUDA Optimization. NVIDIA Corporation

CUDA OPTIMIZATIONS ISC 2011 Tutorial

Portland State University ECE 588/688. Graphics Processors

Fundamental Optimizations in CUDA Peng Wang, Developer Technology, NVIDIA

Tesla Architecture, CUDA and Optimization Strategies

CS 179: GPU Computing LECTURE 4: GPU MEMORY SYSTEMS

Advanced CUDA Programming. Dr. Timo Stich

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

GPU Fundamentals Jeff Larkin November 14, 2016

Kernel optimizations Launch configuration Global memory throughput Shared memory access Instruction throughput / control flow

Parallel Programming Principle and Practice. Lecture 9 Introduction to GPGPUs and CUDA Programming Model

Fundamental Optimizations

CS GPU and GPGPU Programming Lecture 8+9: GPU Architecture 7+8. Markus Hadwiger, KAUST

Supercomputing, Tutorial S03 New Orleans, Nov 14, 2010

CUDA Performance Optimization Mark Harris NVIDIA Corporation

Computer Architecture

Outline Overview Hardware Memory Optimizations Execution Configuration Optimizations Instruction Optimizations Summary

GPGPU LAB. Case study: Finite-Difference Time- Domain Method on CUDA

B. Tech. Project Second Stage Report on

What is GPU? CS 590: High Performance Computing. GPU Architectures and CUDA Concepts/Terms

Parallel Computing: Parallel Architectures Jin, Hai

L17: Asynchronous Concurrent Execution, Open GL Rendering

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

Using GPUs to compute the multilevel summation of electrostatic forces

Analysis Report. Number of Multiprocessors 3 Multiprocessor Clock Rate Concurrent Kernel Max IPC 6 Threads per Warp 32 Global Memory Bandwidth

Threading Hardware in G80

3D ADI Method for Fluid Simulation on Multiple GPUs. Nikolai Sakharnykh, NVIDIA Nikolay Markovskiy, NVIDIA

GRAPHICS PROCESSING UNITS

Lecture 2: CUDA Programming

GPU Performance Optimisation. Alan Gray EPCC The University of Edinburgh

Performance optimization with CUDA Algoritmi e Calcolo Parallelo. Daniele Loiacono

EE382N (20): Computer Architecture - Parallelism and Locality Spring 2015 Lecture 09 GPUs (II) Mattan Erez. The University of Texas at Austin

General Purpose GPU Computing in Partial Wave Analysis

Improving Performance of Machine Learning Workloads

Chapter 04. Authors: John Hennessy & David Patterson. Copyright 2011, Elsevier Inc. All rights Reserved. 1

CS8803SC Software and Hardware Cooperative Computing GPGPU. Prof. Hyesoon Kim School of Computer Science Georgia Institute of Technology

CSE 160 Lecture 24. Graphical Processing Units

NVidia s GPU Microarchitectures. By Stephen Lucas and Gerald Kotas

High Performance Computing on GPUs using NVIDIA CUDA

CUDA Optimization: Memory Bandwidth Limited Kernels CUDA Webinar Tim C. Schroeder, HPC Developer Technology Engineer

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

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

S WHAT THE PROFILER IS TELLING YOU: OPTIMIZING GPU KERNELS. Jakob Progsch, Mathias Wagner GTC 2018

COMP 605: Introduction to Parallel Computing Lecture : GPU Architecture

GPU Profiling and Optimization. Scott Grauer-Gray

Programming in CUDA. Malik M Khan

CUDA Programming Model

CUDA OPTIMIZATION WITH NVIDIA NSIGHT VISUAL STUDIO EDITION

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

CS 179 Lecture 4. GPU Compute Architecture

Numerical Simulation on the GPU

CUDA Memories. Introduction 5/4/11

Advanced CUDA Optimizations. Umar Arshad ArrayFire

Optimizing CUDA NVIDIA Corporation 2009 U. Melbourne GPU Computing Workshop 27/5/2009 1

University of Bielefeld

Persistent RNNs. (stashing recurrent weights on-chip) Gregory Diamos. April 7, Baidu SVAIL

Multi-Processors and GPU

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

Massively Parallel Architectures


Double-Precision Matrix Multiply on CUDA

Lecture 15: Introduction to GPU programming. Lecture 15: Introduction to GPU programming p. 1

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

ME964 High Performance Computing for Engineering Applications

CE 431 Parallel Computer Architecture Spring Graphics Processor Units (GPU) Architecture

Introduction to GPGPU and GPU-architectures

Tesla GPU Computing A Revolution in High Performance Computing

State-of-the-art in Heterogeneous Computing

Lecture 5. Performance programming for stencil methods Vectorization Computing with GPUs

HiPANQ Overview of NVIDIA GPU Architecture and Introduction to CUDA/OpenCL Programming, and Parallelization of LDPC codes.

TUNING CUDA APPLICATIONS FOR MAXWELL

TUNING CUDA APPLICATIONS FOR MAXWELL

Spring Prof. Hyesoon Kim

CUDA OPTIMIZATION TIPS, TRICKS AND TECHNIQUES. Stephen Jones, GTC 2017

GPU Computation Strategies & Tricks. Ian Buck NVIDIA

GPGPUs in HPC. VILLE TIMONEN Åbo Akademi University CSC

GPU Basics. Introduction to GPU. S. Sundar and M. Panchatcharam. GPU Basics. S. Sundar & M. Panchatcharam. Super Computing GPU.

Introduction to Parallel Computing with CUDA. Oswald Haan

CUDA OPTIMIZATION WITH NVIDIA NSIGHT ECLIPSE EDITION

Fast Tridiagonal Solvers on GPU

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

Introduction to CUDA (1 of n*)

Parallelizing FPGA Technology Mapping using GPUs. Doris Chen Deshanand Singh Aug 31 st, 2010

Performance Optimization Part II: Locality, Communication, and Contention

Lecture 2: different memory and variable types

Warps and Reduction Algorithms

Sparse Linear Algebra in CUDA

Performance Insights on Executing Non-Graphics Applications on CUDA on the NVIDIA GeForce 8800 GTX

Mathematical computations with GPUs

Introduction to CUDA Algoritmi e Calcolo Parallelo. Daniele Loiacono

Mattan Erez. The University of Texas at Austin

Adaptive-Mesh-Refinement Hydrodynamic GPU Computation in Astrophysics

Analysis-driven Engineering of Comparison-based Sorting Algorithms on GPUs

Code Optimizations for High Performance GPU Computing

Exploiting graphical processing units for data-parallel scientific applications

Transcription:

Advanced CUDA Optimizing to Get 20x Performance Brent Oster

Outline Motivation for optimizing in CUDA Demo performance increases Tesla 10-series architecture details Optimization case studies Particle Simulation Finite Difference Summary NVIDIA Corporation 2008 2

Motivation for Optimization 20-50X performance over CPU-based code Tesla 10-series chip has 1 TeraFLOPs compute A Tesla workstation can outperform a CPU cluster Demos Particle Simulation Finite Difference Molecular Dynamics Need to optimize code to get performance Not too hard 3 main rules NVIDIA Corporation 2008 3

Tesla 10-series Architecture

Tesla 10-Series Architecture Massively parallel general computing architecture 30 Streaming multiprocessors @ 1.45 GHz with 4.0 GB of RAM 1 TFLOPS single precision (IEEE 754 floating point) 87 GFLOPS double precision NVIDIA Corporation 2008

10-Series Streaming Multiprocessor 8 SP Thread Processors IEEE 754 32-bit floating point 32-bit float and 64-bit integer 16K 32-bit registers 2 SFU Special Function Units 1 Double Precision Unit (DP) IEEE 754 64-bit floating point Fused multiply-add Scalar register-based ISA Multithreaded Instruction Unit 1024 threads, hardware multithreaded Independent thread execution Hardware thread scheduling 16KB Shared Memory Concurrent threads share data Low latency load/store NVIDIA Corporation 2008

10-series DP 64-bit IEEE floating point IEEE 754 64-bit results for all DP instructions DADD, DMUL, DFMA, DtoF, FtoD, DtoI, ItoD, DMAX, DMIN Rounding, denorms, NaNs, +/- Infinity Fused multiply-add (DFMA) D = A*B + C; with no loss of precision in the add DDIV and DSQRT software use FMA-based convergence IEEE 754 rounding: nearest even, zero, +inf, -inf Full-speed denormalized operands and results No exception flags Peak DP (DFMA) performance 87 GFLOPS at 1.45 GHz Applications will almost always be bandwidth limited before limited by double precision compute performance? NVIDIA Corporation 2008

Optimizing CUDA Applications For 10-series Architecture

General Rules for Optimization Optimize memory transfers Minimize memory transfers from host to device Use shared memory as a cache to device memory Take advantage of coalesced memory access Maximize processor occupancy Optimize execution configuration Maximize arithmetic intensity More computation per memory access Re-compute instead of loading data NVIDIA Corporation 2008 9

Data Movement in a CUDA Program Host Memory Device Memory [Shared Memory] COMPUTATION [Shared Memory] Device Memory Host Memory NVIDIA Corporation 2008 10

Particle Simulation Example Newtonian mechanics on point masses: struct particlestruct{ float3 pos; float3 vel; float3 force; }; pos = pos + vel*dt vel = vel + force/mass*dt NVIDIA Corporation 2008 11

Particle Simulation Applications Film Special Effects Game Effects Monte-Carlo Transport Simulation Fluid Dynamics Plasma Simulations NVIDIA Corporation 2008 12

1 million non-interacting particles Radial (inward) and Vortex (tangent) force per particle NVIDIA Corporation 2008 13

Expected Performance 1 Million Particles Pos, Vel = 36 bytes per particle = 36MB total Host to device transfer (PCI-e Gen2) 2 * 36MB / 5.2 GB/s -> 13.8 ms Device memory access 2 * 36MB / 80 GB/s -> 0.9 ms 1 TFLOPS / 1 million particles Compute Euler Integration -> 0.02ms NVIDIA Corporation 2008 14

Visual Profiler NVIDIA Corporation 2008 15

Measured Performance Host to device transfer (PCI-e Gen2) 15.3 ms (one-way) Integration Kernel (including device memory access) 1.32 ms NVIDIA Corporation 2008 16

Host to Device Memory Transfer Host Memory Device Memory Shared Memory COMPUTATION Shared Memory Device Memory Host Memory NVIDIA Corporation 2008 17

Host to Device Memory Transfer cudamemcpy(dst, src, nbytes, direction) Can only go as fast as the PCI-e bus Use page-locked host memory Instead of malloc( ), use cudamallochost( ) Prevents OS from paging host memory Allows PCI-e DMA to run at full speed Use asynchronous data transfers Requires page-locked host memory Copy all data to device memory only once Do all computation locally on T10 card NVIDIA Corporation 2008 18

Asynchronous Data Transfers Use asynchronous data transfers Requires page-locked host memory cudastreamcreate(&stream1); cudastreamcreate(&stream2); cudamemcpyasync(dst1, src1, size, dir, stream1); kernel<<<grid, block, 0, stream1>>>( ); cudamemcpyasync(dst2, src2, size, dir, stream2); kernel<<<grid, block, 0, stream2>>>( ); NVIDIA Corporation 2008 19

OpenGL Interoperability Rendering directly from device memory OpenGL buffer objects can be mapped into the CUDA address space and then used as global memory Vertex buffer objects Pixel buffer objects Allows direct visualization of data from computation No device to host transfer with Quadro or GeForce Data stays in device memory very fast compute / viz Automatic DMA from Tesla to Quadro (via host for now) Data can be accessed from the kernel like any other global data (in device memory) NVIDIA Corporation 2008

Graphics Interoperability Register a buffer object with CUDA cudaglregisterbufferobject(gluint buffobj); OpenGL can use a registered buffer only as a source Unregister the buffer prior to rendering to it by OpenGL Map the buffer object to CUDA memory cudaglmapbufferobject(void **devptr, GLuint buffobj); Returns an address in global memory Buffer must be registered prior to mapping Launch a CUDA kernel to process the buffer Unmap the buffer object prior to use by OpenGL cudaglunmapbufferobject(gluint buffobj); Unregister the buffer object cudaglunregisterbufferobject(gluint buffobj); Optional: needed if the buffer is a render target Use the buffer object in OpenGL code NVIDIA Corporation 2008 21

Moving Data to/from Device Memory Host Memory Device Memory Shared Memory COMPUTATION Shared Memory Device Memory Host Memory NVIDIA Corporation 2008 22

Device and Shared Memory Access SM s can access device memory at 80 GB/s But, with hundreds of cycles of latency! Pipelined execution hides latency Each SM has 16KB of shared memory Essentially a user managed cache Latency comparable to registers Reduces load/stores to device memory Threads cooperatively use shared memory Best case multiple memory access per thread, maximum use of shared memory NVIDIA Corporation 2008

Parallel Memory Sharing Thread Block Registers Shared Memory Registers: per-thread Private per thread Auto variables, register spill Shared Memory: per-block Shared by threads of block Inter-thread communication Device Memory: per-application Shared by all threads Inter-Grid communication Grid 0 Grid 1... Device/Global Memory Sequential Grids in Time... NVIDIA Corporation 2008

Shared memory as a cache P[idx].pos = P[idx].pos + P[idx].vel * dt; P[idx].vel = P[idx].vel + P[idx].force / mass; Data is accessed directly from device memory in this usage case.vel is accessed twice (6 float accesses) Hundreds of cycles of latency each time Make use of shared memory? NVIDIA Corporation 2008 25

Shared memory as a cache shared float3 s_pos[n_threads]; shared float3 s_vel[n_threads]; shared float3 s_force[n_threads]; int tx = threadidx.x; idx = threadidx.x + blockidx.x*blockdim.x; s_pos[tx] = P[idx].pos; s_vel[tx] = P[idx].vel; s_force[tx] = P[idx].force; s_pos[tx] = s_pos[tx] + s_vel[tx] * dt; s_vel[tx] = s_vel[tx] + s_force[tx]/mass * dt ; P[idx].pos = s_pos[tx]; P[idx].vel = s_vel[tx]; NVIDIA Corporation 2008 26

Coalesced Device Memory Access on 10-series architecture When half warp (16 threads) accesses contiguous region of device memory 16 data elements loaded in one instruction int, float: 64 bytes (fastest) int2, float2: 128 bytes int4, float4: 256 bytes (2 transactions) Regions aligned to multiple of size If un-coalesced, issues 16 sequential loads NVIDIA Corporation 2008 27

Address 120 Address 120 Address 96 Address 124 Address 124 Address 100 Thread 0 Thread 1 Thread 2 Thread 3 Address 128 Address 132 Address 136 Address 140 Thread 0 Thread 1 Thread 2 Thread 3 Address 128 Address 132 Address 136 Address 140 Address 104 Address 108 Address 112 Address 116 32B segment Thread 4 Address 144 Thread 4 Address 144 Address 120 Thread 5 Address 148 Thread 5 Address 148 Address 124 Thread 6 Thread 7 Thread 8 Thread 9 Address 152 Address 156 Address 160 Address 164 64B segment Thread 6 Thread 7 Thread 8 Thread 9 Address 152 Address 156 Address 160 Address 164 Thread 0 Thread 1 Thread 2 Thread 3 Address 128 Address 132 Address 136 Address 140 Thread 10 Thread 11 Thread 12 Thread 13 Thread 14 Thread 15 Address 168 Address 172 Address 176 Address 180 Address 184 Address 188 Thread 10 Thread 11 Thread 12 Thread 13 Thread 14 Thread 15 Address 168 Address 172 Address 176 Address 180 Address 184 Address 188 128B segment Thread 4 Thread 5 Thread 6 Thread 7 Thread 8 Thread 9 Address 144 Address 148 Address 152 Address 156 Address 160 Address 164 64B segment Address 192 Address 192 Thread 10 Address 168 Address 196 Address 196 Thread 11 Address 172 Address 200 Address 200 Thread 12 Address 176 Address 204 Address 204 Thread 13 Address 180 Address 208 Address 212 Address 214... Thread 14 Thread 15 Address 184 Address 188 Address 192 Address 218 Address 252 Address 196 Address 222 Address 256 Address 200 NVIDIA Corporation 2008

Particle Simulation Example Worst Case for Coalescing! struct particlestruct{ float3 pos; float3 vel; float3 force; }; Thread 0 1 2 3 15 Load pos.x 0 36 72 108 540 Load pos.y 4 40 76 112 544 Load pos.z 8 44 80 118 548 NVIDIA Corporation 2008 29

Coalesced Memory Access Use structure of arrays instead float3 pos[nparticles] float3 vel[nparticles] float3 force[nparticles] Accesses coalesced within a few segments Thread 0 1 2 3 15 Load pos[idx].x 0 12 24 36 180 Load pos[idx].y 4 16 28 40 184 Load pos[idx].z 8 20 32 44 188 Only using 1/3 bandwidth - Not ideal NVIDIA Corporation 2008 30

Better Coalesced Access Option 1 Structure of Arrays Have separate arrays for pos.x, pos.y, float posx[nparticles]; float posy[nparticles]; float posz[nparticles]; Thread 0 1 2 3 15 Load posx[idx] 0 4 8 12 60 Load posy[idx] 64 68 72 76 124 Load posz[idx] 128 132 136 140 188 All threads of warp within 64byte region 2x NVIDIA Corporation 2008 31

Better Coalesced Access Option 2 - Typecasting Load as array of floats (3x size), then typecast to array of float3 for convenience float fdata[16*3] Thread 0 1 2 3 15 Load fdata[i+0] 0 4 8 12 60 Load fdata[i+16] 64 68 72 76 124 Load fdata[i+32] 128 132 136 140 188 float3* pos = (float3*)&fdata NVIDIA Corporation 2008 32

Shared Memory and Computation Host Memory Device Memory Shared Memory COMPUTATION Shared Memory Device Memory Host Memory NVIDIA Corporation 2008 33

Details of Shared Memory Many threads accessing memory Therefore, memory is divided into banks Essential to achieve high bandwidth Each bank can service one address per cycle A memory can service as many simultaneous accesses as it has banks Bank 0 Bank 1 Bank 2 Bank 3 Bank 4 Bank 5 Bank 6 Bank 7 Multiple simultaneous accesses to a bank result in a bank conflict Conflicting accesses are serialized Bank 15 NVIDIA Corporation 2008 34

Bank Addressing Examples No Bank Conflicts Linear addressing stride == 1 No Bank Conflicts Random 1:1 Permutation Thread 0 Thread 1 Thread 2 Thread 3 Thread 4 Thread 5 Thread 6 Thread 7 Bank 0 Bank 1 Bank 2 Bank 3 Bank 4 Bank 5 Bank 6 Bank 7 Thread 0 Thread 1 Thread 2 Thread 3 Thread 4 Thread 5 Thread 6 Thread 7 Bank 0 Bank 1 Bank 2 Bank 3 Bank 4 Bank 5 Bank 6 Bank 7 Thread 15 Bank 15 Thread 15 Bank 15 NVIDIA Corporation 2008 35

Bank Addressing Examples 2-way Bank Conflicts Thread 0 Thread 1 Thread 2 Thread 3 Thread 4 Thread 8 Thread 9 Thread 10 Thread 11 Linear addressing stride == 2 Bank 0 Bank 1 Bank 2 Bank 3 Bank 4 Bank 5 Bank 6 Bank 7 Bank 15 8-way Bank Conflicts Thread 0 Thread 1 Thread 2 Thread 3 Thread 4 Thread 5 Thread 6 Thread 7 Thread 15 Linear addressing stride == 8 x8 x8 Bank 0 Bank 1 Bank 2 Bank 7 Bank 8 Bank 9 Bank 15 NVIDIA Corporation 2008 36

Shared memory bank conflicts Shared memory access is comparable to registers if there are no bank conflicts Use the visual profiler to check for conflicts warp_serialize signal can usually be used to check for conflicts The fast case: If all threads of a half-warp access different banks, there is no bank conflict If all threads of a half-warp read the identical address, there is no bank conflict (broadcast) The slow case: Bank Conflict: multiple threads in the same half-warp access the same bank Must serialize the accesses Cost = max # of simultaneous accesses to a single bank NVIDIA Corporation 2008 37

Shared Memory Access - Particles Arrays of float3 in shared memory float3 s_pos[n_threads] Do any threads of a half-warp access same bank? Thread 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 s_pos.x 0 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 bank 0 3 6 9 12 15 2 5 8 11 14 1 4 7 10 13 No bank conflicts Always true when stride is a prime of 16 NVIDIA Corporation 2008 38

Optimizing Computation Execution Model Details SIMT Multithread Execution Register and Shared Memory Usage Optimizing for Execution Model 10-series Architecture Details Single and Double Precision Floating Point Optimizing Instruction Throughput NVIDIA Corporation 2008 39

NVIDIA Parallel Execution Model Thread: Runs a kernel program and performs the computation for 1 data item. Thread Index is a built-in variable Has a set of registers containing it s program context NVIDIA Corporation 2008

NVIDIA multi-tier data parallel model Warp: 32 Threads executed together Processed in SIMT on SM All threads execute all branches Half Warp: 16 Threads Coordinated memory access Can coalesce load/stores in batches of 16 elements NVIDIA Corporation 2008

NVIDIA multi-tier data parallel model Thread Warp = 32 Threads Block of Threads............ Block: 1 or more warps running on the same SM Different warps can take different branches Can synchronize all warps within a block Have common shared memory for extremely fast data sharing NVIDIA Corporation 2008

SIMT Multithreaded Execution Single-Instruction Multi-Thread instruction scheduler time warp 8 instruction 11 warp 1 instruction 42 warp 3 instruction 95. warp 8 instruction 12 warp 3 instruction 96 SIMT: Single-Instruction Multi-Thread Warp: the set of 32 parallel threads that execute a SIMT instruction Hardware implements zero-overhead warp and thread scheduling Deeply pipelined to hide memory and instruction latency SIMT warp diverges and converges when threads branch independently Best efficiency and performance when threads of a warp execute together NVIDIA Corporation 2008

Register and Shared Memory Usage Registers Each block has access to a set of registers on the SM 8-series has 8192 32-bit registers 10-series has 16384 32-bit registers Registers are partitioned among threads Total threads * registers/thread should be < number registers Shared Memory NVIDIA Corporation 2008 16KB of shared memory on SM If blocks use <8KB, multiple blocks may run on one SM Warps from multiple blocks

Optimizing Execution Configuration Use maximum number of threads per block Should be multiple of warp size (32) More warps per block, deeper pipeline Hides latency, gives better processor occupancy Limited by available registers Maximize concurrent blocks on SM Use less than 8KB shared memory per block Allows more than one block to run on an SM Can be a tradeoff for shared memory usage NVIDIA Corporation 2008 45

Maximize Arithmetic Intensity Particle simulation is still memory bound How much more computation can we do? Answer is almost unbelievable 100x! DEMO: 500+ GFLOPS! Can use a higher-order integrator? More complex computationally Can take much larger time-steps Computation vs memory access is worth it! NVIDIA Corporation 2008 46

1M particles x 100 fields Executes in 8ms on GTX280 NVIDIA Corporation 2008 47

1M particles x 100 collision spheres executes in 20ms on GTX280 NVIDIA Corporation 2008 48

Particle Simulation Optimization Summary Page-lock host memory Asynchronous host-device transfer Data stays in device memory Using shared memory vs. registers Coalesced data access Optimize execution configuration Higher arithmetic intensity NVIDIA Corporation 2008

Finite Differences Example Solving Poisson equation in 2D on fixed grid u = f u = u(x,y) f = f(x,y) Gauss-Seidel relaxation 5 point stencil NVIDIA Corporation 2008 50

Usual Method Solve sparse matrix problem: A*u = -f (use f so A is pos-def) 4-1 0-1 0 0 0-1 4-1 0-1 0 0 0-1 4-1 0-1 0 u = -f 0 0 0-1 0-1 4 NVIDIA Corporation 2008 51

Bottlenecked by Memory Throughput Matrix is N*N, where N is N x *N y Even a sparse representation is N*M u and f are of size N Memory throughput = N * (M + 2) per frame For a 1024x1024 grid, N = 1 million For a 2 nd order stencil, M = 5 For double precision: 1M * 8 * (5+2) = 56MB Host to device memory transfer takes 10.7ms Device memory load/store time 0.7ms? NVIDIA Corporation 2008 52

Improving Performance Transfer data host to device once at start 56MB easily fits on a 10-series card Iterate to convergence in device memory Use shared memory to buffer u 4x duplicated accesses per block Use constant memory for stencil? (no matrix) Use texture memory for ρ? (read-only) NVIDIA Corporation 2008 53

Using Shared Memory Finite Difference Example Load sub-blocks into shared memory 16x16 = 256 threads 16x16x8 = 2048 KB shared memory Each thread loads one double Need to synchronize block boundaries Only compute stencil on 14x14 center of cell Load ghost cells on edges Overlap onto neighbor blocks Only 2/3 of threads computing? NVIDIA Corporation 2008 54

512x512 grid, Gauss-Seidel Executes in 0.23ms on GTX280 NVIDIA Corporation 2008 55

Constant Memory Special section of device memory Read only Cached Whole warp, same address - one load Additional load for each different address Constant memory declared at file scope Set by cudamemcpytosymbol( ) NVIDIA Corporation 2008 56

Using Constant Memory Finite Difference Example Declare the stencil as constant memory constant double stencil[5] ={4,-1,-1,-1,-1}; NVIDIA Corporation 2008 57

Texture Memory Special section of device memory Read only Cached by spatial location (1D, 2D, 3D) Best performance All threads of a warp hit same cache locale High spatial coherency in algorithm Useful when coalescing methods are impractical NVIDIA Corporation 2008 58

Using Texture Memory Finite Difference Example Declare a texture ref texture<float, 1, > ftex; Bind f to texture ref via an array cudamallocarray(farray, ) cudamemcpy2dtoarray(farray, f, ); cudabindtexturetoarray(ftex, farray ); Access with array texture functions f[x,y] = tex2d(ftex, x,y); NVIDIA Corporation 2008 59

Finite Difference Performance Improvement Maximize execution configuration 256 threads, each loads one double 16 registers * 256 threads = 4096 registers Ok for both 10-series, 8-series Maximize arithmetic intensity for 3D 27-point, 4 th order stencil Same memory bandwidth More compute Can use fewer grid points Faster convergence NVIDIA Corporation 2008 60

General Rules for Optimization Recap Optimize memory transfers Minimize memory transfers from host to device Use shared memory as a cache to device memory Take advantage of coalesced memory access Maximize processor occupancy Use appropriate numbers of threads and blocks Maximize arithmetic intensity More computation per memory access Re-compute instead of loading data NVIDIA Corporation 2008 61