CUDA Kenjiro Taura 1 / 36

Similar documents
Register file. A single large register file (ex. 16K registers) is partitioned among the threads of the dispatched blocks.

CSC266 Introduction to Parallel Computing using GPUs Introduction to CUDA

GPU Computing: Introduction to CUDA. Dr Paul Richmond

Parallel Computing. Lecture 19: CUDA - I

Lecture 3: Introduction to CUDA

CUDA C Programming Mark Harris NVIDIA Corporation

Lecture 2: Introduction to CUDA C

Module 2: Introduction to CUDA C. Objective

Introduction to CUDA C

CUDA C/C++ BASICS. NVIDIA Corporation

Introduction to CUDA C

CS 179: GPU Computing. Lecture 2: The Basics

CUDA Basics. July 6, 2016

Module 2: Introduction to CUDA C

Speed Up Your Codes Using GPU

CUDA C/C++ BASICS. NVIDIA Corporation

CS377P Programming for Performance GPU Programming - I

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

Vector Addition on the Device: main()

Overview. Lecture 1: an introduction to CUDA. Hardware view. Hardware view. hardware view software view CUDA programming

Tesla Architecture, CUDA and Optimization Strategies

Parallel Numerical Algorithms

Programming with CUDA, WS09

CUDA Architecture & Programming Model

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

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

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

INTRODUCTION TO GPU COMPUTING WITH CUDA. Topi Siro

CUDA Programming (Basics, Cuda Threads, Atomics) Ezio Bartocci

Introduction to CUDA CIRC Summer School 2014

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

CUDA Parallel Programming Model Michael Garland

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

Lecture 1: an introduction to CUDA

GPU Programming. Alan Gray, James Perry EPCC The University of Edinburgh

CUDA Workshop. High Performance GPU computing EXEBIT Karthikeyan

High Performance Linear Algebra on Data Parallel Co-Processors I

An Introduction to GPU Computing and CUDA Architecture

CUDA Lecture 2. Manfred Liebmann. Technische Universität München Chair of Optimal Control Center for Mathematical Sciences, M17

CUDA Parallel Programming Model. Scalable Parallel Programming with CUDA

GPU Programming Using CUDA. Samuli Laine NVIDIA Research

Basics of CADA Programming - CUDA 4.0 and newer

Basic CUDA workshop. Outlines. Setting Up Your Machine Architecture Getting Started Programming CUDA. Fine-Tuning. Penporn Koanantakool

GPU Programming Using CUDA. Samuli Laine NVIDIA Research

Learn CUDA in an Afternoon. Alan Gray EPCC The University of Edinburgh

High-Performance Computing Using GPUs

GPU CUDA Programming

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

Information Coding / Computer Graphics, ISY, LiTH. Introduction to CUDA. Ingemar Ragnemalm Information Coding, ISY

CUDA C/C++ Basics GTC 2012 Justin Luitjens, NVIDIA Corporation

ECE 408 / CS 483 Final Exam, Fall 2014

CUDA Exercises. CUDA Programming Model Lukas Cavigelli ETZ E 9 / ETZ D Integrated Systems Laboratory

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

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

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

Lecture 10!! Introduction to CUDA!

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

ECE 574 Cluster Computing Lecture 15

Parallel Programming and Debugging with CUDA C. Geoff Gerfin Sr. System Software Engineer

GPU programming CUDA C. GPU programming,ii. COMP528 Multi-Core Programming. Different ways:

Massively Parallel Algorithms

EEM528 GPU COMPUTING

GPU 1. CSCI 4850/5850 High-Performance Computing Spring 2018

OpenMP and GPU Programming

HPCSE II. GPU programming and CUDA

Scientific discovery, analysis and prediction made possible through high performance computing.

ECE 574 Cluster Computing Lecture 17

Memory concept. Grid concept, Synchronization. GPU Programming. Szénási Sándor.

Mathematical computations with GPUs

Supporting Data Parallelism in Matcloud: Final Report

Cartoon parallel architectures; CPUs and GPUs

Introduction to Parallel Computing with CUDA. Oswald Haan

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

Lecture 6b Introduction of CUDA programming

Introduction to GPU (Graphics Processing Unit) Architecture & Programming

Information Coding / Computer Graphics, ISY, LiTH. Introduction to CUDA. Ingemar Ragnemalm Information Coding, ISY

CS179 GPU Programming Recitation 4: CUDA Particles

HPC Middle East. KFUPM HPC Workshop April Mohamed Mekias HPC Solutions Consultant. Introduction to CUDA programming

Introduction to CUDA Programming

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

Practical Introduction to CUDA and GPU

CUDA programming interface - CUDA C

GPU Computing Workshop CSU Getting Started. Garland Durham Quantos Analytics

CUDA Programming. Aiichiro Nakano

GPU Programming Introduction

