GPU Computation CSCI 4239/5239 Advanced Computer Graphics Spring 2014

Similar documents
General Purpose GPU programming (GP-GPU) with Nvidia CUDA. Libby Shoop

CIS 665: GPU Programming. Lecture 2: The CUDA Programming Model

Intro to GPU s for Parallel Computing

GPGPU. Lecture 2: CUDA

Introduction to CUDA Algoritmi e Calcolo Parallelo. Daniele Loiacono

Lecture 1: Introduction

Using The CUDA Programming Model

Parallel Computing. Lecture 19: CUDA - I

Introduction to CUDA Algoritmi e Calcolo Parallelo. Daniele Loiacono

Lecture 3: Introduction to CUDA

Threading Hardware in G80

CSE 591: GPU Programming. Programmer Interface. Klaus Mueller. Computer Science Department Stony Brook University

Core/Many-Core Architectures and Programming. Prof. Huiyang Zhou

GPU Programming. Lecture 2: CUDA C Basics. Miaoqing Huang University of Arkansas 1 / 34

This is a draft chapter from an upcoming CUDA textbook by David Kirk from NVIDIA and Prof. Wen-mei Hwu from UIUC.

Introduction to CUDA (1 of n*)

Lecture 2: Introduction to CUDA C

CS 677: Parallel Programming for Many-core Processors Lecture 1

Module 2: Introduction to CUDA C

Computation to Core Mapping Lessons learned from a simple application

Lessons learned from a simple application

Many-core Processors Lecture 1. Instructor: Philippos Mordohai Webpage:

Matrix Multiplication in CUDA. A case study

Introduction to CUDA (1 of n*)

EE382N (20): Computer Architecture - Parallelism and Locality Fall 2011 Lecture 22 CUDA

CUDA (Compute Unified Device Architecture)

Module 2: Introduction to CUDA C. Objective

GPU Architecture and Programming. Ozcan Ozturk Bilkent University Ankara, Turkey

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

CUDA Programming Model

Massively Parallel Architectures

COMP 322: Fundamentals of Parallel Programming. Flynn s Taxonomy for Parallel Computers

Introduction to CELL B.E. and GPU Programming. Agenda

CUDA C Programming Mark Harris NVIDIA Corporation

Lecture 11: GPU programming

HPC COMPUTING WITH CUDA AND TESLA HARDWARE. Timothy Lanfear, NVIDIA

Lecture 1: Introduction and Computational Thinking

Introduction to GPGPUs and to CUDA programming model

High Performance Computing

ECE 574 Cluster Computing Lecture 15

Introduction to GPU programming. Introduction to GPU programming p. 1/17

Mattan Erez. The University of Texas at Austin

An Introduction to GPGPU Pro g ra m m ing - CUDA Arc hitec ture

Outline 2011/10/8. Memory Management. Kernels. Matrix multiplication. CIS 565 Fall 2011 Qing Sun

GPGPU/CUDA/C Workshop 2012

Tesla Architecture, CUDA and Optimization Strategies

Nvidia G80 Architecture and CUDA Programming

Data Parallel Execution Model

CS179 GPU Programming Introduction to CUDA. Lecture originally by Luke Durant and Tamas Szalay

CUDA PROGRAMMING MODEL. Carlo Nardone Sr. Solution Architect, NVIDIA EMEA

Josef Pelikán, Jan Horáček CGG MFF UK Praha

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

ECE 574 Cluster Computing Lecture 17

An Example of Physical Reality Behind CUDA. GeForce Speedup of Applications Why Massively Parallel Processor.

Introduction to GPU (Graphics Processing Unit) Architecture & Programming

Overview: Graphics Processing Units

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

ECE/CS 757: Advanced Computer Architecture II GPGPUs

Introduction to GPU Computing Using CUDA. Spring 2014 Westgid Seminar Series

High Performance Linear Algebra on Data Parallel Co-Processors I

Technische Universität München. GPU Programming. Rüdiger Westermann Chair for Computer Graphics & Visualization. Faculty of Informatics

Introduction to Numerical General Purpose GPU Computing with NVIDIA CUDA. Part 1: Hardware design and programming model

