Docsity

Introduction to CUDA and GPU Programming - Lecture Notes | CISC 879, Study notes of Computer Science

Toggle sidebar
bg1
CISC 879 Software Support for Multicore Architectures Spring 2007
Lecture 4: February 21
Lecturer: Xiaoming Li Scribe: Scott Grauer-Gray, Pujan Kafle
4.1 Intro to CUDA and GPU Programming
Professor Xiamoming Li of the Electrical and Computer Engineering department presented an introduction
to CUDA, a langauage used for GPU programming. He discussed the CUDA Programming Model, the
CUDA memory model, the CUDA API, and then went on to give an example CUDA program and an
application of GPU processing and CUDA in his own research with GRAVIT.
4.1.1 CUDA Programming Model
Ideas of CUDA Programming Model
•CUDA is abbreviation for Compute Unified Device Architecture
•Makes general programming for GPU easier
•Don’t need to worry about graphics processing
•Bad idea to compute all instructions on GPU
•Branching not good on GPU
The GPU - graphics processor unit - compute device with own memory (DRAM) used to execute portion
of application that:
•must be executed many times
•can be isolated as a function called a kernel
•works independently on different data
Threads that execute the kernel are organized as a grid of thread blocks
Thread Block
•number of threads in a block limited to 512
•have access to high-speed shared memory
•can synochronize execution between threads in block
•if threads not in same block, communication must be through slower global memory
4-1
pf3
pf4
pf5

Partial preview of the text

Download Introduction to CUDA and GPU Programming - Lecture Notes | CISC 879 and more Study notes Computer Science in PDF only on Docsity!

CISC 879 Software Support for Multicore Architectures Spring 2007

Lecture 4: February 21

Lecturer: Xiaoming Li Scribe: Scott Grauer-Gray, Pujan Kafle

4.1 Intro to CUDA and GPU Programming

Professor Xiamoming Li of the Electrical and Computer Engineering department presented an introduction to CUDA, a langauage used for GPU programming. He discussed the CUDA Programming Model, the CUDA memory model, the CUDA API, and then went on to give an example CUDA program and an application of GPU processing and CUDA in his own research with GRAVIT.

4.1.1 CUDA Programming Model

Ideas of CUDA Programming Model

  • CUDA is abbreviation for Compute Unified Device Architecture
  • Makes general programming for GPU easier
  • Don’t need to worry about graphics processing
  • Bad idea to compute all instructions on GPU
  • Branching not good on GPU

The GPU - graphics processor unit - compute device with own memory (DRAM) used to execute portion of application that:

  • must be executed many times
  • can be isolated as a function called a kernel
  • works independently on different data

Threads that execute the kernel are organized as a grid of thread blocks

Thread Block

  • number of threads in a block limited to 512
  • have access to high-speed shared memory
  • can synochronize execution between threads in block
  • if threads not in same block, communication must be through slower global memory

4-2 Lecture 4: February 21

  • threads in block have IDs and can be indexed into 1, 2, or 3 dimensions, choice doesn’t make a difference in execution time but can simplify programming when processing multidimensional data in applications like image processing, solving PDEs on volumes etc.

Thread Grid

  • consists of multiple thread blocks of same dimensions and size
  • all threads in grid execute same kernel
  • threads in different blocks cannot communicate and synchonize with each other
  • blocks have IDs and can be indexed into 1 or 2 dimensions, choice doesn’t make a difference in execution time but can simplify programming

4.1.2 CUDA Memory Model

Figure 4.1: Illustration of CUDA memory model

Ideas of Memory Model

  • memory hierarchy of GPU different from CPU
  • need to be careful about memory use to speed up programs
  • global memory is slow, constant and texture memory are read-only for device

Shared memory

  • shared between threads in the same block
  • much faster than local/global memory

4-4 Lecture 4: February 21

  • device - variable in global memory space, has lifetime of application, is accessible from all threads in grid and host
  • constant - variable in constant memory space, has lifetime of application, is accessible from all threads in grid and host
  • shared - variable in shared memory space of thread block, has lifetime of block, only accessible from threads within block

Execution configuration

  • must be given for calls to global functions
  • defines dimension of grid and blocks
  • specified between function name and argument lists

Example: the function global void Func(float* parameter);

must be called like:

Func <<< Dg, Db, Ns >>(parameter);

  • Dg gives dimension and size of grid
  • Db gives dimension and size of block
  • Ns - optional argument that gives number of bytes dynamically allocated in shared memory (default is 0)

Built-in variables

  • gridDim = type dim3, dimensions of grid
  • blockIdx = type uint3, block index within grid
  • blockDim = type dim3, dimensions of block
  • threadIdx = type uint3, thread index within block

4.1.4 Example CUDA Program

Calculation of scalar product of 32 vector pairs of 4096 elements each

  • calculation organized as grid of 32 blocks with 256 threads/block -> 4096/256 = 16 slices/vector
  • data given to device as two data arrays and results saved in result array
  • each product of vector pair calculated in slices that are added up for final result

Host program:

Lecture 4: February 21 4-

Figure 4.3: Illustration of scalar product calculation using CUDA

int main(int argc, char* argv[]) { CUT_CHECK_DEVICE();

h_A = (float *)malloc(DATA_SZ); ........ cudaMalloc((void **)&d_A, DATA_SZ); ........ cudaMemcpy(d_A, h_A, DATA_SZ, cudaMemcpyHostToDevice); ........ //call to global function ProdGPU to be executed on GPU for computation of scalar products ProdGPU<<<BLOCK_N, THREAD_N>>>(d_C, d_A, d_B); .......... cudaMemcpy(h_C_GPU, d_C, RESULT_SZ, cudaMemcpyDeviceToHost); ........ CUDA_SAFE_CALL(cudaFree(d_A)); free(h_A); .......... CUT_EXIT(argc, argv); }

Kernel Function that is executed on GPU:

global void ProdGPU(float *d_C, float *d_A, float *d_B) { shared float t[THREAD_N]; // results of single threads shared float r[SLICE_N]; //slice cache; stores results of each slice const int I = threadIdx.x; //thread ID in block

//run on every pair of input vectors; each pair is in a different block in the grid for (int vec_n=blockIdx.x; vec_n < VECTOR_N; vec_n += gridDim.x) {

Lecture 4: February 21 4-

GRAVIT on the GPU

  • ideal algorithm for GPU
  • divide global memory between position/mass and velocity because of different access patterns (see figure 4.4)
  • sets of particles easily divided into blocks
  • each block steps through all particles in slices and mirrors them into shared memory (see figure 4.4)
  • GPU Baseline speedup approx. 60X
  • random nature of initial particles causes unpredictable memory access patterns, causing unpredictable degree of speed-up

Figure 4.4: Memory use in GRAVIT

Optimization

  • optimization using spatial subdivision - idea is to improve locality of memory accesses
  • problems with spatial subdivision include recursion, inter-block commmunication, unknown sizes of result sets (memory usage)
  • approaches to spatial subdivision show potential, but optimizations hard to predict in advance
  • one problem is handling dynamic results in global memory

CUDA is easy to pick up, but hard to push to limit