GPU Computing: A Quick Start

Developing Portable CUDA C/C++ Code with Hemi

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

Introduction to GPGPUs and to CUDA programming model

Stanford University. NVIDIA Tesla M2090. NVIDIA GeForce GTX 690

CSE 599 I Accelerated Computing Programming GPUS. Intro to CUDA C

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

COSC 462 Parallel Programming

Basic Elements of CUDA Algoritmi e Calcolo Parallelo. Daniele Loiacono

GPU programming. Dr. Bernhard Kainz

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

An Introduction to GPU Architecture and CUDA C/C++ Programming. Bin Chen April 4, 2018 Research Computing Center

GPU Computing with CUDA. Part 2: CUDA Introduction

Lecture 11: GPU programming

University of Bielefeld

Transcription:

CUDA Kenjiro Taura 1 / 36

Contents 1 Overview 2 CUDA Basics 3 Kernels 4 Threads and thread blocks 5 Moving data between host and device 6 Data sharing among threads in the device 2 / 36

Contents 1 Overview 2 CUDA Basics 3 Kernels 4 Threads and thread blocks 5 Moving data between host and device 6 Data sharing among threads in the device 3 / 36

Goal learn CUDA, the basic API for programming NVIDA GPUs learn where it is similar to OpenMP and where it is different 4 / 36

CUDA reference official documentation: https://docs.nvidia.com/cuda/index.html book Professional CUDA C Programming https://www.amazon.com/ Professional-CUDA-Programming-John-Cheng/dp/ 1118739329 5 / 36

Compiling/running CUDA programs with NVCC compile with nvcc command 1 $ nvcc program.cu the conventional extension of CUDA programs is.cu nvcc can handle ordinary C/C++ programs too (.cc,.cpp C+) you can have a file with any extension and insist it is a CUDA program (convenient when you maintain a single file that compiles both on CPU and GPU) 1 $ nvcc -x cu program.cc run the executable on a node that has a GPU(s) 1 $ srun -p p./a.out 6 / 36

Contents 1 Overview 2 CUDA Basics 3 Kernels 4 Threads and thread blocks 5 Moving data between host and device 6 Data sharing among threads in the device 7 / 36

GPU is a device separate from CPU as such, code (functions) that runs on GPU must be so designated data must be copied between CPU and GPU a GPU is often called a device, and a CPU a host host (CPU) device (GPU) 8 / 36

Contents 1 Overview 2 CUDA Basics 3 Kernels 4 Threads and thread blocks 5 Moving data between host and device 6 Data sharing among threads in the device 9 / 36

Two things you need to learn first: writing and launching kernels a GPU kernel (or simply a kernel ) is a function that runs on GPU 1 global void f(...) {... } syntactically, a kernel is an ordinary C++ function that returns nothing (void), except for the global keyword a host launches a kernel specifying the number of threads. 1 f<<<nb,bs>>>(...); will create (nb bs) CUDA threads, each executing f(...) bs threads in a thread block.................. nb thread blocks 10 / 36

