Skip to content

Repository files navigation

CUDA Samples

Samples for CUDA Developers which demonstrates features in CUDA Toolkit. This version supports CUDA Toolkit 13.4.

Release Notes

This section describes the release notes for the CUDA Samples on GitHub only.

Change Log

Getting Started

Prerequisites

Download and install the CUDA Toolkit for your corresponding platform. For system requirements and installation instructions of cuda toolkit, please refer to the Linux Installation Guide, and the Windows Installation Guide.

Getting the CUDA Samples

Using git clone the repository of CUDA Samples using the command below.

git clone https://github.com/NVIDIA/cuda-samples.git

Without using git the easiest way to use these samples is to download the zip file containing the current version by clicking the "Download ZIP" button on the repo page. You can then unzip the entire archive and use the samples.

Building CUDA Samples

The CUDA Samples are built using CMake. Follow the instructions below for building on Linux, Windows, and for cross-compilation to Tegra devices.

Linux

Ensure that CMake (version 3.20 or later) is installed. Install it using your package manager if necessary:

e.g. sudo apt install cmake

Navigate to the root of the cloned repository and create a build directory:

mkdir build && cd build

Configure the project with CMake:

cmake ..

Build the samples:

make -j$(nproc)

By default, samples are compiled for all GPU architectures supported by this release. If you only need to target a specific GPU, you can override this to build for a single architecture and reduce build time considerably:

cmake -DCMAKE_CUDA_ARCHITECTURES=<arch> ..

Replace <arch> with your GPU's SM version (e.g. 90 for sm_90).

Run the samples from their respective directories in the build folder.

Building a Single Sample

To build just one sample, configure CMake from within the sample's directory. You must explicitly specify a GPU architecture — standalone builds have no top-level default:

cd cpp/<category>/<sampleName>
mkdir -p build && cd build
cmake -DCMAKE_CUDA_ARCHITECTURES=<arch> ..
make

Replace <arch> with your GPU's SM version (e.g. 90 for sm_90).

Windows

Language services for CMake are available in Visual Studio 2019 version 16.5 or later, and you can directly import the CUDA samples repository from either the root level or from any subdirectory or individual sample.

To build from the command line, open the x64 Native Tools Command Prompt for VS provided with your Visual Studio installation.

Navigate to the root of the cloned repository and create a build directory:

mkdir build && cd build

Configure the project with CMake - for example:

cmake .. -G "Visual Studio 16 2019" -A x64

Open the generated solution file CUDA_Samples.sln in Visual Studio. Build the samples by selecting the desired configuration (e.g., Debug or Release) and pressing F7 (Build Solution).

Run the samples from the output directories specified in Visual Studio.

Enabling On-GPU Debugging

NVIDIA GPUs support on-GPU debugging through cuda-gdb. Enabling this may significantly affect application performance as certain compiler optimizations are disabled in this configuration, hence it's not on by default. Enablement of on-device debugging is controlled via the -G switch to nvcc.

To enable cuda-gdb for samples builds, define the ENABLE_CUDA_DEBUG flag on the CMake command line. For example:

cmake -DENABLE_CUDA_DEBUG=True ...

Platform-Specific Samples

Some CUDA samples are specific to certain platforms, and require passing flags into CMake to enable. In particular, we define the following platform-specific flags:

  • BUILD_TEGRA - for Tegra-specific samples

To build these samples, set the variables either on the command line or through your CMake GUI. For example:

cmake -DBUILD_TEGRA=True ..

Cross-Compilation for Tegra Platforms

Install the NVIDIA toolchain and cross-compilation environment for Tegra devices as described in the Tegra Development Guide.

Ensure that CMake (version 3.20 or later) is installed.

Navigate to the root of the cloned repository and create a build directory:

mkdir build && cd build

Configure the project with CMake, specifying the Tegra toolchain file. And you can use -DTARGET_FS to point to the target file system root path for necessary include and library files:

cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/toolchain-aarch64-linux.cmake -DTARGET_FS=/path/to/target/system/file/system

Build the samples:

make -j$(nproc)

Transfer the built binaries to the Tegra device and execute them there.

Cross Building for Automotive Linux Platforms from the DriveOS Docker containers

