Introduction to CUDA

Overview

Hardly any system today runs without GPU acceleration, so knowing how to program GPUs is essential. Only the native programming model — CUDA for NVIDIA GPUs — gives access to every feature of the hardware and lets you make full use of it. The fundamentals are not limited to NVIDIA GPUs: they also apply to other GPUs and transfer easily.

CUDA, as native programming model of NVIDIA GPUs, allows very fine-grained control over parallel execution compared to higher level programming models such as OpenMP offloading, which helps to optimise performance. In some sense a GPU works like a bee colony: many more or less independent individuals work together to achieve a common goal. In a GPU the individuals are single threads. Each thread has a unique name, and the programmer uses that name to tell each thread what to do. Since many threads typically do similar jobs, the CUDA programming model uses a set of instructions, called a kernel, which many threads execute; the thread’s name is used in some places and makes small differences for each thread. The module provides an introduction to the programming language CUDA which is used to write fast numeric algorithms for NVIDIA GPUs. Focus is on the basic usage of the language, error handling and understanding kernel functions.

After a short software-setup episode, the module has two lesson episodes — the CUDA programming model (kernels, the execution configuration, the thread hierarchy and the grid-stride loop) and the rules for kernels and device functions together with error handling — and closes with a knowledge-check quiz. All code is shown in C/C++ and CUDA Fortran side by side, and each lesson episode ends with a hands-on exercise that you download, complete and compile yourself.

Prerequisites

  • Programming experience in C, C++ or Fortran

  • The ability to edit and compile a program on the machine that has the GPU — from a Linux shell (for example this tutorial) or from an IDE that can call nvcc/nvfortran: the exercises are downloaded, edited and compiled by you

  • Access to an NVIDIA GPU with CUDA Toolkit 12.x or newer installed (12.x if the GPU has compute capability 7.0, see the software-setup episode), either on your own machine or on a cluster, to compile and run the exercises

  • Knowledge of parallel programming is beneficial but not required

  • No prior experience with CUDA or GPU programming is assumed

This module is aimed at researchers, engineers and students who can already write sequential programs in C, C++ or Fortran and want to start programming NVIDIA GPUs. Basic familiarity with an editor or IDE, and with the Linux shell when working on a cluster, is needed for the exercises. Prior knowledge of parallel programming is helpful but not required.

Software setup

Learning outcomes

After completing this module, learners will be able to:

  • Explain the accelerator programming model and identify which parts of a program run on the CPU vs the GPU

  • Write a CUDA kernel using the __global__ specifier and launch it from host code using the triple-chevron syntax in C/C++ and the equivalent syntax in CUDA Fortran (<<<...>>>)

  • Map thread and block indices to data elements using the built-in threadIdx / blockIdx / blockDim / gridDim variables, and apply the grid-stride loop pattern to handle arbitrary data sizes

  • Apply the function execution space specifiers (__global__, __device__, __host__ __device__) correctly and respect the restrictions that apply to device code

  • Detect and handle CUDA errors using error-checking macros, and understand the asynchronous nature of kernel-launch error reporting

Estimated commitment: about 2 hours (115 minutes of lecture and exercises; provisional estimate, to be re-measured at the first delivery).

EVITA skill tree: SD1.2.6 GPU Programming with CUDA — the module covers the skill SD1.2.6.2.1 CUDA Programming Model and Kernels (to be defined in the skill tree). In the EVITA Common Qualification Framework it is module PP.HS7-SD1.2.6.2.1 of course PP.HS7 Programming GPUs with CUDA.

Related topics not covered here:

  • Memory management (managed, manual, pinned and constant memory) — covered in the next module, CUDA Memory Management

  • Streams and concurrency

  • Shared memory

  • Profiling

See also

Credit

This module is based on the CUDA course developed at HLRS, the High-Performance Computing Center Stuttgart (University of Stuttgart). Authors: Tobias Haas and Jasper Seehofer, HLRS.

If you want to reuse the material beyond the terms of the licence below, or if you find errors, please contact the authors through the module repository.

Related EVITA module: CUDA Memory Management

License