CUDA tutorial
Introduction
This tutorial introduces the Graphics Processing Unit (GPU) as a massively parallel computing device, the CUDA parallel programming language, and some of the CUDA numerical libraries for use in high performance computing.
This tutorial uses CUDA to accelerate C or C++ code. A working knowledge of one of these languages is therefore required to gain the most benefit out of it. Even though Fortran is also supported by CUDA, for the purpose of this tutorial we only cover the CUDA C/C++. From here on, we use term CUDA C to refer "CUDA C and C++". CUDA C is essentially a C/C++ that allow one to execute function on both GPU and CPU.
- Understanding the architecture of a GPU.
- Understanding the workflow of a CUDA program
- Managing GPU memory and understanding the various types of GPU memory
- Writing and compiling a minimal CUDA code and compiling CUDA examples
What is GPU ?
GPU, or a graphics processing unit, is a single-chip processor that performs rapid mathematical calculations, primarily for the purpose of rendering images. However, in the recent years, such capability is being harnessed more broadly to accelerate computational workloads of the cutting-edge scientific research areas.
What is CUDA ?
CUDA = Compute Unified Device Architecture Provides access to instructions and memory of massively parallel elements in GPU. Another definition: CUDA is scalable parallel programming model and software environment for parallel computing.
CUDA GPU Architecture
There two main components of the GPU:
- Global memory
- Similar to CPU memory
- Accessible by both CPU and GPU
- Streaming multiprocessors (SMs)
- They perform actual computations
- Each SM has its own control init, registers, execution pipelines, etc
CUDA Programming Model
Before we start talking about programming model, let us go over some useful terminology:
- Host – The CPU and its memory (host memory)
- Device – The GPU and its memory (device memory)
The CUDA programming model is a heterogeneous model in which both the CPU and GPU are used. CUDA code is capable of managing memory of both CPU and GPU as well as executing GPU functions, called kernels. Such kernels are executed by many GPU threads in parallel. Here is the 5-steps recipe of a typical CUDA code:
- Declare and allocate both the Host and Device memories
- Initialize the Host memory
- Transfer data from Host memory to Device memory
- Execute GPU functions (kernels)
- Transfer data back to the Host memory