To build CUDA samples to the target platform from the DriveOS Docker containers, use the following instructions.

Mount the target Root Filesystem (RFS) in the container so that the CUDA cmake process has the correct paths to CUDA and other system libraries required to build the samples.

Create a temporary directory, <temp> is any temporary directory of your choosing, for example, you can use /drive/temp:

$ mkdir /drive/<temp>

Mount the filesystem by running the following command:

$ mount /drive/drive-linux/filesystem/targetfs-images/dev_nsr_desktop_ubuntu-24.04_thor_rfs.img /drive/temp

Configure the project by running the following cmake command:

$ mkdir build && cd build
$ cmake .. -DBUILD_TEGRA=True \
  -DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc \
  -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/toolchain-aarch64-linux.cmake \
  -DTARGET_FS=/drive/temp \
  -DCMAKE_LIBRARY_PATH=/drive/temp/usr/local/cuda-13.1/thor/lib64/ \
  -DCMAKE_INCLUDE_PATH=/drive/temp/usr/local/cuda-13.1/thor/include/

Please note that the following libraries are not pre-installed in the DriveOS dev-nsr target filesystem:

  • libdrm-dev
  • Vulkan

This causes the cmake command to throw errors related to the missing files, and as a result, the related samples will not build in later steps. This issue will be addressed in a future DriveOS release.

To build the samples with ignore the error mentioned above, you can use --ignore-errors/--keep-going or comment out the comment out the corresponding add_subdirectory command in the CMakeLists.txt in the parent folder for the samples requiring Vulkan and libdrm_dev:

$ make -j$(nproc) --ignore-errors # or --keep-going
# In cpp/5_Domain_Specific/CMakeList.txt
# add_subdirectory(simpleGL)
# add_subdirectory(simpleVulkan)
# add_subdirectory(simpleVulkanMMAP)

# In cpp/8_Platform_Specific/Tegra/CMakeList.txt
# add_subdirectory(simpleGLES_EGLOutput)

QNX

Cross-compilation for QNX with CMake is supported in the CUDA 13.0 samples release and newer.

Set up the QNX SDP paths:

export QNX_HOST=/path/to/qnx/host
export QNX_TARGET=/path/to/qnx/target

Build the samples for the Tegra Thor QNX platform:

mkdir -p build && cd build
cmake .. \
  -DBUILD_TEGRA=True \
  -DCMAKE_CUDA_COMPILER=/usr/local/cuda-13.3/bin/nvcc \
  -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/toolchain-aarch64-qnx.cmake \
  -DTARGET_FS=/path/to/qnx/targetfs \
  -DCMAKE_LIBRARY_PATH=/usr/local/cuda-13.3/thor/targets/aarch64-qnx/lib/stubs/ \
  -DCMAKE_INCLUDE_PATH=/usr/local/cuda-13.3/thor/targets/aarch64-qnx/include/
cmake --build .

TARGET_FS is the QNX target filesystem of your board. The cudaNvSci sample needs it, because the NvSci headers and libraries ship with the target filesystem and not with the CUDA toolkit. Without TARGET_FS the build reports NvSCI not found and skips the sample.

The target filesystem is part of the NVIDIA DRIVE OS QNX SDK, which registered users download from NVONLINE. After the SDK is installed, the filesystem is in the DRIVE OS workspace: <NV_WORKSPACE>/drive-qnx for the standard SDK and <NV_WORKSPACE>/drive-qnx-safety for the safety SDK. See the DRIVE OS documentation for the installation guides.

For both QNX toolchains the build looks for nvscibuf.h in <TARGET_FS>/include, <TARGET_FS>/../include and <TARGET_FS>/usr/include, and for libnvscibuf.so in <TARGET_FS>/lib-target, <TARGET_FS>/usr/libnvidia and <TARGET_FS>/usr/lib. If your filesystem uses a different layout, set NVSCIBUF_INCLUDE_DIR, NVSCISYNC_INCLUDE_DIR, NVSCIBUF_LIBRARY and NVSCISYNC_LIBRARY on the cmake command line instead.

QNX Safety (CUDA Safe toolkit)

Cross-compilation for QNX Safety uses the CUDA Safe toolkit (for example