CUDA Introduction
CUDA Introduction
Christian Trefftz / Greg Wolffe Grand Valley State University Supercomputing 2008 Education Program
Terms
What is GPGPU? General-Purpose computing on a Graphics GeneralProcessing Unit Using graphic hardware for non-graphic noncomputations What is CUDA? Compute Unified Device Architecture Software architecture for managing data-parallel dataprogramming
Supercomputing 2008 Education Program 2
Motivation
CPU
Fast caches Branching adaptability High performance Multiple ALUs Fast onboard memory High throughput on parallel tasks
Executes program on each fragment/vertex
GPU
CPUs are great for task parallelism GPUs are great for data parallelism
Supercomputing 2008 Education Program 4
GPU Architecture
Processing Element
Memory Architecture
10
DataData-parallel Programming
Think
of the CPU as a massively-threaded massivelycoco-processor Write kernel functions that execute on the device -- processing multiple data elements in parallel
Keep
it busy! > massive threading Keep your data close! > local memory
Supercomputing 2008 Education Program 11
Hardware Requirements
12
13
Acknowledgements
NVidia
Corporation
developer.nvidia.com/CUDA
NVidia
Queue
Supercomputing 2008 Education Program 14
http://www.acmqueue.org/
15
Credits
The
the Tutorial on CUDA in Dr. Dobbs Journal Andrew Bellenirs code for matrix multiplication Igor Majdandzics code for Voronoi diagrams NVIDIAs CUDA programming guide
16
Software Requirements/Tools
CUDA
Toolkit
Occupancy
17
Allocate memory that will be used for the computation (variable declaration and allocation) Read the data that we will compute on (input) Specify the computation that will be performed Write to the appropriate device the results (output)
18
We need to allocate space in the video cards memory for the variables. The video card does not have I/O devices, hence we need to copy the input data from the memory in the host computer into the memory in the video card, using the variable allocated in the previous step. We need to specify code to execute. execute. Copy the results back to the memory in the host computer.
Supercomputing 2008 Education Program 19
Initially:
20
21
Copy content from the hosts memory to the GPU card memory
22
23
24
The Kernel
It is necessary to write the code that will be executed in the stream processors in the GPU card That code, called the kernel, will be downloaded and executed, simultaneously and in lock-step fashion, in several lock(all?) stream processors in the GPU card How is every instance of the kernel going to know which piece of data it is working on?
25
need to specify:
The grid size: The size and shape of the data that the program will be working on The block size: The block size indicates the subsub-area of the original grid that will be assigned to an MP (a set of stream processors that share local memory)
26
Block Size
Recall
that the stream processors of the GPU are organized as MPs (multi(multiprocessors) and every MP has its own set of resources:
The
block size needs to be chosen such that there are enough resources in an MP to execute a block at a time.
Supercomputing 2008 Education Program 27
In the GPU:
Processing Elements
Array Elements
Block 0 Block 1
28
simple.c simple.cu
simple.c
is ordinary code in C It allocates an array of integers, initializes it to values corresponding to the indices in the array and prints the array. It calls a function that modifies the array The array is printed again.
Supercomputing 2008 Education Program 29
simple.c
#include <stdio.h> #define SIZEOFARRAY 64 extern void fillArray(int *a,int size); /* The main program */ int main(int argc,char *argv[]) { /* Declare the array that will be modified by the GPU */ int a[SIZEOFARRAY]; int i; /* Initialize the array to 0s */ for(i=0;i < SIZEOFARRAY;i++) { a[i]=i; } /* Print the initial array */ printf("Initial state of the array:\n"); array:\ for(i = 0;i < SIZEOFARRAY;i++) { printf("%d ",a[i]); } printf("\ printf("\n"); /* Call the function that will in turn call the function in the GPU that will fill the array */ fillArray(a,SIZEOFARRAY); /* Now print the array after calling fillArray */ printf("Final state of the array:\n"); array:\ for(i = 0;i < SIZEOFARRAY;i++) { printf("%d ",a[i]); } printf("\ printf("\n"); return 0; }
30
simple.cu
simple.cu
fillArray(): A function that will be executed on the host and which takes care of:
Allocating variables in the global GPU memory Copying the array from the host to the GPU memory Setting the grid and block sizes Invoking the kernel that is executed on the GPU Copying the values back to the host memory Freeing the GPU memory
31
fillArray (part 1)
#define BLOCK_SIZE 32 extern "C" void fillArray(int *array,int arraySize){ /* a_d is the GPU counterpart of the array that exists on the host memory */ int *array_d; cudaError_t result; /* allocate memory on device */ /* cudaMalloc allocates space in the memory of the GPU card */ result = cudaMalloc((void**)&array_d,sizeof(int)*arraySize); /* copy the array into the variable array_d in the device */ /* The memory from the host is being copied to the corresponding variable in the GPU global memory */ result = cudaMemcpy(array_d,array,sizeof(int)*arraySize, cudaMemcpyHostToDevice);
32
fillArray (part 2)
/* execution configuration... */ /* Indicate the dimension of the block */ dim3 dimblock(BLOCK_SIZE); /* Indicate the dimension of the grid in blocks */ dim3 dimgrid(arraySize/BLOCK_SIZE); /* actual computation: Call the kernel, the function that is */ /* executed by each and every processing element on the GPU card */ cu_fillArray<<<dimgrid,dimblock>>>(array_d); /* read results back: */ /* Copy the results from the GPU back to the memory on the host */ result = cudaMemcpy(array,array_d,sizeof(int)*arraySize,cudaMemcpyDevice ToHost); /* Release the memory on the GPU card */ cudaFree(array_d); }
33
simple.cu (cont.)
The
cu_fillArray()
This is the kernel that will be executed in every stream processor in the GPU It is identified as a kernel by the use of the keyword: __global__ This function uses the built-in variables built
34
cu_fillArray
__global__ void cu_fillArray(int *array_d){ int x; /* blockIdx.x is a built-in variable in CUDA builtthat returns the blockId in the x axis of the block that is executing this block of code threadIdx.x is another built-in variable in CUDA builtthat returns the threadId in the x axis of the thread that is being executed by this stream processor in this particular block */ x=blockIdx.x*BLOCK_SIZE+threadIdx.x; array_d[x]+=array_d[x]; }
35
To compile:
nvcc
simple.c simple.cu o simple The compiler generates the code for both the host and the GPU Demo on cuda.littlefe.net
36
a minor modification to the code, we can print the blockIds and threadIds We will use two arrays instead of just one.
The
37
In the GPU:
Processing Elements
Thread 0 Thread 1 Thread 2 Thread 3 Thread 0 Thread 1 Thread 2 Thread 3
Array Elements
Block 0 Block 1
38
HandsHands-on Activity
Compile with (one single line) nvcc blockAndThread.c -o blockAndThread Run the program ./blockAndThread
blockAndThread.cu
Edit the file blockAndThread.cu Modify the constant BLOCK_SIZE. The current value is 8, try replacing it with 4. Recompile as above Run the program and compare the output with the previous run.
Supercomputing 2008 Education Program 39
See files:
blockAndThread2D.c blockAndThread2D.cu
41
CUDA:
42
CUDA Code
__global__ void saxpy_parallel(int n, float alpha, float *x, float *y) { int i = blockIdx.x*blockDim.x+threadIdx.x; if (i<n) y[i] = alpha*x[i] + y[i]; }
44
45
Warps
Each active block is split into SIMD ("Single Instruction Multiple Data") groups of threads called "warps". Each warp contains the same number of threads, called the "warp size", which are executed by the multiprocessor in a SIMD fashion. On if statements, or while statements (control transfer) the threads may diverge. Use: __syncthreads()
46
A Real Application
47
Definition
Definition
: Let S be a set of n sites in Euclidean space of dimension d. For each site p of S, the Voronoi cell V(p) of p is the set of points that are closer to p than to other sites of S. The Voronoi diagram V(S) is the space partition induced by Voronoi cells.
48
Algorithms
The
classical sequential algorithm has complexity O(n log n) where n is the number of sites (seeds). If one only needs an approximation, on a grid of points (e.g. digital display):
Assign a different color to each seed Calculate the distance from every point in the grid to all seeds Color each point with the color of its closest seed
Supercomputing 2008 Education Program 49
calculation for every pixel is a good candidate to be carried out in parallel Notice that the locations of the seeds are readread-only in the kernel Thus we can use the texture map area in the GPU card, which is a fast read-only readcache to store the seeds: __device__ __constant__
Supercomputing 2008 Education Program 50
Demo on cuda
51
52
Memory Alignment
Memory
access on the GPU works much better if the data items are aligned at 64 byte boundaries. Hence, allocating 2D arrays so that every row starts at a 64-byte boundary address 64will improve performance. But that is difficult to do for a programmer
53
Memory allocation of 2D arrays so that every row is padded (if necessary). The function determines the best pitch and returns it to the program. The function name is cudaMallocPitch() Memory copy operations that take into account the pitch that was chosen by the memory allocation operation. The function name is cudaMemcpy2D()
54
Pitch
Columns Padding
Rows
Pitch
Supercomputing 2008 Education Program 55
A simple example:
See
pitch.cu A matrix of 30 rows and 10 columns The work is divided into 3 blocks of 10 rows:
56
57
In the kernel:
__global__ void myKernel(int int int int *devPtr, pitch, width, height)
{ int c; int thisRow; thisRow = blockIdx.x * 10 + threadIdx.x; int *row = (int *)((char *)devPtr + thisRow*pitch); for(c = 0;c < width;c++) row[c] = row[c] + 1; }
\
59
60
pitch
Notice
that when using pitch, we divide the work by rows. Instead of using the 2D decomposition of 2D blocks, we are dividing the 2D matrix into blocks of rows.
61
Rows
Block 1
Block 2
Pitch
Supercomputing 2008 Education Program 62
The Mandelbrot set: A set of points in the complex plane, the boundary of which forms a fractal. A complex number, c, is in the Mandelbrot set if, when starting with x0=0 and applying the iteration
xn+1 = xn2 + c
repeatedly, the absolute value of xn never exceeds a certain number (that number depends on c) however large n gets.
63
Demo: Comparison
We
64
Critical for performance Recommended value is 192 or 256 Maximum value is 512 Should be a multiple of 32 since this is the warp size for Series 8 GPUs and thus the native execution size for multiprocessors Limited by number of registers on the MP Series 8 GPU MPs have 8192 registers which are shared between all the threads on an MP
Supercomputing 2008 Education Program 65
Critical for scalability Recommended value is at least 100, but 1000 would scale for many generations of hardware Actual value depends on size of the problem data It should be a multiple of the number of MPs for an even distribution of work (not a requirement though) Example: 24 blocks
Grid will work efficiently on Series 8 (12 MPs), but it will waste resources on new GPUs with 32MPs
66
Control flow instructions diverge (threads take different paths of execution) Example: if, for, while Diverged code prevents SIMD execution it forces serial execution (kills efficiency) One approach is to invoke a simpler kernel multiple times Liberal use of __syncthreads()
67
4 clock cycles for each memory read/write plus additional 400-600 cycles for latency 400Memory latency can be hidden by keeping a large number of threads busy Keep number of threads per block (block size) and number of blocks per grid (grid size) as large as possible Constant memory can be used for constant data (variables that do not change). Constant memory is cached.
68
Device is capable of reading a 32, 64 or 128-bit 128number from memory with a single instruction Data has to be aligned in memory (this can be accomplished by using cudaMallocPitch() calls) If formatted properly, multiple threads from a warp can each receive a piece of memory with a single read instruction
69
Watchdog timer
Operating system GUI may have a "watchdog" timer that causes programs using the primary graphics adapter to time out if they run longer than the maximum allowed time. Individual GPU program launches are limited to a run time of less than this maximum. Exceeding this time limit usually causes a launch failure. Possible solution: run CUDA on a GPU that is NOT attached to a display.
Supercomputing 2008 Education Program 70
Resources on line
http://www.acmqueue.org/modules.php?name= Content&pa=showpage&pid=532 http://www.ddj.com/hpc-high-performancehttp://www.ddj.com/hpc-high-performancecomputing/207200659 http://www.nvidia.com/object/cuda_home.html# http://www.nvidia.com/object/cuda_learn.html Computation of Voronoi diagrams using a graphics processing unit by Igor Majdandzic et al. available through IEEE Digital Library, DOI: 10.1109/EIT.2008.4554342
Supercomputing 2008 Education Program 71