Launching a kernel parallel loop launching a kernel, like 1 f<<<nb,bs>>>(...); executing the following loop in parallel (on GPU, of course) 1 for (i = 0; i < nb * bs; i++) { 2 f(...); // CUDA thread 3 } bs threads in a thread block.................. nb thread blocks 11 / 36

A simplest example writing a kernel 1 global void cuda_thread_fun(int n) { 2 int i = blockdim.x * blockidx.x + threadidx.x; 3 int nthreads = griddim.x * blockdim.x; 4 if (i < nthreads) { 5 printf("hello I am CUDA thread %d out of %d\n", i, nthreads); 6 } 7 } and launching it 1 int thread_block_sz = 64; 2 int n_thread_blocks = (n + thread_block_sz - 1) / thread_block_sz; 3 cuda_thread_fun<<<n_thread_blocks,thread_block_sz>>>(n); will create n threads printing 1 hello I am CUDA thread 0 out of n 2... 3 hello I am CUDA thread n 1 out of n note: the order is unpredictable 12 / 36

A CUDA thread is not like an OpenMP thread launching 10000 CUDA threads is quite common and efficient 1 f<<<1024,256>>(...); launching 10000 threads on CPU is almost always a bad idea this is semantically similar to the above 1 #pragma omp parallel 2 f(); with 1 OMP_NUM_THREADS=10000./a.out but what happens inside is very different CPU way of doing this is: 1 #pragma omp parallel for 2 for (i = 0; i < 1024 * 256; i++) { 3 f(); 4 } with OMP NUM THREADS=the actual number of cores./a.out 13 / 36

About thread IDs for each thread to determine what to do, it needs a unique ID (the loop index) you get it from griddim, block{dim,idx} and threadidx when you launch a kernel by 1 f<<<nb,bs>>>(...); a thread block (blockdim.x threads) threadidx.x=0 =1=2.................. blockidx.x=0 blockidx.x=1 blockidx.x=2... and the grid (griddim.x thread blocks) blockdim.x = bs (the thread block size) griddim.x = nb (the number of blocks = the grid size) threadidx.x = the thread ID within the block ( [0, bs)) blockidx.x = the thread s block ID ( [0, nb)) 14 / 36

Remarks as suggested by.x, a block and the grid can be multidimensional (up to 3D, of.x,.y,.z) and the previous code assumes they are 1D extension to multidimensional block/grid is straightforward 1D: 1 int nb = 100; 2 int bs = 256 3 f<<<nb,bs>>>(...); 2D: 1 dim3 nb(10,10); 2 dim3 bs(8,32); 3 f<<<nb,bs>>>(...); 3D: 1 dim3 nb(10,5,2); 2 dim3 bs(8,8,4); 3 f<<<nb,bs>>>(...); 15 / 36

SpMV in CUDA original serial code 1 for (k = 0; k < A.nnz; k++) { 2 i,j,aij = A.elems[k]; 3 y[i] += Aij * x[j]; 4 } write a kernel that works on a single non-zero element 1 global spmv_dev(a, x, y) { 2 k = blockdim.x * blockidx.x + threadidx.x; // thread id 3 if (k < A.nnz) { 4 i,j,aij = A.elems[k]; 5 y[i] += Aij * x[j]; } } and launch it with nnz threads (we re not done yet) 1 spmv*(a, x, y) { 2 int bs = 256; 3 int nb = (A.nnz + bs - 1) / bs; 4 spmv_dev<<<nb,bs>>(a, x, y); } similarly simple for CSR version 16 / 36

We re not done yet this code 1 global spmv_dev(a, x, y) { 2 k = blockdim.x * blockidx.x + threadidx.x; 3 if (k < nnz) { 4 i,j,aij = A.elems[k]; 5 y[i] += Aij * x[j]; 6 } 7 } does not work yet 1 the device cannot access elements of A, x and y on the host 2 there is a race condition when updating y[i] 17 / 36

Keywords for functions global, device, host callable from code runs on global host/device device device device device host host host global functions cannot return a value (must be void) you can have both host and device in front of a definition, which generates two versions (device and host) 18 / 36

Macros convenient when writing a single file that works both on CPU and GPU NVCC : a macro defined when compiled by nvcc 1 #ifdef NVCC 2 // GPU implementation 3 #else 4 // CPU implementation 5 #endif CUDA ARCH 1 device host f(...) { 2 #ifdef CUDA_ARCH 3 // device code 4 #else 5 // host code 6 #endif 7 } : a macro defined when copiled for device 19 / 36

Contents 1 Overview 2 CUDA Basics 3 Kernels 4 Threads and thread blocks 5 Moving data between host and device 6 Data sharing among threads in the device 20 / 36

Threads and thread blocks (recap) a kernel specifies the action of a CUDA thread when you launch a kernel you specify the number of thread blocks (nb) and the thread block size = the number of threads in a single thread block (bs), to effectively create (nb bs) threads a thread block (blockdim.x threads) threadidx.x=0 =1=2.................. blockidx.x=0 blockidx.x=1 blockidx.x=2... the grid (griddim.x thread blocks) but why you need two separate numbers? 21 / 36

Why two numbers (bs and nb)?......... a single thread block is sent to a single SM and stays there until it finishes GPU core (streaming multiprocessor) GPU sends a thread block to an SM.................. f<<<nb,bs>>(...) 22 / 36

Restrictions affecting the correctness........................... you cannot make a single thread block arbitrarily large for P100, bs 1024 bs R 65536 R = the number of registers used per thread GPU has a faster memory, shared memory, which is only shared within a single thread block it s more like a scratch pad memory (it s a misnomer IMO) GPU core (streaming multiprocessor) shared memory (scratch pad) GPU sends a thread block to an SM f<<<nb,bs>>(...) 23 / 36

About registers each SM has a number of (65536 on P100) registers for an SM to accommodate at least one thread block, it must hold bs R 65536, where R is the number of registers used per thread how you can know R? pass -Xptxas -v to nvcc and see the compiler message can you control it? pass --maxrregcount R to nvcc 24 / 36

Factors affecting performance......... to utilize multiple (many) SMs, you want to create accordingly many thread blocks to efficiently use a single SM, you want to have enough threads in each SM how to choose them affects performance (later weeks) GPU core (streaming multiprocessor) shared memory (scratch pad) GPU sends a thread block to an SM.................. f<<<nb,bs>>(...) 25 / 36

Tips to choose a right thread block size, for now make it a multiple of 32 (warp size) and 1024 32, 64, 96,, 1024 complex kernels may fail with a large block size. reduce it when it happens always check a launch error at runtime! see compiler message -Xptxas -v and control it when necessary --maxrregcount small threads need more of them to fill an SM 26 / 36

Contents 1 Overview 2 CUDA Basics 3 Kernels 4 Threads and thread blocks 5 Moving data between host and device 6 Data sharing among threads in the device 27 / 36

Moving data between host and device host and device memory are separate the device cannot access data on the host and vice versa. i.e., the following does not work 1 double a[n]; 2 f<<<nb,bs>>>(a); 1 global f(double * a) { 2... a[i]... // this will segfault 3 } host (CPU) device (GPU) 28 / 36

Moving data between host and device host and device memory are separate the device cannot access data on the host and vice versa. i.e., the following does not work 1 double a[n]; 2 f<<<nb,bs>>>(a); 1 global f(double * a) { 2... a[i]... // this will segfault 3 } host (CPU) device (GPU) a 28 / 36

Two more things you must master: cudamalloc and cudamemcpy you need to host (CPU) device (GPU) a 29 / 36

Two more things you must master: cudamalloc and cudamemcpy you need to 1 allocate data on device (by cudamalloc) device memory host (CPU) device (GPU) a cudamalloc a_dev 29 / 36

Two more things you must master: cudamalloc and cudamemcpy you need to 1 allocate data on device (by cudamalloc) device memory 2 move data between the host and the device (by cudamemcpy) host (CPU) device (GPU) a cudamemcpy a_dev 29 / 36

Two more things you must master: cudamalloc and cudamemcpy you need to 1 allocate data on device (by cudamalloc) device memory 2 move data between the host and the device (by cudamemcpy) 3 give the kernel the pointer to the device memory host (CPU) device (GPU) a cudamemcpy a_dev 29 / 36

Two more things you must master: cudamalloc and cudamemcpy you need to 1 allocate data on device (by cudamalloc) device memory 2 move data between the host and the device (by cudamemcpy) 3 give the kernel the pointer to the device memory note: call cudamalloc and cudamemcpy on the host, not on the device host (CPU) device (GPU) a cudamemcpy a_dev 29 / 36

Typical steps to send data to the device 1 allocate data of the same size both on host and device 1 double * a =...; // any valid address will do (malloc, &variable, etc.) 2 double * a_dev = 0; 3 cudamalloc((void **)&a_dev, sz); 2 the host works on the host data 1 for (... ) { a[i] =... } // whatever initialization you need 3 copy the data to the device 1 cudamemcpy(a_dev, a, sz, cudamemcpyhosttodevice); 4 pass the device pointer to the kernel 1 f<<<nb,bs>>>(a dev,...) 5 often a good idea to have a struct having both pointers inside 1 typedef struct { 2 double * a; // host pointer 3 double * a_dev; // device pointer 4... 30 / 36

Typical steps to retrieve the result 1 allocate data of the same size both on host and device 1 double * r =... ; 2 double * r_dev = 0; 3 cudamalloc((void **)&r_dev, sz); 2 pass the device pointer to the kernel 1 f<<<nb,bs>>>(..., r_dev); 3 copy the data to the host 1 cudamemcpy(r, r_dev, sz, cudamemcpydevicetohost); 31 / 36

Contents 1 Overview 2 CUDA Basics 3 Kernels 4 Threads and thread blocks 5 Moving data between host and device 6 Data sharing among threads in the device 32 / 36

Data sharing among threads in the device basics : CUDA threads can access only device memory there are several types of device memory global memory : memory allocated via cudamalloc are shared among all threads one thread writes to it, other threads will see it (sooner or later) shared memory : a small on-chip memory accessible only within each SM (i.e., within a single thread block) how to use it exactly will be covered later other weirder memory types not covered in the lecture (constant and texture) 33 / 36

How to resolve race conditions on global memory? CUDA threads run concurrently so they are susceptible to race conditions as in CPUs 1 global spmv_dev(a, x, y) { 2 k = blockdim.x * blockidx.x + threadidx.x; // thread id 3 if (k < nnz) { 4 i,j,aij = A.elems_dev[k]; 5 y[i] += Aij * x[j]; 6 } 7 } 34 / 36

Atomic accumulations atomic accumulations are supported by the hardware and CUDA API atomicadd(p, x) 1 #pragma omp atomic 2 *p += x in OpenMP search the CUDA toolkit documentation for atomicadd there are other primitives, such as compare-and-swap 35 / 36

A working version of COO SpMV 1 global spmv_dev(a, x, y) { 2 k = thread id; 3 if (k < nnz) { 4 i,j,aij = A.elems_dev[k]; 5 atomicadd(&y[i], Aij * x[j]); 6 } 7 } make sure A.elems dev, x and y point to device memory (not shown) note: CSR is simpler to work with if you don t parallelize within a row 36 / 36