Docsity

CUDA Programming: Developing Applications for NVIDIA GPUs - Prof. Ramani Duraiswami, Study notes of Computer Science

Partial preview of the text

Download CUDA Programming: Developing Applications for NVIDIA GPUs - Prof. Ramani Duraiswami and more Study notes Computer Science in PDF only on Docsity!

CUDA Programming

Many slides adapted from the slides of Hwu & Kirk at UIUC; and NVIDIA CUDAtutorials

CUDA Software Development

Is done on the host (CPU)

programming environment, compilers and libraries - Profiler, emulator

Extensions to C

Declspecs - global, device, shared,local, constant - Keywords - threadIdx, blockIdx - Intrinsics - __syncthreads - Runtime API - Memory, symbol,execution management - Function launch device float filter[N];global void convolve (float *image) { shared float region[M];... region[threadIdx] = image[i];__syncthreads()... // Allocate GPU memoryvoid *myimage = cudaMalloc(bytes)// 100 blocks, 10 threads per blockconvolve<<<100, 10>>> (myimage);

© David Kirk/NVIDIA and Wen-mei W. Hwu, 2007-2009ECE 498AL, University of Illinois, Urbana-Champaign Host Kernel 1 Kernel 2 Device Grid 1 Block(0, 0) Block(1, 0) Block(0, 1) Block(1, 1) Grid 2 Courtesy: NDVIA Block (1, 1)^ Thread (0,1,0) Thread(1,1,0) Thread(2,1,0) Thread(3,1,0) Thread(0,0,0) Thread(1,0,0) Thread(2,0,0) Thread(3,0,0) (0,0,1) (1,0,1) (2,0,1) (3,0,1)

Block IDs and Thread IDs

Each thread uses IDs to decidewhat data to work on

Block ID: 1D or 2D - Thread ID: 1D, 2D, or 3D - Simplifies memoryaddressing when processingmultidimensional data - Image processing - Solving PDEs on volumes - …

Data Copies

cudaMemcpy(void *dst, void *src, size_tnbytes, enum cudaMemcpyKind direction);

direction specifies locations (host or device) ofsrc and dst

Blocks CPU thread: returns after the copy iscomplete - Doesn’t start copying until previous CUDA callscomplete

enum cudaMemcpyKind

cudaMemcpyHostToDevice

cudaMemcpyDeviceToHost - cudaMemcpyDeviceToDevice

Data Movement Example

Host variables – hDevice variables – dAllocate and get pointeron host and deviceCopy the data from hostto device (notice theorder of arguments)From device-to-devicefrom device-to-hostFree

Function Qualifiers

Kernels designated by function qualifier: global

Function called from host and executed on device - Must return void - Other CUDA function qualifiers device - Function called from device and run on device - Cannot be called from host code host - Function called from host and executed on host (default) - host and device qualifiers can be combinedto generate both CPU and GPU code

CUDA Built-in Device Variables

Unique Thread ID

Host synchronization

All kernel launches are asynchronous

control returns to CPU immediately

cudaMemcpy() is synchronous

control returns to CPU after copy completes

copy starts after all previous CUDA calls havecompleted

cudaThreadSynchronize()

blocks until all previous CUDA calls complete