CPUs+GPUs. PCIe: 1GB/s per lane x16 => 16GB/s full duplex. DMI2 20 Gb/s 40x PCIe C L1 L2. 40x PCIe. 2 QPI links 2x 19.2GB/s Full Duplex.

Lecture 9. Outline. CUDA : a General-Purpose Parallel Computing Architecture. CUDA Device and Threads CUDA. CUDA Architecture CUDA (I)

Introduction to GPU Computing Using CUDA. Spring 2014 Westgid Seminar Series

GPU Programming Using CUDA

Programming in CUDA. Malik M Khan

Introduction to CUDA

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

CUDA programming model. N. Cardoso & P. Bicudo. Física Computacional (FC5)

L18: Introduction to CUDA

Paralization on GPU using CUDA An Introduction

Introduction to CUDA (2 of 2)

Basic Elements of CUDA Algoritmi e Calcolo Parallelo. Daniele Loiacono

Tesla GPU Computing A Revolution in High Performance Computing

Portland State University ECE 588/688. Graphics Processors

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

Introduc)on to GPU Programming

Introduction to CUDA CME343 / ME May James Balfour [ NVIDIA Research

CUDA Architecture & Programming Model

CUDA PROGRAMMING MODEL Chaithanya Gadiyam Swapnil S Jadhav

Massively Parallel Computing with CUDA. Carlos Alberto Martínez Angeles Cinvestav-IPN

GPUs and GPGPUs. Greg Blanton John T. Lubia

Master Informatics Eng.

Current Trends in Computer Graphics Hardware

Introduction to CUDA Programming

CS/EE 217 GPU Architecture and Parallel Programming. Lecture 22: Introduction to OpenCL

Module 3: CUDA Execution Model -I. Objective

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

GPU & High Performance Computing (by NVIDIA) CUDA. Compute Unified Device Architecture Florian Schornbaum

COMP 322: Fundamentals of Parallel Programming

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

This is a draft chapter from an upcoming CUDA textbook by David Kirk from NVIDIA and Prof. Wen-mei Hwu from UIUC.

Basic Elements of CUDA Algoritmi e Calcolo Parallelo. Daniele Loiacono

Introduction to CUDA

Blockseminar: Verteiltes Rechnen und Parallelprogrammierung. Tim Conrad

High Performance Computing on GPUs using NVIDIA CUDA

CSE 591: GPU Programming. Introduction. Entertainment Graphics: Virtual Realism for the Masses. Computer games need to have: Klaus Mueller

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

COMP 605: Introduction to Parallel Computing Lecture : GPU Architecture

Transcription:

GPU Computation CSCI 4239/5239 Advanced Computer Graphics Spring 2014

Solutions to Parallel Processing Message Passing (distributed) MPI (library) Threads (shared memory) pthreads (library) OpenMP (compiler) GPU Programming (shared bus) CUDA (compiler) OpenCL (library) OpenACC (compiler) 2

Using the GPU for Computation The GPU is very good at floating point. How can we use that to do computations? Write a shader and be the result be a pseudo-color Use CUDA with nvidia hardware Use OpenCL with general hardware Use an OpenGL 4.3 Compute Shader Issues Getting instructions and data to the GPU Precision of computations 3

Text/Notes Programming massively Parallel Processors Kirk and Hwu Good introduction to CUDA and OpenCL Examples, tips and Tricks Most slides taken from their lecture notes CUDA by Example Sanders and Kandrot CUDA only Examples 4

History of Coprocessors Floating point option 8087, 80287, Weitek Floating Point Systems Array Processors Attaches to VAX D chips Analog and special purpose CPUs Graphics Processors

Why Massively Parallel Processor A quiet revolution and potential build-up Calculation: 367 GFLOPS vs. 32 GFLOPS Memory Bandwidth: 86.4 GB/s vs. 8.4 GB/s Until 2006, programmed through graphics API GPU in every PC and workstation massive volume and potential impact 6

CPUs and GPUs have fundamentally different design philosophies ALU ALU ALU ALU Control CPU GPU Cache DRAM DRAM 7

Architecture of a CUDA-capable GPU Host Input Assembler Thread Execution Manager Parallel Data Cache Parallel Data Cache Parallel Data Cache Parallel Data Cache Parallel Data Cache Parallel Data Cache Parallel Data Cache Parallel Data Cache Texture Texture Texture Texture Texture Texture Texture Texture Texture Load/store Load/store Load/store Load/store Load/store Load/store Global Memory 8

GT200 Characteristics 1 TFLOPS peak performance (25-50 times of current high-end microprocessors) 265 GFLOPS sustained for apps such as VMD Massively parallel, 128 cores, 90W Massively threaded, sustains 1000s of threads per app 30-100 times speedup over high-end microprocessors on scientific and media applications: medical imaging, molecular dynamics I think they're right on the money, but the huge performance differential (currently 3 GPUs ~= 300 SGI Altix Itanium2s) will invite close scrutiny so I have to be careful what I say publically until I triple check those numbers. -John Stone, VMD group, Physics UIUC 9

CPU Host Host Interface Vertex Control GPU Vertex Cache VS/T&L A Fixed Function GPU Pipeline Triangle Setup Raster Shader ROP FBI Texture Cache Frame Buffer Memory 10

Unified Graphics Pipeline Host Data Assembler Setup / Rstr / ZCull TF TF TF L1 L1 TF L1 L1 TF L1 L2 FB TF L1 L2 FB Pixel Thread Issue TF L2 FB TF L1 L2 FB Geom Thread Issue L1 L2 FB Thread Processor Vtx Thread Issue L2 FB 11

What is (Historical) GPGPU? General Purpose computation using GPU and graphics API in applications other than 3D graphics Data parallel algorithms leverage GPU attributes GPU accelerates critical path of application Large data arrays, streaming throughput Fine-grain SIMD parallelism Low-latency floating point (FP) computation Applications see GPGPU.org Game effects (FX) physics, image processing Physical modeling, computational engineering, matrix algebra, convolution, correlation, sorting 12

Previous GPGPU Constraints Dealing with graphics API Addressing modes per thread per Shader per Context Fragment Program Texture Limited texture size/dimension Constants Temp Registers Limited outputs Instruction sets Input Registers Shader capabilities Working with the corner cases of the graphics API Lack of Integer & bit ops Output Registers FB Memory Communication limited Between pixels Scatter a[i] = p 13

CUDA Compute Unified Device Architecture General purpose programming model Targeted software stack User kicks off batches of threads on the GPU GPU = dedicated super-threaded, massively data parallel coprocessor Compute oriented drivers, language, and tools Driver for loading computation programs into GPU Standalone Driver - Optimized for computation Interface designed for compute graphics-free API Data sharing with OpenGL buffer objects Guaranteed maximum download & readback speeds Explicit GPU memory management 14

An Example of Physical Reality Behind CUDA CPU (host) GPU w/ local DRAM (device) 15

Parallel Computing on a GPU 8-series GPUs deliver 25 to 200+ GFLOPS on compiled parallel C applications Available in laptops, desktops, and clusters GeForce 8800 GPU parallelism is doubling every year Programming model scales transparently Programmable in C with CUDA tools Multithreaded MD model uses application data parallelism and thread parallelism Tesla D870 16 Tesla S870

Overview CUDA programming model basic concepts and data types CUDA application programming interface - basic Simple examples to illustrate basic concepts and functionalities Performance features will be covered later 17

CUDA C with no shader limitations! Integrated host+device app C program Serial or modestly parallel parts in host C code Highly parallel parts in device MD kernel C code Serial Code (host) Parallel Kernel (device) KernelA<<< nblk, ntid >>>(args);... Serial Code (host) Parallel Kernel (device) KernelB<<< nblk, ntid >>>(args);... 18

CUDA Devices and Threads A compute device Is a coprocessor to the CPU or host Has its own DRAM (device memory) Runs many threads in parallel Is typically a GPU but can also be another type of parallel processing device Data-parallel portions of an application are expressed as device kernels which run on many threads Differences between GPU and CPU threads GPU threads are extremely lightweight Very little creation overhead GPU needs 1000s of threads for full efficiency Multi-core CPU needs only a few 19

G80 Graphics Mode The future of GPUs is programmable processing So build the architecture around the processor Host Input Assembler Setup / Rstr / ZCull TF TF L1 TF L1 TF L1 L1 TF L1 L2 FB TF L1 L2 FB Pixel Thread Issue TF L2 FB TF L1 L2 FB Geom Thread Issue L1 L2 FB Thread Processor Vtx Thread Issue L2 FB 20

G80 CUDA mode A Device Example Processors execute computing threads New operating mode/hw interface for computing Host Input Assembler Thread Execution Manager Parallel Data Cache Parallel Data Cache Parallel Data Cache Parallel Data Cache Parallel Data Cache Parallel Data Cache Parallel Data Cache Parallel Data Cache Texture Texture Texture Texture Texture Texture Texture Texture Texture Load/store Load/store Load/store Load/store Global Memory Load/store Load/store 21

Extended C Declspecs device float filter[n]; global void convolve (float *image) region[threadidx] = image[i]; threadidx, blockidx syncthreads()... Intrinsics syncthreads } Runtime API Memory, symbol, execution management { shared float region[m];... Keywords global, device, shared, local, constant image[j] = result; // Allocate GPU memory void *myimage = cudamalloc(bytes) // 100 blocks, 10 threads per block convolve<<<100, 10>>> (myimage); Function launch 22

Extended C Integrated source (foo.cu) cudacc EDG C/C++ frontend Open64 Global Optimizer GPU Assembly CPU Host Code foo.s foo.cpp OCG gcc / cl G80 SASS foo.sass Mark Murphy, NVIDIA s Experience with Open64, www.capsl.udel.edu/conferences/open64/2008/ Papers/101.doc 23

Arrays of Parallel Threads A CUDA kernel is executed by an array of threads All threads run the same code (MD) Each thread has an ID that it uses to compute memory addresses and make control decisions threadid 0 1 2 3 4 5 6 7 float x = input[threadid]; float y = func(x); output[threadid] = y; 24

Thread Blocks: Scalable Cooperation Divide monolithic thread array into multiple blocks Threads within a block cooperate via shared memory, atomic operations and barrier synchronization Threads in different blocks cannot cooperate Thread Block 1 Thread Block 0 threadid 0 1 2 3 4 5 6 7 float x = input[threadid]; float y = func(x); output[threadid] = y; 0 1 2 3 4 5 6 Thread Block N - 1 7 float x = input[threadid]; float y = func(x); output[threadid] = y; 0 1 2 3 4 5 6 7 float x = input[threadid]; float y = func(x); output[threadid] = y; 25

Block IDs and Thread IDs Each thread uses IDs to decide what data to work on Block ID: 1D or 2D Thread ID: 1D, 2D, or 3D Simplifies memory addressing when processing multidimensional data Image processing Solving PDEs on volumes Host Device Grid 1 Kernel 1 Block (0, 0) Block (1, 0) Block (0, 1) Block (1, 1) Grid 2 Kernel 2 Block (1, 1) (0,0,1) (1,0,1) (2,0,1) (3,0,1) Thread Thread Thread Thread (0,0,0) (1,0,0) (2,0,0) (3,0,0) Thread Thread Thread Thread (0,1,0) (1,1,0) (2,1,0) (3,1,0) Courtesy: NDVIA 26 Figure 3.2. An Example of CUDA Thread Org

CUDA Memory Model Overview Global memory Main means of communicating R/W Data between host and device Contents visible to all threads Long latency access We will focus on global memory for now Constant and texture memory will come later Grid Block (0, 0) Block (1, 0) Shared Memory Host Shared Memory Registers Registers Registers Registers Thread (0, 0) Thread (1, 0) Thread (0, 0) Thread (1, 0) Global Memory 27

CUDA API Highlights: Easy and Lightweight The API is an extension to the ANSI C programming language Low learning curve The hardware is designed to enable lightweight runtime and driver High performance 28

CUDA Device Memory Allocation cudamalloc() Grid Allocates object in the device Global Memory Requires two parameters Address of a pointer to the allocated object Size of of allocated object cudafree() Host Block (0, 0) Block (1, 0) Shared Memory Shared Memory Registers Registers Registers Registers Thread (0, 0) Thread (1, 0) Thread (0, 0) Thread (1, 0) Global Memory Frees object from device Global Memory Pointer to freed object 29

CUDA Device Memory Allocation (cont.) Code example: Allocate a 64 * 64 single precision float array Attach the allocated storage to Md d is often used to indicate a device data structure TILE_WIDTH = 64; Float* Md int size = TILE_WIDTH * TILE_WIDTH * sizeof(float); cudamalloc((void**)&md, size); cudafree(md); 30

CUDA Host-Device Data Transfer cudamemcpy() memory data transfer Requires four parameters Pointer to destination Pointer to source Number of bytes copied Type of transfer Host to Host Host to Device Device to Host Device to Device Host Grid Block (0, 0) Block (1, 0) Shared Memory Shared Memory Registers Registers Registers Registers Thread (0, 0) Thread (1, 0) Thread (0, 0) Thread (1, 0) Global Memory Asynchronous transfer 31

CUDA Host-Device Data Transfer (cont.) Code example: Transfer a 64 * 64 single precision float array M is in host memory and Md is in device memory cudamemcpyhosttodevice and cudamemcpydevicetohost are symbolic constants cudamemcpy(md, M, size, cudamemcpyhosttodevice); cudamemcpy(m, Md, size, cudamemcpydevicetohost); 32

CUDA Function Declarations Executed on the: Only callable from the: device float DeviceFunc() device device global void device host host host host KernelFunc() float HostFunc() global defines a kernel function Must return void device and host can be used together 33

CUDA Function Declarations (cont.) device functions cannot have their address taken For functions executed on the device: No recursion No static variable declarations inside the function No variable number of arguments 34

Calling a Kernel Function Thread Creation A kernel function must be called with an execution configuration: global void KernelFunc(...); dim3 DimGrid(100, 50); // 5000 thread blocks dim3 DimBlock(4, 8, 8); // 256 threads per block size_t SharedMemBytes = 64; // 64 bytes of shared memory KernelFunc<<< DimGrid, DimBlock, SharedMemBytes >>>(...); Any call to a kernel function is asynchronous from CUDA 1.0 on, explicit synch needed for blocking 35

A Simple Running Example Matrix Multiplication A simple matrix multiplication example that illustrates the basic features of memory and thread management in CUDA programs Leave shared memory usage until later Local, register usage Thread ID usage Memory data transfer API between host and device Assume square matrix for simplicity 36

Programming Model: Square Matrix Multiplication Example P = M * N of size Without tiling: WIDTH x WIDTH N P WIDTH M WIDTH One thread calculates one element of P M and N are loaded WIDTH times from global memory WIDTH WIDTH 37

Memory Layout of a Matrix in C M0,0 M1,0 M2,0 M3,0 M0,1 M1,1 M2,1 M3,1 M0,2 M1,2 M2,2 M3,2 M0,3 M1,3 M2,3 M3,3 M M0,0 M1,0 M2,0 M3,0 M0,1 M1,1 M2,1 M3,1 M0,2 M1,2 M2,2 M3,2 M0,3 M1,3 M2,3 M3,3 38

Matrix Multiplication A Simple Host Version in C k WIDTH j WIDTH // Matrix multiplication on the (CPU) host in double precision void MatrixMulOnHost(float* M, float* N, float* P, int Width) N { for (int i = 0; i < Width; ++i) for (int j = 0; j < Width; ++j) { double sum = 0; for (int k = 0; k < Width; ++k) { double a = M[i * width + k]; double b = N[k * width + j]; sum += a * b; } M P P[i * Width + j] = sum; } i } k WIDTH WIDTH 39

Threads and Blocks Nd Grid 1 One Block of threads compute matrix Pd Block 1 Each thread computes one element of Pd 4 Size of matrix limited by the number of threads allowed in a thread block 2 Threa d (2, 2) Each thread Loads a row of matrix Md Loads a column of matrix Nd Perform one multiply and addition for each pair of Md and Nd elements Compute to off-chip memory access ratio close to 1:1 (not very high) 2 6 3 2 5 4 48 WIDTH Md Pd 40

Tiled Kernel Function Nd WIDTH Have each 2D thread block to compute a (TILE_WIDTH)2 sub-matrix (tile) of the result matrix Each has (TILE_WIDTH)2 threads Generate a 2D Grid of (WIDTH/TILE_WIDTH)2 blocks Md Pd by You still need to put a loop around the kernel call for cases where WIDTH/TILE_WIDTH is greater than max grid size (64K)! TILE_WIDTH bx WIDTH WIDTH ty tx WIDTH 41

Kernel Function Code // Matrix multiplication kernel global void MatrixMulKernel(float* Md, float* Nd, float* Pd, int Width) { // Index of thread unsigned int j = blockidx.x*blockdim.x+threadidx.x; unsigned int i = blockidx.y*blockdim.y+threadidx.y; // Calculate element value float sum = 0; for (int k=0;k<n;k++) sum += A[i*n+k] * B[k*n+j]; } // Store element value C[i*n+j] = sum; 42

Step 1: Copy Input Data void MatrixMulOnDevice(float* M, float* N, float* P, int Bw, int Bn) { int Width = Wb*Bn; int size = Width * Width * sizeof(float); float* Md, Nd, Pd; // Allocate and Load M, N to device memory cudamalloc(&md, size); cudamemcpy(md, M, size, cudamemcpyhosttodevice); cudamalloc(&nd, size); cudamemcpy(nd, N, size, cudamemcpyhosttodevice); // Allocate P on the device cudamalloc(&pd, size); 43

Step 2: Kernel Invocation // Setup the execution configuration dim3 dimgrid(bw, Bw); dim3 dimblock(bn, Bn); // Launch the device computation threads! MatrixMulKernel<<<dimGrid, dimblock>>>(md, Nd, Pd, Bw*Bn); 44

Step 3: Copy Output Data // Read P from the device cudamemcpy(p, Pd, size, cudamemcpydevicetohost); // Free device matrices cudafree(md); cudafree(nd); cudafree (Pd); } 45

Compiling a CUDA Program C/C++ CUDA Application float4 me = gx[gtid]; me.x += me.y * me.z; NVCC CPU Code PTX Code Virtual Physical PTX to Target Compiler G80 Target code ld.global.v4.f32 mad.f32 Parallel Thread execution (PTX) Virtual Machine and ISA Programming model Execution resources and state {$f1,$f3,$f5,$f7}, [$r9+0]; $f1, $f5, $f3, $f1; GPU 46

Compilation Any source file containing CUDA language extensions must be compiled with NVCC NVCC is a compiler driver Works by invoking all the necessary tools and compilers like cudacc, g++, cl,... NVCC outputs: C code (host CPU Code) Must then be compiled with the rest of the application using another tool PTX Object code directly Or, PTX source, interpreted at runtime 47

Linking Any executable with CUDA code requires two dynamic libraries: The CUDA runtime library (cudart) The CUDA core library (cuda) 48

Debugging Using the Device Emulation Mode An executable compiled in device emulation mode (nvcc -deviceemu) runs completely on the host using the CUDA runtime No need of any device and CUDA driver Each device thread is emulated with a host thread Running in device emulation mode, one can: Use host native debug support (breakpoints, inspection, etc.) Access any device-specific data from host code and vice-versa Call any host function from device code (e.g. printf) and vice-versa Detect deadlock situations caused by improper usage of syncthreads 49

Device Emulation Mode Pitfalls Emulated device threads execute sequentially, so simultaneous accesses of the same memory location by multiple threads could produce different results. Dereferencing device pointers on the host or host pointers on the device can produce correct results in device emulation mode, but will generate an error in device execution mode 50

Floating Point Results of floating-point computations will slightly differ because of: Different compiler outputs, instruction sets Use of extended precision for intermediate results There are various options to force strict single precision on the host 51